Skip to content

Commit

Permalink
Upgrade rustix
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Jul 21, 2024
1 parent 54322c0 commit e4d1902
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
42 changes: 21 additions & 21 deletions userspace/ksud/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion userspace/ksud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ chrono = "0.4"
hole-punch = { git = "https://github.com/tiann/hole-punch" }

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
rustix = { git = "https://github.com/Kernel-SU/rustix.git", branch = "main", features = [
rustix = { git = "https://github.com/Kernel-SU/rustix.git", branch = "nolibc", features = [
"all-apis",
] }
# some android specific dependencies which compiles under unix are also listed here for convenience of coding
Expand Down
20 changes: 10 additions & 10 deletions userspace/ksud/src/sepolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down Expand Up @@ -660,19 +660,19 @@ impl<'a> TryFrom<&'a PolicyStatement<'a>> for Vec<AtomicStatement> {
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::<libc::c_char>(),
PolicyObject::One(s) => s.as_ptr().cast::<ffi::c_char>(),
}
}

Expand Down
28 changes: 7 additions & 21 deletions userspace/ksud/src/su.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<libc::c_void>(),
);
}
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
Expand All @@ -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) })
Expand All @@ -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<String> = std::env::args().collect();
let env_args: Vec<String> = env::args().collect();
let program = env_args[0].clone();
let args = env_args
.iter()
Expand Down Expand Up @@ -154,7 +140,7 @@ pub fn root_shell() -> Result<()> {
.collect::<Vec<String>>();

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);
Expand Down Expand Up @@ -282,7 +268,7 @@ pub fn root_shell() -> Result<()> {

set_identity(uid, gid, &groups);

std::result::Result::Ok(())
Result::Ok(())
})
};

Expand Down

0 comments on commit e4d1902

Please sign in to comment.