-
Notifications
You must be signed in to change notification settings - Fork 287
Reject oversized BMFF box sizes before serialization #3199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -397,13 +397,12 @@ avifResult avifRWStreamWriteChars(avifRWStream * stream, const char * chars, siz | |
| avifResult avifRWStreamWriteFullBox(avifRWStream * stream, const char * type, size_t contentSize, int version, uint32_t flags, avifBoxMarker * marker) | ||
| { | ||
| assert(stream->numUsedBitsInPartialByte == 0); // Byte alignment is required. | ||
| if (marker) { | ||
| *marker = stream->offset; | ||
| } | ||
| avifBoxMarker oldOffset = stream->offset; | ||
| size_t headerSize = sizeof(uint32_t) + 4 /* size of type */; | ||
| if (version != -1) { | ||
| headerSize += 4; | ||
| } | ||
| AVIF_CHECKERR(contentSize <= UINT32_MAX - headerSize, AVIF_RESULT_INVALID_ARGUMENT); | ||
|
|
||
| AVIF_CHECKRES(makeRoom(stream, headerSize)); | ||
| memset(stream->raw->data + stream->offset, 0, headerSize); | ||
|
|
@@ -416,6 +415,9 @@ avifResult avifRWStreamWriteFullBox(avifRWStream * stream, const char * type, si | |
| stream->raw->data[stream->offset + 10] = (uint8_t)((flags >> 8) & 0xff); | ||
| stream->raw->data[stream->offset + 11] = (uint8_t)((flags >> 0) & 0xff); | ||
| } | ||
| if (marker) { | ||
| *marker = oldOffset; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jmestwa-coder: The original location of this if statement is also correct; it only has the minor downside that the output parameter |
||
| stream->offset += headerSize; | ||
|
|
||
| return AVIF_RESULT_OK; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,6 +202,42 @@ TEST(StreamTest, WriteBitsLimit) { | |
| AVIF_RESULT_INVALID_ARGUMENT); | ||
| } | ||
|
|
||
| TEST(StreamTest, WriteBoxSizeLimit) { | ||
| testutil::AvifRwData rw_data; | ||
| avifRWStream rw_stream; | ||
| avifRWStreamStart(&rw_stream, &rw_data); | ||
| const char box_type[] = "type"; | ||
| avifBoxMarker marker = 123; | ||
|
|
||
| EXPECT_EQ( | ||
| avifRWStreamWriteBox( | ||
| &rw_stream, box_type, | ||
| std::numeric_limits<uint32_t>::max() - sizeof(uint32_t) - 4, &marker), | ||
| AVIF_RESULT_OK); | ||
| EXPECT_EQ(marker, size_t{0}); | ||
| EXPECT_EQ(avifRWStreamOffset(&rw_stream), sizeof(uint32_t) + 4); | ||
|
|
||
| EXPECT_EQ( | ||
| avifRWStreamWriteBox( | ||
| &rw_stream, box_type, | ||
| std::numeric_limits<uint32_t>::max() - sizeof(uint32_t) - 3, &marker), | ||
| AVIF_RESULT_INVALID_ARGUMENT); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: The code below assumes that We can allow this, but let's add the following |
||
|
|
||
| EXPECT_EQ(avifRWStreamWriteFullBox( | ||
| &rw_stream, box_type, | ||
| std::numeric_limits<uint32_t>::max() - sizeof(uint32_t) - 8, | ||
| /*version=*/0, /*flags=*/0, &marker), | ||
| AVIF_RESULT_OK); | ||
| EXPECT_EQ(marker, sizeof(uint32_t) + 4); | ||
| EXPECT_EQ(avifRWStreamOffset(&rw_stream), 2 * (sizeof(uint32_t) + 4) + 4); | ||
|
|
||
| EXPECT_EQ(avifRWStreamWriteFullBox( | ||
| &rw_stream, box_type, | ||
| std::numeric_limits<uint32_t>::max() - sizeof(uint32_t) - 7, | ||
| /*version=*/0, /*flags=*/0, &marker), | ||
| AVIF_RESULT_INVALID_ARGUMENT); | ||
| } | ||
|
|
||
| // Test the overflow checks in the makeRoom() function in src/stream.c. | ||
| TEST(StreamTest, OverflowChecksInMakeRoom) { | ||
| testutil::AvifRwData rw_data; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avifRWStreamWriteFullBox()and the relatedavifRWStreamWriteBox()function are internal functions, so they don't need to check for invalid input arguments as thoroughly as a public API function. But it is fine to hold them to a higher standard.I audited the callers of
avifRWStreamWriteFullBox()andavifRWStreamWriteBox(). They all passAVIF_BOX_SIZE_TBDas thecontentSizeargument, with only two exceptions:sizeof(uint16_t)and 0.