Commit 7cdf2d24 authored by vvb2060's avatar vvb2060 Committed by John Wu

Cleanup su handler

parent c59a41a6
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() { ...@@ -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
} }
} }
......
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( ...@@ -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)
} }
...@@ -9,7 +9,7 @@ import com.topjohnwu.magisk.core.model.su.SuPolicy.Companion.INTERACTIVE ...@@ -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( ...@@ -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,
......
...@@ -2,7 +2,6 @@ package com.topjohnwu.magisk.core.su ...@@ -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 { ...@@ -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 { ...@@ -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
......
...@@ -4,11 +4,11 @@ import android.content.Intent ...@@ -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( ...@@ -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( ...@@ -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 {
......
...@@ -2,31 +2,17 @@ package com.topjohnwu.magisk.ktx ...@@ -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
) }
val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
currentLocale
) }
val timeFormatMedium by lazy { DateFormat.getDateInstance(DateFormat.MEDIUM,
currentLocale
) }
val timeFormatTime by lazy { SimpleDateFormat("h:mm a",
currentLocale currentLocale
) } ) }
val timeDateFormat by lazy {
val timeDateFormat: DateFormat by lazy {
DateFormat.getDateTimeInstance( DateFormat.getDateTimeInstance(
DateFormat.DEFAULT, DateFormat.DEFAULT,
DateFormat.DEFAULT, DateFormat.DEFAULT,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment