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