Skip to content

Commit 28fd25f

Browse files
committed
fix(install): zero umask at startup so ancestor dirs are created at exact 0755
GNU install calls umask(0) early in main() so that ancestor directories created by -D/-d always get exactly DEFAULT_MODE (0755) regardless of the caller's umask. Our implementation passed DEFAULT_MODE to mkdirat / fs::create_dir_all without zeroing umask first, so restrictive umasks (e.g. 0027, 0077, 0111) produced wrong ancestor modes. Changes: - Add zero_umask() to uucore::mode using the existing rustix dependency - Call it at the top of install's uumain (unix only) - Fix the -d code path to use DirBuilder::mode(DEFAULT_MODE) instead of fs::create_dir_all, which defaults to 0777 and would create 0777 ancestors with umask now zeroed - Replace the broken probe-based assertions in two tests with direct assert_eq!(0o40_755) checks, matching the GNU-guaranteed value Fixes uutils#12714 Related: uutils#11363, uutils#12713
1 parent 2c9a666 commit 28fd25f

3 files changed

Lines changed: 46 additions & 17 deletions

File tree

‎src/uu/install/src/install.rs‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
191191
let args: Vec<OsString> = args.collect();
192192
// Kept for the caret in mode diagnostics, which needs the mode as typed.
193193
let diag_args = uucore::diagnostics::operands(&args);
194+
// Match GNU install: zero the process umask so ancestor directories and
195+
// target files are created with exact modes rather than umask-modified ones.
196+
// chmod is applied explicitly for the target, and ancestors always use
197+
// DEFAULT_MODE (0755) without umask interference.
198+
#[cfg(unix)]
199+
uucore::mode::zero_umask();
194200
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
195201

196202
let paths: Vec<OsString> = matches
@@ -512,11 +518,26 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
512518
//
513519
// NOTE: the GNU "install" sets the expected mode only for the
514520
// target directory. All created ancestor directories will have
515-
// the default mode. Hence it is safe to use fs::create_dir_all
516-
// and then only modify the target's dir mode.
517-
if let Err(e) = fs::create_dir_all(&path_to_create).map_err_context(
518-
|| translate!("install-error-create-dir-failed", "path" => path_to_create.quote()),
519-
) {
521+
// DEFAULT_MODE (0755). We use DirBuilder with an explicit mode
522+
// rather than fs::create_dir_all so that the zeroed umask
523+
// (set in uumain) does not cause ancestors to be created at 0777.
524+
#[cfg(unix)]
525+
{
526+
use std::os::unix::fs::DirBuilderExt;
527+
let mut builder = fs::DirBuilder::new();
528+
builder.recursive(true).mode(DEFAULT_MODE);
529+
if let Err(e) = builder
530+
.create(path_to_create.as_path())
531+
.map_err_context(|| translate!("install-error-create-dir-failed", "path" => path_to_create.as_path().quote()))
532+
{
533+
show!(e);
534+
continue;
535+
}
536+
}
537+
#[cfg(not(unix))]
538+
if let Err(e) = fs::create_dir_all(path_to_create.as_path())
539+
.map_err_context(|| translate!("install-error-create-dir-failed", "path" => path_to_create.as_path().quote()))
540+
{
520541
show!(e);
521542
continue;
522543
}

‎src/uucore/src/lib/features/mode.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,18 @@ pub fn get_umask() -> u32 {
387387
}
388388
}
389389

390+
/// Set the process umask to 0 and return the previous value.
391+
///
392+
/// GNU install calls umask(0) at startup so that directories and files are
393+
/// created with exact modes rather than umask-modified ones. Call this early
394+
/// in tools that need identical behaviour.
395+
#[cfg(unix)]
396+
pub fn zero_umask() -> u32 {
397+
use rustix::fs::Mode;
398+
use rustix::process::umask;
399+
umask(Mode::empty()).bits() as u32
400+
}
401+
390402
#[cfg(test)]
391403
mod tests {
392404

‎tests/by-util/test_install.rs‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ fn test_install_ancestors_mode_directories() {
102102
let target_dir = "ancestor1/ancestor2/target_dir";
103103
let directories_arg = "-d";
104104
let mode_arg = "--mode=200";
105-
let probe = "probe";
106-
107-
at.mkdir(probe);
108-
let default_perms = at.metadata(probe).permissions().mode();
109105

110106
ucmd.args(&[mode_arg, directories_arg, target_dir])
111107
.succeeds()
@@ -115,8 +111,10 @@ fn test_install_ancestors_mode_directories() {
115111
assert!(at.dir_exists(ancestor2));
116112
assert!(at.dir_exists(target_dir));
117113

118-
assert_eq!(default_perms, at.metadata(ancestor1).permissions().mode());
119-
assert_eq!(default_perms, at.metadata(ancestor2).permissions().mode());
114+
// GNU install zeros umask at startup and creates ancestor dirs at exactly
115+
// 0755 (DEFAULT_MODE). --mode applies only to the final target.
116+
assert_eq!(0o40_755_u32, at.metadata(ancestor1).permissions().mode());
117+
assert_eq!(0o40_755_u32, at.metadata(ancestor2).permissions().mode());
120118

121119
// Expected mode only on the target_dir.
122120
assert_eq!(0o40_200_u32, at.metadata(target_dir).permissions().mode());
@@ -131,10 +129,6 @@ fn test_install_ancestors_mode_directories_with_file() {
131129
let directories_arg = "-D";
132130
let mode_arg = "--mode=200";
133131
let file = "file";
134-
let probe = "probe";
135-
136-
at.mkdir(probe);
137-
let default_perms = at.metadata(probe).permissions().mode();
138132

139133
at.touch(file);
140134

@@ -146,8 +140,10 @@ fn test_install_ancestors_mode_directories_with_file() {
146140
assert!(at.dir_exists(ancestor2));
147141
assert!(at.file_exists(target_file));
148142

149-
assert_eq!(default_perms, at.metadata(ancestor1).permissions().mode());
150-
assert_eq!(default_perms, at.metadata(ancestor2).permissions().mode());
143+
// GNU install zeros umask at startup and creates ancestor dirs at exactly
144+
// 0755 (DEFAULT_MODE). --mode applies only to the final target.
145+
assert_eq!(0o40_755_u32, at.metadata(ancestor1).permissions().mode());
146+
assert_eq!(0o40_755_u32, at.metadata(ancestor2).permissions().mode());
151147

152148
// Expected mode only on the target_file.
153149
assert_eq!(0o100_200_u32, at.metadata(target_file).permissions().mode());

0 commit comments

Comments
 (0)