python / python/cpython

PyMemoryView_FromBuffer discards owner reference from Py_buffer

Open
#148,493 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-C-API type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

https://github.com/python/cpython/blob/0274d8304e5eec23de100d827eb4da06ab7fd8aa/Objects/memoryobject.c#L769-L788

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 owner around -- 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.