python / python/cpython

crossinterp: Segfault in `check_missing___main___attr` if AttributeError args are not normal

未关闭
#156,121 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules topic-subinterpreters type-crash
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Crash report

What happened?

crossinterp doesn't do much checking on the arguments of the passed-in exception, allowing lone surrogate unicode, or non-str arguments cause segfaults:

Lone surrogate example:

from concurrent import interpreters
import __main__

x = 1

def f():
    return x  # forces pickle fallback

f.__name__ = f.__qualname__ = "\ud800"
setattr(__main__, "\ud800", f)

interp = interpreters.create()
interp.call(f)
> ./python.exe temp/crossinterp.py
fish: Job 1, './python.exe temp/crossinterp.py' terminated by signal SIGSEGV (Address boundary error)

Non-str argument example (a bit more convoluted):

from concurrent import interpreters

x = 1

def f():
    return x  # forces pickle fallback

interp = interpreters.create()
interp.exec("""
import pickle

def loads(data):
    raise AttributeError(42)

pickle.loads = loads
""")
interp.call(f)
> ./python.exe temp/crossinterp-2.py
fish: Job 1, './python.exe temp/crossinterp-2…' terminated by signal SIGSEGV (Address boundary error)

This is because check_missing___main___attr doesn't check the result of PyUnicode_AsUTF8 before passing it to strncmp, so if the object is not a PyUnicode object or if it can't be converted to utf8, you get a crash:

https://github.com/python/cpython/blob/91d71dd67074d4599b6bd49cc933f41f8bd57058/Python/crossinterp.c#L666-669

The fix should cover both cases, I guess:

A PyUnicode_Check on msgobj, and then a check that the PyUnicode_AsUTF8 return is valid.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

Python 3.16.0a0 (heads/main:e8158d1a02d, Aug 20 2026, 15:09:24) [Clang 21.0.0 (clang-2100.3.27.1)]

Linked PRs
  • gh-156128

贡献指南

打开贡献指南

从这里开始

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

调研方向

按照报告中的定位,从 Python/crossinterp.c 的 check_missing___main___attr 开始,重现带有 lone-surrogate 的 AttributeError 情况,以及带有非字符串参数的 AttributeError 情况。验证格式错误的异常参数不再导致 segfault,并确认现有的 cross-interpreter 调用行为保持不变;gh-156128 已经关联到此 issue。

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

评估

技术栈
c, python
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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