Skip to content

Guard JPEG XMP parsing sizes#3249

Merged
maryla-uc merged 1 commit into
AOMediaCodec:mainfrom
Alb3e3:guard-jpeg-xmp-xml-size
Jun 11, 2026
Merged

Guard JPEG XMP parsing sizes#3249
maryla-uc merged 1 commit into
AOMediaCodec:mainfrom
Alb3e3:guard-jpeg-xmp-xml-size

Conversation

@Alb3e3

@Alb3e3 Alb3e3 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • route JPEG gain-map XMP parsing through one helper that rejects buffers larger than libxml's int-sized API can represent
  • apply the guard to gain-map detection, gain-map metadata parsing, and extended-XMP merging
  • add a regression test that passes an oversized XMP length without allocating it

Testing

  • git diff --check
  • /tmp/cmake-3.29.6-linux-x86_64/bin/cmake -S . -B build-xmp-size -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DAVIF_BUILD_TESTS=ON -DAVIF_GTEST=LOCAL -DAVIF_JPEG=LOCAL -DAVIF_LIBXML2=LOCAL -DAVIF_ZLIBPNG=LOCAL -DAVIF_CODEC_AOM=OFF -DAVIF_CODEC_DAV1D=OFF -DAVIF_CODEC_LIBGAV1=OFF -DAVIF_CODEC_RAV1E=OFF -DAVIF_CODEC_SVT=OFF -DAVIF_LIBYUV=OFF -DAVIF_LIBSHARPYUV=OFF -DAVIF_ENABLE_WERROR=ON -DAVIF_BUILD_APPS=OFF -DAVIF_ENABLE_GOLDEN_TESTS=OFF
  • /tmp/cmake-3.29.6-linux-x86_64/bin/cmake --build build-xmp-size --target avifjpeggainmaptest -- -j2
  • build-xmp-size/tests/avifjpeggainmaptest tests/data/

@maryla-uc maryla-uc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me. Thanks for the fix!

@maryla-uc maryla-uc merged commit 36ccfc1 into AOMediaCodec:main Jun 11, 2026
25 checks passed
wantehchang added a commit to wantehchang/libavif that referenced this pull request Jun 11, 2026
Actually allocate a too large xmp_data buffer. Make sure the
avifJPEGParseGainMapXMP() call would succeed if the size > INT_MAX check
were removed.

A follow-up to PR AOMediaCodec#3249.
GainMapPtr gain_map(avifGainMapCreate());
avifBool is_avif_gain_map;
EXPECT_FALSE(avifJPEGParseGainMapXMP(
&xmp, static_cast<size_t>(std::numeric_limits<int>::max()) + 1,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test passes an incorrect xmp buffer size to the avifJPEGParseGainMapXMP() function. (The correct xmp buffer size is 1.) We should avoid this.

This test assumes the only thing avifJPEGParseGainMapXMP() does with its xmpSize parameter is to pass it to avifJPEGReadXMLMemory(). We should not assume that.

Another problem with this test is that the xmp buffer does not contain valid data, so if I remove the size > INT_MAX check in avifJPEGReadXMLMemory(), the test still passes (because avifJPEGReadXMLMemory() still fails, for the wrong reason).

I fixed both of these problems in PR #3250.

@wantehchang

Copy link
Copy Markdown
Member

In the summary of this PR, we have:

  • add a regression test that passes an oversized XMP length without allocating it

I have seen this technique in several regression tests recently. The problem with this technique is that the XMP length is not only oversized but also incorrect.

Did you write this regression test manually, or by using an AI tool? If you used an AI tool, please instruct the tool to NOT use an incorrect buffer size, i.e., the test should pass the actual allocated buffer size to the function under test.

@Alb3e3

Alb3e3 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for catching this. Yes, the regression test was AI-assisted, and I missed that it passed a size that did not match the actual allocated buffer. That made the test weaker than intended and could let it pass for the wrong reason. I'll make sure future tests pass the actual allocated buffer size and validate the intended failure path. Thanks for fixing it in #3250.

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.

3 participants