Skip to content

SIGSEV with method descriptors called without a second argument #132747

Closed

Description

@Changaco

Crash report

What happened?

Since Python 3.12, the following code triggers a segmentation fault:

import _io, sys; _io._TextIOBase.detach.__get__(sys.stderr)

CPython versions tested on:

3.9, 3.11, 3.12, 3.13, 3.14

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

No response

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Apr 20, 2025
  2. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Apr 20, 2025
  3. picnixz commented on Apr 20, 2025

    @picnixz
    Member

    It appears that there is some issue with method resolution. When using _io.TextIOWrapper, it's fine but not when using _io._TextIOBase.

    Note: _io.TextIOWrapper.detach is a different function by the way.
    Note 2: It actually happens with all method descriptors of _TextIOBase.

  4. changed the title [-]Segfault in `_io` module[/-] [+]SIGSEV with method descriptors of `_io.TextIOBase` on `sys.stderr`[/+] on Apr 20, 2025
  5. changed the title [-]SIGSEV with method descriptors of `_io.TextIOBase` on `sys.stderr`[/-] [+]SIGSEV with method descriptors of `_io._TextIOBase`[/+] on Apr 20, 2025
  6. picnixz commented on Apr 20, 2025

    @picnixz
    Member

    This also crashes:

    import io, _io
    _io._TextIOBase.read.__get__(io.StringIO())
  7. Alexandr153 commented on Apr 20, 2025

    @Alexandr153
    Contributor

    It appears that there is some issue with method resolution. When using _io.TextIOWrapper, it's fine but not when using _io._TextIOBase.

    As I understand it, the _TextIOBase class is an abstract class because it inherits from the abstract IOBase class and does not implement all of its methods. TextIOWrapper, on the other hand, implements all the methods of the parent abstract class and executes its methods directly.

  8. picnixz commented on Apr 20, 2025

    @picnixz
    Member

    Actually, I'm not sure about the resolution. It's as if there was a missing tp_flag somewhere or something else. It doesn't seem to occur with other static types. I don't have time to investigate though.

  9. Changaco commented on Apr 20, 2025

    @Changaco
    Author

    In addition to _TextIOBase, the _IOBase, _BufferedIOBase, BufferedReader and BufferedWriter classes are also affected.

    Example: import io, _io; _io._IOBase.truncate.__get__(io.StringIO()).

  10. ZeroIntensity commented on Apr 21, 2025

    @ZeroIntensity
    Member

    From what I can tell, this is a nasty bug related to descriptors themselves, not _io. You can trigger the same crash with _queue, for example:

    import _queue
    print(_queue.SimpleQueue.get.__get__(_queue.SimpleQueue()))

    This seems to affect all heap types with methods. The issue is that method_get doesn't expect the type to be NULL. I think we just need a guard.

    I'm really suprised that nobody noticed this for this long.

  11. Changaco commented on Apr 21, 2025

    @Changaco
    Author

    I guess nobody noticed this bug before because nobody calls those __get__ methods with only one argument. I only found it accidentally by running Python code that calls all the non-Python __get__ methods it can find on objects in memory, without passing the second argument.

  12. added a commit that references this issue on Apr 21, 2025
  13. added a commit that references this issue on Apr 21, 2025
  14. added a commit that references this issue on Apr 21, 2025
  15. changed the title [-]SIGSEV with method descriptors of `_io._TextIOBase`[/-] [+]SIGSEV with method descriptors called without a second argument[/+] on Apr 24, 2025
  16. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    and removed on Apr 24, 2025
  17. picnixz commented on Apr 24, 2025

    @picnixz
    Member

    Thanks for the report @Changaco and the fix @ZeroIntensity

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

Metadata

Metadata

Assignees

Labels

3.13only security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions