forked from topjohnwu/Magisk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ContentProvider call method for communication
Previously, we use either BroadcastReceivers or Activities to receive messages from our native daemon, but both have their own downsides. Some OEMs blocks broadcasts if the app is not running in the background, regardless of who the caller is. Activities on the other hand, despite working 100% of the time, will steal the focus of the current foreground app, even though we are just doing some logging and showing a toast. In addition, since stubs for hiding Magisk Manager is introduced, our only communication method is left with the broadcast option, as only broadcasting allows targeting a specific package name, not a component name (which will be obfuscated in the case of stubs). To make sure root requests will work on all devices, Magisk had to do some experiments every boot to test whether broadcast is deliverable or not. This makes the whole thing even more complicated then ever. So lets take a look at another kind of component in Android apps: ContentProviders. It is a vital part of Android's ecosystem, and as far as I know no OEMs will block requests to ContentProviders (or else tons of functionality will break catastrophically). Starting at API 11, the system supports calling a specific method in ContentProviders, optionally sending extra data along with the method call. This is perfect for the native daemon to start a communication with Magisk Manager. Another cool thing is that we no longer need to know the component name of the reciever, as ContentProviders identify themselves with an "authority" name, which in Magisk Manager's case is tied to the package name. We already have a mechanism to keep track of our current manager package name, so this works out of the box. So yay! No more flaky broadcast tests, no more stupid OEMs blocking broadcasts for some bizzare reasons. This method should in theory work on almost all devices and situations.
- Loading branch information
topjohnwu
committed
Nov 4, 2019
1 parent
472cde2
commit 25c5572
Showing
19 changed files
with
238 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
app/src/main/java/com/topjohnwu/magisk/utils/SuHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.topjohnwu.magisk.utils | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.os.Process | ||
import android.widget.Toast | ||
import com.topjohnwu.magisk.* | ||
import com.topjohnwu.magisk.data.repository.LogRepository | ||
import com.topjohnwu.magisk.extensions.get | ||
import com.topjohnwu.magisk.extensions.startActivity | ||
import com.topjohnwu.magisk.extensions.startActivityWithRoot | ||
import com.topjohnwu.magisk.extensions.subscribeK | ||
import com.topjohnwu.magisk.model.entity.MagiskPolicy | ||
import com.topjohnwu.magisk.model.entity.toLog | ||
import com.topjohnwu.magisk.model.entity.toPolicy | ||
import com.topjohnwu.magisk.ui.surequest.SuRequestActivity | ||
import com.topjohnwu.superuser.Shell | ||
import timber.log.Timber | ||
import java.util.* | ||
|
||
object SuHandler : ProviderCallHandler { | ||
|
||
const val REQUEST = "request" | ||
const val LOG = "log" | ||
const val NOTIFY = "notify" | ||
const val TEST = "test" | ||
|
||
override fun call(context: Context, method: String, arg: String?, extras: Bundle?): Bundle? { | ||
invoke(context.wrap(), method, extras) | ||
return null | ||
} | ||
|
||
operator fun invoke(context: Context, action: String?, data: Bundle?) { | ||
data ?: return | ||
|
||
// Debug messages | ||
if (BuildConfig.DEBUG) { | ||
Timber.d(action) | ||
data.let { bundle -> | ||
bundle.keySet().forEach { | ||
Timber.d("[%s]=[%s]", it, bundle[it]) | ||
} | ||
} | ||
} | ||
|
||
when (action) { | ||
REQUEST -> { | ||
val intent = context.intent<SuRequestActivity>() | ||
.setAction(action) | ||
.putExtras(data) | ||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK) | ||
if (Build.VERSION.SDK_INT >= 29) { | ||
// Android Q does not allow starting activity from background | ||
intent.startActivityWithRoot() | ||
} else { | ||
intent.startActivity(context) | ||
} | ||
} | ||
LOG -> handleLogs(context, data) | ||
NOTIFY -> handleNotify(context, data) | ||
TEST -> { | ||
val mode = data.getInt("mode", 2) | ||
Shell.su( | ||
"magisk --connect-mode $mode", | ||
"magisk --use-broadcast" | ||
).submit() | ||
} | ||
} | ||
} | ||
|
||
private fun Any?.toInt(): Int? { | ||
return when (this) { | ||
is Int -> this | ||
is Long -> this.toInt() | ||
else -> null | ||
} | ||
} | ||
|
||
private fun handleLogs(context: Context, data: Bundle) { | ||
val fromUid = data["from.uid"].toInt() ?: return | ||
if (fromUid == Process.myUid()) | ||
return | ||
|
||
val pm = context.packageManager | ||
|
||
val notify = data.getBoolean("notify", true) | ||
val allow = data["policy"].toInt() ?: return | ||
|
||
val policy = runCatching { fromUid.toPolicy(pm, allow) }.getOrElse { return } | ||
|
||
if (notify) | ||
notify(context, policy) | ||
|
||
val toUid = data["to.uid"].toInt() ?: return | ||
val pid = data["pid"].toInt() ?: return | ||
|
||
val command = data.getString("command") ?: return | ||
val log = policy.toLog( | ||
toUid = toUid, | ||
fromPid = pid, | ||
command = command, | ||
date = Date() | ||
) | ||
|
||
val logRepo = get<LogRepository>() | ||
logRepo.put(log).subscribeK(onError = { Timber.e(it) }) | ||
} | ||
|
||
private fun handleNotify(context: Context, data: Bundle) { | ||
val fromUid = data["from.uid"].toInt() ?: return | ||
if (fromUid == Process.myUid()) | ||
return | ||
|
||
val pm = context.packageManager | ||
val allow = data["policy"].toInt() ?: return | ||
|
||
runCatching { | ||
val policy = fromUid.toPolicy(pm, allow) | ||
if (policy.policy >= 0) | ||
notify(context, policy) | ||
} | ||
} | ||
|
||
private fun notify(context: Context, policy: MagiskPolicy) { | ||
if (policy.notification && Config.suNotification == Config.Value.NOTIFICATION_TOAST) { | ||
val resId = if (policy.policy == MagiskPolicy.ALLOW) | ||
R.string.su_allow_toast | ||
else | ||
R.string.su_deny_toast | ||
|
||
Utils.toast(context.getString(resId, policy.appName), Toast.LENGTH_SHORT) | ||
} | ||
} | ||
} |
Oops, something went wrong.