Cleanup su handler

This commit is contained in:
vvb2060 2022-01-20 20:10:11 +08:00 committed by John Wu
parent c59a41a607
commit 7cdf2d244d
6 changed files with 38 additions and 59 deletions

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.core.magiskdb package com.topjohnwu.magisk.core.magiskdb
import android.content.pm.PackageManager
import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.model.su.SuPolicy import com.topjohnwu.magisk.core.model.su.SuPolicy
import com.topjohnwu.magisk.core.model.su.toPolicy import com.topjohnwu.magisk.core.model.su.toPolicy
@ -54,13 +53,9 @@ class PolicyDao : BaseDao() {
private fun Map<String, String>.toPolicyOrNull(): SuPolicy? { private fun Map<String, String>.toPolicyOrNull(): SuPolicy? {
return runCatching { toPolicy(AppContext.packageManager) }.getOrElse { return runCatching { toPolicy(AppContext.packageManager) }.getOrElse {
Timber.e(it) Timber.w(it)
if (it is PackageManager.NameNotFoundException) { val uid = getOrElse("uid") { return null }
val uid = getOrElse("uid") { null } ?: return null GlobalScope.launch { delete(uid.toInt()) }
GlobalScope.launch {
delete(uid.toInt())
}
}
null null
} }
} }

View File

@ -1,10 +1,8 @@
package com.topjohnwu.magisk.core.model.su package com.topjohnwu.magisk.core.model.su
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.topjohnwu.magisk.ktx.timeFormatTime import com.topjohnwu.magisk.ktx.now
import com.topjohnwu.magisk.ktx.toTime
@Entity(tableName = "logs") @Entity(tableName = "logs")
data class SuLog( data class SuLog(
@ -15,8 +13,7 @@ data class SuLog(
val appName: String, val appName: String,
val command: String, val command: String,
val action: Boolean, val action: Boolean,
val time: Long = -1 val time: Long = now
) { ) {
@PrimaryKey(autoGenerate = true) var id: Int = 0 @PrimaryKey(autoGenerate = true) var id: Int = 0
@Ignore val timeString = time.toTime(timeFormatTime)
} }

View File

@ -9,7 +9,7 @@ import com.topjohnwu.magisk.core.model.su.SuPolicy.Companion.INTERACTIVE
import com.topjohnwu.magisk.ktx.getLabel import com.topjohnwu.magisk.ktx.getLabel
data class SuPolicy( data class SuPolicy(
var uid: Int, val uid: Int,
val packageName: String, val packageName: String,
val appName: String, val appName: String,
val icon: Drawable, val icon: Drawable,
@ -27,8 +27,7 @@ data class SuPolicy(
fun toLog(toUid: Int, fromPid: Int, command: String) = SuLog( fun toLog(toUid: Int, fromPid: Int, command: String) = SuLog(
uid, toUid, fromPid, packageName, appName, uid, toUid, fromPid, packageName, appName,
command, policy == ALLOW, System.currentTimeMillis() command, policy == ALLOW)
)
fun toMap() = mapOf( fun toMap() = mapOf(
"uid" to uid, "uid" to uid,

View File

@ -2,7 +2,6 @@ package com.topjohnwu.magisk.core.su
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.os.Process
import android.widget.Toast import android.widget.Toast
import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
@ -41,36 +40,37 @@ object SuCallbackHandler {
} }
} }
private fun Any?.toInt(): Int? { // https://android.googlesource.com/platform/frameworks/base/+/547bf5487d52b93c9fe183aa6d56459c170b17a4
return when (this) { private fun Bundle.getIntComp(key: String, defaultValue: Int): Int {
is Number -> this.toInt() val value = get(key) ?: return defaultValue
else -> null return when (value) {
is Int -> value
is Long -> value.toInt()
else -> defaultValue
} }
} }
private fun handleLogging(context: Context, data: Bundle) { private fun handleLogging(context: Context, data: Bundle) {
val fromUid = data["from.uid"].toInt() ?: return val fromUid = data.getIntComp("from.uid", -1)
if (fromUid == Process.myUid()) val notify = data.getBoolean("notify", true)
return val allow = data.getIntComp("policy", SuPolicy.ALLOW)
val pm = context.packageManager val pm = context.packageManager
val notify = data.getBoolean("notify", true)
val allow = data["policy"].toInt() ?: return
val policy = runCatching { val policy = runCatching {
fromUid.toPolicy(pm, allow) fromUid.toPolicy(pm, allow)
}.getOrElse { }.getOrElse {
GlobalScope.launch { ServiceLocator.policyDB.delete(fromUid) }
fromUid.toUidPolicy(pm, allow) fromUid.toUidPolicy(pm, allow)
} }
if (notify) if (notify)
notify(context, policy) notify(context, policy)
val toUid = data["to.uid"].toInt() ?: return val toUid = data.getIntComp("to.uid", -1)
val pid = data["pid"].toInt() ?: return val pid = data.getIntComp("pid", -1)
val command = data.getString("command") ?: return val command = data.getString("command", "")
val log = policy.toLog( val log = policy.toLog(
toUid = toUid, toUid = toUid,
fromPid = pid, fromPid = pid,
@ -83,22 +83,23 @@ object SuCallbackHandler {
} }
private fun handleNotify(context: Context, data: Bundle) { private fun handleNotify(context: Context, data: Bundle) {
val fromUid = data["from.uid"].toInt() ?: return val fromUid = data.getIntComp("from.uid", -1)
if (fromUid == Process.myUid()) val allow = data.getIntComp("policy", SuPolicy.ALLOW)
return
val pm = context.packageManager val pm = context.packageManager
val allow = data["policy"].toInt() ?: return
runCatching {
val policy = fromUid.toPolicy(pm, allow) val policy = runCatching {
if (policy.policy >= 0) fromUid.toPolicy(pm, allow)
notify(context, policy) }.getOrElse {
GlobalScope.launch { ServiceLocator.policyDB.delete(fromUid) }
fromUid.toUidPolicy(pm, allow)
} }
notify(context, policy)
} }
private fun notify(context: Context, policy: SuPolicy) { private fun notify(context: Context, policy: SuPolicy) {
if (policy.notification && Config.suNotification == Config.Value.NOTIFICATION_TOAST) { if (Config.suNotification == Config.Value.NOTIFICATION_TOAST) {
val resId = if (policy.policy == SuPolicy.ALLOW) val resId = if (policy.policy == SuPolicy.ALLOW)
R.string.su_allow_toast R.string.su_allow_toast
else else

View File

@ -4,11 +4,11 @@ import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.magiskdb.PolicyDao import com.topjohnwu.magisk.core.magiskdb.PolicyDao
import com.topjohnwu.magisk.core.model.su.SuPolicy import com.topjohnwu.magisk.core.model.su.SuPolicy
import com.topjohnwu.magisk.core.model.su.toPolicy import com.topjohnwu.magisk.core.model.su.toPolicy
import com.topjohnwu.magisk.ktx.now import com.topjohnwu.magisk.ktx.now
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -35,8 +35,10 @@ class SuRequestHandler(
return false return false
// Never allow com.topjohnwu.magisk (could be malware) // Never allow com.topjohnwu.magisk (could be malware)
if (policy.packageName == BuildConfig.APPLICATION_ID) if (policy.packageName == BuildConfig.APPLICATION_ID) {
Shell.su("(pm uninstall ${BuildConfig.APPLICATION_ID})& >/dev/null 2>&1").exec()
return false return false
}
when (Config.suAutoResponse) { when (Config.suAutoResponse) {
Config.Value.SU_AUTO_DENY -> { Config.Value.SU_AUTO_DENY -> {
@ -87,7 +89,6 @@ class SuRequestHandler(
policy.policy = action policy.policy = action
policy.until = until policy.until = until
policy.uid = policy.uid % 100000 + Const.USER_ID * 100000
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
try { try {

View File

@ -2,31 +2,17 @@ package com.topjohnwu.magisk.ktx
import com.topjohnwu.magisk.core.utils.currentLocale import com.topjohnwu.magisk.core.utils.currentLocale
import java.text.DateFormat import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
val now get() = System.currentTimeMillis() val now get() = System.currentTimeMillis()
fun Long.toTime(format: DateFormat) = format.format(this).orEmpty() fun Long.toTime(format: DateFormat) = format.format(this).orEmpty()
fun String.toTime(format: DateFormat) = try {
format.parse(this)?.time ?: -1
} catch (e: ParseException) {
-1L
}
val timeFormatFull by lazy { SimpleDateFormat("yyyy/MM/dd_HH:mm:ss", val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
currentLocale currentLocale
) } ) }
val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
currentLocale val timeDateFormat: DateFormat by lazy {
) }
val timeFormatMedium by lazy { DateFormat.getDateInstance(DateFormat.MEDIUM,
currentLocale
) }
val timeFormatTime by lazy { SimpleDateFormat("h:mm a",
currentLocale
) }
val timeDateFormat by lazy {
DateFormat.getDateTimeInstance( DateFormat.getDateTimeInstance(
DateFormat.DEFAULT, DateFormat.DEFAULT,
DateFormat.DEFAULT, DateFormat.DEFAULT,