Unix permission bits given as a decimal literal

ID

rust.correctness.unix_file_permissions_set_octal

Severity

low

Remediation Complexity

trivial

Remediation Risk

medium

Remediation Effort

low

Resource

Idiom

Language

Rust

Description

Reports a Unix permission mode passed as a decimal literal to mode, set_mode or from_mode. Rust has no octal-by-leading-zero rule, so 600 is decimal six hundred and not the intended rw-------.

Rationale

Permission bits are three octal digits, and every reference and chmod invocation writes them that way. A decimal literal is read as those same digits but stored as a different bit pattern - 600 becomes 0o1130, which sets the setgid bit and grants group write - so the file ends up with permissions nobody in the review chain intended. Nothing in the type system or at run time flags the mismatch.

The following code illustrates the pattern detected by this rule:

use std::fs::{self, DirBuilder, OpenOptions, Permissions};
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt};
fn create_key_file(path: &str) -> std::io::Result<std::fs::File> {
    // FLAGGED: Unix permission bits given as a decimal literal
    OpenOptions::new().write(true).create(true).mode(600).open(path)
}

Remediation

Write the mode as an octal literal: 0o600, 0o644, 0o755. When the value comes from configuration, parse it with u32::from_str_radix(value, 8) so the same base applies.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved