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
+7 -3
View File
@@ -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);
+3 -1
View File
@@ -50,7 +50,9 @@ static void collect_users(vector<int> &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);
}
}
+7 -11
View File
@@ -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<i32, TrackedFile>,
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, "")
}
+4 -1
View File
@@ -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)?;
+3
View File
@@ -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 =