base: eliminate /proc/self/fd path resolution

Remove reliance on /proc/self/fd symlink resolution across base and core
components:
- Add top-level pre_order_walk and post_order_walk that track entry
  paths via an internal buffer, and unify Directory walk methods to
  support path-tracking and pathless traversals without extra overhead.
- Add DirEntry::get_stat using fstatat(AT_SYMLINK_NOFOLLOW) directly on
  directory file descriptors.
- Update Directory::copy_into and link_into to operate directly on file
  descriptors with fd_get_attr/fd_set_attr, passing paths only for
  symlink attribute updates.
- Refactor find_apk_path and restore_tmpcon to use pre_order_walk, and
  update get_app_no_list to use DirEntry::get_stat.
- Remove DirEntry::resolve_path, Directory::path_at,
  Directory::resolve_path, and fd_path.
- Replace remaining consumers of xrealpath and canonical_path (in su
  and magiskinit) with standard realpath or partition matching, allowing
  removal of Utf8CStr::realpath and its C FFI exports.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-10-02 17:17:48 -07:00
parent 0e228db61f
commit 8348d6e39c
9 changed files with 131 additions and 151 deletions
+12 -22
View File
@@ -4,7 +4,7 @@ use crate::ffi::{DbEntryKey, get_magisk_tmp, install_apk, uninstall_pkg};
use base::WalkResult::{Abort, Continue, Skip};
use base::{
BufReadExt, Directory, FsPathBuilder, LoggedResult, ReadExt, ResultExt, Utf8CStrBuf,
Utf8CString, cstr, error, fd_get_attr, warn,
Utf8CString, cstr, error, fd_get_attr, pre_order_walk, warn,
};
use bit_set::BitSet;
use nix::fcntl::OFlag;
@@ -150,15 +150,15 @@ fn read_certificate(apk: &mut File, version: i32) -> Vec<u8> {
}
fn find_apk_path(pkg: &str) -> LoggedResult<Utf8CString> {
let mut buf = cstr::buf::default();
Directory::open(cstr!("/data/app"))?.pre_order_walk(|e| {
let mut buf = Utf8CString::default();
pre_order_walk(cstr!("/data/app"), |e, path| {
if !e.is_dir() {
return Ok(Skip);
}
let name_bytes = e.name().as_bytes();
if name_bytes.starts_with(pkg.as_bytes()) && name_bytes[pkg.len()] == b'-' {
if name_bytes.starts_with(pkg.as_bytes()) && name_bytes.get(pkg.len()) == Some(&b'-') {
// Found the APK path, we can abort now
e.resolve_path(&mut buf)?;
buf.append_path(path).append_path("base.apk");
return Ok(Abort);
}
if name_bytes.starts_with(b"~~") {
@@ -166,10 +166,7 @@ fn find_apk_path(pkg: &str) -> LoggedResult<Utf8CString> {
}
Ok(Skip)
})?;
if !buf.is_empty() {
buf.push_str("/base.apk");
}
Ok(buf.to_owned())
Ok(buf)
}
enum Status {
@@ -492,19 +489,12 @@ impl MagiskD {
Ok(dir) => dir,
};
// For each package
loop {
match user_dir.read()? {
None => break,
Some(e) => {
let mut entry_path = cstr::buf::default();
e.resolve_path(&mut entry_path)?;
let attr = entry_path.get_attr()?;
let app_id = to_app_id(attr.st.st_uid as i32);
if (AID_APP_START..=AID_APP_END).contains(&app_id) {
let app_no = app_id - AID_APP_START;
list.insert(app_no as usize);
}
}
while let Some(e) = user_dir.read()? {
let st = e.get_stat()?;
let app_id = to_app_id(st.st_uid as i32);
if (AID_APP_START..=AID_APP_END).contains(&app_id) {
let app_no = app_id - AID_APP_START;
list.insert(app_no as usize);
}
}
}
+8 -7
View File
@@ -1,6 +1,9 @@
use crate::consts::{DATABIN, LOG_PIPE, MAGISK_LOG_CON, MAGISKDB, MODULEROOT, SECURE_DIR};
use crate::ffi::get_magisk_tmp;
use base::{Directory, FsPathBuilder, LoggedResult, ResultExt, Utf8CStr, Utf8CStrBuf, cstr, libc};
use base::{
Directory, FsPathBuilder, LoggedResult, ResultExt, Utf8CStr, Utf8CStrBuf, WalkResult, cstr,
libc, pre_order_walk,
};
use nix::fcntl::OFlag;
use std::io::Write;
@@ -80,16 +83,14 @@ pub(crate) fn restore_tmpcon() -> LoggedResult<()> {
unsafe { libc::chmod(tmp.as_ptr(), 0o711) };
}
let mut path = cstr::buf::default();
let mut dir = Directory::open(tmp)?;
while let Some(ref e) = dir.read()? {
pre_order_walk(tmp, |e, path| {
if !e.is_symlink() {
e.resolve_path(&mut path)?;
path.set_secontext(SYSTEM_CON).log_ok();
}
}
Ok(WalkResult::Skip)
})?;
path.clear();
let mut path = cstr::buf::default();
path.append_path(tmp).append_path(LOG_PIPE);
path.set_secontext(cstr!(MAGISK_LOG_CON))?;
+3 -2
View File
@@ -420,9 +420,10 @@ void exec_root_shell(int client, int pid, SuRequest &req, MntNsMode mode) {
umask(022);
char path[32];
ssprintf(path, sizeof(path), "/proc/%d/cwd", pid);
char cwd[4096];
if (canonical_path(path, cwd, sizeof(cwd)) > 0)
if (auto cwd = realpath(path, nullptr)) {
chdir(cwd);
free(cwd);
}
ssprintf(path, sizeof(path), "/proc/%d/environ", pid);
auto env = full_read(path);
clearenv();