From 2793d209a40da098abba270e9f4bab5c052e81df Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 7 May 2022 00:46:23 -0700 Subject: [PATCH] Allow requesting root from non app process --- .../magisk/core/su/SuRequestHandler.kt | 6 +++++- .../topjohnwu/magisk/core/utils/RootUtils.kt | 12 ++++++++--- .../magisk/ui/surequest/SuRequestViewModel.kt | 20 ++++++++++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt index f56390ce1..adc0d8c46 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt @@ -66,7 +66,11 @@ class SuRequestHandler( val fifo = intent.getStringExtra("fifo") ?: throw SuRequestError() val uid = intent.getIntExtra("uid", -1).also { if (it < 0) throw SuRequestError() } val pid = intent.getIntExtra("pid", -1) - pkgInfo = pm.getPackageInfo(uid, pid) ?: throw SuRequestError() + pkgInfo = pm.getPackageInfo(uid, pid) ?: PackageInfo().apply { + val name = pm.getNameForUid(uid) ?: throw SuRequestError() + // We only fill in sharedUserId and leave other fields uninitialized + sharedUserId = name.split(":")[0] + } output = DataOutputStream(FileOutputStream(fifo).buffered()) policy = SuPolicy(uid) true diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/RootUtils.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/RootUtils.kt index 4c272cd17..c99bd76f9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/utils/RootUtils.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/RootUtils.kt @@ -12,6 +12,7 @@ import com.topjohnwu.superuser.ipc.RootService import com.topjohnwu.superuser.nio.FileSystemManager import timber.log.Timber import java.io.File +import java.io.IOException import java.util.concurrent.locks.AbstractQueuedSynchronizer class RootUtils(stub: Any?) : RootService() { @@ -55,9 +56,14 @@ class RootUtils(stub: Any?) : RootService() { if (proc != null) return proc // Find PPID - File("/proc/$pid/status").useLines { - val s = it.find { line -> line.startsWith("PPid:") } - pid = s?.substring(5)?.trim()?.toInt() ?: -1 + try { + File("/proc/$pid/status").useLines { + val line = it.find { l -> l.startsWith("PPid:") } ?: return null + pid = line.substring(5).trim().toInt() + } + } catch (e: IOException) { + // The process died unexpectedly + return null } } return null diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt index 9ad5f1358..5e53991ae 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt @@ -110,11 +110,21 @@ class SuRequestViewModel( private fun showDialog() { val pm = handler.pm val info = handler.pkgInfo - val prefix = if (info.sharedUserId == null) "" else "[SharedUID] " + val app = info.applicationInfo + + if (app == null) { + // The request is not coming from an app process, and the UID is a + // shared UID. We have no way to know where this request comes from. + icon = pm.defaultActivityIcon + title = "[SharedUID] ${info.sharedUserId}" + packageName = info.sharedUserId + } else { + val prefix = if (info.sharedUserId == null) "" else "[SharedUID] " + icon = app.loadIcon(pm) + title = "$prefix${app.getLabel(pm)}" + packageName = info.packageName + } - icon = info.applicationInfo.loadIcon(pm) - title = "$prefix${info.applicationInfo.getLabel(pm)}" - packageName = info.packageName selectedItemPosition = timeoutPrefs.getInt(packageName, 0) // Set timer @@ -135,7 +145,7 @@ class SuRequestViewModel( timer.cancel() val pos = selectedItemPosition - timeoutPrefs.edit().putInt(handler.pkgInfo.packageName, pos).apply() + timeoutPrefs.edit().putInt(packageName, pos).apply() viewModelScope.launch { handler.respond(action, Config.Value.TIMEOUT_LIST[pos])