From c275a4ea306a5270f7126fa244cc4f9db87bb4c5 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 22 Oct 2026 03:38:14 +0000 Subject: [PATCH] Embed trusted certificate in flags.rs - Extract the signing certificate from keyStore via keytool in dump_flags_native() and emit TRUSTED_CERT in flags.rs (or an empty slice when keyStore is not configured). - Replace dynamic certificate extraction from stub.apk in preserve_stub_apk() with the static TRUSTED_CERT constant. - Move get_package_uid() out of MagiskD into a standalone function. Assisted-by: Gemini 4 Argon --- build.py | 22 ++++++++++++++++++++ native/src/core/package.rs | 42 +++++++++++++++----------------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/build.py b/build.py index 7ecce253c..2918a76fb 100755 --- a/build.py +++ b/build.py @@ -5,6 +5,7 @@ import glob import os import re import shutil +import ssl import stat import subprocess import sys @@ -278,8 +279,29 @@ def dump_flags_native(): native_gen_path.mkdir(mode=0o755, parents=True, exist_ok=True) write_if_diff(native_gen_path / "flags.h", flag_txt) + cert = b"" + if "keyStore" in config: + ensure_jdk() + pem = cmd_out( + [ + "keytool", + "-exportcert", + "-rfc", + "-keystore", + config["keyStore"], + "-storepass", + config["keyStorePass"], + "-alias", + config["keyAlias"], + ] + ) + if pem: + cert = ssl.PEM_cert_to_DER_cert(pem) + + cert_hex = ", ".join(f"0x{b:02x}" for b in cert) rust_flag_txt = f'pub const MAGISK_VERSION: &str = "{config["version"]}";\n' rust_flag_txt += f'pub const MAGISK_VER_CODE: i32 = {config["versionCode"]};\n' + rust_flag_txt += f"pub const TRUSTED_CERT: &[u8] = &[{cert_hex}];\n" write_if_diff(native_gen_path / "flags.rs", rust_flag_txt) diff --git a/native/src/core/package.rs b/native/src/core/package.rs index 0822c5261..c2a78d528 100644 --- a/native/src/core/package.rs +++ b/native/src/core/package.rs @@ -1,4 +1,4 @@ -use crate::consts::{APP_DATA_DIR, APP_PACKAGE_NAME, MAGISK_VER_CODE}; +use crate::consts::{APP_DATA_DIR, APP_PACKAGE_NAME, MAGISK_VER_CODE, TRUSTED_CERT}; use crate::daemon::{AID_APP_END, AID_APP_START, AID_USER_OFFSET, MagiskD, to_app_id}; use crate::ffi::{DbEntryKey, get_magisk_tmp, install_apk, uninstall_pkg}; use base::WalkResult::{Abort, Continue, Skip}; @@ -169,6 +169,16 @@ fn find_apk_path(pkg: &str) -> LoggedResult { Ok(buf) } +fn get_package_uid(user: i32, pkg: &str) -> i32 { + let path = cstr::buf::default() + .join_path(APP_DATA_DIR) + .join_path_fmt(user) + .join_path(pkg); + path.get_attr() + .map(|attr| attr.st.st_uid as i32) + .unwrap_or(-1) +} + enum Status { Installed, NotInstalled, @@ -177,7 +187,6 @@ enum Status { pub struct ManagerInfo { stub_apk_fd: Option, - trusted_cert: Vec, repackaged_app_id: i32, repackaged_pkg: String, repackaged_cert: Vec, @@ -188,7 +197,6 @@ impl Default for ManagerInfo { fn default() -> Self { ManagerInfo { stub_apk_fd: None, - trusted_cert: Vec::new(), repackaged_app_id: -1, repackaged_pkg: String::new(), repackaged_cert: Vec::new(), @@ -248,7 +256,7 @@ impl ManagerInfo { } }; - if cert.is_empty() || cert != self.trusted_cert { + if cert.is_empty() || (!TRUSTED_CERT.is_empty() && cert != TRUSTED_CERT) { error!("pkg: dyn APK signature mismatch: {}", apk); #[cfg(all(feature = "check-signature", not(debug_assertions)))] { @@ -295,7 +303,7 @@ impl ManagerInfo { Err(_) => return Status::NotInstalled, }; - if cert.is_empty() || cert != self.trusted_cert { + if cert.is_empty() || (!TRUSTED_CERT.is_empty() && cert != TRUSTED_CERT) { error!("pkg: APK signature mismatch: {}", apk); #[cfg(all(feature = "check-signature", not(debug_assertions)))] { @@ -370,7 +378,7 @@ impl ManagerInfo { }; } // orig APK is still the same - let uid = daemon.get_package_uid(user, APP_PACKAGE_NAME); + let uid = get_package_uid(user, APP_PACKAGE_NAME); return if uid < 0 { (-1, "") } else { @@ -405,7 +413,7 @@ impl ManagerInfo { match self.check_orig(user) { Status::Installed => { - let uid = daemon.get_package_uid(user, APP_PACKAGE_NAME); + let uid = get_package_uid(user, APP_PACKAGE_NAME); return if uid < 0 { (-1, "") } else { @@ -428,30 +436,12 @@ impl ManagerInfo { } impl MagiskD { - fn get_package_uid(&self, user: i32, pkg: &str) -> i32 { - let path = cstr::buf::default() - .join_path(APP_DATA_DIR) - .join_path_fmt(user) - .join_path(pkg); - path.get_attr() - .map(|attr| attr.st.st_uid as i32) - .unwrap_or(-1) - } - pub fn preserve_stub_apk(&self) { let mut info = self.manager_info.lock(); - let apk = cstr::buf::default() .join_path(get_magisk_tmp()) .join_path("stub.apk"); - - if let Ok(mut fd) = apk.open(OFlag::O_RDONLY | OFlag::O_CLOEXEC) { - info.trusted_cert = read_certificate(&mut fd, MAGISK_VER_CODE); - // Seek the fd back to start - fd.seek(SeekFrom::Start(0)).log_ok(); - info.stub_apk_fd = Some(fd); - } - + info.stub_apk_fd = apk.open(OFlag::O_RDONLY | OFlag::O_CLOEXEC).ok(); apk.remove().log_ok(); }