mirror of
https://github.com/uutils/shadow.git
synced 2026-06-10 16:14:57 -07:00
security: address Copilot review on PR #46 — platform guards, error handling
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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})"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user