| 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 |  | 
|  | 19 | #include "IGateKeeperService.h" | 
|  | 20 |  | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 21 | #include <errno.h> | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 22 | #include <fcntl.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 23 | #include <inttypes.h> | 
|  | 24 | #include <stdint.h> | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 25 | #include <unistd.h> | 
| Justin Yun | 919cc55 | 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 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> | 
|  | 29 | #include <binder/IServiceManager.h> | 
|  | 30 | #include <binder/PermissionCache.h> | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 31 | #include <gatekeeper/password_handle.h> // for password_handle_t | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 32 | #include <hardware/gatekeeper.h> | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 33 | #include <hardware/hw_auth_token.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 34 | #include <keystore/IKeystoreService.h> | 
|  | 35 | #include <keystore/keystore.h> // For error code | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 36 | #include <log/log.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 37 | #include <utils/Log.h> | 
|  | 38 | #include <utils/String16.h> | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 39 |  | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 40 | #include "SoftGateKeeperDevice.h" | 
| Andres Morales | 1cf7d25 | 2015-08-04 16:57:12 -0700 | [diff] [blame] | 41 | #include "IUserManager.h" | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 42 |  | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 43 | #include <hidl/HidlSupport.h> | 
|  | 44 | #include <android/hardware/gatekeeper/1.0/IGatekeeper.h> | 
|  | 45 |  | 
|  | 46 | using android::sp; | 
|  | 47 | using android::hardware::gatekeeper::V1_0::IGatekeeper; | 
|  | 48 | using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode; | 
|  | 49 | using android::hardware::gatekeeper::V1_0::GatekeeperResponse; | 
|  | 50 | using android::hardware::Return; | 
|  | 51 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 52 | namespace android { | 
|  | 53 |  | 
|  | 54 | static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE"); | 
|  | 55 | static const String16 DUMP_PERMISSION("android.permission.DUMP"); | 
|  | 56 |  | 
|  | 57 | class GateKeeperProxy : public BnGateKeeperService { | 
|  | 58 | public: | 
|  | 59 | GateKeeperProxy() { | 
| Chris Phoenix | a84ce0c | 2017-01-24 13:09:39 -0800 | [diff] [blame] | 60 | hw_device = IGatekeeper::getService(); | 
| Andres Morales | fef908e | 2015-07-07 10:28:15 -0700 | [diff] [blame] | 61 |  | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 62 | if (hw_device == nullptr) { | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 63 | ALOGW("falling back to software GateKeeper"); | 
|  | 64 | soft_device.reset(new SoftGateKeeperDevice()); | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 65 | } | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | if (mark_cold_boot()) { | 
|  | 68 | ALOGI("cold boot: clearing state"); | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 69 | if (hw_device != nullptr) { | 
|  | 70 | hw_device->deleteAllUsers([](const GatekeeperResponse &){}); | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 71 | } | 
|  | 72 | } | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | virtual ~GateKeeperProxy() { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 78 | void store_sid(uint32_t uid, uint64_t sid) { | 
|  | 79 | char filename[21]; | 
| George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 80 | snprintf(filename, sizeof(filename), "%u", uid); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 81 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); | 
|  | 82 | if (fd < 0) { | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 83 | ALOGE("could not open file: %s: %s", filename, strerror(errno)); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 84 | return; | 
|  | 85 | } | 
|  | 86 | write(fd, &sid, sizeof(sid)); | 
|  | 87 | close(fd); | 
|  | 88 | } | 
|  | 89 |  | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 90 | bool mark_cold_boot() { | 
|  | 91 | const char *filename = ".coldboot"; | 
|  | 92 | if (access(filename, F_OK) == -1) { | 
|  | 93 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); | 
|  | 94 | if (fd < 0) { | 
|  | 95 | ALOGE("could not open file: %s : %s", filename, strerror(errno)); | 
|  | 96 | return false; | 
|  | 97 | } | 
|  | 98 | close(fd); | 
|  | 99 | return true; | 
|  | 100 | } | 
|  | 101 | return false; | 
|  | 102 | } | 
|  | 103 |  | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 104 | void maybe_store_sid(uint32_t uid, uint64_t sid) { | 
|  | 105 | char filename[21]; | 
| George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 106 | snprintf(filename, sizeof(filename), "%u", uid); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 107 | if (access(filename, F_OK) == -1) { | 
|  | 108 | store_sid(uid, sid); | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | uint64_t read_sid(uint32_t uid) { | 
|  | 113 | char filename[21]; | 
|  | 114 | uint64_t sid; | 
| George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 115 | snprintf(filename, sizeof(filename), "%u", uid); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 116 | int fd = open(filename, O_RDONLY); | 
|  | 117 | if (fd < 0) return 0; | 
|  | 118 | read(fd, &sid, sizeof(sid)); | 
| Andres Morales | 0b0435e | 2015-07-10 09:47:09 -0700 | [diff] [blame] | 119 | close(fd); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 120 | return sid; | 
|  | 121 | } | 
|  | 122 |  | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 123 | void clear_sid(uint32_t uid) { | 
|  | 124 | char filename[21]; | 
| George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 125 | snprintf(filename, sizeof(filename), "%u", uid); | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 126 | if (remove(filename) < 0) { | 
|  | 127 | ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno)); | 
|  | 128 | store_sid(uid, 0); | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 132 | virtual int enroll(uint32_t uid, | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 133 | const uint8_t *current_password_handle, uint32_t current_password_handle_length, | 
|  | 134 | const uint8_t *current_password, uint32_t current_password_length, | 
|  | 135 | const uint8_t *desired_password, uint32_t desired_password_length, | 
|  | 136 | uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) { | 
|  | 137 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 138 | const int calling_pid = ipc->getCallingPid(); | 
|  | 139 | const int calling_uid = ipc->getCallingUid(); | 
|  | 140 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { | 
|  | 141 | return PERMISSION_DENIED; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | // need a desired password to enroll | 
|  | 145 | if (desired_password_length == 0) return -EINVAL; | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 146 |  | 
|  | 147 | int ret; | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 148 | if (hw_device != nullptr) { | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 149 | const gatekeeper::password_handle_t *handle = | 
|  | 150 | reinterpret_cast<const gatekeeper::password_handle_t *>(current_password_handle); | 
|  | 151 |  | 
| Andres Morales | 7f6dcf6 | 2015-06-24 18:40:24 -0700 | [diff] [blame] | 152 | if (handle != NULL && handle->version != 0 && !handle->hardware_backed) { | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 153 | // handle is being re-enrolled from a software version. HAL probably won't accept | 
|  | 154 | // the handle as valid, so we nullify it and enroll from scratch | 
|  | 155 | current_password_handle = NULL; | 
|  | 156 | current_password_handle_length = 0; | 
|  | 157 | current_password = NULL; | 
|  | 158 | current_password_length = 0; | 
|  | 159 | } | 
|  | 160 |  | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 161 | android::hardware::hidl_vec<uint8_t> curPwdHandle; | 
|  | 162 | curPwdHandle.setToExternal(const_cast<uint8_t*>(current_password_handle), | 
|  | 163 | current_password_handle_length); | 
|  | 164 | android::hardware::hidl_vec<uint8_t> curPwd; | 
|  | 165 | curPwd.setToExternal(const_cast<uint8_t*>(current_password), | 
|  | 166 | current_password_length); | 
|  | 167 | android::hardware::hidl_vec<uint8_t> newPwd; | 
|  | 168 | newPwd.setToExternal(const_cast<uint8_t*>(desired_password), | 
|  | 169 | desired_password_length); | 
|  | 170 |  | 
|  | 171 | Return<void> hwRes = hw_device->enroll(uid, curPwdHandle, curPwd, newPwd, | 
|  | 172 | [&ret, enrolled_password_handle, enrolled_password_handle_length] | 
|  | 173 | (const GatekeeperResponse &rsp) { | 
|  | 174 | ret = static_cast<int>(rsp.code); // propagate errors | 
|  | 175 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { | 
|  | 176 | if (enrolled_password_handle != nullptr && | 
|  | 177 | enrolled_password_handle_length != nullptr) { | 
|  | 178 | *enrolled_password_handle = new uint8_t[rsp.data.size()]; | 
|  | 179 | *enrolled_password_handle_length = rsp.data.size(); | 
|  | 180 | memcpy(*enrolled_password_handle, rsp.data.data(), | 
|  | 181 | *enrolled_password_handle_length); | 
|  | 182 | } | 
|  | 183 | ret = 0; // all success states are reported as 0 | 
|  | 184 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && rsp.timeout > 0) { | 
|  | 185 | ret = rsp.timeout; | 
|  | 186 | } | 
|  | 187 | }); | 
| Steven Moreland | 8133093 | 2017-01-03 17:01:27 -0800 | [diff] [blame] | 188 | if (!hwRes.isOk()) { | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 189 | ALOGE("enroll transaction failed\n"); | 
|  | 190 | ret = -1; | 
|  | 191 | } | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 192 | } else { | 
|  | 193 | ret = soft_device->enroll(uid, | 
|  | 194 | current_password_handle, current_password_handle_length, | 
|  | 195 | current_password, current_password_length, | 
|  | 196 | desired_password, desired_password_length, | 
|  | 197 | enrolled_password_handle, enrolled_password_handle_length); | 
|  | 198 | } | 
|  | 199 |  | 
| Alexey Polyudov | 8c63536 | 2016-09-07 18:51:28 -0700 | [diff] [blame] | 200 | if (ret == GATEKEEPER_RESPONSE_OK && (*enrolled_password_handle == nullptr || | 
|  | 201 | *enrolled_password_handle_length != sizeof(password_handle_t))) { | 
|  | 202 | ret = GATEKEEPER_RESPONSE_ERROR; | 
|  | 203 | ALOGE("HAL: password_handle=%p size_of_handle=%" PRIu32 "\n", | 
|  | 204 | *enrolled_password_handle, *enrolled_password_handle_length); | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | if (ret == GATEKEEPER_RESPONSE_OK) { | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 208 | gatekeeper::password_handle_t *handle = | 
|  | 209 | reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle); | 
|  | 210 | store_sid(uid, handle->user_id); | 
| Andres Morales | 531e3e8 | 2015-06-01 17:23:04 -0700 | [diff] [blame] | 211 | bool rr; | 
|  | 212 |  | 
|  | 213 | // immediately verify this password so we don't ask the user to enter it again | 
|  | 214 | // if they just created it. | 
|  | 215 | verify(uid, *enrolled_password_handle, sizeof(password_handle_t), desired_password, | 
|  | 216 | desired_password_length, &rr); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 217 | } | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 218 |  | 
|  | 219 | return ret; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 222 | virtual int verify(uint32_t uid, | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 223 | const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 224 | const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) { | 
| Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 225 | uint8_t *auth_token; | 
|  | 226 | uint32_t auth_token_length; | 
|  | 227 | return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length, | 
|  | 228 | provided_password, provided_password_length, | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 229 | &auth_token, &auth_token_length, request_reenroll); | 
| Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 230 | } | 
|  | 231 |  | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 232 | virtual int verifyChallenge(uint32_t uid, uint64_t challenge, | 
| Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 233 | const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, | 
|  | 234 | const uint8_t *provided_password, uint32_t provided_password_length, | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 235 | uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 236 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 237 | const int calling_pid = ipc->getCallingPid(); | 
|  | 238 | const int calling_uid = ipc->getCallingUid(); | 
|  | 239 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { | 
|  | 240 | return PERMISSION_DENIED; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | // can't verify if we're missing either param | 
|  | 244 | if ((enrolled_password_handle_length | provided_password_length) == 0) | 
|  | 245 | return -EINVAL; | 
|  | 246 |  | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 247 | int ret; | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 248 | if (hw_device != nullptr) { | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 249 | const gatekeeper::password_handle_t *handle = | 
|  | 250 | reinterpret_cast<const gatekeeper::password_handle_t *>(enrolled_password_handle); | 
| Andres Morales | 7f6dcf6 | 2015-06-24 18:40:24 -0700 | [diff] [blame] | 251 | // handle version 0 does not have hardware backed flag, and thus cannot be upgraded to | 
|  | 252 | // a HAL if there was none before | 
|  | 253 | if (handle->version == 0 || handle->hardware_backed) { | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 254 | android::hardware::hidl_vec<uint8_t> curPwdHandle; | 
|  | 255 | curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolled_password_handle), | 
|  | 256 | enrolled_password_handle_length); | 
|  | 257 | android::hardware::hidl_vec<uint8_t> enteredPwd; | 
|  | 258 | enteredPwd.setToExternal(const_cast<uint8_t*>(provided_password), | 
|  | 259 | provided_password_length); | 
|  | 260 | Return<void> hwRes = hw_device->verify(uid, challenge, curPwdHandle, enteredPwd, | 
|  | 261 | [&ret, request_reenroll, auth_token, auth_token_length] | 
|  | 262 | (const GatekeeperResponse &rsp) { | 
|  | 263 | ret = static_cast<int>(rsp.code); // propagate errors | 
|  | 264 | if (auth_token != nullptr && auth_token_length != nullptr && | 
|  | 265 | rsp.code >= GatekeeperStatusCode::STATUS_OK) { | 
|  | 266 | *auth_token = new uint8_t[rsp.data.size()]; | 
|  | 267 | *auth_token_length = rsp.data.size(); | 
|  | 268 | memcpy(*auth_token, rsp.data.data(), *auth_token_length); | 
|  | 269 | if (request_reenroll != nullptr) { | 
|  | 270 | *request_reenroll = (rsp.code == GatekeeperStatusCode::STATUS_REENROLL); | 
|  | 271 | } | 
|  | 272 | ret = 0; // all success states are reported as 0 | 
|  | 273 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && | 
|  | 274 | rsp.timeout > 0) { | 
|  | 275 | ret = rsp.timeout; | 
|  | 276 | } | 
|  | 277 | }); | 
| Steven Moreland | 8133093 | 2017-01-03 17:01:27 -0800 | [diff] [blame] | 278 | if (!hwRes.isOk()) { | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 279 | ALOGE("verify transaction failed\n"); | 
|  | 280 | ret = -1; | 
|  | 281 | } | 
| Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 282 | } else { | 
|  | 283 | // upgrade scenario, a HAL has been added to this device where there was none before | 
|  | 284 | SoftGateKeeperDevice soft_dev; | 
|  | 285 | ret = soft_dev.verify(uid, challenge, | 
|  | 286 | enrolled_password_handle, enrolled_password_handle_length, | 
|  | 287 | provided_password, provided_password_length, auth_token, auth_token_length, | 
|  | 288 | request_reenroll); | 
|  | 289 |  | 
|  | 290 | if (ret == 0) { | 
|  | 291 | // success! re-enroll with HAL | 
|  | 292 | *request_reenroll = true; | 
|  | 293 | } | 
|  | 294 | } | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 295 | } else { | 
|  | 296 | ret = soft_device->verify(uid, challenge, | 
|  | 297 | enrolled_password_handle, enrolled_password_handle_length, | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 298 | provided_password, provided_password_length, auth_token, auth_token_length, | 
|  | 299 | request_reenroll); | 
| Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 300 | } | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 301 |  | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 302 | if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 303 | // TODO: cache service? | 
|  | 304 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 305 | sp<IBinder> binder = sm->getService(String16("android.security.keystore")); | 
|  | 306 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); | 
|  | 307 | if (service != NULL) { | 
| Janis Danisevskis | 72030fb | 2016-10-24 14:47:00 +0100 | [diff] [blame] | 308 | auto ret = service->addAuthToken(*auth_token, *auth_token_length); | 
|  | 309 | if (!ret.isOk()) { | 
|  | 310 | ALOGE("Failure sending auth token to KeyStore: %" PRId32, int32_t(ret)); | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 311 | } | 
|  | 312 | } else { | 
|  | 313 | ALOGE("Unable to communicate with KeyStore"); | 
|  | 314 | } | 
|  | 315 | } | 
|  | 316 |  | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 317 | if (ret == 0) { | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 318 | maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>( | 
|  | 319 | enrolled_password_handle)->user_id); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 322 | return ret; | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
|  | 325 | virtual uint64_t getSecureUserId(uint32_t uid) { | 
| Andres Morales | 1cf7d25 | 2015-08-04 16:57:12 -0700 | [diff] [blame] | 326 | uint64_t sid = read_sid(uid); | 
|  | 327 | if (sid == 0) { | 
|  | 328 | // might be a work profile, look up the parent | 
|  | 329 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 330 | sp<IBinder> binder = sm->getService(String16("user")); | 
|  | 331 | sp<IUserManager> um = interface_cast<IUserManager>(binder); | 
|  | 332 | int32_t parent = um->getCredentialOwnerProfile(uid); | 
|  | 333 | if (parent < 0) { | 
|  | 334 | return 0; | 
|  | 335 | } else if (parent != (int32_t) uid) { | 
|  | 336 | return read_sid(parent); | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 | return sid; | 
|  | 340 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
| Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 343 | virtual void clearSecureUserId(uint32_t uid) { | 
|  | 344 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 345 | const int calling_pid = ipc->getCallingPid(); | 
|  | 346 | const int calling_uid = ipc->getCallingUid(); | 
|  | 347 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { | 
|  | 348 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); | 
|  | 349 | return; | 
|  | 350 | } | 
| Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 351 | clear_sid(uid); | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 352 |  | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 353 | if (hw_device != nullptr) { | 
|  | 354 | hw_device->deleteUser(uid, [] (const GatekeeperResponse &){}); | 
| Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 355 | } | 
| Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 358 | virtual status_t dump(int fd, const Vector<String16> &) { | 
|  | 359 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 360 | const int pid = ipc->getCallingPid(); | 
|  | 361 | const int uid = ipc->getCallingUid(); | 
|  | 362 | if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) { | 
|  | 363 | return PERMISSION_DENIED; | 
|  | 364 | } | 
|  | 365 |  | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 366 | if (hw_device == NULL) { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 367 | const char *result = "Device not available"; | 
|  | 368 | write(fd, result, strlen(result) + 1); | 
|  | 369 | } else { | 
|  | 370 | const char *result = "OK"; | 
|  | 371 | write(fd, result, strlen(result) + 1); | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | return NO_ERROR; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | private: | 
| Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 378 | sp<IGatekeeper> hw_device; | 
| Justin Yun | 919cc55 | 2017-08-16 18:54:20 +0900 | [diff] [blame] | 379 | std::unique_ptr<SoftGateKeeperDevice> soft_device; | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 380 | }; | 
|  | 381 | }// namespace android | 
|  | 382 |  | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 383 | int main(int argc, char* argv[]) { | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 384 | ALOGI("Starting gatekeeperd..."); | 
| Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 385 | if (argc < 2) { | 
|  | 386 | ALOGE("A directory must be specified!"); | 
|  | 387 | return 1; | 
|  | 388 | } | 
|  | 389 | if (chdir(argv[1]) == -1) { | 
|  | 390 | ALOGE("chdir: %s: %s", argv[1], strerror(errno)); | 
|  | 391 | return 1; | 
|  | 392 | } | 
|  | 393 |  | 
| Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 394 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); | 
|  | 395 | android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy(); | 
|  | 396 | android::status_t ret = sm->addService( | 
|  | 397 | android::String16("android.service.gatekeeper.IGateKeeperService"), proxy); | 
|  | 398 | if (ret != android::OK) { | 
|  | 399 | ALOGE("Couldn't register binder service!"); | 
|  | 400 | return -1; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | /* | 
|  | 404 | * We're the only thread in existence, so we're just going to process | 
|  | 405 | * Binder transaction as a single-threaded program. | 
|  | 406 | */ | 
|  | 407 | android::IPCThreadState::self()->joinThreadPool(); | 
|  | 408 | return 0; | 
|  | 409 | } |