Skip to content

[3.12] gh-157947: Keep exception fields alive during suggestion lookups - #157973

Closed
nvnzno-a11y wants to merge 2 commits into
python:3.12from
nvnzno-a11y:fix/attrerror-suggestions-borrowed-refs
Closed

nvnzno-a11y wants to merge 2 commits into
python:3.12from
nvnzno-a11y:fix/attrerror-suggestions-borrowed-refs

Conversation

@nvnzno-a11y

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.

… 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.
@python-cla-bot

Copy link
Copy Markdown

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

CLA not signed

@StanFromIreland

Copy link
Copy Markdown
Member

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

@nvnzno-a11y

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.

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