Skip to content

Replace remaining sprintf() with snprintf()#9476

Open
myagmartseren wants to merge 2 commits intopython-pillow:mainfrom
myagmartseren:fix/sprintf-to-snprintf
Open

Replace remaining sprintf() with snprintf()#9476
myagmartseren wants to merge 2 commits intopython-pillow:mainfrom
myagmartseren:fix/sprintf-to-snprintf

Conversation

@myagmartseren
Copy link

@myagmartseren myagmartseren commented Mar 19, 2026

Replace All Remaining sprintf() with snprintf()

Summary

Multiple C source files still use sprintf() without bounds checking — the same class of vulnerability fixed in CVE-2024-28219. This PR replaces all remaining instances with snprintf().

Affected Files (6 call sites)

File Line Context
src/libImaging/QuantPngQuant.c 129 sprintf(version, "%d.%d.%d", ...)
src/libImaging/JpegEncode.c 405 sprintf(version, "%d.%d", ...)
src/_webp.c 56 sprintf(message, "could not assemble chunks: %s", ...)
src/_webp.c 58 sprintf(message, "could not set %.4s chunk: %s", ...)
src/_webp.c 652 sprintf(message, ": Image size exceeds WebP limit...")
src/_webp.c 746 sprintf(version, "%d.%d.%d", ...)

Fix

All sprintf(buf, ...)snprintf(buf, sizeof(buf), ...)

Classification

  • CWE-120 (Buffer Copy without Checking Size of Input)
  • CWE-676 (Use of Potentially Dangerous Function)
  • CVSS 3.1: 4.8 (Medium)
  • Related: CVE-2024-28219 (same pattern, partially fixed)

Your Name and others added 2 commits March 19, 2026 16:53
Replace unsafe sprintf() calls with bounds-checked snprintf() in:
- src/libImaging/QuantPngQuant.c (version string)
- src/libImaging/JpegEncode.c (version string)
- src/_webp.c (error messages and version string, 4 call sites)

This is consistent with the fix applied in CVE-2024-28219 which
addressed the same class of vulnerability in font rendering code.

Security: CWE-120 (Buffer Copy without Checking Size of Input)
@EricSoroos
Copy link

Thanks for running a scanner -- note that we have a security policy here: https://github.com/python-pillow/Pillow/blob/main/.github/SECURITY.md that does not include posting security sensitive issues in public.

Also note that in this case, some of the items that are running through sprintf are statically determined at compile time and have no way of being attacker controlled.

@aclark4life aclark4life changed the title Replace remaining sprintf() with snprintf() (security) Replace remaining sprintf() with snprintf() Mar 19, 2026
@aclark4life aclark4life added the 🤖-assisted AI-assisted label Mar 19, 2026
@radarhere
Copy link
Member

This does seem like a rather difficult exploit.

If an attacker managed to change the value WEBP_MAX_DIMENSION or JPEG_LIB_VERSION, it really sounds like they're able to modify the source code of one of our dependencies. liq_version() and WebPGetDecoderVersion() are virtually constants, and kErrorMessages is internal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants