| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 |  | 
|  | 17 | #define LOG_TAG "gatekeeperd" | 
|  | 18 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 19 | #include <android/service/gatekeeper/BnGateKeeperService.h> | 
|  | 20 | #include <gatekeeper/GateKeeperResponse.h> | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 21 |  | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 22 | #include <endian.h> | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 23 | #include <errno.h> | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 24 | #include <fcntl.h> | 
|  | 25 | #include <unistd.h> | 
| Justin Yun | 68b0ec6 | 2017-08-16 18:54:20 +0900 | [diff] [blame] | 26 | #include <memory> | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 27 |  | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 28 | #include <android-base/logging.h> | 
|  | 29 | #include <android-base/properties.h> | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 30 | #include <android/binder_ibinder.h> | 
|  | 31 | #include <android/binder_manager.h> | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 32 | #include <binder/IPCThreadState.h> | 
|  | 33 | #include <binder/IServiceManager.h> | 
|  | 34 | #include <binder/PermissionCache.h> | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 35 | #include <gatekeeper/password_handle.h>  // for password_handle_t | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 36 | #include <hardware/hw_auth_token.h> | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 37 | #include <libgsi/libgsi.h> | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 38 | #include <log/log.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 39 | #include <utils/String16.h> | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 40 |  | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 41 | #include <aidl/android/hardware/security/keymint/HardwareAuthToken.h> | 
|  | 42 | #include <aidl/android/security/authorization/IKeystoreAuthorization.h> | 
| Louis Chang | 4c66b8a | 2021-01-18 10:01:12 +0000 | [diff] [blame] | 43 | #include <android/hardware/gatekeeper/1.0/IGatekeeper.h> | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 44 | #include <hidl/HidlSupport.h> | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | using android::sp; | 
| Louis Chang | 4c66b8a | 2021-01-18 10:01:12 +0000 | [diff] [blame] | 47 | using android::hardware::Return; | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 48 | using android::hardware::gatekeeper::V1_0::GatekeeperResponse; | 
|  | 49 | using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode; | 
|  | 50 | using android::hardware::gatekeeper::V1_0::IGatekeeper; | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 51 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 52 | using ::android::binder::Status; | 
|  | 53 | using ::android::service::gatekeeper::BnGateKeeperService; | 
|  | 54 | using GKResponse = ::android::service::gatekeeper::GateKeeperResponse; | 
|  | 55 | using GKResponseCode = ::android::service::gatekeeper::ResponseCode; | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 56 | using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType; | 
|  | 57 | using ::aidl::android::hardware::security::keymint::HardwareAuthToken; | 
|  | 58 | using ::aidl::android::security::authorization::IKeystoreAuthorization; | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 59 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 60 | namespace android { | 
|  | 61 |  | 
|  | 62 | static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE"); | 
|  | 63 | static const String16 DUMP_PERMISSION("android.permission.DUMP"); | 
|  | 64 |  | 
|  | 65 | class GateKeeperProxy : public BnGateKeeperService { | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 66 | public: | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 67 | GateKeeperProxy() { | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 68 | clear_state_if_needed_done = false; | 
| Chris Phoenix | a84ce0c | 2017-01-24 13:09:39 -0800 | [diff] [blame] | 69 | hw_device = IGatekeeper::getService(); | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 70 | is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false); | 
| Andres Morales | fef908e | 2015-07-07 10:28:15 -0700 | [diff] [blame] | 71 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 72 | if (!hw_device) { | 
|  | 73 | LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad."; | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 74 | } | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 77 | virtual ~GateKeeperProxy() {} | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 78 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 79 | void store_sid(uint32_t userId, uint64_t sid) { | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 80 | char filename[21]; | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 81 | snprintf(filename, sizeof(filename), "%u", userId); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 82 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); | 
|  | 83 | if (fd < 0) { | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 84 | ALOGE("could not open file: %s: %s", filename, strerror(errno)); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 85 | return; | 
|  | 86 | } | 
|  | 87 | write(fd, &sid, sizeof(sid)); | 
|  | 88 | close(fd); | 
|  | 89 | } | 
|  | 90 |  | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 91 | void clear_state_if_needed() { | 
|  | 92 | if (clear_state_if_needed_done) { | 
|  | 93 | return; | 
|  | 94 | } | 
|  | 95 |  | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 96 | if (mark_cold_boot() && !is_running_gsi) { | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 97 | ALOGI("cold boot: clearing state"); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 98 | if (hw_device) { | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 99 | hw_device->deleteAllUsers([](const GatekeeperResponse&) {}); | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | clear_state_if_needed_done = true; | 
|  | 104 | } | 
|  | 105 |  | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 106 | bool mark_cold_boot() { | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 107 | const char* filename = ".coldboot"; | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 108 | if (access(filename, F_OK) == -1) { | 
|  | 109 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); | 
|  | 110 | if (fd < 0) { | 
|  | 111 | ALOGE("could not open file: %s : %s", filename, strerror(errno)); | 
|  | 112 | return false; | 
|  | 113 | } | 
|  | 114 | close(fd); | 
|  | 115 | return true; | 
|  | 116 | } | 
|  | 117 | return false; | 
|  | 118 | } | 
|  | 119 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 120 | void maybe_store_sid(uint32_t userId, uint64_t sid) { | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 121 | char filename[21]; | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 122 | snprintf(filename, sizeof(filename), "%u", userId); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 123 | if (access(filename, F_OK) == -1) { | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 124 | store_sid(userId, sid); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 125 | } | 
|  | 126 | } | 
|  | 127 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 128 | uint64_t read_sid(uint32_t userId) { | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 129 | char filename[21]; | 
|  | 130 | uint64_t sid; | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 131 | snprintf(filename, sizeof(filename), "%u", userId); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 132 | int fd = open(filename, O_RDONLY); | 
|  | 133 | if (fd < 0) return 0; | 
|  | 134 | read(fd, &sid, sizeof(sid)); | 
| Andres Morales | 0b0435e | 2015-07-10 09:47:09 -0700 | [diff] [blame] | 135 | close(fd); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 136 | return sid; | 
|  | 137 | } | 
|  | 138 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 139 | void clear_sid(uint32_t userId) { | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 140 | char filename[21]; | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 141 | snprintf(filename, sizeof(filename), "%u", userId); | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 142 | if (remove(filename) < 0) { | 
|  | 143 | ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno)); | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 144 | store_sid(userId, 0); | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 145 | } | 
|  | 146 | } | 
|  | 147 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 148 | // This should only be called on userIds being passed to the GateKeeper HAL. It ensures that | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 149 | // secure storage shared across a GSI image and a host image will not overlap. | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 150 | uint32_t adjust_userId(uint32_t userId) { | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 151 | static constexpr uint32_t kGsiOffset = 1000000; | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 152 | CHECK(userId < kGsiOffset); | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 153 | CHECK(hw_device != nullptr); | 
|  | 154 | if (is_running_gsi) { | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 155 | return userId + kGsiOffset; | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 156 | } | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 157 | return userId; | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 160 | #define GK_ERROR *gkResponse = GKResponse::error(), Status::ok() | 
|  | 161 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 162 | Status enroll(int32_t userId, const std::optional<std::vector<uint8_t>>& currentPasswordHandle, | 
| Jooyung Han | 57110a4 | 2020-01-23 13:28:36 +0900 | [diff] [blame] | 163 | const std::optional<std::vector<uint8_t>>& currentPassword, | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 164 | const std::vector<uint8_t>& desiredPassword, GKResponse* gkResponse) override { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 165 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 166 | const int calling_pid = ipc->getCallingPid(); | 
|  | 167 | const int calling_uid = ipc->getCallingUid(); | 
|  | 168 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 169 | return GK_ERROR; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 172 | // Make sure to clear any state from before factory reset as soon as a credential is | 
|  | 173 | // enrolled (which may happen during device setup). | 
|  | 174 | clear_state_if_needed(); | 
|  | 175 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 176 | // need a desired password to enroll | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 177 | if (desiredPassword.size() == 0) return GK_ERROR; | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 178 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 179 | if (!hw_device) { | 
|  | 180 | LOG(ERROR) << "has no HAL to talk to"; | 
|  | 181 | return GK_ERROR; | 
|  | 182 | } | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 183 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 184 | android::hardware::hidl_vec<uint8_t> curPwdHandle; | 
|  | 185 | android::hardware::hidl_vec<uint8_t> curPwd; | 
|  | 186 |  | 
|  | 187 | if (currentPasswordHandle && currentPassword) { | 
|  | 188 | if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) { | 
|  | 189 | LOG(INFO) << "Password handle has wrong length"; | 
|  | 190 | return GK_ERROR; | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 191 | } | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 192 | curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()), | 
|  | 193 | currentPasswordHandle->size()); | 
|  | 194 | curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()), | 
|  | 195 | currentPassword->size()); | 
|  | 196 | } | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 197 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 198 | android::hardware::hidl_vec<uint8_t> newPwd; | 
|  | 199 | newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size()); | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 200 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 201 | uint32_t hw_userId = adjust_userId(userId); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 202 | Return<void> hwRes = hw_device->enroll( | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 203 | hw_userId, curPwdHandle, curPwd, newPwd, | 
|  | 204 | [&gkResponse](const GatekeeperResponse& rsp) { | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 205 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { | 
|  | 206 | *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()}); | 
|  | 207 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && | 
|  | 208 | rsp.timeout > 0) { | 
|  | 209 | *gkResponse = GKResponse::retry(rsp.timeout); | 
|  | 210 | } else { | 
|  | 211 | *gkResponse = GKResponse::error(); | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 212 | } | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 213 | }); | 
|  | 214 | if (!hwRes.isOk()) { | 
|  | 215 | LOG(ERROR) << "enroll transaction failed"; | 
|  | 216 | return GK_ERROR; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) { | 
|  | 220 | if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) { | 
|  | 221 | LOG(ERROR) << "HAL returned password handle of invalid length " | 
|  | 222 | << gkResponse->payload().size(); | 
|  | 223 | return GK_ERROR; | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 224 | } | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 225 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 226 | const gatekeeper::password_handle_t* handle = | 
|  | 227 | reinterpret_cast<const gatekeeper::password_handle_t*>( | 
|  | 228 | gkResponse->payload().data()); | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 229 | store_sid(userId, handle->user_id); | 
| Andres Morales | 531e3e8 | 2015-06-01 17:23:04 -0700 | [diff] [blame] | 230 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 231 | GKResponse verifyResponse; | 
| Andres Morales | 531e3e8 | 2015-06-01 17:23:04 -0700 | [diff] [blame] | 232 | // immediately verify this password so we don't ask the user to enter it again | 
|  | 233 | // if they just created it. | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 234 | auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 235 | if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) { | 
|  | 236 | LOG(ERROR) << "Failed to verify password after enrolling"; | 
|  | 237 | } | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 238 | } | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 239 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 240 | return Status::ok(); | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 243 | Status verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle, | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 244 | const ::std::vector<uint8_t>& providedPassword, GKResponse* gkResponse) override { | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 245 | return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword, | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 246 | gkResponse); | 
| Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 249 | Status verifyChallenge(int32_t userId, int64_t challenge, | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 250 | const std::vector<uint8_t>& enrolledPasswordHandle, | 
|  | 251 | const std::vector<uint8_t>& providedPassword, | 
|  | 252 | GKResponse* gkResponse) override { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 253 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 254 | const int calling_pid = ipc->getCallingPid(); | 
|  | 255 | const int calling_uid = ipc->getCallingUid(); | 
|  | 256 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 257 | return GK_ERROR; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
|  | 260 | // can't verify if we're missing either param | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 261 | if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 262 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 263 | if (!hw_device) return GK_ERROR; | 
|  | 264 |  | 
|  | 265 | if (enrolledPasswordHandle.size() != sizeof(gatekeeper::password_handle_t)) { | 
|  | 266 | LOG(INFO) << "Password handle has wrong length"; | 
|  | 267 | return GK_ERROR; | 
|  | 268 | } | 
|  | 269 | const gatekeeper::password_handle_t* handle = | 
|  | 270 | reinterpret_cast<const gatekeeper::password_handle_t*>( | 
|  | 271 | enrolledPasswordHandle.data()); | 
|  | 272 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 273 | uint32_t hw_userId = adjust_userId(userId); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 274 | android::hardware::hidl_vec<uint8_t> curPwdHandle; | 
|  | 275 | curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()), | 
|  | 276 | enrolledPasswordHandle.size()); | 
|  | 277 | android::hardware::hidl_vec<uint8_t> enteredPwd; | 
|  | 278 | enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()), | 
|  | 279 | providedPassword.size()); | 
|  | 280 |  | 
|  | 281 | Return<void> hwRes = hw_device->verify( | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 282 | hw_userId, challenge, curPwdHandle, enteredPwd, | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 283 | [&gkResponse](const GatekeeperResponse& rsp) { | 
|  | 284 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { | 
|  | 285 | *gkResponse = GKResponse::ok( | 
|  | 286 | {rsp.data.begin(), rsp.data.end()}, | 
|  | 287 | rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */); | 
|  | 288 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) { | 
|  | 289 | *gkResponse = GKResponse::retry(rsp.timeout); | 
|  | 290 | } else { | 
|  | 291 | *gkResponse = GKResponse::error(); | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 292 | } | 
|  | 293 | }); | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 294 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 295 | if (!hwRes.isOk()) { | 
|  | 296 | LOG(ERROR) << "verify transaction failed"; | 
|  | 297 | return GK_ERROR; | 
|  | 298 | } | 
|  | 299 |  | 
|  | 300 | if (gkResponse->response_code() == GKResponseCode::OK) { | 
|  | 301 | if (gkResponse->payload().size() != 0) { | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 302 | // try to connect to IKeystoreAuthorization AIDL service first. | 
|  | 303 | AIBinder* authzAIBinder = | 
| Janis Danisevskis | 36ac55f | 2021-03-10 11:19:37 -0800 | [diff] [blame] | 304 | AServiceManager_getService("android.security.authorization"); | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 305 | ::ndk::SpAIBinder authzBinder(authzAIBinder); | 
|  | 306 | auto authzService = IKeystoreAuthorization::fromBinder(authzBinder); | 
|  | 307 | if (authzService) { | 
|  | 308 | if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) { | 
|  | 309 | LOG(ERROR) << "Incorrect size of AuthToken payload."; | 
|  | 310 | return GK_ERROR; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | const hw_auth_token_t* hwAuthToken = | 
|  | 314 | reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data()); | 
|  | 315 | HardwareAuthToken authToken; | 
|  | 316 |  | 
|  | 317 | authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp); | 
|  | 318 | authToken.challenge = hwAuthToken->challenge; | 
| Janis Danisevskis | 8e0035c | 2021-02-11 23:16:58 -0800 | [diff] [blame] | 319 | authToken.userId = hwAuthToken->user_id; | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 320 | authToken.authenticatorId = hwAuthToken->authenticator_id; | 
|  | 321 | authToken.authenticatorType = static_cast<HardwareAuthenticatorType>( | 
|  | 322 | betoh32(hwAuthToken->authenticator_type)); | 
|  | 323 | authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]); | 
|  | 324 | auto result = authzService->addAuthToken(authToken); | 
|  | 325 | if (!result.isOk()) { | 
|  | 326 | LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService."; | 
|  | 327 | return GK_ERROR; | 
|  | 328 | } | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 329 | } else { | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 330 | LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with " | 
|  | 331 | "Keystore."; | 
|  | 332 | return GK_ERROR; | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 333 | } | 
|  | 334 | } | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 335 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 336 | maybe_store_sid(userId, handle->user_id); | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 337 | } | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 338 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 339 | return Status::ok(); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 340 | } | 
|  | 341 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 342 | Status getSecureUserId(int32_t userId, int64_t* sid) override { | 
|  | 343 | *sid = read_sid(userId); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 344 | return Status::ok(); | 
|  | 345 | } | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 346 |  | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 347 | Status clearSecureUserId(int32_t userId) override { | 
| Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 348 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 349 | const int calling_pid = ipc->getCallingPid(); | 
|  | 350 | const int calling_uid = ipc->getCallingUid(); | 
|  | 351 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { | 
|  | 352 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 353 | return Status::ok(); | 
| Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 354 | } | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 355 | clear_sid(userId); | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 356 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 357 | if (hw_device) { | 
| Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 358 | uint32_t hw_userId = adjust_userId(userId); | 
|  | 359 | hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {}); | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 360 | } | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 361 | return Status::ok(); | 
| Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 362 | } | 
|  | 363 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 364 | Status reportDeviceSetupComplete() override { | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 365 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 366 | const int calling_pid = ipc->getCallingPid(); | 
|  | 367 | const int calling_uid = ipc->getCallingUid(); | 
|  | 368 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { | 
|  | 369 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 370 | return Status::ok(); | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 371 | } | 
|  | 372 |  | 
|  | 373 | clear_state_if_needed(); | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 374 | return Status::ok(); | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 377 | status_t dump(int fd, const Vector<String16>&) override { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 378 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 379 | const int pid = ipc->getCallingPid(); | 
|  | 380 | const int uid = ipc->getCallingUid(); | 
|  | 381 | if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) { | 
|  | 382 | return PERMISSION_DENIED; | 
|  | 383 | } | 
|  | 384 |  | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 385 | if (hw_device == NULL) { | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 386 | const char* result = "Device not available"; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 387 | write(fd, result, strlen(result) + 1); | 
|  | 388 | } else { | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 389 | const char* result = "OK"; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 390 | write(fd, result, strlen(result) + 1); | 
|  | 391 | } | 
|  | 392 |  | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 393 | return OK; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 396 | private: | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 397 | sp<IGatekeeper> hw_device; | 
| Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 398 |  | 
|  | 399 | bool clear_state_if_needed_done; | 
| David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 400 | bool is_running_gsi; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 401 | }; | 
| Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 402 | }  // namespace android | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 403 |  | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 404 | int main(int argc, char* argv[]) { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 405 | ALOGI("Starting gatekeeperd..."); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 406 | if (argc < 2) { | 
|  | 407 | ALOGE("A directory must be specified!"); | 
|  | 408 | return 1; | 
|  | 409 | } | 
|  | 410 | if (chdir(argv[1]) == -1) { | 
|  | 411 | ALOGE("chdir: %s: %s", argv[1], strerror(errno)); | 
|  | 412 | return 1; | 
|  | 413 | } | 
|  | 414 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 415 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); | 
|  | 416 | android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy(); | 
|  | 417 | android::status_t ret = sm->addService( | 
|  | 418 | android::String16("android.service.gatekeeper.IGateKeeperService"), proxy); | 
|  | 419 | if (ret != android::OK) { | 
|  | 420 | ALOGE("Couldn't register binder service!"); | 
|  | 421 | return -1; | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | /* | 
|  | 425 | * We're the only thread in existence, so we're just going to process | 
|  | 426 | * Binder transaction as a single-threaded program. | 
|  | 427 | */ | 
|  | 428 | android::IPCThreadState::self()->joinThreadPool(); | 
|  | 429 | return 0; | 
|  | 430 | } |