From 1232113772139f89b8b339dcd69b1f23bcf13c2a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 9 Dec 2020 02:07:58 -0800 Subject: [PATCH] Update preference migration implementation Only try to read preference through content provider when the app is fresh install and a previous package ID is set. Also catch all Exceptions to prevent crashing the app. This prevents malicious settings injection and crashes when multiple manager is installed. Fix #3542 --- app/src/main/AndroidManifest.xml | 2 +- app/src/main/java/com/topjohnwu/magisk/core/Config.kt | 8 ++++---- app/src/main/java/com/topjohnwu/magisk/core/Const.kt | 2 +- .../main/java/com/topjohnwu/magisk/core/SplashActivity.kt | 6 +++--- .../main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt | 3 ++- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c0beb16f9..73c3175b0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Config.kt b/app/src/main/java/com/topjohnwu/magisk/core/Config.kt index 867497820..a854886db 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Config.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Config.kt @@ -18,7 +18,6 @@ import com.topjohnwu.magisk.ktx.inject import com.topjohnwu.magisk.ui.theme.Theme import org.xmlpull.v1.XmlPullParser import java.io.File -import java.io.IOException import java.io.InputStream object Config : PreferenceModel, DBConfig { @@ -159,12 +158,13 @@ object Config : PreferenceModel, DBConfig { private const val SU_FINGERPRINT = "su_fingerprint" - fun load(pkg: String) { - try { + fun load(pkg: String?) { + // Only try to load prefs when fresh install and a previous package name is set + if (pkg != null && prefs.all.isEmpty()) runCatching { context.contentResolver.openInputStream(Provider.PREFS_URI(pkg))?.use { prefs.edit { parsePrefs(it) } } - } catch (e: IOException) {} + } prefs.edit { // Settings migration diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Const.kt b/app/src/main/java/com/topjohnwu/magisk/core/Const.kt index df40c9919..2aa5094bb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Const.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Const.kt @@ -58,7 +58,7 @@ object Const { object Key { // intents const val OPEN_SECTION = "section" - const val HIDDEN_PKG = "hidden_pkg" + const val PREV_PKG = "prev_pkg" } object Value { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/SplashActivity.kt b/app/src/main/java/com/topjohnwu/magisk/core/SplashActivity.kt index 484600906..c88c73fbb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/SplashActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/SplashActivity.kt @@ -48,10 +48,10 @@ open class SplashActivity : Activity() { // Pre-initialize root shell Shell.getShell() - val hiddenPackage = intent.getStringExtra(Const.Key.HIDDEN_PKG) + val prevPkg = intent.getStringExtra(Const.Key.PREV_PKG) - Config.load(hiddenPackage ?: APPLICATION_ID) - handleRepackage(hiddenPackage) + Config.load(prevPkg) + handleRepackage(prevPkg) Notifications.setup(this) UpdateCheckService.schedule(this) Shortcuts.setupDynamic(this) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt index 90064e747..7cabf15e6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt @@ -123,6 +123,7 @@ object HideAPK { Config.suManager = pkg grantUriPermission(pkg, APK_URI, Intent.FLAG_GRANT_READ_URI_PERMISSION) grantUriPermission(pkg, PREFS_URI, Intent.FLAG_GRANT_READ_URI_PERMISSION) + intent.putExtra(Const.Key.PREV_PKG, packageName) startActivity(intent) } @@ -167,7 +168,7 @@ object HideAPK { Config.suManager = "" grantUriPermission(APPLICATION_ID, APK_URI, Intent.FLAG_GRANT_READ_URI_PERMISSION) grantUriPermission(APPLICATION_ID, PREFS_URI, Intent.FLAG_GRANT_READ_URI_PERMISSION) - intent.putExtra(Const.Key.HIDDEN_PKG, packageName) + intent.putExtra(Const.Key.PREV_PKG, packageName) startActivity(intent) }