Skip to content

Commit 9cb4348

Browse files
authored
Merge pull request #7617 from MidnightRocket/mktemp/prevent-race-condition-tempdir-permissions
mktemp: Prevent race condition when setting permissions for tempdir
2 parents ed8689e + 32fed17 commit 9cb4348

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/uu/mktemp/src/mktemp.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,18 @@ fn dry_exec(tmpdir: &Path, prefix: &str, rand: usize, suffix: &str) -> UResult<P
458458
fn make_temp_dir(dir: &Path, prefix: &str, rand: usize, suffix: &str) -> UResult<PathBuf> {
459459
let mut builder = Builder::new();
460460
builder.prefix(prefix).rand_bytes(rand).suffix(suffix);
461+
462+
// On *nix platforms grant read-write-execute for owner only.
463+
// The directory is created with these permission at creation time, using mkdir(3) syscall.
464+
// This is not relevant on Windows systems. See: https://docs.rs/tempfile/latest/tempfile/#security
465+
// `fs` is not imported on Windows anyways.
466+
#[cfg(not(windows))]
467+
builder.permissions(fs::Permissions::from_mode(0o700));
468+
461469
match builder.tempdir_in(dir) {
462470
Ok(d) => {
463471
// `into_path` consumes the TempDir without removing it
464472
let path = d.into_path();
465-
#[cfg(not(windows))]
466-
fs::set_permissions(&path, fs::Permissions::from_mode(0o700))?;
467473
Ok(path)
468474
}
469475
Err(e) if e.kind() == ErrorKind::NotFound => {

0 commit comments

Comments
 (0)