Respond deny when pkg name not found

This commit is contained in:
vvb2060 2022-05-07 18:08:53 +08:00 committed by John Wu
parent 0772f6dcaf
commit e11508f84d

View File

@ -12,7 +12,6 @@ import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.io.Closeable
import java.io.DataOutputStream import java.io.DataOutputStream
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
@ -21,11 +20,10 @@ import java.util.concurrent.TimeUnit
class SuRequestHandler( class SuRequestHandler(
val pm: PackageManager, val pm: PackageManager,
private val policyDB: PolicyDao private val policyDB: PolicyDao
) : Closeable { ) {
private lateinit var output: DataOutputStream private lateinit var output: DataOutputStream
lateinit var policy: SuPolicy private lateinit var policy: SuPolicy
private set
lateinit var pkgInfo: PackageInfo lateinit var pkgInfo: PackageInfo
private set private set
@ -54,35 +52,36 @@ class SuRequestHandler(
return true return true
} }
override fun close() { private fun close() {
if (::output.isInitialized) if (::output.isInitialized)
runCatching { output.close() } runCatching { output.close() }
} }
private class SuRequestError : IOException()
private suspend fun init(intent: Intent) = withContext(Dispatchers.IO) { private suspend fun init(intent: Intent) = withContext(Dispatchers.IO) {
try { try {
val fifo = intent.getStringExtra("fifo") ?: throw SuRequestError() val fifo = intent.getStringExtra("fifo") ?: throw IOException("fifo == null")
val uid = intent.getIntExtra("uid", -1).also { if (it < 0) throw SuRequestError() } output = DataOutputStream(FileOutputStream(fifo))
val pid = intent.getIntExtra("pid", -1) val uid = intent.getIntExtra("uid", -1)
pkgInfo = pm.getPackageInfo(uid, pid) ?: PackageInfo().apply { if (uid <= 0) {
val name = pm.getNameForUid(uid) ?: throw SuRequestError() throw IOException("uid == $uid")
// We only fill in sharedUserId and leave other fields uninitialized
sharedUserId = name.split(":")[0]
} }
output = DataOutputStream(FileOutputStream(fifo).buffered())
policy = SuPolicy(uid) policy = SuPolicy(uid)
true val pid = intent.getIntExtra("pid", -1)
} catch (e: Exception) { try {
when (e) { pkgInfo = pm.getPackageInfo(uid, pid) ?: PackageInfo().apply {
is IOException, is PackageManager.NameNotFoundException -> { val name = pm.getNameForUid(uid) ?: throw PackageManager.NameNotFoundException()
Timber.e(e) // We only fill in sharedUserId and leave other fields uninitialized
close() sharedUserId = name.split(":")[0]
false
} }
else -> throw e // Unexpected error return@withContext true
} catch (e: PackageManager.NameNotFoundException) {
respond(SuPolicy.DENY, -1)
return@withContext false
} }
} catch (e: IOException) {
Timber.e(e)
close()
return@withContext false
} }
} }