fix: use-after-free in CleanNode when TidyGDocClean discards STYLE element - #1180
Open
SongTonyLi wants to merge 1 commit into
Open
fix: use-after-free in CleanNode when TidyGDocClean discards STYLE element#1180SongTonyLi wants to merge 1 commit into
SongTonyLi wants to merge 1 commit into
Conversation
…ement Fixes htacg#1175. CleanNode() saves next = child->next then conditionally prunes child. The STYLE and empty-P checks were separate if statements, so when DiscardElement() frees child in the STYLE branch, control falls through to the P check which dereferences the already-freed pointer (CWE-416). Fix: capture DiscardElement return into next and make branches mutually exclusive (else if) so freed child is never re-evaluated.
SongTonyLi
marked this pull request as ready for review
May 6, 2026 14:17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1175.
CleanNode()ingdoc.ctraverses the DOM tree, savingnext = child->nextthen conditionally pruningchild. The STYLE and empty-P checks were separateifstatements (notelse if), so when the STYLE branch callsDiscardElement()to freechild, control falls through to the P check which dereferences the already-freedchild. This is a heap-use-after-free (CWE-416) reachable via crafted HTML withTidyGDocCleanenabled.Root cause
Call chain:
tidyCleanAndRepair()->tidyDocCleanAndRepair()->prvTidyCleanGoogleDocument()->CleanNode().The loop at
gdoc.c:106savesnext = child->next, then at lines 110-111 the STYLE branch callsTY_(DiscardElement)(doc, child)which freeschildthroughprvTidyFreeNode(). The immediately followingif (nodeIsP(child) && !child->content)at line 112 is a separate condition (notelse if), so it dereferences the freed pointer.Forward instrumentation confirmed:
DiscardElementfreed element at0x50b0000001a0, and ASan reported invalid read at0x50b0000001d8, which is 56 bytes inside the freed 112-byte region[0x50b0000001a0, 0x50b000000210).Fix
Two changes in
CleanNode()atsrc/gdoc.c:next = TY_(DiscardElement)(doc, child)so the traversal cursor advances past the freed node.else if, preventing re-evaluation of freedchildin the same iteration.Why this approach
I looked at three possible fixes:
DiscardElement's return intonext, aligning with the existing pattern in this function whereDiscardContaineralready updatesnextvia out-param.DiscardElement/FreeNode: too much scope and regression risk for a localized traversal bug.Verification
Tested with ASan-instrumented build (
-fsanitize=address,detect_leaks=1):SUMMARY: AddressSanitizer: heap-use-after-free /src/tidy-html5/src/gdoc.c:112:21 in CleanNodeNote: ASan still reports 192 bytes leaked in 8 allocations, but these are pre-existing leaks in
prvTidynewStack(present with and without the patch) and unrelated to this fix.