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 uid = intent.getIntExtra("uid", -1)
if (uid <= 0) {
throw IOException("uid == $uid")
}
policy = SuPolicy(uid)
val pid = intent.getIntExtra("pid", -1) val pid = intent.getIntExtra("pid", -1)
try {
pkgInfo = pm.getPackageInfo(uid, pid) ?: PackageInfo().apply { pkgInfo = pm.getPackageInfo(uid, pid) ?: PackageInfo().apply {
val name = pm.getNameForUid(uid) ?: throw SuRequestError() val name = pm.getNameForUid(uid) ?: throw PackageManager.NameNotFoundException()
// We only fill in sharedUserId and leave other fields uninitialized // We only fill in sharedUserId and leave other fields uninitialized
sharedUserId = name.split(":")[0] sharedUserId = name.split(":")[0]
} }
output = DataOutputStream(FileOutputStream(fifo).buffered()) return@withContext true
policy = SuPolicy(uid) } catch (e: PackageManager.NameNotFoundException) {
true respond(SuPolicy.DENY, -1)
} catch (e: Exception) { return@withContext false
when (e) { }
is IOException, is PackageManager.NameNotFoundException -> { } catch (e: IOException) {
Timber.e(e) Timber.e(e)
close() close()
false return@withContext false
}
else -> throw e // Unexpected error
}
} }
} }