summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Raphael Kim <raphk@google.com> 2025-02-28 14:59:54 -0800
committer Raphael Kim <raphk@google.com> 2025-02-28 15:07:07 -0800
commit0475831ec37ad4611776aaef5d0d25f1f45531c7 (patch)
tree70b4b02d913beb99f4b19e0d0b51da8865c6309f
parent9848646dac597100e682ba81094391065c68a2a6 (diff)
[CDM] Fix race condition that caused handshake init collision detection to be bypassed occasionally.
Bug: 390216902 Test: Manual test Test: atest CtsWearableSensingServiceTestCases:android.wearable.cts.WearableSensingManagerIsolatedServiceTest#provideConnection_allowConcurrent_canReceiveDataInWss --rerun-until-failure 100 Flag: EXEMPT bug fix Change-Id: I005dd637b215593898af2b8cde22c6e005c4802e
-rw-r--r--services/companion/java/com/android/server/companion/securechannel/SecureChannel.java37
1 files changed, 25 insertions, 12 deletions
diff --git a/services/companion/java/com/android/server/companion/securechannel/SecureChannel.java b/services/companion/java/com/android/server/companion/securechannel/SecureChannel.java
index 6c7c9b3e073d..4c62c0deb2df 100644
--- a/services/companion/java/com/android/server/companion/securechannel/SecureChannel.java
+++ b/services/companion/java/com/android/server/companion/securechannel/SecureChannel.java
@@ -73,6 +73,8 @@ public class SecureChannel {
private int mVerificationResult = FLAG_FAILURE_UNKNOWN;
private boolean mPskVerified;
+ private final Object mHandshakeLock = new Object();
+
/**
* Create a new secure channel object. This secure channel allows secure messages to be
@@ -342,20 +344,22 @@ public class SecureChannel {
}
private void initiateHandshake() throws IOException, BadHandleException , HandshakeException {
- if (mConnectionContext != null) {
- Slog.d(TAG, "Ukey2 handshake is already completed.");
- return;
- }
+ synchronized (mHandshakeLock) {
+ if (mConnectionContext != null) {
+ Slog.d(TAG, "Ukey2 handshake is already completed.");
+ return;
+ }
- mRole = Role.INITIATOR;
- mHandshakeContext = D2DHandshakeContext.forInitiator();
- mClientInit = mHandshakeContext.getNextHandshakeMessage();
+ mRole = Role.INITIATOR;
+ mHandshakeContext = D2DHandshakeContext.forInitiator();
+ mClientInit = mHandshakeContext.getNextHandshakeMessage();
- // Send Client Init
- if (DEBUG) {
- Slog.d(TAG, "Sending Ukey2 Client Init message");
+ // Send Client Init
+ if (DEBUG) {
+ Slog.d(TAG, "Sending Ukey2 Client Init message");
+ }
+ sendMessage(MessageType.HANDSHAKE_INIT, constructHandshakeInitMessage(mClientInit));
}
- sendMessage(MessageType.HANDSHAKE_INIT, constructHandshakeInitMessage(mClientInit));
}
// In an occasion where both participants try to initiate a handshake, resolve the conflict
@@ -414,8 +418,17 @@ public class SecureChannel {
// Mark "in-progress" upon receiving the first message
mInProgress = true;
+ // Complete a series of handshake exchange and processing
+ synchronized (mHandshakeLock) {
+ completeHandshake(handshakeInitMessage);
+ }
+ }
+
+ private void completeHandshake(byte[] initMessage) throws IOException, HandshakeException,
+ BadHandleException, CryptoException, AlertException {
+
// Handle a potential collision where both devices tried to initiate a connection
- byte[] handshakeMessage = handleHandshakeCollision(handshakeInitMessage);
+ byte[] handshakeMessage = handleHandshakeCollision(initMessage);
// Proceed with the rest of Ukey2 handshake
if (mHandshakeContext == null) { // Server-side logic