Creating a directory with DirBuilder::new().mode(0o777) (or any other mode whose “other” triplet has the write bit set) lets any local user add, remove, or replace files inside the directory regardless of the individual files’ permissions. This can enable symlink attacks, race conditions during file replacement, and privilege escalation if a privileged process later reads or executes files from the directory. Prefer 0o755 for directories that need to be world-readable or 0o750 to restrict to a group.
usestd::fs::DirBuilder;usestd::os::unix::fs::DirBuilderExt;fninvalid()-> std::io::Result<()>{// World-writable — classic mistakes
DirBuilder::new().mode(0o777).create("/tmp/a")?;DirBuilder::new().mode(0o666).create("/tmp/b")?;// Other world-writable triplets
DirBuilder::new().mode(0o776).create("/tmp/c")?;// last digit 6 — rw for others
DirBuilder::new().mode(0o002).create("/tmp/d")?;// last digit 2 — write only
DirBuilder::new().mode(0o773).create("/tmp/e")?;// last digit 3 — wx for others
// 4-digit (with sticky/setuid) — still world-writable
DirBuilder::new().mode(0o7777).create("/tmp/f")?;// Fully qualified path
std::fs::DirBuilder::new().mode(0o777).create("/tmp/g")?;// Underscore separators
DirBuilder::new().mode(0o7_7_7).create("/tmp/h")?;Ok(())}
Compliant Code Examples
usestd::fs::DirBuilder;usestd::os::unix::fs::DirBuilderExt;fnvalid()-> std::io::Result<()>{// Valid modes — no world write
DirBuilder::new().mode(0o755).create("/tmp/a")?;// world-readable, not writable
DirBuilder::new().mode(0o750).create("/tmp/b")?;// group-readable only
DirBuilder::new().mode(0o700).create("/tmp/c")?;// owner-only
DirBuilder::new().mode(0o770).create("/tmp/d")?;// group-writable but NOT world
DirBuilder::new().mode(0o740).create("/tmp/e")?;// last digit 0 — fine
Ok(())}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
How to use this rule
1
2
rulesets:- rust-security # Rules to enforce Rust security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Security scans to your CI pipelines