native: Remove custom package and cert validation

Remove support for custom/repackaged manager package names
(su_manager / repackaged_pkg) and secondary certificate verification
(repackaged_cert) from the native daemon and database.

- Remove SuManager from DbEntryKey in CXX bridge and db.rs.
- Remove unused get_db_string and rm_db_string methods from MagiskD.
- Delete check_dyn and check_stub in package.rs.
- Remove repackaged_* fields from ManagerInfo and derive Default.
- Simplify find_apk_path, get_package_uid, and get_manager to target
  APP_PACKAGE_NAME and verify against TRUSTED_CERT.
- Bump SQLite DB_VERSION to 13, drop strings table on migration, and
  remove create_strings from new database initialization.
- Remove obsolete hidden/stub app sections from FAQ documentation.

Assisted-by: Gemini 4 Argon
This commit is contained in:
topjohnwu
2026-10-22 14:06:48 -07:00
committed by John Wu
parent 7d9b0dfb3e
commit 5f2faee618
5 changed files with 34 additions and 185 deletions
-14
View File
@@ -16,20 +16,6 @@ The following details should ensure that modules are properly disabled:
Magisk no longer handles root hiding. There are plenty of Magisk/Zygisk modules available that specifically provide these functionalities, please search around 😉
### Q: Magisk App shows Magisk Installed = N/A after an update but magisk su is still working.
If upgrading with App hidden (ie. you took the 'Hide the Magisk app' option), the stub app (for hiding Magisk) may remain while a full Magisk app is also installed. This creates a conflict and the full app fails to see or access root... Uninstalling and reinstalling the full app can fix this, but not if a hidden app (stub) still exists.
The solution is to check for a hidden stub app and remove it. It may not show up normally in your launcher homescreen any longer, but should be visible from general settings, Apps. The hidden app will be named 'Settings' (default) or whatever you named it during the hiding process. Note that it is possible to have multiple obfuscated apps present. Uninstall any iterations of the hidden app you find and try opening the full app again. If necessary, uninstall it and reinstall the full app matching the binaries installed. Typing magisk -c in a terminal emulator app will show the version and version code for Magisk binaries installed (despite Installed = N/A showing).
Additionally, if a 'second space', eg. Workspace, Parallel Space etc, or another sandboxed environment, eg. a Multiple User additional profile, Island app or similar, is set up, check that no iterations of Magisk (either hidden or full apps) are running within these environments.
### Q: After I take the 'Hide the Magisk app' option the app icon is broken.
When hiding the Magisk app, it will install a "stub" APK that has nothing in it. The only functionality this stub app has is to download the full Magisk app APK data into its internal storage and dynamically load it. Due to the fact that the stub APK is literally empty, it does not contain the image resource for the app icon.
When you open the hidden Magisk app, it will offer you the option to create a shortcut in the homescreen (which has both the correct app name and icon) for your convenience. You can also manually ask the app to create the icon in app settings.
### Q: How to use Magisk in the emulator?
With the emulator running and accessible via ADB, run `./build.py emulator <path to Magisk APK>` to temporarily install Magisk on to the emulator. The patch is not persistent, meaning Magisk will be lost after a reboot, so re-execute the script to emulate a reboot if required.
-22
View File
@@ -87,7 +87,6 @@ impl DbEntryKey {
DbEntryKey::DenylistConfig => "denylist",
DbEntryKey::ZygiskConfig => "zygisk",
DbEntryKey::BootloopCount => "bootloop",
DbEntryKey::SuManager => "requester",
_ => "",
}
}
@@ -272,27 +271,6 @@ impl MagiskD {
Ok(cfg)
}
pub fn get_db_string(&self, key: DbEntryKey) -> String {
let mut val = "".to_string();
let mut func = |_: &[String], values: &DbValues| {
val.push_str(values.get_text(0));
};
self.db_exec_with_rows(
"SELECT value FROM strings WHERE key=?",
&[Text(key.to_str())],
&mut func,
)
.sql_result()
.log()
.ok();
val
}
pub fn rm_db_string(&self, key: DbEntryKey) -> SqliteResult<()> {
self.db_exec("DELETE FROM strings WHERE key=?", &[Text(key.to_str())])
.sql_result()
}
pub fn db_exec_for_cli(&self, mut file: UnixStream) -> LoggedResult<()> {
let mut reader = BufReader::new(&mut file);
let sql: String = reader.read_decodable()?;
-1
View File
@@ -90,7 +90,6 @@ pub mod ffi {
DenylistConfig,
ZygiskConfig,
BootloopCount,
SuManager,
}
#[repr(i32)]
+26 -140
View File
@@ -1,10 +1,12 @@
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 crate::daemon::{AID_APP_END, AID_APP_START, MagiskD, to_app_id};
use crate::ffi::{get_magisk_tmp, install_apk};
#[cfg(all(feature = "check-signature", not(debug_assertions)))]
use crate::ffi::uninstall_pkg;
use base::WalkResult::{Abort, Continue, Skip};
use base::{
BufReadExt, Directory, FsPathBuilder, LoggedResult, ReadExt, ResultExt, Utf8CStrBuf,
Utf8CString, cstr, error, fd_get_attr, pre_order_walk, warn,
Utf8CString, cstr, error, pre_order_walk,
};
use bit_set::BitSet;
use nix::fcntl::OFlag;
@@ -12,7 +14,6 @@ use std::collections::BTreeMap;
use std::fs::File;
use std::io;
use std::io::{Cursor, Read, Seek, SeekFrom};
use std::os::fd::AsRawFd;
use std::time::Duration;
const EOCD_MAGIC: u32 = 0x06054B50;
@@ -149,14 +150,16 @@ fn read_certificate(apk: &mut File, version: i32) -> Vec<u8> {
res.log().unwrap_or(vec![])
}
fn find_apk_path(pkg: &str) -> LoggedResult<Utf8CString> {
fn find_apk_path() -> Option<Utf8CString> {
let mut buf = Utf8CString::default();
pre_order_walk(cstr!("/data/app"), |e, path| {
if !e.is_dir() {
return Ok(Skip);
}
let name_bytes = e.name().as_bytes();
if name_bytes.starts_with(pkg.as_bytes()) && name_bytes.get(pkg.len()) == Some(&b'-') {
if name_bytes.starts_with(APP_PACKAGE_NAME.as_bytes())
&& name_bytes.get(APP_PACKAGE_NAME.len()) == Some(&b'-')
{
// Found the APK path, we can abort now
buf.append_path(path).append_path("base.apk");
return Ok(Abort);
@@ -165,15 +168,16 @@ fn find_apk_path(pkg: &str) -> LoggedResult<Utf8CString> {
return Ok(Continue);
}
Ok(Skip)
})?;
Ok(buf)
})
.ok()?;
if buf.is_empty() { None } else { Some(buf) }
}
fn get_package_uid(user: i32, pkg: &str) -> i32 {
fn get_package_uid(user: i32) -> i32 {
let path = cstr::buf::default()
.join_path(APP_DATA_DIR)
.join_path_fmt(user)
.join_path(pkg);
.join_path(APP_PACKAGE_NAME);
path.get_attr()
.map(|attr| attr.st.st_uid as i32)
.unwrap_or(-1)
@@ -182,29 +186,16 @@ fn get_package_uid(user: i32, pkg: &str) -> i32 {
enum Status {
Installed,
NotInstalled,
#[cfg(all(feature = "check-signature", not(debug_assertions)))]
CertMismatch,
}
#[derive(Default)]
pub struct ManagerInfo {
stub_apk_fd: Option<File>,
repackaged_app_id: i32,
repackaged_pkg: String,
repackaged_cert: Vec<u8>,
tracked_files: BTreeMap<i32, TrackedFile>,
}
impl Default for ManagerInfo {
fn default() -> Self {
ManagerInfo {
stub_apk_fd: None,
repackaged_app_id: -1,
repackaged_pkg: String::new(),
repackaged_cert: Vec::new(),
tracked_files: BTreeMap::new(),
}
}
}
#[derive(Default)]
struct TrackedFile {
path: Utf8CString,
@@ -235,66 +226,8 @@ impl TrackedFile {
}
impl ManagerInfo {
fn check_dyn(&mut self, user: i32, pkg: &str) -> Status {
let apk = cstr::buf::default()
.join_path(APP_DATA_DIR)
.join_path_fmt(user)
.join_path(pkg)
.join_path("dyn")
.join_path("current.apk");
let uid: i32;
let cert = match apk.open(OFlag::O_RDONLY | OFlag::O_CLOEXEC) {
Ok(mut fd) => {
uid = fd_get_attr(fd.as_raw_fd())
.map(|attr| attr.st.st_uid as i32)
.unwrap_or(-1);
read_certificate(&mut fd, MAGISK_VER_CODE)
}
Err(_) => {
warn!("pkg: no dyn APK, ignore");
return Status::NotInstalled;
}
};
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)))]
{
return Status::CertMismatch;
}
}
self.repackaged_app_id = to_app_id(uid);
self.tracked_files
.insert(user, TrackedFile::new(apk.to_owned()));
Status::Installed
}
fn check_stub(&mut self, user: i32, pkg: &str) -> Status {
let Ok(apk) = find_apk_path(pkg) else {
return Status::NotInstalled;
};
let cert = match apk.open(OFlag::O_RDONLY | OFlag::O_CLOEXEC) {
Ok(mut fd) => read_certificate(&mut fd, -1),
Err(_) => return Status::NotInstalled,
};
if cert.is_empty() || (pkg == self.repackaged_pkg && cert != self.repackaged_cert) {
error!("pkg: repackaged APK signature invalid: {}", apk);
uninstall_pkg(&apk);
return Status::CertMismatch;
}
self.repackaged_pkg.clear();
self.repackaged_pkg.push_str(pkg);
self.repackaged_cert = cert;
self.tracked_files.insert(user, TrackedFile::new(apk));
Status::Installed
}
fn check_orig(&mut self, user: i32) -> Status {
let Ok(apk) = find_apk_path(APP_PACKAGE_NAME) else {
let Some(apk) = find_apk_path() else {
return Status::NotInstalled;
};
@@ -338,14 +271,12 @@ impl ManagerInfo {
}
}
fn get_manager(&mut self, daemon: &MagiskD, user: i32, mut install: bool) -> (i32, &str) {
let db_pkg = daemon.get_db_string(DbEntryKey::SuManager);
// If database changed, always re-check files
if db_pkg != self.repackaged_pkg {
self.tracked_files.remove(&user);
}
fn get_manager(
&mut self,
daemon: &MagiskD,
user: i32,
#[allow(unused_mut)] mut install: bool,
) -> (i32, &str) {
if let Some(file) = self.tracked_files.get(&user)
&& file.is_same()
{
@@ -356,29 +287,8 @@ impl ManagerInfo {
}
return (-1, "");
}
// dyn APK is still the same
if file.path.starts_with(APP_DATA_DIR) {
return (
user * AID_USER_OFFSET + self.repackaged_app_id,
&self.repackaged_pkg,
);
}
// stub APK is still the same
if !self.repackaged_pkg.is_empty() {
return if matches!(
self.check_dyn(user, self.repackaged_pkg.clone().as_str()),
Status::Installed
) {
(
user * AID_USER_OFFSET + self.repackaged_app_id,
&self.repackaged_pkg,
)
} else {
(-1, "")
};
}
// orig APK is still the same
let uid = get_package_uid(user, APP_PACKAGE_NAME);
let uid = get_package_uid(user);
return if uid < 0 {
(-1, "")
} else {
@@ -386,40 +296,16 @@ impl ManagerInfo {
};
}
if !db_pkg.is_empty() {
match self.check_stub(user, &db_pkg) {
Status::Installed => {
return if matches!(self.check_dyn(user, &db_pkg), Status::Installed) {
(
user * AID_USER_OFFSET + self.repackaged_app_id,
&self.repackaged_pkg,
)
} else {
(-1, "")
};
}
Status::NotInstalled => {
daemon.rm_db_string(DbEntryKey::SuManager).ok();
}
Status::CertMismatch => {
install = true;
daemon.rm_db_string(DbEntryKey::SuManager).ok();
}
}
}
self.repackaged_pkg.clear();
self.repackaged_cert.clear();
match self.check_orig(user) {
Status::Installed => {
let uid = get_package_uid(user, APP_PACKAGE_NAME);
let uid = get_package_uid(user);
return if uid < 0 {
(-1, "")
} else {
(uid, APP_PACKAGE_NAME)
};
}
#[cfg(all(feature = "check-signature", not(debug_assertions)))]
Status::CertMismatch => install = true,
Status::NotInstalled => {}
}
+8 -8
View File
@@ -6,8 +6,8 @@
using namespace std;
#define DB_VERSION 12
#define DB_VERSION_STR "12"
#define DB_VERSION 13
#define DB_VERSION_STR "13"
// SQLite APIs
@@ -211,11 +211,6 @@ sqlite3 *open_and_init_db() {
"CREATE TABLE IF NOT EXISTS settings "
"(key TEXT, value INT, PRIMARY KEY(key))");
};
auto create_strings = [&] {
return sql_exec_impl(db.get(),
"CREATE TABLE IF NOT EXISTS strings "
"(key TEXT, value TEXT, PRIMARY KEY(key))");
};
auto create_denylist = [&] {
return sql_exec_impl(db.get(),
"CREATE TABLE IF NOT EXISTS denylist "
@@ -232,11 +227,11 @@ sqlite3 *open_and_init_db() {
// 10: remove table `logs`
// 11: remove table `hidelist` and create table `denylist` (same data structure)
// 12: rebuild table `policies` to drop column `package_name`
// 13: remove table `strings`
if (/* 0, 1, 2, 3, 4, 5, 6 */ ver <= 6) {
sql_chk_log(create_policy);
sql_chk_log(create_settings);
sql_chk_log(create_strings);
sql_chk_log(create_denylist);
// Directly jump to latest
@@ -295,6 +290,11 @@ sqlite3 *open_and_init_db() {
ver = 12;
upgrade = true;
}
if (ver == 12) {
sql_chk_log(sql_exec_impl, db.get(), "DROP TABLE IF EXISTS strings;");
ver = 13;
upgrade = true;
}
if (upgrade) {
// Set version