From dd2f889af01fdc22e0b393085e85879a78de186e Mon Sep 17 00:00:00 2001 From: Pierre Warnier Date: Mon, 23 Mar 2026 16:56:21 +0100 Subject: [PATCH] =?UTF-8?q?security:=20address=20Copilot=20review=20on=20P?= =?UTF-8?q?R=20#46=20=E2=80=94=20platform=20guards,=20error=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shadow-core/src/atomic.rs | 5 ++++- src/uu/passwd/src/passwd.rs | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/shadow-core/src/atomic.rs b/src/shadow-core/src/atomic.rs index 9da92d9..f1a70d4 100644 --- a/src/shadow-core/src/atomic.rs +++ b/src/shadow-core/src/atomic.rs @@ -112,7 +112,10 @@ where // Zero-length output guard: a zero-length shadow file locks out all users. // OpenBSD checks this in pw_mkdb before replacing the original. - let written = tmp_file.metadata().map(|m| m.len()).unwrap_or(0); + let written = tmp_file + .metadata() + .map_err(|e| ShadowError::IoPath(e, tmp_path.clone()))? + .len(); if written == 0 { return Err(ShadowError::Other( "refusing to write zero-length file".into(), diff --git a/src/uu/passwd/src/passwd.rs b/src/uu/passwd/src/passwd.rs index 60bf54d..1a5b040 100644 --- a/src/uu/passwd/src/passwd.rs +++ b/src/uu/passwd/src/passwd.rs @@ -117,12 +117,16 @@ impl UError for PasswdError { /// OpenBSD's `pw_init()` sets `RLIMIT_CORE=0`. A core dump from a setuid /// passwd process could expose password hashes and plaintext passwords. fn suppress_core_dumps() { - // RLIMIT_CORE = 0: no core dumps. + // RLIMIT_CORE = 0: no core dumps. Best-effort — ignore errors. let _ = nix::sys::resource::setrlimit(nix::sys::resource::Resource::RLIMIT_CORE, 0, 0); // PR_SET_DUMPABLE = 0: prevent ptrace attachment and /proc/pid/mem reads. - // SAFETY: prctl with PR_SET_DUMPABLE is a simple flag set, no pointers. - unsafe { - libc::prctl(libc::PR_SET_DUMPABLE, 0); + // Linux-specific — silently skipped on other platforms. + #[cfg(target_os = "linux")] + { + // SAFETY: prctl with PR_SET_DUMPABLE is a simple flag set, no pointers. + unsafe { + libc::prctl(libc::PR_SET_DUMPABLE, 0); + } } } @@ -1572,14 +1576,14 @@ mod tests { #[test] fn test_raise_file_size_limit() { - // After calling raise_file_size_limit(), `RLIMIT_FSIZE` should be RLIM_INFINITY. raise_file_size_limit(); let (soft, _hard) = nix::sys::resource::getrlimit(nix::sys::resource::Resource::RLIMIT_FSIZE).unwrap(); - assert_eq!( - soft, - nix::sys::resource::RLIM_INFINITY, - "`RLIMIT_FSIZE` should be RLIM_INFINITY" + // In environments where the hard limit is already restricted (containers, + // CI), we may not reach RLIM_INFINITY. Verify it's at least very large. + assert!( + soft >= 1024 * 1024 * 1024 || soft == nix::sys::resource::RLIM_INFINITY, + "RLIMIT_FSIZE should be raised (got {soft})" ); }