Skip to content

Commit b5b5e97

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 0c16403 commit b5b5e97

3 files changed

Lines changed: 47 additions & 17 deletions

File tree

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ static ARG_FILES: &str = "files";
186186
///
187187
#[uucore::main]
188188
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
189+
// Match GNU install: zero the process umask so ancestor directories and
190+
// target files are created with exact modes rather than umask-modified ones.
191+
// chmod is applied explicitly for the target, and ancestors always use
192+
// DEFAULT_MODE (0755) without umask interference.
193+
#[cfg(unix)]
194+
uucore::mode::zero_umask();
195+
189196
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
190197

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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ pub fn get_umask() -> u32 {
202202
}
203203
}
204204

205+
/// Set the process umask to 0 and return the previous value.
206+
///
207+
/// GNU install calls umask(0) at startup so that directories and files are
208+
/// created with exact modes rather than umask-modified ones. Call this early
209+
/// in tools that need identical behaviour.
210+
#[cfg(unix)]
211+
pub fn zero_umask() -> u32 {
212+
use rustix::fs::Mode;
213+
use rustix::process::umask;
214+
umask(Mode::empty()).bits() as u32
215+
}
216+
205217
#[cfg(test)]
206218
mod tests {
207219

‎tests/by-util/test_install.rs‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ fn test_install_ancestors_mode_directories() {
105105
let target_dir = "ancestor1/ancestor2/target_dir";
106106
let directories_arg = "-d";
107107
let mode_arg = "--mode=200";
108-
let probe = "probe";
109-
110-
at.mkdir(probe);
111-
let default_perms = at.metadata(probe).permissions().mode();
112108

113109
ucmd.args(&[mode_arg, directories_arg, target_dir])
114110
.succeeds()
@@ -118,8 +114,10 @@ fn test_install_ancestors_mode_directories() {
118114
assert!(at.dir_exists(ancestor2));
119115
assert!(at.dir_exists(target_dir));
120116

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

124122
// Expected mode only on the target_dir.
125123
assert_eq!(0o40_200_u32, at.metadata(target_dir).permissions().mode());
@@ -134,10 +132,6 @@ fn test_install_ancestors_mode_directories_with_file() {
134132
let directories_arg = "-D";
135133
let mode_arg = "--mode=200";
136134
let file = "file";
137-
let probe = "probe";
138-
139-
at.mkdir(probe);
140-
let default_perms = at.metadata(probe).permissions().mode();
141135

142136
at.touch(file);
143137

@@ -149,8 +143,10 @@ fn test_install_ancestors_mode_directories_with_file() {
149143
assert!(at.dir_exists(ancestor2));
150144
assert!(at.file_exists(target_file));
151145

152-
assert_eq!(default_perms, at.metadata(ancestor1).permissions().mode());
153-
assert_eq!(default_perms, at.metadata(ancestor2).permissions().mode());
146+
// GNU install zeros umask at startup and creates ancestor dirs at exactly
147+
// 0755 (DEFAULT_MODE). --mode applies only to the final target.
148+
assert_eq!(0o40_755_u32, at.metadata(ancestor1).permissions().mode());
149+
assert_eq!(0o40_755_u32, at.metadata(ancestor2).permissions().mode());
154150

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

0 commit comments

Comments
 (0)