Debian Codegraph @

src/system/audit.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #![cfg_attr(not(feature = "unstable-remote-sudoers"), allow(unused_imports))]
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#, c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#};
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/, DirBuilderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/DirBuilder#, Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, Metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/Metadata#, OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#};
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#, Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#, ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#};
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::Shutdownrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/Shutdown#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/::{AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#, AsRawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/AsRawFd#, BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#, FromRawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/FromRawFd#, OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#};
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::{
9 ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#,
10 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/::{DirBuilderExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/DirBuilderExt#, MetadataExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/MetadataExt#, PermissionsExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/PermissionsExt#},
11 netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/::UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#,
12 preluderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/prelude/::OpenOptionsExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/OpenOptionsExt#,
13 };
14 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#, Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#};
15
16 use superrust-analyzer cargo sudo-rs 0.2.14 system/::{
17 Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#, GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#, Userrust-analyzer cargo sudo-rs 0.2.14 system/User#, UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#, cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr()., inject_grouprust-analyzer cargo sudo-rs 0.2.14 system/inject_group()., interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/::UnixUserrust-analyzer cargo sudo-rs 0.2.14 system/interface/UnixUser#, set_supplementary_groupsrust-analyzer cargo sudo-rs 0.2.14 system/set_supplementary_groups().,
18 };
19 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::commonrust-analyzer cargo sudo-rs 0.2.14 common/::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/::CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#;
20
21 #[cfg(target_os = "linux")]
22 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn no_new_privs_enabledrust-analyzer cargo sudo-rs 0.2.14 system/audit/no_new_privs_enabled().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<bool> {
23 // SAFETY: prctl(PR_GET_NO_NEW_PRIVS) can never cause UB
24 let no_new_privs =
25 craterust-analyzer cargo sudo-rs 0.2.14 crate/::cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::prctlrust-analyzer cargo libc 0.2.186 prctl().(libcrust-analyzer cargo libc 0.2.186 crate/::PR_GET_NO_NEW_PRIVSrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/PR_GET_NO_NEW_PRIVS., 0, 0, 0, 0) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
26 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(no_new_privs rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().0)
27 }
28
29 /// Temporary change privileges --- essentially a 'mini sudo'
30 /// This is only used for sudoedit.
31 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn sudo_callrust-analyzer cargo sudo-rs 0.2.14 system/audit/sudo_call().<T>(
32 target_user: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
33 target_group: &Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#,
34 operation: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T,
35 ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<T> {
36 const KEEP_UIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_UID.: libcrust-analyzer cargo libc 0.2.186 crate/::uid_trust-analyzer cargo libc 0.2.186 unix/uid_t# = -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1i32 as libcrust-analyzer cargo libc 0.2.186 crate/::uid_trust-analyzer cargo libc 0.2.186 unix/uid_t#;
37 const KEEP_GIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_GID.: libcrust-analyzer cargo libc 0.2.186 crate/::gid_trust-analyzer cargo libc 0.2.186 unix/gid_t# = -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1i32 as libcrust-analyzer cargo libc 0.2.186 crate/::gid_trust-analyzer cargo libc 0.2.186 unix/gid_t#;
38
39 // SAFETY: these libc functions are always safe to call
40 let (cur_user_id, cur_group_id) =
41 unsafe { (UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]new().(libcrust-analyzer cargo libc 0.2.186 crate/::geteuidrust-analyzer cargo libc 0.2.186 geteuid().()), GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(libcrust-analyzer cargo libc 0.2.186 crate/::getegidrust-analyzer cargo libc 0.2.186 getegid().())) };
42
43 let cur_groups = {
44 // SAFETY: calling with size 0 does not modify through the pointer, and is
45 // a documented way of getting the length needed.
46 let len = cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::getgroupsrust-analyzer cargo libc 0.2.186 getgroups().(0, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().()) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
47
48 let mut buf: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(KEEP_GIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_GID.); len as usize];
49 // SAFETY: we pass a correct pointer to a slice of the given length
50 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe {
51 // We can cast to gid_t because `GroupId` is marked as transparent
52 libcrust-analyzer cargo libc 0.2.186 crate/::getgroupsrust-analyzer cargo libc 0.2.186 getgroups().(len, buf.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<libcrust-analyzer cargo libc 0.2.186 crate/::gid_trust-analyzer cargo libc 0.2.186 unix/gid_t#>())
53 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
54
55 buf
56 };
57
58 let mut target_groups = target_user.groupsrust-analyzer cargo sudo-rs 0.2.14 system/User#groups..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().();
59 inject_grouprust-analyzer cargo sudo-rs 0.2.14 system/inject_group().(target_group.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid., &mut target_groups);
60
61 if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(test)
62 && target_user.uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid. rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId][`PartialEq<Self>`]eq().==rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId][`PartialEq<Self>`]eq(). rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId][`PartialEq<Self>`]eq().cur_user_id
63 && target_group.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid. rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId][`PartialEq<Self>`]eq().==rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId][`PartialEq<Self>`]eq(). rust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId][`PartialEq<Self>`]eq().cur_group_id
64 && target_groups.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<_>>() rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`][`PartialEq<Self>`]eq().cur_groups.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<_>>()
65 {
66 // we are not actually switching users, simply run the closure
67 // (this would also be safe in production mode, but it is a needless check)
68 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(operation());
69 }
70
71 struct ResetUserGuardrust-analyzer cargo sudo-rs 0.2.14 system/audit/ResetUserGuard#(UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#, GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#>);
72
73 impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for ResetUserGuardrust-analyzer cargo sudo-rs 0.2.14 system/audit/ResetUserGuard# {
74 fn droprust-analyzer cargo sudo-rs 0.2.14 system/audit/impl#[ResetUserGuard][Drop]drop().(&mut self) {
75 // restore privileges in reverse order
76 (|| {
77 // SAFETY: this function is always safe to call
78 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::setresuidrust-analyzer cargo libc 0.2.186 setresuid().(KEEP_UIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_UID., UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().(&self.0rust-analyzer cargo sudo-rs 0.2.14 system/audit/ResetUserGuard#0.), KEEP_UIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_UID.) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
79 // SAFETY: this function is always safe to call
80 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::setresgidrust-analyzer cargo libc 0.2.186 setresgid().(KEEP_GIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_GID., GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]inner().(&self.1rust-analyzer cargo sudo-rs 0.2.14 system/audit/ResetUserGuard#1.), KEEP_GIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_GID.) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
81 set_supplementary_groupsrust-analyzer cargo sudo-rs 0.2.14 system/set_supplementary_groups().(&self.2rust-analyzer cargo sudo-rs 0.2.14 system/audit/ResetUserGuard#2.)
82 })()
83 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("could not restore to saved user id");
84 }
85 }
86
87 let guard = ResetUserGuardrust-analyzer cargo sudo-rs 0.2.14 system/audit/ResetUserGuard#(cur_user_id, cur_group_id, cur_groups);
88
89 set_supplementary_groupsrust-analyzer cargo sudo-rs 0.2.14 system/set_supplementary_groups().(&target_groups)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
90 // SAFETY: this function is always safe to call
91 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::setresgidrust-analyzer cargo libc 0.2.186 setresgid().(KEEP_GIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_GID., GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]inner().(&target_group.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid.), KEEP_GIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_GID.) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
92 // SAFETY: this function is always safe to call
93 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::setresuidrust-analyzer cargo libc 0.2.186 setresuid().(KEEP_UIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_UID., UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().(&target_user.uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid.), KEEP_UIDrust-analyzer cargo sudo-rs 0.2.14 system/audit/KEEP_UID.) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
94
95 let result = operation();
96
97 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(guard);
98 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result)
99 }
100
101 /// Drop root privileges by setting effective user id equal to the real user id.
102 /// This routine will panic if the process is not privileged.
103 pub fn irrevocably_drop_privilegesrust-analyzer cargo sudo-rs 0.2.14 system/audit/irrevocably_drop_privileges().() {
104 // SAFETY:
105 // - getuid() and geteuid() are always safe to call
106 // - setuid() does not change any memory and only affects OS state.
107 unsafe {
108 let real_uid = libcrust-analyzer cargo libc 0.2.186 crate/::getuidrust-analyzer cargo libc 0.2.186 getuid().();
109 let effective_uid = libcrust-analyzer cargo libc 0.2.186 crate/::geteuidrust-analyzer cargo libc 0.2.186 geteuid().();
110 // we never use setuid/setgid/setgroups except in the pre_exec hook before exec'ing,
111 // or in sudo_call (which always resets the user state after the closure finishes).
112 // this extra check is here to punish programming mistakes due to sloppiness.
113 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(effective_uid, UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::ROOTrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]ROOT..innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().(), "setuid violation");
114
115 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::setuidrust-analyzer cargo libc 0.2.186 setuid().(real_uid)).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("setuid violation");
116 }
117 }
118
119 // of course we can also write "file & 0o040 != 0", but this makes the intent explicit
120 enum Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op# {
121 Readrust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Read# = 4,
122 Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write# = 2,
123 Execrust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Exec# = 1,
124 }
125 enum Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category# {
126 Ownerrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Owner# = 2,
127 Grouprust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Group# = 1,
128 Worldrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#World# = 0,
129 }
130
131 fn moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(who: Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#, what: Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#) -> u32 {
132 (what as u32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<Self>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<Self>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<Self>`]shl().(3 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Mul<Self>`]mul().who as u32)
133 }
134
135 /// Open sudo configuration using various security checks
136 pub fn secure_open_sudoersrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_sudoers().(path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#> {
137 let mut open_options = OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]new().();
138 open_options.readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]read().(true);
139
140 secure_open_implrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_impl().(path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), &mut open_options, false)
141 }
142
143 #[cfg(feature = "unstable-remote-sudoers")]
144 pub fn secure_open_remote_sudoers(path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#<UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#>> {
145 secure_open_socket_impl(path.as_ref())
146 }
147
148 /// Open a timestamp cookie file using various security checks
149 pub fn secure_open_cookie_filerust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_cookie_file().(path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#> {
150 let mut open_options = OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]new().();
151 open_options
152 .readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]read().(true)
153 .writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]write().(true)
154 .createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]create().(true)
155 .truncaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]truncate().(false)
156 .moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[OpenOptions][OpenOptionsExt]mode().(moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Ownerrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Owner#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Ownerrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Owner#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Readrust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Read#));
157
158 secure_open_implrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_impl().(path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), &mut open_options, true)
159 }
160
161 /// Return the system zoneinfo path after validating that it is safe
162 pub fn zoneinfo_pathrust-analyzer cargo sudo-rs 0.2.14 system/audit/zoneinfo_path().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'static str> {
163 let paths = [
164 "/usr/share/zoneinfo",
165 "/usr/share/lib/zoneinfo",
166 "/usr/lib/zoneinfo",
167 ];
168
169 paths.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|p| {
170 let path = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(p);
171 path.metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]metadata().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|meta| checksrust-analyzer cargo sudo-rs 0.2.14 system/audit/checks().(path, meta)).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()
172 })
173 }
174
175 fn checksrust-analyzer cargo sudo-rs 0.2.14 system/audit/checks().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, meta: Metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/Metadata#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
176 let error = |msg| Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::PermissionDeniedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#PermissionDenied#, msg);
177
178 let path_mode = meta.permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]permissions().().moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[Permissions][PermissionsExt]mode().();
179 if meta.uidrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]uid().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0 {
180 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error(xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!(
181 "{path} must be owned by root",
182 path = path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
183 )))
184 } else if meta.gidrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]gid().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0 && (path_mode rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Grouprust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Group#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0) {
185 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error(xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!(
186 "{path} cannot be group-writable",
187 path = path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
188 )))
189 } else if path_mode rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Worldrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#World#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0 {
190 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error(xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!(
191 "{path} cannot be world-writable",
192 path = path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
193 )))
194 } else {
195 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
196 }
197 }
198
199 // Open `path` with options `open_options`, provided that it is "secure".
200 // "Secure" means that it passes the `checks` function above.
201 // If `check_parent_dir` is set, also check that the parent directory is "secure" also.
202 // If `create_parent_dirs` is set, create the path to the file if it does not already exist.
203 fn secure_open_implrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_impl().(
204 path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#,
205 open_options: &mut OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#,
206 create_parent_dirs: bool,
207 ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#> {
208 let error = |msg| Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::PermissionDeniedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#PermissionDenied#, msg);
209 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent_dir) = path.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().() {
210 // if we should create parent dirs and it does not yet exist, create it
211 if create_parent_dirs && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().parent_dir.existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() {
212 DirBuilderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/DirBuilder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirBuilder]new().()
213 .recursiverust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirBuilder]recursive().(true)
214 .moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::DirBuilder`][DirBuilderExt]mode().(
215 moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Ownerrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Owner#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write#)
216 |rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Ownerrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Owner#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Readrust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Read#)
217 |rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Ownerrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Owner#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Execrust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Exec#)
218 |rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Grouprust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Group#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Execrust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Exec#)
219 |rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Worldrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#World#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Execrust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Exec#),
220 )
221 .createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirBuilder]create().(parent_dir)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
222 }
223
224 let parent_meta = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/metadata().(parent_dir)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
225 checksrust-analyzer cargo sudo-rs 0.2.14 system/audit/checks().(parent_dir, parent_meta)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
226 } else {
227 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error(xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!(
228 "{path} has no valid parent directory",
229 path = path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
230 )));
231 }
232
233 let file = open_options.openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
234 let meta = file.metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]metadata().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
235 checksrust-analyzer cargo sudo-rs 0.2.14 system/audit/checks().(path, meta)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
236
237 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(file)
238 }
239
240 // Open the socket at path, provided that it is "secure".
241 // "Secure" means that it passes the `checks` function above.
242 #[cfg(feature = "unstable-remote-sudoers")]
243 fn secure_open_socket_impl(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#<UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#>> {
244 let meta = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/metadata().(path)?;
245 checksrust-analyzer cargo sudo-rs 0.2.14 system/audit/checks().(path, meta)?;
246 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent_dir) = path.parent() {
247 let parent_meta = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/metadata().(parent_dir)?;
248 checksrust-analyzer cargo sudo-rs 0.2.14 system/audit/checks().(parent_dir, parent_meta)?;
249 } else {
250 let error = |msg| Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::new(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::PermissionDeniedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#PermissionDenied#, msg);
251
252 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error(xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!(
253 "{path} has no valid parent directory",
254 path = path.display()
255 )));
256 }
257
258 let stream = UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#::connect(path)?;
259 stream.shutdown(Shutdownrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/Shutdown#::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/Shutdown#Write#)?;
260 let reader = BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::new(stream);
261
262 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(reader)
263 }
264
265 fn open_atrust-analyzer cargo sudo-rs 0.2.14 system/audit/open_at().(parent: BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#, file_name: &CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, create: bool) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#> {
266 let flags = if create {
267 libcrust-analyzer cargo libc 0.2.186 crate/::O_NOFOLLOWrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/O_NOFOLLOW. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().libcrust-analyzer cargo libc 0.2.186 crate/::O_RDWRrust-analyzer cargo libc 0.2.186 unix/linux_like/O_RDWR. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().libcrust-analyzer cargo libc 0.2.186 crate/::O_CREATrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/O_CREAT.
268 } else {
269 libcrust-analyzer cargo libc 0.2.186 crate/::O_NOFOLLOWrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/O_NOFOLLOW. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().libcrust-analyzer cargo libc 0.2.186 crate/::O_RDONLYrust-analyzer cargo libc 0.2.186 unix/linux_like/O_RDONLY.
270 };
271
272 // the mode for files that are created is hardcoded, as it is in ogsudo
273 let mode = libcrust-analyzer cargo libc 0.2.186 crate/::S_IRUSRrust-analyzer cargo libc 0.2.186 unix/linux_like/S_IRUSR. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().libcrust-analyzer cargo libc 0.2.186 crate/::S_IWUSRrust-analyzer cargo libc 0.2.186 unix/linux_like/S_IWUSR. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().libcrust-analyzer cargo libc 0.2.186 crate/::S_IRGRPrust-analyzer cargo libc 0.2.186 unix/linux_like/S_IRGRP. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().libcrust-analyzer cargo libc 0.2.186 crate/::S_IROTHrust-analyzer cargo libc 0.2.186 unix/linux_like/S_IROTH.;
274
275 // SAFETY: by design, a correct CStr pointer is passed to openat; only if this call succeeds
276 // is the file descriptor it returns (which is then necessarily valid) passed to from_raw_fd
277 unsafe {
278 let fd = cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::openatrust-analyzer cargo libc 0.2.186 openat().(
279 parent.as_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`BorrowedFd<'_>`][AsRawFd]as_raw_fd().(),
280 file_name.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(),
281 flags,
282 c_uintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_uint#::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`From<Self>`]from().(mode),
283 ))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
284
285 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#::from_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[OwnedFd][FromRawFd]from_raw_fd().(fd))
286 }
287 }
288
289 fn faccess_atrust-analyzer cargo sudo-rs 0.2.14 system/audit/faccess_at().(parent: BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#, path: &CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, mode: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, flags: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
290 // SAFETY: by design, a correct CStr pointer is passed to faccessat
291 cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::faccessatrust-analyzer cargo libc 0.2.186 faccessat().(parent.as_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`BorrowedFd<'_>`][AsRawFd]as_raw_fd().(), path.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), mode, flags) }).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|_| ())
292 }
293
294 /// This opens a file for sudoedit, performing security checks (see below) and
295 /// opening with reduced privileges.
296 pub fn secure_open_for_sudoeditrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_for_sudoedit().(
297 path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>,
298 current_user: &CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#,
299 target_user: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
300 target_group: &Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#,
301 ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#> {
302 if current_user.is_rootrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]is_root().() {
303 sudo_callrust-analyzer cargo sudo-rs 0.2.14 system/audit/sudo_call().(target_user, target_group, || {
304 OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]new().()
305 .readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]read().(true)
306 .writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]write().(true)
307 .createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]create().(true)
308 .truncaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]truncate().(false)
309 .openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(path)
310 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
311 } else {
312 traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().(path, current_user, target_user, target_group)
313 }
314 }
315
316 /// This opens a file making sure that
317 /// - no directory leading up to the file is editable by the user
318 /// - no components are a symbolic link
319 fn traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().(
320 path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>,
321 #[cfg(not(test))] forbidden_user: &CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#,
322 #[cfg(test)] forbidden_user: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
323 target_user: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
324 target_group: &Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#,
325 ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#> {
326 let path = path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
327
328 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(file_name) = path.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().() else {
329 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
330 ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#,
331 xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!("invalid path"),
332 ));
333 };
334
335 let mut components = path.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().("")).componentsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]components().();
336 if components.nextrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[`Components<'a>`][Iterator]next().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::RootDirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#RootDir#) {
337 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
338 ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#,
339 xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!("path must be absolute"),
340 ));
341 }
342
343 let user_cannot_write = |file: &Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#| -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
344 let meta = file.metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]metadata().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
345 let perms = meta.permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]permissions().().moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[Permissions][PermissionsExt]mode().();
346
347 if meta.uidrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]uid().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().forbidden_user.uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid..innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().() {
348 // Owner can change file permissions
349 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
350 ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::PermissionDeniedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#PermissionDenied#,
351 xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!("cannot open a file in a path writable by the user"),
352 ));
353 }
354
355 let user_has_write_perms = if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(test) {
356 // During testing we do a less comprehensive check as we don't have
357 // permission to set the real user id to arbitrary users, but faccessat
358 // looks at the real user id.
359 perms rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Worldrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#World#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0
360 || (perms rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Grouprust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Group#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0)
361 && forbidden_user.in_group_by_gidrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]in_group_by_gid().(GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(meta.gidrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]gid().()))
362 || (perms rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().moderust-analyzer cargo sudo-rs 0.2.14 system/audit/mode().(Categoryrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#::Ownerrust-analyzer cargo sudo-rs 0.2.14 system/audit/Category#Owner#, Oprust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#::Writerust-analyzer cargo sudo-rs 0.2.14 system/audit/Op#Write#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0)
363 && forbidden_user.uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid..innerrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]inner().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().meta.uidrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]uid().()
364 } else {
365 // Only works when forbidden_user is current user. This is enforced
366 // by accepting CurrentUser outside of test mode.
367 // We don't pass AT_EACCESS to faccessat to make it check using the
368 // real user id rather than the effective user id.
369 faccess_atrust-analyzer cargo sudo-rs 0.2.14 system/audit/faccess_at().(file.as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`fs::File`][AsFd]as_fd().(), c"", libcrust-analyzer cargo libc 0.2.186 crate/::W_OKrust-analyzer cargo libc 0.2.186 unix/linux_like/W_OK., libcrust-analyzer cargo libc 0.2.186 crate/::AT_EMPTY_PATHrust-analyzer cargo libc 0.2.186 unix/linux_like/AT_EMPTY_PATH.).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()
370 };
371
372 if user_has_write_perms {
373 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
374 ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::PermissionDeniedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#PermissionDenied#,
375 xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!("cannot open a file in a path writable by the user"),
376 ))
377 } else {
378 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
379 }
380 };
381
382 let mut cur = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().("/")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
383 user_cannot_write(&cur)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
384
385 for component in components {
386 let dir: CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString# = match component {
387 Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::Normalrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#Normal#(dir) => CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(dir.as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
388 Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::CurDirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#CurDir# => c".".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CStr][ToOwned]to_owned().(),
389 Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::ParentDirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#ParentDir# => c"..".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CStr][ToOwned]to_owned().(),
390 _ => {
391 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
392 ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#,
393 xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!("error in provided path"),
394 ));
395 }
396 };
397
398 sudo_callrust-analyzer cargo sudo-rs 0.2.14 system/audit/sudo_call().(target_user, target_group, || {
399 cur = open_atrust-analyzer cargo sudo-rs 0.2.14 system/audit/open_at().(cur.as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`fs::File`][AsFd]as_fd().(), &dir, false)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
400 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#::Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
401 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
402 user_cannot_write(&cur)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
403 }
404 sudo_callrust-analyzer cargo sudo-rs 0.2.14 system/audit/sudo_call().(target_user, target_group, || {
405 cur = open_atrust-analyzer cargo sudo-rs 0.2.14 system/audit/open_at().(cur.as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`fs::File`][AsFd]as_fd().(), &CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(file_name.as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., true)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
406 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#::Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
407 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
408 user_cannot_write(&cur)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
409
410 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(cur)
411 }
412
413 #[cfg(test)]
414 mod testrust-analyzer cargo sudo-rs 0.2.14 system/audit/test/ {
415 use superrust-analyzer cargo sudo-rs 0.2.14 system/audit/::*;
416
417 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
418 fn secure_open_is_predictablerust-analyzer cargo sudo-rs 0.2.14 system/audit/test/secure_open_is_predictable().() {
419 // /etc/hosts should be readable and "secure" (if this test fails, you have been compromised)
420 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().("/etc/hosts").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
421 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(secure_open_sudoersrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_sudoers().("/etc/hosts").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
422
423 // /tmp should be readable, but not secure (writable by group other than root)
424 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().("/tmp").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
425 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(secure_open_sudoersrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_sudoers().("/tmp").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
426
427 #[cfg(target_os = "linux")]
428 {
429 // /var/log/wtmp should be readable, but not secure (writable by group other than root)
430 // It doesn't exist on many non-Linux systems however.
431 if stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().("/var/log/wtmp").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() {
432 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(secure_open_sudoersrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_sudoers().("/var/log/wtmp").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
433 }
434 }
435
436 // /etc/shadow should not be readable
437 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().("/etc/shadow").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
438 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(secure_open_sudoersrust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_sudoers().("/etc/shadow").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
439 }
440
441 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
442 fn test_secure_open_cookie_filerust-analyzer cargo sudo-rs 0.2.14 system/audit/test/test_secure_open_cookie_file().() {
443 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(secure_open_cookie_filerust-analyzer cargo sudo-rs 0.2.14 system/audit/secure_open_cookie_file().("/etc/hosts").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
444 }
445
446 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
447 fn test_traverse_secure_open_negativerust-analyzer cargo sudo-rs 0.2.14 system/audit/test/test_traverse_secure_open_negative().() {
448 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::commonrust-analyzer cargo sudo-rs 0.2.14 common/::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/::CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#;
449
450 let root = Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_name().(c"root").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
451 let user = CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser]resolve().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
452
453 // not allowed -- invalid
454 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().("/", &root, &user, &user.grouprust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]group().()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
455 // not allowed since the path is not absolute
456 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().("./hello.txt", &root, &user, &user.grouprust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]group().()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
457 // not allowed since root can write to "/"
458 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().("/hello.txt", &root, &user, &user.grouprust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]group().()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
459 // not allowed since "/tmp" is a directory
460 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().("/tmp", &user, &user, &user.grouprust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]group().()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
461 // not allowed since anybody can write to "/tmp"
462 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().("/tmp/foo/hello.txt", &user, &user, &user.grouprust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]group().()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
463 // not allowed since "/bin" is a symlink
464 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().("/bin/hello.txt", &user, &user, &user.grouprust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]group().()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
465 }
466
•467 // Debian: this makes assumptions that are note valid in autopkgtest context
468 #[allow(dead_code)]
469 fn test_traverse_secure_open_positiverust-analyzer cargo sudo-rs 0.2.14 system/audit/test/test_traverse_secure_open_positive().() {
470 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::commonrust-analyzer cargo sudo-rs 0.2.14 common/::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/::CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#;
471 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::{GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#, UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#};
472
473 let user = CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser]resolve().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
474 let other_user = CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#::fakerust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser]fake().(Userrust-analyzer cargo sudo-rs 0.2.14 system/User# {
475 uidrust-analyzer cargo sudo-rs 0.2.14 system/User#uid.: UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]new().(1042),
476 gidrust-analyzer cargo sudo-rs 0.2.14 system/User#gid.: GroupIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/GroupId#::newrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[GroupId]new().(1042),
477
478 namerust-analyzer cargo sudo-rs 0.2.14 system/User#name.: "test".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
479 homerust-analyzer cargo sudo-rs 0.2.14 system/User#home.: "/home/test".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
480 shellrust-analyzer cargo sudo-rs 0.2.14 system/User#shell.: "/bin/sh".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
481 groupsrust-analyzer cargo sudo-rs 0.2.14 system/User#groups.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![],
482 });
483
484 // allowed!
485 let path = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::current_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/current_dir().()
486 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
487 .joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("sudo-rs-test-file.txt");
488 let file = traversed_secure_openrust-analyzer cargo sudo-rs 0.2.14 system/audit/traversed_secure_open().(&path, &other_user, &user, &user.grouprust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[`super::User`][UnixUser]group().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
489 if file.metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]metadata().().is_ok_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok_and().(|meta| meta.lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().0) {
490 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_file().(path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
491 }
492 }
493 }