Skip to content
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

sudo: respect --login regardless of the presence of --chdir #794

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/common/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ impl CommandAndArguments {
arguments = vec!["-c".to_string(), escaped(arguments)]
}
} else {
command = arguments
.get(0)
.map(|s| s.into())
.unwrap_or_else(PathBuf::new);
command = arguments.get(0).map(|s| s.into()).unwrap_or_default();
arguments.remove(0);

// remember the original binary name before resolving symlinks; this is not
Expand Down
28 changes: 15 additions & 13 deletions src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,22 @@
if let Some(arg0) = options.arg0() {
command.arg0(arg0);
}

if options.is_login() {
// signal to the operating system that the command is a login shell by prefixing "-"
let mut process_name = qualified_path
.file_name()
.map(|osstr| osstr.as_bytes().to_vec())
.unwrap_or_else(Vec::new);
process_name.insert(0, b'-');
command.arg0(OsStr::from_bytes(&process_name));
}

Check warning on line 76 in src/exec/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/exec/mod.rs#L68-L76

Added lines #L68 - L76 were not covered by tests

// Decide if the pwd should be changed. `--chdir` takes precedence over `-i`.
let path = options.chdir().cloned().or_else(|| {
options.is_login().then(|| {
// signal to the operating system that the command is a login shell by prefixing "-"
let mut process_name = qualified_path
.file_name()
.map(|osstr| osstr.as_bytes().to_vec())
.unwrap_or_else(Vec::new);
process_name.insert(0, b'-');
command.arg0(OsStr::from_bytes(&process_name));

options.user().home.clone()
})
});
let path = options
.chdir()
.cloned()
.or_else(|| options.is_login().then(|| options.user().home.clone()));

Check warning on line 82 in src/exec/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/exec/mod.rs#L79-L82

Added lines #L79 - L82 were not covered by tests

// set target user and groups
set_target_user(
Expand Down
16 changes: 16 additions & 0 deletions test-framework/sudo-compliance-tests/src/sudo/flag_chdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,19 @@ fn target_user_has_insufficient_perms() -> Result<()> {

Ok(())
}

#[test]
fn flag_login_is_respected() -> Result<()> {
let expected = "-bash";
let env = Env("ALL ALL=(ALL:ALL) CWD=* ALL").build()?;

let output = Command::new("sh")
.arg("-c")
.arg("sudo --login --chdir /tmp echo '$0'")
.output(&env)?
.stdout()?;

assert_eq!(expected, output);

Ok(())
}
Loading