libbinder: trusty: Disconnect TIPC on server destruction

The RpcServerTrusty contains the tipc_port used to create the TIPC
server that the RpcServerTrusty uses, but that port data is supposed to
outlive the TIPC server. Previously, when/if the RpcServerTrusty were
destroyed, the port data would dangle. Tear down the TIPC server before
the RpcServerTrusty so that the TIPC event loop can't read after this
data is freed.

Bug: 514731794
Test: presubmits
Ignore-AOSP-First: security fix
Flag: EXEMPT BUGFIX
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:c0fbe7b7b8b7de9082eb799338e324945664664c
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:84481bfc6d58c42586ef3055e94ec31363ed3fb6
Merged-In: I858df991918958a4917e113827ecd48ec78a2c66
Change-Id: I858df991918958a4917e113827ecd48ec78a2c66
diff --git a/libs/binder/trusty/RpcServerTrusty.cpp b/libs/binder/trusty/RpcServerTrusty.cpp
index 17919c2..dc4ff1a 100644
--- a/libs/binder/trusty/RpcServerTrusty.cpp
+++ b/libs/binder/trusty/RpcServerTrusty.cpp
@@ -50,7 +50,8 @@
         return nullptr;
     }
 
-    int rc = tipc_add_service(handleSet, &srv->mTipcPort, 1, 0, &kTipcOps);
+    int rc =
+            tipc_add_service_ports(handleSet, &srv->mTipcPort, 1, 0, &kTipcOps, &srv->mTipcPortCtx);
     if (rc != NO_ERROR) {
         ALOGE("Failed to create RpcServerTrusty: can't add service: %d", rc);
         return nullptr;
@@ -58,11 +59,18 @@
     return srv;
 }
 
+RpcServerTrusty::~RpcServerTrusty() {
+    if (mTipcPortCtx != nullptr) {
+        tipc_remove_service(mTipcPortCtx);
+    }
+}
+
 RpcServerTrusty::RpcServerTrusty(std::unique_ptr<RpcTransportCtx> ctx, std::string&& portName,
                                  std::shared_ptr<const PortAcl>&& portAcl, size_t msgMaxSize)
       : mRpcServer(makeRpcServer(std::move(ctx))),
         mPortName(std::move(portName)),
-        mPortAcl(std::move(portAcl)) {
+        mPortAcl(std::move(portAcl)),
+        mTipcPortCtx(nullptr) {
     mTipcPort.name = mPortName.c_str();
     mTipcPort.msg_max_size = msgMaxSize;
     mTipcPort.msg_queue_len = 6; // Three each way
diff --git a/libs/binder/trusty/include/binder/RpcServerTrusty.h b/libs/binder/trusty/include/binder/RpcServerTrusty.h
index fe44ea5..f80d606 100644
--- a/libs/binder/trusty/include/binder/RpcServerTrusty.h
+++ b/libs/binder/trusty/include/binder/RpcServerTrusty.h
@@ -46,6 +46,8 @@
         const void* extraData;
     };
 
+    ~RpcServerTrusty();
+
     /**
      * Creates an RPC server listening on the given port and adds it to the
      * Trusty handle set at |handleSet|.
@@ -140,6 +142,7 @@
     std::vector<const uuid*> mUuidPtrs;
     tipc_port_acl mTipcPortAcl;
     tipc_port mTipcPort;
+    tipc_port_ctx* mTipcPortCtx;
 };
 
 } // namespace android
diff --git a/libs/binder/trusty/include_mock/lib/tipc/tipc_srv.h b/libs/binder/trusty/include_mock/lib/tipc/tipc_srv.h
index 2747314..a370b1b 100644
--- a/libs/binder/trusty/include_mock/lib/tipc/tipc_srv.h
+++ b/libs/binder/trusty/include_mock/lib/tipc/tipc_srv.h
@@ -45,7 +45,19 @@
     void (*on_channel_cleanup)(void* ctx);
 };
 
+struct tipc_port_ctx;
+
 static inline int tipc_add_service(struct tipc_hset*, const struct tipc_port*, uint32_t, uint32_t,
                                    const struct tipc_srv_ops*) {
     return 0;
 }
+
+static inline int tipc_add_service_ports(struct tipc_hset*, const struct tipc_port*, uint32_t,
+                                         uint32_t, const struct tipc_srv_ops*,
+                                         struct tipc_port_ctx**) {
+    return 0;
+}
+
+static inline int tipc_remove_service(struct tipc_port_ctx*) {
+    return 0;
+}