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
This commit is contained in:
topjohnwu
2026-10-22 14:06:48 -07:00
committed by John Wu
parent e31ef291d4
commit e8e8c3e3f1
7 changed files with 27 additions and 19 deletions
@@ -68,7 +68,7 @@ object Info {
val isConnected = MutableLiveData(false) val isConnected = MutableLiveData(false)
val showSuperUser: Boolean get() { 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) || Config.suMultiuserMode == Config.Value.MULTIUSER_MODE_USER)
} }
@@ -108,13 +108,13 @@ fun PackageManager.getPackageInfo(uid: Int, pid: Int): PackageInfo? {
// Try to find package name from PID // Try to find package name from PID
val proc = RootUtils.getAppProcess(pid) val proc = RootUtils.getAppProcess(pid)
if (proc == null) { 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. // 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, // 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. // because the client is forked from ADB shell, not any app process.
return getPackageInfo("com.android.shell", flag) 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) return getPackageInfo(proc.pkgList[0], flag)
} }
+7 -3
View File
@@ -178,15 +178,19 @@ impl MagiskD {
setup_logfile(); setup_logfile();
info!("** boot-complete triggered"); 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 // At this point it's safe to create the folder
let secure_dir = cstr!(SECURE_DIR); let secure_dir = cstr!(SECURE_DIR);
if !secure_dir.exists() { if !secure_dir.exists() {
secure_dir.mkdir(0o700).log_ok(); 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(); setup_preinit_dir();
if self.zygisk_enabled.load(Ordering::Relaxed) { if self.zygisk_enabled.load(Ordering::Relaxed) {
self.zygisk.lock().reset(true); self.zygisk.lock().reset(true);
+3 -1
View File
@@ -50,7 +50,9 @@ static void collect_users(vector<int> &users) {
return; return;
dirent *entry; dirent *entry;
while ((entry = xreaddir(data_dir.get()))) { 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);
} }
} }
+7 -11
View File
@@ -9,7 +9,6 @@ use base::{
}; };
use bit_set::BitSet; use bit_set::BitSet;
use nix::fcntl::OFlag; use nix::fcntl::OFlag;
use std::collections::BTreeMap;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::{Cursor, Read, Seek, SeekFrom}; use std::io::{Cursor, Read, Seek, SeekFrom};
@@ -191,7 +190,7 @@ enum Status {
#[derive(Default)] #[derive(Default)]
pub struct ManagerInfo { pub struct ManagerInfo {
tracked_files: BTreeMap<i32, TrackedFile>, tracked_file: TrackedFile,
} }
#[derive(Default)] #[derive(Default)]
@@ -224,7 +223,7 @@ impl TrackedFile {
} }
impl ManagerInfo { impl ManagerInfo {
fn check_orig(&mut self, user: i32) -> Status { fn check_orig(&mut self) -> Status {
let Some(apk) = find_apk_path() else { let Some(apk) = find_apk_path() else {
return Status::NotInstalled; 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 Status::Installed
} }
fn get_manager(&mut self, user: i32) -> (i32, &str) { fn get_manager(&mut self, user: i32) -> (i32, &str) {
if let Some(file) = self.tracked_files.get(&user) if self.tracked_file.is_same() {
&& file.is_same()
{
// no APK // no APK
if &file.path == PACKAGES_XML { if &self.tracked_file.path == PACKAGES_XML {
return (-1, ""); return (-1, "");
} }
// orig APK is still the same // orig APK is still the same
@@ -264,7 +261,7 @@ impl ManagerInfo {
}; };
} }
match self.check_orig(user) { match self.check_orig() {
Status::Installed => { Status::Installed => {
let uid = get_package_uid(user); let uid = get_package_uid(user);
return if uid < 0 { return if uid < 0 {
@@ -279,8 +276,7 @@ impl ManagerInfo {
} }
// If we cannot find any manager, track packages.xml for new package installs // If we cannot find any manager, track packages.xml for new package installs
self.tracked_files self.tracked_file = TrackedFile::new(PACKAGES_XML.into());
.insert(user, TrackedFile::new(PACKAGES_XML.into()));
(-1, "") (-1, "")
} }
+4 -1
View File
@@ -176,7 +176,7 @@ impl MagiskD {
let cfg = self.get_db_settings()?; let cfg = self.get_db_settings()?;
// Check multiuser settings // Check multiuser settings
let eval_uid = match cfg.multiuser_mode { let mut eval_uid = match cfg.multiuser_mode {
MultiuserMode::OwnerOnly => { MultiuserMode::OwnerOnly => {
if to_user_id(uid) != 0 { if to_user_id(uid) != 0 {
return Ok(Arc::new(SuInfo::deny(uid))); return Ok(Arc::new(SuInfo::deny(uid)));
@@ -186,6 +186,9 @@ impl MagiskD {
MultiuserMode::OwnerManaged => to_app_id(uid), MultiuserMode::OwnerManaged => to_app_id(uid),
_ => uid, _ => uid,
}; };
if to_app_id(eval_uid) < AID_APP_START {
eval_uid = to_app_id(eval_uid);
}
let mut settings = RootSettings::default(); let mut settings = RootSettings::default();
self.get_root_settings(eval_uid, &mut settings)?; self.get_root_settings(eval_uid, &mut settings)?;
+3
View File
@@ -98,6 +98,9 @@ impl MagiskD {
MultiuserMode::OwnerManaged => uid = to_app_id(uid), 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 granted = false;
let mut output_fn = let mut output_fn =