Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iter __reduce__ can segfault if accessing __builtins__.__dict__['iter'] mutates the iter object #101765

Closed
ionite34 opened this issue Feb 9, 2023 · 1 comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@ionite34
Copy link
Contributor

ionite34 commented Feb 9, 2023

Crash report

Example from @chilaxan

corrupt = iter(lambda:0, 0)

class Cstr:
    def __hash__(self):
        return hash('iter')
    def __eq__(self, other):
        [*corrupt]
        return other == 'iter'

builtins = __builtins__.__dict__ if hasattr(__builtins__, '__dict__') else __builtins__
oiter = builtins['iter']
del builtins['iter']
builtins[Cstr()] = oiter

print(corrupt.__reduce__())

Expected result

This should return a valid __reduce__ tuple of the exhausted iterator. Instead behavior is inconsistent between segmentation faults, SystemErrors, and sometimes returning the iterator without being exhausted.

Error messages

  • 3.11, windows, PYTHONMALLOC=debug
  • 3.12.0a4, windows, PYTHONMALLOC=debug
Windows fatal exception: access violation
> exit code -1073741819 (0xC0000005)
  • 3.12.04a4, windows, compiled with debug mode
    print(corrupt.__reduce__())
          ^^^^^^^^^^^^^^^^^^^^
SystemError: NULL object passed to Py_BuildValue
  • 3.11, ubuntu
(<built-in function iter>, (<function  at 0x7fb772c3c4a0>, 0))
> terminated by signal SIGSEGV (Address boundary error)
  • 3.12.0a4, ubuntu
(<built-in function iter>, (<function  at 0x7f3480d71f80>, 0))
  • 3.12.0a4, ubuntu, PYTHONMALLOC=debug
Fatal Python error: Segmentation fault

Linked PRs

@ionite34 ionite34 added the type-crash A hard crash of the interpreter, possibly with a core dump label Feb 9, 2023
@godlygeek
Copy link
Contributor

godlygeek commented Feb 9, 2023

This (horrific) reproducer provokes undefined defined behavior from this statement:

return Py_BuildValue("N(OO)", _PyEval_GetBuiltin(&_Py_ID(iter)),
it->it_callable, it->it_sentinel);

The call to _PyEval_GetBuiltin to find the iter builtin is calling Cstr.__eq__, which exhausts the iterator, causing the Py_CLEAR in calliter_iternext to be executed, setting it->it_callable and it->it_sentinel to NULL. But the order of evaluation of arguments in a function call isn't specified, and modifying an argument by evaluating another argument is a bug.

On some platforms, it->it_callable and it->it_sentinel are being evaluated before _PyEval_GetBuiltin, and so Py_BuildValue is being passed pointers to objects that got freed inside the _PyEval_GetBuiltin call.

@ionite34 ionite34 changed the title iter.__reduce__ can segfault if accessing __builtins__.__dict__['iter'] exhausts the iter object iter __reduce__ can segfault if accessing __builtins__.__dict__['iter'] mutates the iter object Feb 10, 2023
@arhadthedev arhadthedev added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Feb 10, 2023
ionite34 added a commit to ionite34/cpython that referenced this issue Feb 10, 2023
JelleZijlstra pushed a commit that referenced this issue Feb 24, 2023
…when internal access of `builtins.__dict__` exhausts the iterator (#101769)
ionite34 added a commit to ionite34/cpython that referenced this issue Feb 25, 2023
…`__reduce__` when internal access of `builtins.__dict__` exhausts the iterator (pythonGH-101769).

(cherry picked from commit 54dfa14)

Co-authored-by: Ionite <dev@ionite.io>
ionite34 added a commit to ionite34/cpython that referenced this issue Feb 25, 2023
…`__reduce__` when internal access of `builtins.__dict__` exhausts the iterator (pythonGH-101769).

(cherry picked from commit 54dfa14)

Co-authored-by: Ionite <dev@ionite.io>
ionite34 added a commit to ionite34/cpython that referenced this issue Feb 25, 2023
…`__reduce__` when internal access of `builtins.__dict__` exhausts the iterator (pythonGH-101769).

(cherry picked from commit 54dfa14)

Co-authored-by: Ionite <dev@ionite.io>
JelleZijlstra pushed a commit that referenced this issue Feb 25, 2023
…uce__` when internal access of `builtins.__dict__` exhausts the iterator (GH-101769) (#102228)

(cherry picked from commit 54dfa14)
JelleZijlstra pushed a commit that referenced this issue Feb 25, 2023
…uce__` when internal access of `builtins.__dict__` exhausts the iterator (GH-101769) (#102229)

(cherry picked from commit 54dfa14)
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Feb 25, 2023
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Feb 26, 2023
…ing (pythonGH-102265)

Followup from pythonGH-101769..
(cherry picked from commit d71edbd)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Feb 26, 2023
…ing (pythonGH-102265)

Followup from pythonGH-101769..
(cherry picked from commit d71edbd)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Feb 26, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 26, 2023
…2283)

(cherry picked from commit 8d0f09b)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 26, 2023
…2283)

(cherry picked from commit 8d0f09b)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington added a commit that referenced this issue Feb 26, 2023
(cherry picked from commit 8d0f09b)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington added a commit that referenced this issue Feb 26, 2023
(cherry picked from commit 8d0f09b)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
carljm added a commit to carljm/cpython that referenced this issue Feb 28, 2023
* main: (67 commits)
  pythongh-99108: Add missing md5/sha1 defines to Modules/Setup (python#102308)
  pythongh-100227: Move _str_replace_inf to PyInterpreterState (pythongh-102333)
  pythongh-100227: Move the dtoa State to PyInterpreterState (pythongh-102331)
  pythonGH-102305: Expand some macros in generated_cases.c.h (python#102309)
  Migrate to new PSF mailgun account (python#102284)
  pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Python/) (python#102193)
  pythonGH-90744: Fix erroneous doc links in the sys module (python#101319)
  pythongh-87092: Make jump target label equal to the offset of the target in the instructions sequence (python#102093)
  pythongh-101101: Unstable C API tier (PEP 689) (pythonGH-101102)
  IDLE: Simplify DynOptionsMenu __init__code (python#101371)
  pythongh-101561: Add typing.override decorator (python#101564)
  pythongh-101825: Clarify that as_integer_ratio() output is always normalized (python#101843)
  pythongh-101773: Optimize creation of Fractions in private methods (python#101780)
  pythongh-102251: Updates to test_imp Toward Fixing Some Refleaks (pythongh-102254)
  pythongh-102296 Document that inspect.Parameter kinds support ordering (pythonGH-102297)
  pythongh-102250: Fix double-decref in COMPARE_AND_BRANCH error case (pythonGH-102287)
  pythongh-101100: Fix sphinx warnings in `types` module (python#102274)
  pythongh-91038: Change default argument value to `False` instead of `0` (python#31621)
  pythongh-101765: unicodeobject: use Py_XDECREF correctly (python#102283)
  [doc] Improve grammar/fix missing word (pythonGH-102060)
  ...
JelleZijlstra pushed a commit to JelleZijlstra/cpython that referenced this issue Sep 10, 2024
…ce__` when internal access of `builtins.__dict__` exhausts the iterator (python#101769)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

3 participants