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

PyType_GetModule() should warn/fail when type has Py_TPFLAGS_BASETYPE #86584

Closed
tiran opened this issue Nov 20, 2020 · 5 comments
Closed

PyType_GetModule() should warn/fail when type has Py_TPFLAGS_BASETYPE #86584

tiran opened this issue Nov 20, 2020 · 5 comments
Labels
3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@tiran
Copy link
Member

tiran commented Nov 20, 2020

BPO 42418
Nosy @vstinner, @tiran, @encukou, @corona10, @shihai1991

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-11-20.10:28:13.231>
labels = ['interpreter-core', 'type-bug', '3.10']
title = 'PyType_GetModule() should warn/fail when type has Py_TPFLAGS_BASETYPE'
updated_at = <Date 2020-11-24.13:43:57.477>
user = 'https://github.com/tiran'

bugs.python.org fields:

activity = <Date 2020-11-24.13:43:57.477>
actor = 'petr.viktorin'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2020-11-20.10:28:13.231>
creator = 'christian.heimes'
dependencies = []
files = []
hgrepos = []
issue_num = 42418
keywords = []
message_count = 4.0
messages = ['381475', '381493', '381532', '381740']
nosy_count = 5.0
nosy_names = ['vstinner', 'christian.heimes', 'petr.viktorin', 'corona10', 'shihai1991']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue42418'
versions = ['Python 3.10']

@tiran
Copy link
Member Author

tiran commented Nov 20, 2020

PyType_GetModule() may return wrong or unexpected result when a type has
the feature flag Py_TPFLAGS_BASETYPE. The base type flag permits subclassing of an extension class. The function will return NULL when a type is subclasses in Python space or a different module object when the type is subclasses and initialized in a differen C extension module.

All subclassable types must use _PyType_GetModuleByDef() to safely access module and module state struct. It's easy to miss the problem. I missed it when I ported some of my code to heap types and multiphase init.

I propose to either a guard to PyType_GetModule() to prevent misuse of the function or to add a variant of the function that prevents misuse.

@tiran tiran added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 20, 2020
@shihai1991
Copy link
Member

I propose to either a guard to PyType_GetModule() to prevent misuse of the function or to add a variant of the function that prevents misuse.
+1. Looks like check Py_TPFLAGS_BASETYPE in PyType_GetModule() is an easy way:)

@encukou
Copy link
Member

encukou commented Nov 21, 2020

I don't understand this issue. Why would Py_TPFLAGS_BASETYPE make the result of PyType_GetModule() wrong? Is the note in the documentation of PyType_GetModule() not clear?
A type and its subtypes may be defined in different modules; why would it be wrong to ask which module the superclass was defined in?

Note that PyType_GetModule() is needed if users want to implement something like _PyType_GetModuleByDef().

@encukou
Copy link
Member

encukou commented Nov 24, 2020

I missed it when I ported some of my code to heap types and multiphase init.

I guess PyType_GetModule() is not good for that use case.
If your code is open, could you add a link?

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@erlend-aasland erlend-aasland added topic-C-API 3.13 new features, bugs and security fixes and removed 3.10 only security fixes labels Jan 4, 2024
@encukou
Copy link
Member

encukou commented Jan 5, 2024

I will close this issue.
The docs have a description of what PyType_GetModule does, and I'll be working on better API that avoids this potential surprise.

@encukou encukou closed this as completed Jan 5, 2024
@encukou encukou closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants