Skip to content

segfault in property.getter/setter/deleter if property subclass has weird __new__ #100942

Description

@cfbolz

CPython crashes if run on the following code:

class pro(property):
    def __new__(typ, *args, **kwargs):
        return "abcdef"
class A:
    pass

p = property.__new__(pro)
p.__set_name__(A, 1)
np = p.getter(lambda self: 1)

The crash happens on the last line. The problem is the following code in property_copy:

    new =  PyObject_CallFunctionObjArgs(type, get, set, del, doc, NULL);
    Py_DECREF(type);
    if (new == NULL)
        return NULL;

    Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
    return new;

In the crashing code, new is a string, so casting it to propertyobject and writing to prop_name is wrong.

This is synthetic code, I found the problem while porting some 3.10 features to PyPy and thinking about corner cases.

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Jan 11, 2023
  2. mdboom commented on Jan 11, 2023

    @mdboom
    Contributor

    Confirmed on 3.10, 3.11 and main.

  3. added a commit that references this issue on Jan 11, 2023
  4. sobolevn commented on Jan 11, 2023

    @sobolevn
    Member

    __set_name__ part is not required for the repro:

    class pro(property):
        def __new__(typ, *args, **kwargs):
            return "abcdef"
    
    p = property.__new__(pro)
    np = p.getter(lambda self: 1)

    This code also crashes on 3.12

  5. self-assigned this
    on Jan 11, 2023
  6. rhettinger commented on Jan 11, 2023

    @rhettinger
    Contributor

    This did not fail on 3.9. Soonish, I will hunt down the offending check in. Off hand, I suspect the disasterous and now deprecated descriptor chaining logic.

  7. sobolevn commented on Jan 11, 2023

    @sobolevn
    Member

    Commit that introduced these lines: c56387f

  8. rhettinger commented on Jan 12, 2023

    @rhettinger
    Contributor

    Confirmed, c56387f is the culprit.

  9. added 2 commits that reference this issue on Jan 12, 2023
  10. added 2 commits that reference this issue on Jan 13, 2023
  11. rhettinger commented on Jan 14, 2023

    @rhettinger
    Contributor

    @sobolevn Thank you for the back ports.

  12. added 2 commits that reference this issue on Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.10 (EOL)end of life3.11only security fixes3.12only security fixestype-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