-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Mkdir setgid #10428 #10456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Mkdir setgid #10428 #10456
Changes from all commits
5425415
ede0cfb
7c235a4
1cdbd4f
9baf20f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,11 @@ use clap::builder::ValueParser; | |
| use clap::parser::ValuesRef; | ||
| use clap::{Arg, ArgAction, ArgMatches, Command}; | ||
| use std::ffi::OsString; | ||
| #[cfg(all(unix, not(target_os = "linux")))] | ||
| use std::os::unix::fs::MetadataExt; | ||
| use std::io::{Write, stdout}; | ||
| use std::path::{Path, PathBuf}; | ||
| #[cfg(all(unix, target_os = "linux"))] | ||
| #[cfg(not(windows))] | ||
| use uucore::error::FromIo; | ||
| use uucore::error::{UResult, USimpleError}; | ||
| use uucore::translate; | ||
|
|
@@ -196,8 +198,9 @@ pub fn mkdir(path: &Path, config: &Config) -> UResult<()> { | |
| create_dir(path, false, config) | ||
| } | ||
|
|
||
| /// Only needed on Linux to add ACL permission bits after directory creation. | ||
| #[cfg(all(unix, target_os = "linux"))] | ||
| /// Only needed on Linux to add ACL permission bits after directory creation and on | ||
| /// macos / bsd to inherit setgid bits | ||
| #[cfg(not(windows))] | ||
| fn chmod(path: &Path, mode: u32) -> UResult<()> { | ||
| use std::fs::{Permissions, set_permissions}; | ||
| use std::os::unix::fs::PermissionsExt; | ||
|
|
@@ -293,6 +296,25 @@ fn create_dir_with_mode(path: &Path, _mode: u32) -> std::io::Result<()> { | |
| std::fs::create_dir(path) | ||
| } | ||
|
|
||
| // macos and bsd systems do not inherit the parents directories SETGID bit on the kernel | ||
| // level so we have to manually set the SETGID | ||
| #[cfg(all(unix, not(target_os = "linux")))] | ||
| fn inherit_setgid_bit(path: &Path) -> UResult<()> { | ||
| if let Some(parent) = path.parent() { | ||
| if let Ok(parent_metadata) = parent.metadata() { | ||
| if let Ok(paths_metadata) = path.metadata() { | ||
| if (parent_metadata.mode() & 0o2000) != (paths_metadata.mode() & 0o2000) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. condition checks if bits are different, should check if parent has setgid AND child doesn't: `(parent_metadata.mode() & 0o2000) != 0 && (paths_metadata.mode() & 0o2000) == 0 no ? |
||
| chmod( | ||
| path, | ||
| paths_metadata.mode() | parent_metadata.mode() & 0o2000, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
| )?; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Helper function to create a single directory with appropriate permissions | ||
| // `is_parent` argument is not used on windows | ||
| #[allow(unused_variables)] | ||
|
|
@@ -319,6 +341,10 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<( | |
| translate!("mkdir-verbose-created-directory", "util_name" => uucore::util_name(), "path" => path.quote()) | ||
| )?; | ||
| } | ||
| // macos and bsd systems do not inherit the parents directories SETGID bit on the kernel | ||
| // level so we have to manually set the SETGID | ||
| #[cfg(all(unix, not(target_os = "linux")))] | ||
| inherit_setgid_bit(path)?; | ||
|
|
||
| // On Linux, we may need to add ACL permission bits via chmod. | ||
| // On other Unix systems, the directory was already created with the correct mode. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens in case of error on metadata() ?