Skip to content

gh-160773: Fix use-after-free in OSError.__str__ - #160822

Merged
StanFromIreland merged 3 commits into
python:mainfrom
alonfaraj:gh-160773-oserror-str
Oct 29, 2026
Merged

StanFromIreland merged 3 commits into
python:mainfrom
alonfaraj:gh-160773-oserror-str

Conversation

@alonfaraj

@alonfaraj alonfaraj commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #160773

OSError.__str__ passed borrowed references to its attributes (errno, strerror, filename, filename2, and winerror on Windows) to PyUnicode_FromFormat. Formatting one attribute with %S/%R can run arbitrary Python code. If that code modifies another attribute, the object being formatted can be freed while still in use.

The fix now take strong references to the attributes before formatting and release them afterwards.

Added a regression test which cover the filename and filename -> filename2 formats.

Tested on linux (Ubuntu 26.04).

@python-cla-bot

python-cla-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland StanFromIreland added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 29, 2026
@StanFromIreland
StanFromIreland enabled auto-merge (squash) October 29, 2026 10:50
@StanFromIreland
StanFromIreland merged commit 7e5227f into python:main Oct 29, 2026
64 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @alonfaraj for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 29, 2026

Copy link
Copy Markdown

GH-160884 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 29, 2026
@bedevere-app

bedevere-app Bot commented Oct 29, 2026

Copy link
Copy Markdown

GH-160885 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 29, 2026
@StanFromIreland

Copy link
Copy Markdown
Member

Merged, thanks!

StanFromIreland added a commit that referenced this pull request Oct 29, 2026
…#160885)

(cherry picked from commit 7e5227f)

Co-authored-by: Alon <alonfaraj@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Oct 29, 2026
…#160884)

(cherry picked from commit 7e5227f)

Co-authored-by: Alon <alonfaraj@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use-after-free in OSError.__str__ when a formatting callback clears filename

2 participants