Skip to content

require capsule destructor closures to be 'static#6049

Merged
davidhewitt merged 2 commits into
PyO3:mainfrom
davidhewitt:capsule-destructor-lifetimes
May 16, 2026
Merged

require capsule destructor closures to be 'static#6049
davidhewitt merged 2 commits into
PyO3:mainfrom
davidhewitt:capsule-destructor-lifetimes

Conversation

@davidhewitt
Copy link
Copy Markdown
Member

It turns out that while we have T: 'static requirements on capsule constructors, for the variants which also accept F type parameters for destructors we lack F: 'static.

This of course means that it's possible to define a destructor which captures data by-reference from the stack, which will most likely lead to instant segfault when the destructor runs on capsule cleanup.

I am undecided whether this is a severe enough issue that it warrants reporting to the Rust advisory-db; on the one hand it's very likely that code which accidentally did this would crash in test suites, and the fact nobody reported this probably means it doesn't exist in the wild. On the other hand, it is possible that there are accidental UAFs implemented in user code due to this gap. Opinions welcome.

(Credit to Codex security scanning for this discovery.)

Copy link
Copy Markdown
Contributor

@Tpt Tpt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very likely that code which accidentally did this would crash in test suites, and the fact nobody reported this probably means it doesn't exist in the wild

Agreed. This is the kind of thing that is not checked at all by C/C++ and wouldn't be a CVE there.

@davidhewitt davidhewitt mentioned this pull request May 16, 2026
@davidhewitt davidhewitt enabled auto-merge May 16, 2026 13:20
@davidhewitt davidhewitt added this pull request to the merge queue May 16, 2026
Merged via the queue into PyO3:main with commit a792452 May 16, 2026
48 checks passed
@davidhewitt davidhewitt deleted the capsule-destructor-lifetimes branch May 16, 2026 14:35
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.

2 participants