From 62297a74a6a736d28f11ebe67cd63794ca2295fe Mon Sep 17 00:00:00 2001 From: LoveSy Date: Mon, 22 Jul 2024 07:43:59 +0800 Subject: [PATCH] Upgrade rustix (#1900) --- userspace/ksud/Cargo.lock | 42 +++++++++++++++++----------------- userspace/ksud/src/sepolicy.rs | 20 ++++++++-------- userspace/ksud/src/su.rs | 28 ++++++----------------- 3 files changed, 38 insertions(+), 52 deletions(-) diff --git a/userspace/ksud/Cargo.lock b/userspace/ksud/Cargo.lock index d429686df900..8db180d9ac1a 100644 --- a/userspace/ksud/Cargo.lock +++ b/userspace/ksud/Cargo.lock @@ -203,9 +203,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "block-buffer" @@ -902,7 +902,7 @@ dependencies = [ "regex", "retry", "rust-embed", - "rustix 0.38.30", + "rustix 0.38.34 (git+https://github.com/Kernel-SU/rustix.git?branch=main)", "serde", "serde_json", "sha1", @@ -1112,13 +1112,13 @@ version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "731e0d9356b0c25f16f33b5be79b1c57b562f141ebfcdb0ad8ac2c13a24293b4" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "chrono", "flate2", "hex", "lazy_static", "procfs-core", - "rustix 0.38.34", + "rustix 0.38.34 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1127,7 +1127,7 @@ version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d3554923a69f4ce04c4a754260c338f505ce22642d3830e049a399fc2059a29" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "chrono", "hex", ] @@ -1324,28 +1324,28 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustix" -version = "0.38.30" -source = "git+https://github.com/Kernel-SU/rustix.git?branch=main#0e270bce2d97466be6b987bb5f7ea5b1e8d84969" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno 0.3.9", - "itoa", "libc", "linux-raw-sys", - "once_cell", "windows-sys", ] [[package]] name = "rustix" version = "0.38.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +source = "git+https://github.com/Kernel-SU/rustix.git?branch=main#4a53fbc7cb7a07cabe87125cc21dbc27db316259" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno 0.3.9", + "itoa", "libc", "linux-raw-sys", + "once_cell", "windows-sys", ] @@ -1366,18 +1366,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.202" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.202" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", @@ -1386,9 +1386,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.117" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -1633,7 +1633,7 @@ checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7" dependencies = [ "either", "home", - "rustix 0.38.34", + "rustix 0.38.34 (registry+https://github.com/rust-lang/crates.io-index)", "winsafe", ] diff --git a/userspace/ksud/src/sepolicy.rs b/userspace/ksud/src/sepolicy.rs index 7e623d15aaf4..b7286a06a769 100644 --- a/userspace/ksud/src/sepolicy.rs +++ b/userspace/ksud/src/sepolicy.rs @@ -11,7 +11,7 @@ use nom::{ sequence::Tuple, IResult, Parser, }; -use std::{path::Path, vec}; +use std::{ffi, path::Path, vec}; type SeObject<'a> = Vec<&'a str>; @@ -660,19 +660,19 @@ impl<'a> TryFrom<&'a PolicyStatement<'a>> for Vec { struct FfiPolicy { cmd: u32, subcmd: u32, - sepol1: *const libc::c_char, - sepol2: *const libc::c_char, - sepol3: *const libc::c_char, - sepol4: *const libc::c_char, - sepol5: *const libc::c_char, - sepol6: *const libc::c_char, - sepol7: *const libc::c_char, + sepol1: *const ffi::c_char, + sepol2: *const ffi::c_char, + sepol3: *const ffi::c_char, + sepol4: *const ffi::c_char, + sepol5: *const ffi::c_char, + sepol6: *const ffi::c_char, + sepol7: *const ffi::c_char, } -fn to_c_ptr(pol: &PolicyObject) -> *const libc::c_char { +fn to_c_ptr(pol: &PolicyObject) -> *const ffi::c_char { match pol { PolicyObject::None | PolicyObject::All => std::ptr::null(), - PolicyObject::One(s) => s.as_ptr().cast::(), + PolicyObject::One(s) => s.as_ptr().cast::(), } } diff --git a/userspace/ksud/src/su.rs b/userspace/ksud/src/su.rs index b19bf7a21ca1..7884137e7ef2 100644 --- a/userspace/ksud/src/su.rs +++ b/userspace/ksud/src/su.rs @@ -19,30 +19,16 @@ use rustix::{ #[cfg(any(target_os = "linux", target_os = "android"))] pub fn grant_root(global_mnt: bool) -> Result<()> { - const KERNEL_SU_OPTION: u32 = 0xDEAD_BEEF; - const CMD_GRANT_ROOT: u64 = 0; - - let mut result: u32 = 0; - unsafe { - #[allow(clippy::cast_possible_wrap)] - libc::prctl( - KERNEL_SU_OPTION as i32, // supposed to overflow - CMD_GRANT_ROOT, - 0, - 0, - std::ptr::addr_of_mut!(result).cast::(), - ); - } + rustix::process::ksu_grant_root()?; - anyhow::ensure!(result == KERNEL_SU_OPTION, "grant root failed"); - let mut command = std::process::Command::new("sh"); + let mut command = Command::new("sh"); let command = unsafe { command.pre_exec(move || { if global_mnt { let _ = utils::switch_mnt_ns(1); let _ = utils::unshare_mnt_ns(); } - std::result::Result::Ok(()) + Result::Ok(()) }) }; // add /data/adb/ksu/bin to PATH @@ -64,7 +50,7 @@ fn print_usage(program: &str, opts: Options) { fn set_identity(uid: u32, gid: u32, groups: &[u32]) { #[cfg(any(target_os = "linux", target_os = "android"))] { - rustix::process::set_groups( + rustix::thread::set_thread_groups( groups .iter() .map(|g| unsafe { Gid::from_raw(*g) }) @@ -89,7 +75,7 @@ pub fn root_shell() -> Result<()> { // we are root now, this was set in kernel! use anyhow::anyhow; - let env_args: Vec = std::env::args().collect(); + let env_args: Vec = env::args().collect(); let program = env_args[0].clone(); let args = env_args .iter() @@ -154,7 +140,7 @@ pub fn root_shell() -> Result<()> { .collect::>(); let matches = match opts.parse(&args[1..]) { - std::result::Result::Ok(m) => m, + Result::Ok(m) => m, Err(f) => { println!("{f}"); print_usage(&program, opts); @@ -282,7 +268,7 @@ pub fn root_shell() -> Result<()> { set_identity(uid, gid, &groups); - std::result::Result::Ok(()) + Result::Ok(()) }) };