Skip to content

Avoid getattrlist TOCTOU issue for atomic file writing#1805

Merged
jmschonfeld merged 1 commit intoswiftlang:mainfrom
jmschonfeld:data-writing-unique
Mar 10, 2026
Merged

Avoid getattrlist TOCTOU issue for atomic file writing#1805
jmschonfeld merged 1 commit intoswiftlang:mainfrom
jmschonfeld:data-writing-unique

Conversation

@jmschonfeld
Copy link
Copy Markdown
Contributor

Use FSOPT_UNIQUE in getattrlist when writing files atomically

Motivation:

getattrlist can provide stale information due to TOCTOU behavior exhibited by the kernel. To avoid this issue, we can use FSOPT_UNIQUE to have the kernel fail for us if there are multiple hard link locations rather than checking the returned data ourselves

Modifications:

Adds FSOPT_UNIQUE to the getattrlist calls in data atomic writing and remove the nlink check of the result

Result:

We no longer read potentially stale information from the getattrlist result

Testing:

This is a small window for a race condition in the kernel that is not deterministically testable. It is also in FOUNDATION_FRAMEWORK code

@jmschonfeld jmschonfeld requested a review from a team as a code owner March 10, 2026 02:08
@jmschonfeld
Copy link
Copy Markdown
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit 19afe00 into swiftlang:main Mar 10, 2026
22 checks passed
@jmschonfeld jmschonfeld deleted the data-writing-unique branch March 10, 2026 16:19
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