Skip to content

Commit c8d9500

Browse files
committed
add test that root can bypass security measures
1 parent fb00263 commit c8d9500

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/system/audit.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use std::os::unix::{
1010
};
1111
use std::path::{Component, Path};
1212

13-
use super::{cerr, inject_group, set_supplementary_groups, Group, GroupId, User, UserId};
13+
use super::{
14+
cerr, inject_group, interface::UnixUser, set_supplementary_groups, Group, GroupId, User, UserId,
15+
};
1416
use crate::common::resolve::CurrentUser;
1517

1618
/// Temporary change privileges --- essentially a 'mini sudo'
@@ -109,6 +111,7 @@ pub fn secure_open_cookie_file(path: impl AsRef<Path>) -> io::Result<File> {
109111
.read(true)
110112
.write(true)
111113
.create(true)
114+
.truncate(false)
112115
.mode(mode(Category::Owner, Op::Write) | mode(Category::Owner, Op::Read));
113116

114117
secure_open_impl(path.as_ref(), &mut open_options, true, true)
@@ -214,7 +217,16 @@ pub fn secure_open_for_sudoedit(
214217
target_group: &Group,
215218
) -> io::Result<File> {
216219
sudo_call(target_user, target_group, || {
217-
traversed_secure_open(path, current_user)
220+
if current_user.is_root() {
221+
OpenOptions::new()
222+
.read(true)
223+
.write(true)
224+
.create(true)
225+
.truncate(false)
226+
.open(path)
227+
} else {
228+
traversed_secure_open(path, current_user)
229+
}
218230
})?
219231
}
220232

test-framework/sudo-compliance-tests/src/sudoedit/limits.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ fn cannot_edit_writable_paths() {
4040
}
4141
}
4242

43+
#[test]
44+
fn can_edit_writable_paths_as_root() {
45+
// note: we already have tests that sudoedit "works" so we are skipping
46+
// the content check here---the point here is that sudoedit does not stop
47+
// the user.
48+
49+
let env = Env(SUDOERS_ALL_ALL_NOPASSWD)
50+
.user(USERNAME)
51+
.directory(Directory("/tmp/bar").chmod("755"))
52+
.file(DEFAULT_EDITOR, TextFile(EDITOR_DUMMY).chmod(CHMOD_EXEC))
53+
.build();
54+
55+
let file = "/tmp/foo.sh";
56+
Command::new("sudoedit")
57+
.arg(file)
58+
.output(&env)
59+
.assert_success();
60+
}
61+
4362
#[test]
4463
fn cannot_edit_symlinks() {
4564
let env = Env(SUDOERS_ALL_ALL_NOPASSWD)

0 commit comments

Comments
 (0)