Skip to content

gh-112564: Fix UAF for gc debug print - #160662

Open
johng wants to merge 1 commit into
python:mainfrom
johng:gh-112564-gc-debug-uaf
Open

johng wants to merge 1 commit into
python:mainfrom
johng:gh-112564-gc-debug-uaf

Conversation

@johng

@johng johng commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

In both uncollectable and collectables we would
iterate through gc items using GC_NEXT(gc).
However in the case the current item was cleaned
during iteration this would reuslt in UAF when
fetching the next step in the loop.

The fix is to move each item to a temporary seen list
before printing it, and always take the next
item from the head of the original list, so the
loop never reads from a freed item. This takes
a similar approach to finalize_garbage().

In both uncollectable and collectables we would
iterate through gc items using `GC_NEXT(gc)`.
However in the case the current item was cleaned
during iteration this would reuslt in UAF when
fetching the next step in the loop.

The fix is to move each item to a temporary `seen`
list before printing it, and always take the next
item from the head of the original list, so the
loop never reads from a freed item. This takes
a similar approach to `finalize_garbage()`.
@johng

johng commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Wonder if we just call PyUnstable_GC_VisitObjects to avoid maintaining two copies of the traversing code here

@sergey-miryanov
sergey-miryanov self-requested a review October 25, 2026 16:49

@sergey-miryanov sergey-miryanov 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.

Same notes about tests and list traversal.

Comment thread Python/gc.c
}
stats.uncollectable = n;
stats.uncollectable = gc_list_size(&finalizers);

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.

This requires an extra pass, which we should avoid.

@bedevere-app

bedevere-app Bot commented Oct 25, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants