Skip to content

Commit

Permalink
Merge pull request #1325 from tiann/rustix
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 authored Feb 1, 2024
2 parents 4b1fb12 + 4d4bd47 commit d6cab60
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 168 deletions.
75 changes: 11 additions & 64 deletions userspace/ksud/Cargo.lock

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

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

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
sys-mount = { git = "https://github.com/tiann/sys-mount", branch = "loopfix" }
rustix = { version = "0.38", features = ["all-apis"] }
# some android specific dependencies which compiles under unix are also listed here for convenience of coding
android-properties = { version = "0.2.2", features = ["bionic-deprecated"] }
procfs = "0.16"
loopdev = { git = "https://github.com/tiann/loopdev", branch = "loopfix" }

[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.13"
Expand Down
1 change: 1 addition & 0 deletions userspace/ksud/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{bail, Context, Result};
use log::{info, warn};
#[cfg(target_os = "android")]
use std::path::PathBuf;
use std::{collections::HashMap, path::Path};

Expand Down
17 changes: 14 additions & 3 deletions userspace/ksud/src/ksu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ use crate::{
utils::{self, umask},
};

#[cfg(any(target_os = "linux", target_os = "android"))]
use rustix::{
process::getuid,
thread::{set_thread_res_gid, set_thread_res_uid, Gid, Uid},
};

pub const KERNEL_SU_OPTION: u32 = 0xDEAD_BEEF;

const CMD_GRANT_ROOT: u64 = 0;
Expand Down Expand Up @@ -65,8 +71,13 @@ fn set_identity(uid: u32, gid: u32, groups: &[u32]) {
if !groups.is_empty() {
libc::setgroups(groups.len(), groups.as_ptr());
}
libc::setresgid(gid, gid, gid);
libc::setresuid(uid, uid, uid);
}
#[cfg(any(target_os = "linux", target_os = "android"))]
{
let gid = unsafe { Gid::from_raw(gid) };
let uid = unsafe { Uid::from_raw(uid) };
set_thread_res_gid(gid, gid, gid).ok();
set_thread_res_uid(uid, uid, uid).ok();
}
}

Expand Down Expand Up @@ -203,7 +214,7 @@ pub fn root_shell() -> Result<()> {
}

// use current uid if no user specified, these has been done in kernel!
let mut uid = unsafe { libc::getuid() };
let mut uid = getuid().as_raw();
if free_idx < matches.free.len() {
let name = &matches.free[free_idx];
uid = unsafe {
Expand Down
Loading

0 comments on commit d6cab60

Please sign in to comment.