Skip to content

Fix GH-22121: double-free in gdImageSetStyle() after overflow early return#71

Closed
iliaal wants to merge 1 commit into
masterfrom
fix/gh-22121-gd-set-style-double-free
Closed

Fix GH-22121: double-free in gdImageSetStyle() after overflow early return#71
iliaal wants to merge 1 commit into
masterfrom
fix/gh-22121-gd-set-style-double-free

Conversation

@iliaal
Copy link
Copy Markdown
Owner

@iliaal iliaal commented May 22, 2026

gdImageSetStyle (ext/gd/libgd/gd.c:2880) frees im->style before calling overflow2(). When the overflow check trips and the function returns, im->style is left dangling and the next gdImageSetStyle, gdImageDestroy, or gdImageSetPixel gdStyled/gdStyledBrushed dispatch frees or dereferences it.

Move the overflow check above the free to match upstream libgd (libgd/libgd src/gd.c::gdImageSetStyle), which has always had the check first. The divergence was an oversight in 77ba248 when the overflow check was ported from libgd 2.0.29.

Fixes php#22121

@devnexen
Copy link
Copy Markdown

Thanks @iliaal do not forget to upstream this fix, there is a sliver of hope seemingly someone is taking charge over there ;)

@iliaal
Copy link
Copy Markdown
Owner Author

iliaal commented May 22, 2026

Thanks @iliaal do not forget to upstream this fix, there is a sliver of hope seemingly someone is taking charge over there ;)

You are too quick, I am still testing :) Yeah, I'll do PR to GD upstream too once CI/CD confirms this works

…y return

gdImageSetStyle freed im->style before checking overflow2(). When the
overflow check tripped and the function early-returned, im->style was
left dangling. The next gdImageSetStyle, gdImageDestroy, or
gdImageSetPixel gdStyled/gdStyledBrushed dispatch then freed or
dereferenced it. Move the overflow check above the free to match
upstream libgd (libgd/libgd src/gd.c::gdImageSetStyle), which has
always had the check first. The original divergence was an oversight
in 77ba248 when the overflow check was ported from libgd 2.0.29.

Fixes phpGH-22121
@iliaal iliaal force-pushed the fix/gh-22121-gd-set-style-double-free branch from 4606a48 to e45f5f6 Compare May 22, 2026 11:59
@iliaal
Copy link
Copy Markdown
Owner Author

iliaal commented May 22, 2026

Thanks @iliaal do not forget to upstream this fix, there is a sliver of hope seemingly someone is taking charge over there ;)

Actually this is a result of a bad port from 2016, upstream actually does things correctly, so doing a correct sync.

@iliaal
Copy link
Copy Markdown
Owner Author

iliaal commented May 22, 2026

Submitted upstream as php#22125. Closing this staging PR.

@iliaal iliaal closed this May 22, 2026
@devnexen
Copy link
Copy Markdown

nice but by upstream, I meant this repository (and need to come up with a C reproducer).

@iliaal
Copy link
Copy Markdown
Owner Author

iliaal commented May 22, 2026

Upstream (GD) doesn't have the problem, because it does overflow check before the free, the issue was only in PHP

@devnexen
Copy link
Copy Markdown

ah I missed your previous comment, sorry. Yes indeed upstream gd is correct.

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.

Double free in gdImageSetStyle()

2 participants