Skip to content

Commit 74263c4

Browse files
MhmRddXiaoTong6666
authored andcommitted
Fix silent error paths, challenge error code, and null handling
(cherry picked from commit d839b69)
1 parent 0ac2a2b commit 74263c4

5 files changed

Lines changed: 22 additions & 11 deletions

File tree

app/src/main/java/org/matrix/TEESimulator/attestation/AttestationBuilder.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,11 @@ object AttestationBuilder {
418418
)
419419
)
420420

421-
// Collect unique signature digests from the signing history.
422-
packageInfo.signingInfo?.signingCertificateHistory?.forEach { signature ->
423-
val digest = sha256.digest(signature.toByteArray())
424-
signatureDigests.add(Digest(digest))
421+
val certs =
422+
packageInfo.signingInfo?.signingCertificateHistory
423+
?: packageInfo.signingInfo?.apkContentsSigners
424+
certs?.forEach { signature ->
425+
signatureDigests.add(Digest(sha256.digest(signature.toByteArray())))
425426
}
426427
}
427428

app/src/main/java/org/matrix/TEESimulator/interception/keystore/InterceptorUtils.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,13 @@ object InterceptorUtils {
168168

169169
/** Checks if a reply parcel contains an exception without consuming it. */
170170
fun hasException(reply: Parcel): Boolean {
171-
val exception = runCatching { reply.readException() }.exceptionOrNull()
172-
if (exception != null) reply.setDataPosition(0)
173-
return exception != null
171+
val pos = reply.dataPosition()
172+
return try {
173+
reply.readException()
174+
false
175+
} catch (_: Exception) {
176+
reply.setDataPosition(pos)
177+
true
178+
}
174179
}
175180
}

app/src/main/java/org/matrix/TEESimulator/interception/keystore/ListEntriesHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ object ListEntriesHandler {
8181
// See AOSP function `get_key_descriptor_for_lookup` in service.rs.
8282
val keysToInject =
8383
extractGeneratedKeyDescriptors(callingUid, callingUid.toLong(), params.startPastAlias)
84-
val originalList = reply.createTypedArray(KeyDescriptor.CREATOR)!!
84+
val originalList = reply.createTypedArray(KeyDescriptor.CREATOR) ?: emptyArray()
8585
val mergedArray = mergeKeyDescriptors(originalList, keysToInject)
8686

8787
// Limit response size to avoid binder buffer overflow.

app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/OperationInterceptor.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Parcel
55
import android.system.keystore2.IKeystoreOperation
66
import org.matrix.TEESimulator.interception.core.BinderInterceptor
77
import org.matrix.TEESimulator.interception.keystore.InterceptorUtils
8+
import org.matrix.TEESimulator.logging.SystemLogger
89

910
/**
1011
* Intercepts calls to an `IKeystoreOperation` service. This is used to log the data manipulation
@@ -28,7 +29,11 @@ class OperationInterceptor(
2829
logTransaction(txId, methodName, callingUid, callingPid, true)
2930

3031
if (code == FINISH_TRANSACTION || code == ABORT_TRANSACTION) {
31-
KeyMintSecurityLevelInterceptor.removeOperationInterceptor(target, backdoor)
32+
try {
33+
KeyMintSecurityLevelInterceptor.removeOperationInterceptor(target, backdoor)
34+
} catch (e: Exception) {
35+
SystemLogger.error("[TX_ID: $txId] Failed to unregister operation interceptor.", e)
36+
}
3237
}
3338

3439
return TransactionResult.ContinueAndSkipPost

app/src/main/java/org/matrix/TEESimulator/pki/CertificateGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ object CertificateGenerator {
9292
): List<Certificate>? {
9393
val challenge = params.attestationChallenge
9494
if (challenge != null && challenge.size > AttestationConstants.CHALLENGE_LENGTH_LIMIT)
95-
throw IllegalArgumentException(
96-
"Attestation challenge exceeds length limit (${challenge.size} > ${AttestationConstants.CHALLENGE_LENGTH_LIMIT})"
95+
throw android.os.ServiceSpecificException(
96+
-21 // INVALID_INPUT_LENGTH (KM_ERROR_INVALID_INPUT_LENGTH)
9797
)
9898

9999
return runCatching {

0 commit comments

Comments
 (0)