From e8e8c3e3f1f2702553a7c82529d5335b48485423 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 22 Oct 2026 18:29:55 +0000 Subject: [PATCH] Fix HSUM su policy lookup and manager tracking Normalize system and shared-UID (< 10000) processes to their app ID when evaluating su policies and uid_granted_root so policies stored by PolicyDao.normalizeUid match across non-zero users on HSUM devices. - Normalize eval_uid in build_su_info and uid in uid_granted_root when app_id < AID_APP_START. - Ensure SECURE_DIR exists before writing settings in boot_complete(), seed SuMultiuserMode on HSUM, and check isHeadlessSystemUser in Info.showSuperUser. - Normalize UID comparisons in PackageManager.getPackageInfo(uid, pid). - Simplify ManagerInfo to track a single TrackedFile instead of a per-user BTreeMap. - Skip negative parse_int results in deny collect_users. Assisted-by: Gemini 4 Argon --- .../java/com/topjohnwu/magisk/core/Info.kt | 2 +- .../com/topjohnwu/magisk/core/ktx/XAndroid.kt | 4 ++-- native/src/core/bootstages.rs | 10 +++++++--- native/src/core/deny/utils.cpp | 4 +++- native/src/core/package.rs | 18 +++++++----------- native/src/core/su/daemon.rs | 5 ++++- native/src/core/su/db.rs | 3 +++ 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt index 87144f16e..5ea3280db 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt @@ -68,7 +68,7 @@ object Info { val isConnected = MutableLiveData(false) val showSuperUser: Boolean get() { - return env.isActive && (Const.USER_ID == 0 + return env.isActive && (Const.USER_ID == 0 || isHeadlessSystemUser || Config.suMultiuserMode == Config.Value.MULTIUSER_MODE_USER) } diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/ktx/XAndroid.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/ktx/XAndroid.kt index 287a41d99..3fb78974b 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/ktx/XAndroid.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/ktx/XAndroid.kt @@ -108,13 +108,13 @@ fun PackageManager.getPackageInfo(uid: Int, pid: Int): PackageInfo? { // Try to find package name from PID val proc = RootUtils.getAppProcess(pid) if (proc == null) { - if (uid == Process.SHELL_UID) { + if (PolicyDao.normalizeUid(uid) == Process.SHELL_UID) { // It is possible that some apps installed are sharing UID with shell. // We will not be able to find a package from the active process list, // because the client is forked from ADB shell, not any app process. return getPackageInfo("com.android.shell", flag) } - } else if (uid == proc.uid) { + } else if (PolicyDao.normalizeUid(uid) == PolicyDao.normalizeUid(proc.uid)) { return getPackageInfo(proc.pkgList[0], flag) } diff --git a/native/src/core/bootstages.rs b/native/src/core/bootstages.rs index e408d0cb1..d6da6fc91 100644 --- a/native/src/core/bootstages.rs +++ b/native/src/core/bootstages.rs @@ -178,15 +178,19 @@ impl MagiskD { setup_logfile(); info!("** boot-complete triggered"); - // Reset the bootloop counter once we have boot-complete - self.set_db_setting(DbEntryKey::BootloopCount, 0).log_ok(); - // At this point it's safe to create the folder let secure_dir = cstr!(SECURE_DIR); if !secure_dir.exists() { secure_dir.mkdir(0o700).log_ok(); } + // Reset the bootloop counter once we have boot-complete + self.set_db_setting(DbEntryKey::BootloopCount, 0).log_ok(); + if self.is_headless { + self.set_db_setting(DbEntryKey::SuMultiuserMode, MultiuserMode::User as i32) + .log_ok(); + } + setup_preinit_dir(); if self.zygisk_enabled.load(Ordering::Relaxed) { self.zygisk.lock().reset(true); diff --git a/native/src/core/deny/utils.cpp b/native/src/core/deny/utils.cpp index 1c808b8fc..4dd702e21 100644 --- a/native/src/core/deny/utils.cpp +++ b/native/src/core/deny/utils.cpp @@ -50,7 +50,9 @@ static void collect_users(vector &users) { return; dirent *entry; while ((entry = xreaddir(data_dir.get()))) { - users.emplace_back(parse_int(entry->d_name)); + int u = parse_int(entry->d_name); + if (u >= 0) + users.emplace_back(u); } } diff --git a/native/src/core/package.rs b/native/src/core/package.rs index c5efd721e..2c929d6c0 100644 --- a/native/src/core/package.rs +++ b/native/src/core/package.rs @@ -9,7 +9,6 @@ use base::{ }; use bit_set::BitSet; use nix::fcntl::OFlag; -use std::collections::BTreeMap; use std::fs::File; use std::io; use std::io::{Cursor, Read, Seek, SeekFrom}; @@ -191,7 +190,7 @@ enum Status { #[derive(Default)] pub struct ManagerInfo { - tracked_files: BTreeMap, + tracked_file: TrackedFile, } #[derive(Default)] @@ -224,7 +223,7 @@ impl TrackedFile { } impl ManagerInfo { - fn check_orig(&mut self, user: i32) -> Status { + fn check_orig(&mut self) -> Status { let Some(apk) = find_apk_path() else { return Status::NotInstalled; }; @@ -243,16 +242,14 @@ impl ManagerInfo { } } - self.tracked_files.insert(user, TrackedFile::new(apk)); + self.tracked_file = TrackedFile::new(apk); Status::Installed } fn get_manager(&mut self, user: i32) -> (i32, &str) { - if let Some(file) = self.tracked_files.get(&user) - && file.is_same() - { + if self.tracked_file.is_same() { // no APK - if &file.path == PACKAGES_XML { + if &self.tracked_file.path == PACKAGES_XML { return (-1, ""); } // orig APK is still the same @@ -264,7 +261,7 @@ impl ManagerInfo { }; } - match self.check_orig(user) { + match self.check_orig() { Status::Installed => { let uid = get_package_uid(user); return if uid < 0 { @@ -279,8 +276,7 @@ impl ManagerInfo { } // If we cannot find any manager, track packages.xml for new package installs - self.tracked_files - .insert(user, TrackedFile::new(PACKAGES_XML.into())); + self.tracked_file = TrackedFile::new(PACKAGES_XML.into()); (-1, "") } diff --git a/native/src/core/su/daemon.rs b/native/src/core/su/daemon.rs index 0f8a54954..1f30c0c18 100644 --- a/native/src/core/su/daemon.rs +++ b/native/src/core/su/daemon.rs @@ -176,7 +176,7 @@ impl MagiskD { let cfg = self.get_db_settings()?; // Check multiuser settings - let eval_uid = match cfg.multiuser_mode { + let mut eval_uid = match cfg.multiuser_mode { MultiuserMode::OwnerOnly => { if to_user_id(uid) != 0 { return Ok(Arc::new(SuInfo::deny(uid))); @@ -186,6 +186,9 @@ impl MagiskD { MultiuserMode::OwnerManaged => to_app_id(uid), _ => uid, }; + if to_app_id(eval_uid) < AID_APP_START { + eval_uid = to_app_id(eval_uid); + } let mut settings = RootSettings::default(); self.get_root_settings(eval_uid, &mut settings)?; diff --git a/native/src/core/su/db.rs b/native/src/core/su/db.rs index ba69bdd2a..dc40afb32 100644 --- a/native/src/core/su/db.rs +++ b/native/src/core/su/db.rs @@ -98,6 +98,9 @@ impl MagiskD { MultiuserMode::OwnerManaged => uid = to_app_id(uid), _ => {} } + if to_app_id(uid) < AID_APP_START { + uid = to_app_id(uid); + } let mut granted = false; let mut output_fn =