rust-lang / rust-lang/rust-clippy

TOCTOU: File creation followed by setting permissions

Open
#17,154 1 comment 0 reactions 1 assignee View on GitHub

@asder8215 is already working on this.

Since Aug 2, 2026.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Detect when a file is being created and then the same file path is used to set file permissions. This is a classic source of Time Of Check, Time Of Use bugs.

Advantage

The recommended code creates the file and sets its attributes in a single syscall, avoiding TOCTOU bugs.

Drawbacks

No response

Example
    // Create with default permissions
    fs::create_dir(&path)?;
    // Fix up permissions
    fs::set_permissions(&path, Permissions::from_mode(0o700))?;

Could be written using OpenOptions::mode() and DirBuilderExt::mode().

Comparison with existing lints

No response

Additional Context

This is one of the bugs found in uutils during Canonical's audit: https://corrode.dev/blog/bugs-rust-wont-catch/#set-permissions-at-creation-time-not-after

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.