Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
})
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 }
}

/**
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 13 additions & 5 deletions zygisk/module/daemon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 27 additions & 9 deletions zygisk/module/service.sh
Original file line number Diff line number Diff line change
@@ -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