GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [3.12] gh-157947: Keep exception fields alive during suggestion lookups by nvnzno-a11y · Pull Request #157973 · python/cpython

Conversation

@nvnzno-a11y

@nvnzno-a11y nvnzno-a11y commented Sep 23, 2026

Copy link
Copy Markdown

Summary

get_suggestions_for_attribute_error(), offer_suggestions_for_name_error() and
offer_suggestions_for_import_error() in Python/suggestions.c use borrowed
references to exc->name / exc->obj / exc->name_from and to the traceback
frame across calls that can run arbitrary Python code:

  • PyObject_Dir(obj) / PyObject_Dir(mod) → __dir__
  • PyImport_GetModule(mod_name) → sys.modules overrides
  • _PyObject_LookupAttr(self, name, ...) → __getattr__/__getattribute__

That code can rebind or clear the exception's attributes (exc.name = ...,
exc.obj = None, exc.__traceback__ = None) and free the referenced objects.
calculate_suggestions() then dereferences the dangling pointers →
use-after-free (deterministic SIGSEGV when the freed name is a large,
mmap-backed string; reproducer in gh-157947).

This only affects 3.12 — the AttributeError suggestion path was refactored
away on 3.13/main.

The fix takes strong references to name, obj, mod_name, name_from and
frame for the duration of the calls that may execute arbitrary code.

Reproducer

import sys

def fresh_name():
    return ("X" * (4 * 1024 * 1024)) + "!"   # mmap'd -> munmap on free

class Evil:
    def __dir__(self):
        exc = getattr(sys, "last_value", None)
        if isinstance(exc, AttributeError):
            exc.name = "replaced"; exc.obj = None
        return ["x"]

raise AttributeError("boom", name=fresh_name(), obj=Evil())

On the 3.12 branch this segfaults during exception printing
(unpatched_exit=139); with the patch the exception prints normally.

Tests

  • New regression test test_attribute_error_name_cleared_in_dir in
    Lib/test/test_traceback.py (runs in both the pure-Python and the
    _testcapi exception-printing variants).
  • All 58 tests in {Pure,CPython}SuggestionFormattingTests pass.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
nvnzno-a11y added 2 commits September 23, 2026 03:20
@nvnzno-a11y
[3.12] pythongh-157947: Keep exception fields alive during suggestion…
f03febe
… lookups

get_suggestions_for_attribute_error(), offer_suggestions_for_name_error()
and offer_suggestions_for_import_error() used borrowed references to
exc->name/exc->obj/exc->name_from and the traceback frame across calls
that can invoke arbitrary Python code (PyObject_Dir, PyImport_GetModule,
_PyObject_LookupAttr). That code can rebind or clear the exception's
attributes and free the referenced objects, leaving dangling pointers
that calculate_suggestions() then dereferences -> use-after-free.

Take strong references for the duration of the calls that may run
arbitrary code.
@nvnzno-a11y
Add NEWS entry
32b4697
@nvnzno-a11y
nvnzno-a11y requested a review from iritkatriel as a code owner September 23, 2026 02:01
@python-cla-bot

python-cla-bot Bot commented Sep 23, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

  • [email protected]

CLA not signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot mentioned this pull request Sep 23, 2026
Use-after-free in {Attribute,Name,Import}Error suggestion generation #157947
Open
@bedevere-app bedevere-app Bot added the awaiting review label Sep 23, 2026
@StanFromIreland

StanFromIreland commented Sep 23, 2026

Copy link
Copy Markdown
Member

The CLA is not signed, and please don't open PRs before a conclusion is reached on the issue.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland StanFromIreland closed this Sep 23, 2026
@nvnzno-a11y

nvnzno-a11y commented Sep 23, 2026

Copy link
Copy Markdown
Author

Understood — apologies for opening this before the discussion on the issue had reached a conclusion. I'll follow #157947 and wait for the maintainers' decision there. Thanks for the heads-up.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

@iritkatriel iritkatriel Awaiting requested review from iritkatriel iritkatriel is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@nvnzno-a11y @StanFromIreland
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.