Move find_apk_path to Rust

This commit is contained in:
topjohnwu
2023-06-10 01:40:45 -07:00
parent 40f25f4d56
commit f33f1d25d0
6 changed files with 44 additions and 64 deletions

View File

@@ -1,8 +1,12 @@
use crate::logging::{magisk_logging, zygisk_logging};
use std::cell::RefCell;
use std::fs::File;
use std::io;
use std::sync::{Mutex, OnceLock};
use base::{copy_str, cstr, Directory, ResultExt, WalkResult};
use crate::logging::{magisk_logging, zygisk_logging};
// Global magiskd singleton
pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
@@ -29,3 +33,30 @@ pub fn get_magiskd() -> &'static MagiskD {
}
impl MagiskD {}
pub fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize {
use WalkResult::*;
fn inner(pkg: &[u8], data: &mut [u8]) -> io::Result<usize> {
let mut len = 0_usize;
Directory::open(cstr!("/data/app"))?.pre_order_walk(|e| {
if !e.is_dir() {
return Ok(Skip);
}
let d_name = e.d_name().to_bytes();
if d_name.starts_with(pkg) && d_name[pkg.len()] == b'-' {
// Found the APK path, we can abort now
len = e.path(data)?;
return Ok(Abort);
}
if d_name.starts_with(b"~~") {
return Ok(Continue);
}
Ok(Skip)
})?;
if len > 0 {
len += copy_str(&mut data[len..], "/base.apk");
}
Ok(len)
}
inner(pkg, data).log().unwrap_or(0)
}

View File

@@ -21,6 +21,7 @@ pub mod ffi {
fn android_logging();
fn magisk_logging();
fn zygisk_logging();
fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize;
}
#[namespace = "rust"]

View File

@@ -169,8 +169,9 @@ int get_manager(int user_id, string *pkg, bool install) {
if (stat(app_path, &st) == 0) {
int app_id = to_app_id(st.st_uid);
string apk = find_apk_path(str[SU_MANAGER].data());
int fd = xopen(apk.data(), O_RDONLY | O_CLOEXEC);
byte_array<PATH_MAX> apk;
find_apk_path(byte_view(str[SU_MANAGER]), apk);
int fd = xopen((const char *) apk.buf(), O_RDONLY | O_CLOEXEC);
string cert = read_certificate(fd);
close(fd);
@@ -178,7 +179,7 @@ int get_manager(int user_id, string *pkg, bool install) {
if (str[SU_MANAGER] == *mgr_pkg) {
if (app_id != mgr_app_id || cert != *mgr_cert) {
// app ID or cert should never change
LOGE("pkg: repackaged APK signature invalid: %s\n", apk.data());
LOGE("pkg: repackaged APK signature invalid: %s\n", apk.buf());
uninstall_pkg(mgr_pkg->data());
invalid = true;
install = true;