Skip to content

Commit d6cab60

Browse files
authored
Merge pull request #1325 from tiann/rustix
2 parents 4b1fb12 + 4d4bd47 commit d6cab60

File tree

6 files changed

+100
-168
lines changed

6 files changed

+100
-168
lines changed

userspace/ksud/Cargo.lock

Lines changed: 11 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

userspace/ksud/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ chrono = "0.4"
3939
hole-punch = { git = "https://github.com/tiann/hole-punch" }
4040

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

4748
[target.'cfg(target_os = "android")'.dependencies]
4849
android_logger = "0.13"

userspace/ksud/src/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{bail, Context, Result};
22
use log::{info, warn};
3+
#[cfg(target_os = "android")]
34
use std::path::PathBuf;
45
use std::{collections::HashMap, path::Path};
56

userspace/ksud/src/ksu.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ use crate::{
1414
utils::{self, umask},
1515
};
1616

17+
#[cfg(any(target_os = "linux", target_os = "android"))]
18+
use rustix::{
19+
process::getuid,
20+
thread::{set_thread_res_gid, set_thread_res_uid, Gid, Uid},
21+
};
22+
1723
pub const KERNEL_SU_OPTION: u32 = 0xDEAD_BEEF;
1824

1925
const CMD_GRANT_ROOT: u64 = 0;
@@ -65,8 +71,13 @@ fn set_identity(uid: u32, gid: u32, groups: &[u32]) {
6571
if !groups.is_empty() {
6672
libc::setgroups(groups.len(), groups.as_ptr());
6773
}
68-
libc::setresgid(gid, gid, gid);
69-
libc::setresuid(uid, uid, uid);
74+
}
75+
#[cfg(any(target_os = "linux", target_os = "android"))]
76+
{
77+
let gid = unsafe { Gid::from_raw(gid) };
78+
let uid = unsafe { Uid::from_raw(uid) };
79+
set_thread_res_gid(gid, gid, gid).ok();
80+
set_thread_res_uid(uid, uid, uid).ok();
7081
}
7182
}
7283

@@ -203,7 +214,7 @@ pub fn root_shell() -> Result<()> {
203214
}
204215

205216
// use current uid if no user specified, these has been done in kernel!
206-
let mut uid = unsafe { libc::getuid() };
217+
let mut uid = getuid().as_raw();
207218
if free_idx < matches.free.len() {
208219
let name = &matches.free[free_idx];
209220
uid = unsafe {

0 commit comments

Comments
 (0)