Merge changes Ib7f69f7e,I3c71d17e into udc-dev am: bf2382d922

Original change: https://googleplex-android-review.googlesource.com/c/device/google/cuttlefish/+/24995309

Change-Id: I54088d414b4b3348ecb8cdc22293d18bc0740d58
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/host/commands/run_cvd/Android.bp b/host/commands/run_cvd/Android.bp
index 6f87114..335a51f 100644
--- a/host/commands/run_cvd/Android.bp
+++ b/host/commands/run_cvd/Android.bp
@@ -17,34 +17,6 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
-// Allow the KeyMint reference implementation to be selected at build time.
-soong_config_module_type {
-    name: "keymint_impl_defaults",
-    module_type: "cc_defaults",
-    config_namespace: "secure_env",
-    variables: ["keymint_impl"],
-    properties: ["cflags"],
-}
-
-soong_config_string_variable {
-    name: "keymint_impl",
-    values: ["rust", "cpp"],
-}
-
-keymint_impl_defaults {
-    name: "secure_env_keymint_impl_defaults",
-    soong_config_variables: {
-        keymint_impl: {
-            rust: {
-                cflags: ["-DCUTTLEFISH_KEYMINT_RUST",],
-            },
-            cpp: {},
-            conditions_default: {},
-        },
-    },
-}
-
-
 cc_binary {
     name: "run_cvd",
     srcs: [
@@ -100,6 +72,5 @@
         "cuttlefish_host",
         "cuttlefish_libicuuc",
         "cvd_cc_defaults",
-        "secure_env_keymint_impl_defaults",
     ],
 }
diff --git a/host/commands/run_cvd/launch/secure_env.cpp b/host/commands/run_cvd/launch/secure_env.cpp
index b047ae1..a70fc14 100644
--- a/host/commands/run_cvd/launch/secure_env.cpp
+++ b/host/commands/run_cvd/launch/secure_env.cpp
@@ -53,12 +53,7 @@
 
     const auto& secure_hals = config_.secure_hals();
     bool secure_keymint = secure_hals.count(SecureHal::Keymint) > 0;
-#ifdef CUTTLEFISH_KEYMINT_RUST
-    command.AddParameter("-keymint_impl=",
-                         secure_keymint ? "rust-tpm" : "rust-software");
-#else
     command.AddParameter("-keymint_impl=", secure_keymint ? "tpm" : "software");
-#endif
     bool secure_gatekeeper = secure_hals.count(SecureHal::Gatekeeper) > 0;
     auto gatekeeper_impl = secure_gatekeeper ? "tpm" : "software";
     command.AddParameter("-gatekeeper_impl=", gatekeeper_impl);
diff --git a/host/commands/secure_env/rust/lib.rs b/host/commands/secure_env/rust/lib.rs
index 4d4e2f4..71f0289 100644
--- a/host/commands/secure_env/rust/lib.rs
+++ b/host/commands/secure_env/rust/lib.rs
@@ -46,7 +46,7 @@
     log::set_logger(&AndroidCppLogger).unwrap();
     log::set_max_level(log::LevelFilter::Debug); // Filtering happens elsewhere
     info!(
-        "KeyMint TA running with fd_in={}, fd_out={}, security_level={:?}",
+        "KeyMint Rust TA running with fd_in={}, fd_out={}, security_level={:?}",
         fd_in, fd_out, security_level,
     );
 
diff --git a/host/commands/secure_env/secure_env_linux_main.cpp b/host/commands/secure_env/secure_env_linux_main.cpp
index 610b527..56428c9 100644
--- a/host/commands/secure_env/secure_env_linux_main.cpp
+++ b/host/commands/secure_env/secure_env_linux_main.cpp
@@ -70,8 +70,7 @@
               "The TPM implementation. \"in_memory\" or \"host_device\"");
 
 DEFINE_string(keymint_impl, "tpm",
-              "The KeyMint implementation. \"tpm\", \"software\", \"rust-tpm\" "
-              "or \"rust-software\"");
+              "The KeyMint implementation. \"tpm\" or \"software\"");
 
 DEFINE_string(gatekeeper_impl, "tpm",
               "The gatekeeper implementation. \"tpm\" or \"software\"");
@@ -237,62 +236,68 @@
 
   std::vector<std::thread> threads;
 
