Setting a file’s “other” permission bit to writable (modes like 0o777, 0o666, 0o776, 0o002) lets any local user on the system overwrite the file. This enables privilege escalation if the file is executable, data injection if it’s read by privileged processes, and tampering of secrets, config, or audit logs. Prefer 0o644 for regular files and 0o755 for executables.
usestd::fs::Permissions;usestd::os::unix::fs::PermissionsExt;fninvalid()-> std::io::Result<()>{// World-writable — classic mistakes
let_=Permissions::from_mode(0o777);let_=Permissions::from_mode(0o666);// Method form
letmutp=Permissions::from_mode(0o600);p.set_mode(0o777);p.set_mode(0o666);// Other world-writable triplets
let_=Permissions::from_mode(0o776);// last digit 6 — rw for others
let_=Permissions::from_mode(0o002);// last digit 2 — write only
let_=Permissions::from_mode(0o773);// last digit 3 — wx for others
// 4-digit (with sticky/setuid) — still world-writable
let_=Permissions::from_mode(0o7777);// Via the trait name
let_=PermissionsExt::from_mode(0o777);// Fully qualified
let_=std::os::unix::fs::PermissionsExt::from_mode(0o666);// Underscore separators
let_=Permissions::from_mode(0o7_7_7);Ok(())}
Compliant Code Examples
usestd::fs::Permissions;usestd::os::unix::fs::PermissionsExt;fnvalid()-> std::io::Result<()>{// Restrictive modes — no world write
let_=Permissions::from_mode(0o644);// owner rw, group/other r
let_=Permissions::from_mode(0o755);// typical executable
let_=Permissions::from_mode(0o600);// owner-only rw
let_=Permissions::from_mode(0o770);// group-writable but NOT world
let_=Permissions::from_mode(0o640);// last digit 0 — fine
// Method form, restrictive mode
letmutp=Permissions::from_mode(0o644);p.set_mode(0o755);// Non-literal mode — can't tell statically, don't flag
letmode: u32=std::env::var("MODE").unwrap().parse().unwrap();p.set_mode(mode);// Unrelated method named set_mode but with non-octal literal
structWidget;implWidget{fnset_mode(&self,_: u32){}}letw=Widget;w.set_mode(0o644);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