PyMemoryView_FromBuffer discards owner reference from Py_buffer
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
This means that if you use PyMemoryView_FromBuffer with a buffer that requires an owner to be kept alive, you get a UAF hazard here.
This is not mentioned in the documentation: https://docs.python.org/3/c-api/memoryview.html#c.PyMemoryView_FromBuffer
An extremely cursory didn't find any vulnerable callers within CPython itself, however this was discovered in the process of implementing https://github.com/PyO3/pyo3/pull/5937 where it definitely would have been an issue.
It seems like there's two possible choices here:
- Change this to keep
owneraround -- potentially breaks existing callers? - Document this, and introduce a new API with clearer semantics here.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Objects/memoryobject.c at the linked PyMemoryView_FromBuffer implementation and read the PyMemoryView_FromBuffer C-API documentation. Review PyO3 pull request #5937 to understand the owner-lifetime requirement, then compare the compatibility implications of retaining owner versus introducing a new API. Done means the ownership semantics are decided, documented, and covered by appropriate CPython tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100