-  if (android::base::StartsWith(FLAGS_keymint_impl, "rust-")) {
-    // Use the Rust reference implementation of KeyMint.
-    LOG(DEBUG) << "starting Rust KeyMint implementation";
-    int security_level;
-    if (FLAGS_keymint_impl == "rust-software") {
-      security_level = KM_SECURITY_LEVEL_SOFTWARE;
-    } else if (FLAGS_keymint_impl == "rust-tpm") {
-      security_level = KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
-    } else {
-      LOG(FATAL) << "Unknown keymaster implementation " << FLAGS_keymint_impl;
-      return -1;
-    }
-
-    int keymint_in = FLAGS_keymint_fd_in;
-    int keymint_out = FLAGS_keymint_fd_out;
-    TpmResourceManager* rm = resource_manager;
-    threads.emplace_back([rm, keymint_in, keymint_out, security_level]() {
-      kmr_ta_main(keymint_in, keymint_out, security_level, rm);
-    });
-
+  int security_level;
+  if (FLAGS_keymint_impl == "software") {
+    security_level = KM_SECURITY_LEVEL_SOFTWARE;
+  } else if (FLAGS_keymint_impl == "tpm") {
+    security_level = KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
   } else {
-    // Use the C++ reference implementation of KeyMint.
-    LOG(DEBUG) << "starting C++ KeyMint implementation";
-    if (FLAGS_keymint_impl == "software") {
-      // TODO: See if this is the right KM version.
-      keymaster_context.reset(new keymaster::PureSoftKeymasterContext(
-          keymaster::KmVersion::KEYMINT_3, KM_SECURITY_LEVEL_SOFTWARE));
-    } else if (FLAGS_keymint_impl == "tpm") {
-      keymaster_context.reset(
-          new TpmKeymasterContext(*resource_manager, *keymaster_enforcement));
-    } else {
-      LOG(FATAL) << "Unknown keymaster implementation " << FLAGS_keymint_impl;
-      return -1;
-    }
-    // keymaster::AndroidKeymaster puts the context pointer into a UniquePtr,
-    // taking ownership.
-    keymaster.reset(new keymaster::AndroidKeymaster(
-        new ProxyKeymasterContext(*keymaster_context), kOperationTableSize,
-        keymaster::MessageVersion(keymaster::KmVersion::KEYMINT_3,
-                                  0 /* km_date */)));
-
-    auto keymaster_in = DupFdFlag(FLAGS_keymaster_fd_in);
-    auto keymaster_out = DupFdFlag(FLAGS_keymaster_fd_out);
-    keymaster::AndroidKeymaster* borrowed_km = keymaster.get();
-    threads.emplace_back([keymaster_in, keymaster_out, borrowed_km]() {
-      while (true) {
-        SharedFdKeymasterChannel keymaster_channel(keymaster_in, keymaster_out);
-
-        KeymasterResponder keymaster_responder(keymaster_channel, *borrowed_km);
-
-        while (keymaster_responder.ProcessMessage()) {
-        }
-      }
-    });
+    LOG(FATAL) << "Unknown keymint implementation " << FLAGS_keymint_impl;
+    return -1;
   }
 
+  // The guest image may have either the C++ implementation of
+  // KeyMint/Keymaster, xor the Rust implementation of KeyMint.  Those different
+  // implementations each need to have a matching TA implementation in
+  // secure_env, but they use distincts ports (/dev/hvc3 for C++, /dev/hvc11 for
+  // Rust) so start threads for *both* TA implementations -- only one of them
+  // will receive any traffic from the guest.
+
+  // Start the Rust reference implementation of KeyMint.
+  LOG(INFO) << "starting Rust KeyMint TA implementation in a thread";
+
+  int keymint_in = FLAGS_keymint_fd_in;
+  int keymint_out = FLAGS_keymint_fd_out;
+  TpmResourceManager* rm = resource_manager;
+  threads.emplace_back([rm, keymint_in, keymint_out, security_level]() {
+    kmr_ta_main(keymint_in, keymint_out, security_level, rm);
+  });
+
+  // Start the C++ reference implementation of KeyMint.
+  LOG(INFO) << "starting C++ KeyMint implementation in a thread with FDs in="
+            << FLAGS_keymaster_fd_in << ", out=" << FLAGS_keymaster_fd_out;
+  if (security_level == KM_SECURITY_LEVEL_SOFTWARE) {
+    keymaster_context.reset(new keymaster::PureSoftKeymasterContext(
+        keymaster::KmVersion::KEYMINT_3, KM_SECURITY_LEVEL_SOFTWARE));
+  } else if (security_level == KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT) {
+    keymaster_context.reset(
+        new TpmKeymasterContext(*resource_manager, *keymaster_enforcement));
+  } else {
+    LOG(FATAL) << "Unknown keymaster security level " << security_level
+               << " for " << FLAGS_keymint_impl;
+    return -1;
+  }
+  // keymaster::AndroidKeymaster puts the context pointer into a UniquePtr,
+  // taking ownership.
+  keymaster.reset(new keymaster::AndroidKeymaster(
+      new ProxyKeymasterContext(*keymaster_context), kOperationTableSize,
+      keymaster::MessageVersion(keymaster::KmVersion::KEYMINT_3,
+                                0 /* km_date */)));
+
+  auto keymaster_in = DupFdFlag(FLAGS_keymaster_fd_in);
+  auto keymaster_out = DupFdFlag(FLAGS_keymaster_fd_out);
+  keymaster::AndroidKeymaster* borrowed_km = keymaster.get();
+  threads.emplace_back([keymaster_in, keymaster_out, borrowed_km]() {
+    while (true) {
+      SharedFdKeymasterChannel keymaster_channel(keymaster_in, keymaster_out);
+
+      KeymasterResponder keymaster_responder(keymaster_channel, *borrowed_km);
+
+      while (keymaster_responder.ProcessMessage()) {
+      }
+    }
+  });
+
   auto gatekeeper_in = DupFdFlag(FLAGS_gatekeeper_fd_in);
   auto gatekeeper_out = DupFdFlag(FLAGS_gatekeeper_fd_out);
   threads.emplace_back([gatekeeper_in, gatekeeper_out, &gatekeeper]() {
diff --git a/shared/device.mk b/shared/device.mk
index 7c8a054..59338ed 100644
--- a/shared/device.mk
+++ b/shared/device.mk
@@ -509,12 +509,6 @@
     LOCAL_KEYMINT_PRODUCT_PACKAGE := android.hardware.security.keymint-service.rust
 endif
 
-ifeq ($(LOCAL_KEYMINT_PRODUCT_PACKAGE),android.hardware.security.keymint-service.rust)
-    # KeyMint HAL has been overridden to force use of the Rust reference implementation.
-    # Set the build config for secure_env to match.
-    $(call soong_config_set,secure_env,keymint_impl,rust)
-endif
-
 PRODUCT_PACKAGES += \
     $(LOCAL_KEYMINT_PRODUCT_PACKAGE) \