| 1 | 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_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#, c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#}; |
| 2 | 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/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/}; |
| 3 | |
| 4 | use 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().; |
| 5 | |
| 6 | /// Set the profile for the next exec call if AppArmor is enabled |
| 7 | pub fn set_profile_for_next_execrust-analyzer cargo sudo-rs 0.2.14 apparmor/set_profile_for_next_exec().(profile_name: &str) -> 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#<()> { |
| 8 | if apparmor_is_enabledrust-analyzer cargo sudo-rs 0.2.14 apparmor/apparmor_is_enabled().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 9 | apparmor_prepare_execrust-analyzer cargo sudo-rs 0.2.14 apparmor/apparmor_prepare_exec().(profile_name) |
| 10 | } else { |
| 11 | // if the sysadmin doesn't have apparmor enabled, fail softly |
| 12 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | fn apparmor_is_enabledrust-analyzer cargo sudo-rs 0.2.14 apparmor/apparmor_is_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> { |
| 17 | match fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().("/sys/module/apparmor/parameters/enabled") { |
| 18 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(enabled) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(enabled.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("Y")), |
| 19 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) if e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false), |
| 20 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /// Switch the apparmor profile to the given profile on the next exec call |
| 25 | fn apparmor_prepare_execrust-analyzer cargo sudo-rs 0.2.14 apparmor/apparmor_prepare_exec().(new_profile: &str) -> 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#<()> { |
| 26 | // SAFETY: Always safe to call |
| 27 | unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::dlerrorrust-analyzer cargo libc 0.2.186 dlerror().() }; // Clear any existing error |
| 28 | |
| 29 | // SAFETY: Loading a known safe dylib. LD_LIBRARY_PATH is ignored because we are setuid. |
| 30 | let handle = unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::dlopenrust-analyzer cargo libc 0.2.186 dlopen().(c"libapparmor.so.1".as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), libcrust-analyzer cargo libc 0.2.186 crate/::RTLD_NOWrust-analyzer cargo libc 0.2.186 unix/linux_like/linux_l4re_shared/RTLD_NOW.) }; |
| 31 | if handle.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() { |
| 32 | // SAFETY: In case of an error, dlerror returns a valid C string. |
| 33 | 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().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#, unsafe { |
| 34 | CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().(libcrust-analyzer cargo libc 0.2.186 crate/::dlerrorrust-analyzer cargo libc 0.2.186 dlerror().()) |
| 35 | .to_string_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CStr]to_string_lossy().() |
| 36 | .into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().() |
| 37 | })); |
| 38 | } |
| 39 | |
| 40 | // SAFETY: dlsym will either return a function pointer of the right signature or NULL. |
| 41 | let aa_change_onexec = unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::dlsymrust-analyzer cargo libc 0.2.186 dlsym().(handle, c"aa_change_onexec".as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().()) }; |
| 42 | |
| 43 | if aa_change_onexec.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() { |
| 44 | // SAFETY: Always safe to call |
| 45 | let err = unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::dlerrorrust-analyzer cargo libc 0.2.186 dlerror().() }; |
| 46 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(if err.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() { |
| 47 | // There was no error in dlsym, but the symbol itself was defined as NULL pointer. |
| 48 | // This is still an error for us, but dlerror will not return any error. |
| 49 | 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().( |
| 50 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, |
| 51 | "aa_change_onexec symbol is a NULL pointer", |
| 52 | ) |
| 53 | } else { |
| 54 | // SAFETY: In case of an error, dlerror returns a valid C string. |
| 55 | 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().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#, unsafe { |
| 56 | CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().(err).to_string_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CStr]to_string_lossy().().into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().() |
| 57 | }) |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | //SAFETY: aa_change_onexec is non-NULL, so we can cast it into a function pointer |
| 62 | let aa_change_onexec: unsafe extern "C" fn(*const c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#) -> c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int# = |
| 63 | unsafe { memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(aa_change_onexec) }; |
| 64 | |
| 65 | let new_profile_cstr = 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().(new_profile)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 66 | // SAFETY: new_profile_cstr provided by CString ensures a valid ptr |
| 67 | cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(unsafe { aa_change_onexec(new_profile_cstr.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().()) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 68 | |
| 69 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 70 | } |