Skip to content

Comments

gainmap: prevent integer overflow in plane allocation#3049

Closed
rootvector2 wants to merge 1 commit intoAOMediaCodec:mainfrom
rootvector2:fix/gainmap-allocation-overflow
Closed

gainmap: prevent integer overflow in plane allocation#3049
rootvector2 wants to merge 1 commit intoAOMediaCodec:mainfrom
rootvector2:fix/gainmap-allocation-overflow

Conversation

@rootvector2
Copy link
Contributor

No description provided.

wantehchang added a commit to wantehchang/libavif that referenced this pull request Feb 21, 2026
Prevent integer overflows in multiplications involving width, height,
and rowBytes in src/gainmap.c by performing the multiplications in the
size_t type. The size_t type is large enough because pixel buffers for
the width, height, and rowBytes have been allocated successfully.

"Dexter.k" <164054284+rootvector2@users.noreply.github.com> reported an
integer overflow in the allocation of the gainMapF buffers in
avifRGBImageComputeGainMap() and suggested a fix in
AOMediaCodec#3049.
Copy link
Collaborator

@wantehchang wantehchang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dexter.k: Thank you very much for the pull request. After I started to review and improve your pull request, I quickly discovered that much more changes were needed. So I audited the entire src/gainmap.c file and wrote #3051. I will treat this pull request as a bug report.

// --- After this point, the function should exit with 'goto cleanup' to free allocated resources.
// Overflow protection: 'width * height * sizeof(float)' uses signed int
// multiplication which is undefined behavior on overflow in C. Compute the
// allocation size in size_t with explicit overflow checks instead.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[All of my review comments are for future reference.]

Omit this kind of comment that explains an overflow check in detail. There are a lot of such overflow checks in libavif. We cannot afford to explain all of them in detail. libavif maintainers are expected to be familiar with them.

if (gainMapPlaneRowBytes != 0 && baseRgbImage->height > SIZE_MAX / gainMapPlaneRowBytes) {
res = AVIF_RESULT_INVALID_ARGUMENT;
goto cleanup;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the width and height are for a buffer that have been allocated successfully, it is safe to multiply width and height in the size_t type. We can take advantage of this fact here. So for the problematic expression width * height * sizeof(float), we only need one overflow check:

    const size_t numPixels = (size_t)width * height;
    if (numPixels > SIZE_MAX / sizeof(float)) {
        res = AVIF_RESULT_INVALID_ARGUMENT;
        goto cleanup;
    }
    const size_t gainMapPlaneSize = numPixels * sizeof(float);

@rootvector2
Copy link
Contributor Author

Thank you for the detailed review and for auditing the file more thoroughly. I appreciate the improvements in #3051 and am glad the report was useful.

@wantehchang
Copy link
Collaborator

Dexter.k: I noticed you have focused your audit of libavif on calculations of allocation sizes. Since width, height, and rowBytes are all stored in uint32_t struct members, when we multiply width or rowBytes by height (or a loop index that may be as large as height - 1), we need to cast the operands to size_t. If you are interested in auditing more libavif code, I would encourage you to search for "rowBytes" (case-insensitive) and check any multiplication involving "rowBytes". After that is done, repeat it for "width".

wantehchang added a commit that referenced this pull request Feb 24, 2026
Prevent integer overflows in multiplications involving width, height,
and rowBytes in src/gainmap.c by performing the multiplications in the
size_t type. The size_t type is large enough because pixel buffers for
the width, height, and rowBytes have been allocated successfully.

"Dexter.k" <164054284+rootvector2@users.noreply.github.com> reported an
integer overflow in the allocation of the gainMapF buffers in
avifRGBImageComputeGainMap() and suggested a fix in
#3049.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants