1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "host/commands/secure_env/tpm_resource_manager.h"
17 
18 #include <mutex>
19 
20 #include <android-base/logging.h>
21 #include <tss2/tss2_esys.h>
22 #include <tss2/tss2_rc.h>
23 
24 namespace cuttlefish {
25 
EsysLock(ESYS_CONTEXT * esys,std::unique_lock<std::mutex> guard)26 EsysLock::EsysLock(ESYS_CONTEXT* esys, std::unique_lock<std::mutex> guard)
27     : esys_(esys), guard_(std::move(guard)) {}
28 
ObjectSlot(TpmResourceManager * resource_manager)29 TpmResourceManager::ObjectSlot::ObjectSlot(TpmResourceManager* resource_manager)
30     : ObjectSlot(resource_manager, ESYS_TR_NONE) {
31 }
32 
ObjectSlot(TpmResourceManager * resource_manager,ESYS_TR resource)33 TpmResourceManager::ObjectSlot::ObjectSlot(TpmResourceManager* resource_manager,
34                                            ESYS_TR resource)
35     : resource_manager_(resource_manager), resource_(resource) {
36   LOG(VERBOSE) << "Resource allocated";
37 }
38 
~ObjectSlot()39 TpmResourceManager::ObjectSlot::~ObjectSlot() {
40   if (resource_ != ESYS_TR_NONE) {
41     LOG(VERBOSE) << "Freeing resource";
42     auto rc = Esys_FlushContext(resource_manager_->esys_, resource_);
43     if (rc != TPM2_RC_SUCCESS) {
44       LOG(ERROR) << "Esys_FlushContext failed: " << Tss2_RC_Decode(rc)
45                 << "(" << rc << ")";
46     }
47   } else {
48     LOG(VERBOSE) << "Resource is NONE";
49   }
50   resource_manager_->used_slots_--;
51 }
52 
get()53 ESYS_TR TpmResourceManager::ObjectSlot::get() {
54   return resource_;
55 }
56 
set(ESYS_TR resource)57 void TpmResourceManager::ObjectSlot::set(ESYS_TR resource) {
58   resource_ = resource;
59 }
60 
TpmResourceManager(ESYS_CONTEXT * esys)61 TpmResourceManager::TpmResourceManager(ESYS_CONTEXT* esys)
62     : esys_(esys), maximum_object_slots_(3), used_slots_(0) {
63   // TODO(b/158791154): Find maximum_object_slots dynamically using
64   // TPM2_GetCapability. Now equal to MAX_LOADED_OBJECTS from TpmProfile.h.
65 }
66 
~TpmResourceManager()67 TpmResourceManager::~TpmResourceManager() {
68   if (used_slots_ > 0) {
69     LOG(FATAL) << "Outstanding TpmResourceManager::ObjectSlot instances. "
70                   "These hold a dangling pointer to this instance.";
71   }
72 }
73 
Esys()74 EsysLock TpmResourceManager::Esys() {
75   return EsysLock(esys_, std::unique_lock<std::mutex>(mu_));
76 }
77 
ReserveSlot()78 TpmObjectSlot TpmResourceManager::ReserveSlot() {
79   auto slot_num = used_slots_.fetch_add(1);
80   if (slot_num >= maximum_object_slots_) {
81       used_slots_--;
82       return nullptr;
83   }
84   return TpmObjectSlot{new ObjectSlot(this)};
85 }
86 
87 }  // namespace cuttlefish
88