From 5dd89a2bf33972adbf15db58402bb4a8bcd94d1c Mon Sep 17 00:00:00 2001 From: McTr0 <1334853459@qq.com> Date: Sun, 22 Nov 2026 07:49:00 -0500 Subject: [PATCH 1/2] Keep the daemon's resource-cache shadow inside its own mount namespace The daemon shadows /data/resource-cache so its root app_process does not write the boot's framework idmaps into the cache every other process shares. The shadow is only safe while service.sh actually puts it in a private mount namespace: when that isolation is not available (toybox unshare lacking the flags, busybox and toybox disagreeing, or the SELinux domain refusing unshare outright), the bind mount used to land in the global namespace and hide the idmaps behind the framework-res overlay redirects from every process launched afterwards. A system_server booting behind a leaked shadow then reads the un-overlaid config_servicesExtensionPackage value. On GMS builds that names android.ext.services, which does not exist on the system image, and PMS init throws from getRequiredServicesExtensionPackageLPr every cycle - the system_server boot loop of #1101. service.sh now degrades gracefully instead: when a private namespace cannot be established the daemon starts unshadowed (an idmap2 write at most) rather than leaking a shadow or not starting at all, and the daemon script mounts its shadow only when service.sh confirms the namespace is private (VECTOR_SHADOW_OK). Fixes the boot-loop half of #1101. --- zygisk/module/daemon | 18 +++++++++++++----- zygisk/module/service.sh | 36 +++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/zygisk/module/daemon b/zygisk/module/daemon index 79a70d799..a4b519177 100644 --- a/zygisk/module/daemon +++ b/zygisk/module/daemon @@ -30,11 +30,19 @@ fi # framework idmaps of this boot into the cache every other process shares. Bind an empty directory # rather than a tmpfs: a bind mount reuses the superblock of /data, so it claims none of the # kernel-global anonymous device minors, whose gaps are visible from any process on the device. -# Note that this script runs inside an isolated mount namespace (via unshare). -emptyDir="$dir/.resource-cache" -rm -rf "$emptyDir" -mkdir -p "$emptyDir" -mount -o bind "$emptyDir" /data/resource-cache +# +# Only while service.sh has put us in a private mount namespace (VECTOR_SHADOW_OK). In the global +# namespace this mount would hide the cache — and with it the idmaps behind the framework-res +# overlay redirects — from every process launched afterwards, and a system_server booting behind +# it dies in PMS init on GMS builds: config_servicesExtensionPackage resolves to a package name +# nothing on the system image answers to. Without a private namespace the daemon therefore runs +# unshadowed; that costs an idmap2 write at most, while a leaked shadow costs a boot. +if [ "$VECTOR_SHADOW_OK" = "1" ]; then + emptyDir="$dir/.resource-cache" + rm -rf "$emptyDir" + mkdir -p "$emptyDir" + mount -o bind "$emptyDir" /data/resource-cache +fi # Wait for Zygote socket using a polling loop if [ ! -S "/dev/socket/zygote" ]; then diff --git a/zygisk/module/service.sh b/zygisk/module/service.sh index cc00edf41..e1b13b03e 100644 --- a/zygisk/module/service.sh +++ b/zygisk/module/service.sh @@ -1,13 +1,31 @@ -# Extract the directory path and change directory +# Extract the directory path and change directory MODDIR="${0%/*}" cd "$MODDIR" || exit 1 +# The daemon shadows /data/resource-cache while it runs, so its own app_process does not write the +# boot's framework idmaps into the cache every other process shares. That shadow is only safe +# inside a mount namespace nothing else can see: in the global one it blanks the RRO idmap cache +# for every process launched afterwards, including a freshly booting system_server — whose +# framework-res overlay redirects then stop resolving, and PMS refuses to start when +# config_servicesExtensionPackage does not name a package on the system image. That is a full +# system_server boot loop on GMS builds. +# # A root manager runs this script with its own busybox, but a manual bootstrap has nothing in -# front of it except Android's utilities, so this is written for toybox's unshare: it makes a -# mount namespace with -m and takes no propagation flag. The namespace therefore marks its own -# root slave before handing over, which is what keeps the daemon's dex2oat bind-mounts out of -# the global one. -unshare -m /system/bin/sh -c ' - mount -o rslave none / || echo "vector: this mount namespace stays shared" >&2 - exec "$0" "$@" -' "$MODDIR/daemon" "$@" & +# front of it except Android's utilities, and the two disagree: toybox unshare takes no +# propagation flag, busybox unshare does; and whether unshare is permitted at all depends on the +# SELinux domain the script runs in. So every branch here degrades to starting the daemon +# unshadowed rather than to not starting it, and the daemon script mounts its shadow only when +# this script confirms the namespace around it is private (VECTOR_SHADOW_OK). +if unshare -m true 2>/dev/null; then + unshare -m /system/bin/sh -c ' + if mount -o rslave none /; then + VECTOR_SHADOW_OK=1 exec "$0" "$@" + else + echo "vector: mount namespace stays shared; daemon starts without the resource-cache shadow" >&2 + VECTOR_NO_SHADOW=1 exec "$0" "$@" + fi + ' "$MODDIR/daemon" "$@" & +else + echo "vector: no private mount namespace available; daemon starts without the resource-cache shadow" >&2 + VECTOR_NO_SHADOW=1 "$MODDIR/daemon" "$@" & +fi From 9954bdfb6b143cada9b112fa3d0cc862256af142 Mon Sep 17 00:00:00 2001 From: McTr0 <1334853459@qq.com> Date: Sun, 22 Nov 2026 07:49:00 -0500 Subject: [PATCH 2/2] Normalize the system framework's scope name to one spelling The manager stores the System Framework scope as `system`, but the libxposed API, the modules asking for a scope through it, and the CLI all use `android` for the same target - and the rows were stored verbatim. The one query that serves system_server only matched `system`, so a module scoped through any spelling but the manager's was silently absent from the module list: nothing loaded into system_server, onSystemServerStarting dispatched over an empty set, and the modules log showed no system_server entries at all. That is the never-dispatched half of #1101, and it reproduced on clean boot cycles because it was not a race. Writes are normalized at the storage boundary (setModuleScope, the scope request approval, and getModuleScope's read-back), the system_server reader accepts both spellings (deduped, since both rows can exist), removal clears both, and the cache expansion treats the alias the same way. The unresolved apkPath skip in getModulesForSystemServer also stops being silent. Verified on HyperOS 2.0 (Android 15, beryl): with a scope row stored as `android`, system_server now loads the module and dispatches onSystemServerStarting on the first soft reboot after this change. Fixes the dispatch half of #1101. --- .../org/matrix/vector/daemon/VectorService.kt | 15 ++++- .../matrix/vector/daemon/data/ConfigCache.kt | 20 +++++- .../vector/daemon/data/ModuleDatabase.kt | 63 +++++++++++++++---- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt index aa22bc6a1..cd97f85db 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt @@ -532,17 +532,26 @@ object VectorService : IVectorDaemon.Stub() { val scopes = ModuleDatabase.getModuleScope(packageName) ?: mutableListOf() var added = false granted.forEach { scopePackageName -> + // Spelled the way the row will be stored: a module asks for the framework under the + // libxposed name `android`, and a row under that spelling used to be invisible to + // the system_server reader. [ModuleDatabase.setModuleScope] normalizes on the way + // in as well; this is spelled up front so the duplicate check below compares like + // with like. + val storedName = + if (scopePackageName == ModuleDatabase.SYSTEM_FRAMEWORK_ALIAS) + ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE + else scopePackageName // Compared against where the row will land, not against the user who asked: the // framework is stored under user 0 whoever requested it, so for "system" this test // never matched and every approval appended a duplicate and rewrote the whole // table. - val storedUserId = if (scopePackageName == "system") 0 else userId + val storedUserId = if (storedName == ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE) 0 else userId val present = - scopes.any { it.packageName == scopePackageName && it.userId == storedUserId } + scopes.any { it.packageName == storedName && it.userId == storedUserId } if (!present) { scopes.add( ScopeEntry().apply { - this.packageName = scopePackageName + this.packageName = storedName this.userId = storedUserId }) added = true diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt index b79aa1181..6054225ef 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt @@ -341,14 +341,19 @@ object ConfigCache { } ModuleDatabase.enabledScopeRows().forEach { scopeRow -> - val appPkg = scopeRow.appPackage + val appPkg = + // The framework scope answers to two spellings in the wild (the manager's `system` and + // the libxposed spelling `android`); both name the same target here. + if (scopeRow.appPackage == ModuleDatabase.SYSTEM_FRAMEWORK_ALIAS) + ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE + else scopeRow.appPackage val modPkg = scopeRow.modulePackage val userId = scopeRow.userId val module = newModules[modPkg] ?: return@forEach val holders = moduleUsers[modPkg].orEmpty() - if (appPkg == "system") { + if (appPkg == ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE) { // system_server is one process for the whole device and belongs to no user, so any user // holding the module may hook it and the row is stored under user 0 whoever asked. It is // the one target the boundary below does not apply to, because there is no second copy @@ -502,7 +507,16 @@ object ConfigCache { val pkgName = row.packageName // A row with no recorded path has never been resolved; the rebuild will fill it in, // and injecting from a null path is not something to attempt in the meantime. - val apkPath = row.apkPath ?: return@forEach + val apkPath = row.apkPath + if (apkPath == null) { + // The silent version of this skip sent issue #1101's reporter looking at the dispatch + // chain, because nothing anywhere said the module was never served. + Log.w( + TAG, + "Module $pkgName is scoped to the system framework but its APK path is not " + + "resolved yet; it is not served to system_server this boot") + return@forEach + } val cached = currentState.modules[pkgName] if (cached != null) { diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt index 733b2c3ee..aad048e0d 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt @@ -36,6 +36,17 @@ private const val TAG = "VectorModuleDatabase" */ object ModuleDatabase { + /** + * The package name the system framework is stored under. The manager uses this spelling; the + * libxposed API, the modules asking for the scope through it, and the CLI (which forwards what + * the user typed) all use [SYSTEM_FRAMEWORK_ALIAS] for the same target — and a scope stored + * under that spelling used to be invisible to the one reader that serves system_server, leaving + * every android-scoped module unloaded without a word. Writes are normalized to this spelling, + * and the readers accept both. + */ + const val SYSTEM_FRAMEWORK_PACKAGE = "system" + const val SYSTEM_FRAMEWORK_ALIAS = "android" + /** The one database handle. [ConfigCache], [PreferenceStore] and the CLI all borrow it. */ val dbHelper = Database() @@ -55,7 +66,12 @@ object ModuleDatabase { while (cursor.moveToNext()) { result.add( ScopeEntry().apply { - this.packageName = cursor.getString(0) + this.packageName = + cursor.getString(0).let { + // One spelling out, so the manager sees its framework entry where a + // pre-normalization row spells it differently. + if (it == SYSTEM_FRAMEWORK_ALIAS) SYSTEM_FRAMEWORK_PACKAGE else it + } this.userId = cursor.getInt(1) }) } @@ -182,8 +198,8 @@ object ModuleDatabase { .query( "scope INNER JOIN modules ON scope.mid = modules.mid", arrayOf("module_pkg_name", "apk_path"), - "app_pkg_name=? AND enabled=1", - arrayOf("system"), + "app_pkg_name IN (?, ?) AND enabled=1", + arrayOf(SYSTEM_FRAMEWORK_PACKAGE, SYSTEM_FRAMEWORK_ALIAS), null, null, null) @@ -192,7 +208,9 @@ object ModuleDatabase { rows += EnabledModuleRow(cursor.getString(0), cursor.getString(1)) } } - return rows + // A module scoped under both spellings (a CLI row from before the writes were normalized, then + // the manager's) must be served once: twice in this list is twice loaded into system_server. + return rows.distinctBy { it.packageName } } /** @@ -319,8 +337,15 @@ object ModuleDatabase { // a backup written by an older manager, which recorded the framework under the module's // own user, silently lost the one target the module may have cared about — and said // nothing about it. - val userId = if (app.packageName == "system") 0 else app.userId - values.put("app_pkg_name", app.packageName) + // + // The framework target is normalized to one spelling here: the manager says `system`, the + // libxposed API, the modules and the CLI say `android`, and a row stored under the alias + // spelling used to match no reader at all. + val appPackage = + if (app.packageName == SYSTEM_FRAMEWORK_ALIAS) SYSTEM_FRAMEWORK_PACKAGE + else app.packageName + val userId = if (appPackage == SYSTEM_FRAMEWORK_PACKAGE) 0 else app.userId + values.put("app_pkg_name", appPackage) values.put("user_id", userId) db.insertWithOnConflict("scope", null, values, SQLiteDatabase.CONFLICT_IGNORE) } @@ -369,16 +394,32 @@ object ModuleDatabase { // matches no row at all - which meant a module outside user 0 could take system scope and then // never give it back. It reached that state through the very same call: removeScope returns // nothing, so the module was told its request had been honoured. - val storedUserId = if (scopePackageName == "system") 0 else userId + // + // Both spellings go, not just the caller's: a row written before the writes were normalized + // must not survive a removal the caller believes removed it. + val isFrameworkTarget = + scopePackageName == SYSTEM_FRAMEWORK_PACKAGE || scopePackageName == SYSTEM_FRAMEWORK_ALIAS + val storedUserId = if (isFrameworkTarget) 0 else userId val db = dbHelper.writableDatabase val mid = db.compileStatement("SELECT mid FROM modules WHERE module_pkg_name = ?") .apply { bindString(1, packageName) } .simpleQueryForLong() - db.delete( - "scope", - "mid = ? AND app_pkg_name = ? AND user_id = ?", - arrayOf(mid.toString(), scopePackageName, storedUserId.toString())) + if (isFrameworkTarget) { + db.delete( + "scope", + "mid = ? AND app_pkg_name IN (?, ?) AND user_id = ?", + arrayOf( + mid.toString(), + SYSTEM_FRAMEWORK_PACKAGE, + SYSTEM_FRAMEWORK_ALIAS, + storedUserId.toString())) + } else { + db.delete( + "scope", + "mid = ? AND app_pkg_name = ? AND user_id = ?", + arrayOf(mid.toString(), scopePackageName, storedUserId.toString())) + } ConfigCache.requestCacheUpdate() return true }