python / python/cpython

PyMemoryView_FromBuffer discards owner reference from Py_buffer

未关闭
#148,493 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core topic-C-API type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Objects/memoryobject.c 中链接的 PyMemoryView_FromBuffer 实现开始,并阅读 PyMemoryView_FromBuffer C-API 文档。查看 PyO3 pull request #5937 以了解 owner 生命周期要求,然后比较保留 owner 与引入新 API 对兼容性的影响。完成的标准是确定并记录 ownership 语义,并由适当的 CPython 测试覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, python
领域
api
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。