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> |
| 22 | #include <stdint.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
| 26 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 27 | #include <cutils/log.h> |
| 28 | #include <utils/Log.h> |
| 29 | |
| 30 | #include <binder/IPCThreadState.h> |
| 31 | #include <binder/IServiceManager.h> |
| 32 | #include <binder/PermissionCache.h> |
| 33 | #include <utils/String16.h> |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 34 | #include <utils/Log.h> |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 35 | |
| 36 | #include <keystore/IKeystoreService.h> |
Andres Morales | 2ae8b4c | 2015-04-13 09:20:09 -0700 | [diff] [blame] | 37 | #include <keystore/keystore.h> // For error code |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 38 | #include <gatekeeper/password_handle.h> // for password_handle_t |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 39 | #include <hardware/gatekeeper.h> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 40 | #include <hardware/hw_auth_token.h> |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 41 | |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 42 | #include "SoftGateKeeperDevice.h" |
| 43 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 44 | namespace android { |
| 45 | |
| 46 | static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE"); |
| 47 | static const String16 DUMP_PERMISSION("android.permission.DUMP"); |
| 48 | |
| 49 | class GateKeeperProxy : public BnGateKeeperService { |
| 50 | public: |
| 51 | GateKeeperProxy() { |
| 52 | int ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &module); |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 53 | if (ret < 0) { |
| 54 | ALOGW("falling back to software GateKeeper"); |
| 55 | soft_device.reset(new SoftGateKeeperDevice()); |
| 56 | } else { |
| 57 | ret = gatekeeper_open(module, &device); |
| 58 | if (ret < 0) |
| 59 | LOG_ALWAYS_FATAL_IF(ret < 0, "Unable to open GateKeeper HAL"); |
| 60 | } |
Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame^] | 61 | |
| 62 | if (mark_cold_boot()) { |
| 63 | ALOGI("cold boot: clearing state"); |
| 64 | if (device != NULL && device->delete_all_users != NULL) { |
| 65 | device->delete_all_users(device); |
| 66 | } |
| 67 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | virtual ~GateKeeperProxy() { |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 71 | if (device) gatekeeper_close(device); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 74 | void store_sid(uint32_t uid, uint64_t sid) { |
| 75 | char filename[21]; |
| 76 | sprintf(filename, "%u", uid); |
| 77 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); |
| 78 | if (fd < 0) { |
Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 79 | ALOGE("could not open file: %s: %s", filename, strerror(errno)); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | write(fd, &sid, sizeof(sid)); |
| 83 | close(fd); |
| 84 | } |
| 85 | |
Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame^] | 86 | bool mark_cold_boot() { |
| 87 | const char *filename = ".coldboot"; |
| 88 | if (access(filename, F_OK) == -1) { |
| 89 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); |
| 90 | if (fd < 0) { |
| 91 | ALOGE("could not open file: %s : %s", filename, strerror(errno)); |
| 92 | return false; |
| 93 | } |
| 94 | close(fd); |
| 95 | return true; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 100 | void maybe_store_sid(uint32_t uid, uint64_t sid) { |
| 101 | char filename[21]; |
| 102 | sprintf(filename, "%u", uid); |
| 103 | if (access(filename, F_OK) == -1) { |
| 104 | store_sid(uid, sid); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | uint64_t read_sid(uint32_t uid) { |
| 109 | char filename[21]; |
| 110 | uint64_t sid; |
| 111 | sprintf(filename, "%u", uid); |
| 112 | int fd = open(filename, O_RDONLY); |
| 113 | if (fd < 0) return 0; |
| 114 | read(fd, &sid, sizeof(sid)); |
| 115 | return sid; |
| 116 | } |
| 117 | |
Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 118 | void clear_sid(uint32_t uid) { |
| 119 | char filename[21]; |
| 120 | sprintf(filename, "%u", uid); |
| 121 | if (remove(filename) < 0) { |
| 122 | ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno)); |
| 123 | store_sid(uid, 0); |
| 124 | } |
| 125 | } |
| 126 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 127 | virtual int enroll(uint32_t uid, |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 128 | const uint8_t *current_password_handle, uint32_t current_password_handle_length, |
| 129 | const uint8_t *current_password, uint32_t current_password_length, |
| 130 | const uint8_t *desired_password, uint32_t desired_password_length, |
| 131 | uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) { |
| 132 | IPCThreadState* ipc = IPCThreadState::self(); |
| 133 | const int calling_pid = ipc->getCallingPid(); |
| 134 | const int calling_uid = ipc->getCallingUid(); |
| 135 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 136 | return PERMISSION_DENIED; |
| 137 | } |
| 138 | |
| 139 | // need a desired password to enroll |
| 140 | if (desired_password_length == 0) return -EINVAL; |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 141 | |
| 142 | int ret; |
| 143 | if (device) { |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 144 | const gatekeeper::password_handle_t *handle = |
| 145 | reinterpret_cast<const gatekeeper::password_handle_t *>(current_password_handle); |
| 146 | |
| 147 | if (handle != NULL && !handle->hardware_backed) { |
| 148 | // handle is being re-enrolled from a software version. HAL probably won't accept |
| 149 | // the handle as valid, so we nullify it and enroll from scratch |
| 150 | current_password_handle = NULL; |
| 151 | current_password_handle_length = 0; |
| 152 | current_password = NULL; |
| 153 | current_password_length = 0; |
| 154 | } |
| 155 | |
| 156 | ret = device->enroll(device, uid, current_password_handle, current_password_handle_length, |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 157 | current_password, current_password_length, |
| 158 | desired_password, desired_password_length, |
| 159 | enrolled_password_handle, enrolled_password_handle_length); |
| 160 | } else { |
| 161 | ret = soft_device->enroll(uid, |
| 162 | current_password_handle, current_password_handle_length, |
| 163 | current_password, current_password_length, |
| 164 | desired_password, desired_password_length, |
| 165 | enrolled_password_handle, enrolled_password_handle_length); |
| 166 | } |
| 167 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 168 | if (ret == 0) { |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 169 | gatekeeper::password_handle_t *handle = |
| 170 | reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle); |
| 171 | store_sid(uid, handle->user_id); |
Andres Morales | 531e3e8 | 2015-06-01 17:23:04 -0700 | [diff] [blame] | 172 | bool rr; |
| 173 | |
| 174 | // immediately verify this password so we don't ask the user to enter it again |
| 175 | // if they just created it. |
| 176 | verify(uid, *enrolled_password_handle, sizeof(password_handle_t), desired_password, |
| 177 | desired_password_length, &rr); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 178 | } |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 179 | |
| 180 | return ret; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 183 | virtual int verify(uint32_t uid, |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 184 | const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 185 | 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] | 186 | uint8_t *auth_token; |
| 187 | uint32_t auth_token_length; |
| 188 | return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length, |
| 189 | provided_password, provided_password_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 190 | &auth_token, &auth_token_length, request_reenroll); |
Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 193 | virtual int verifyChallenge(uint32_t uid, uint64_t challenge, |
Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 194 | const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, |
| 195 | const uint8_t *provided_password, uint32_t provided_password_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 196 | uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 197 | IPCThreadState* ipc = IPCThreadState::self(); |
| 198 | const int calling_pid = ipc->getCallingPid(); |
| 199 | const int calling_uid = ipc->getCallingUid(); |
| 200 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 201 | return PERMISSION_DENIED; |
| 202 | } |
| 203 | |
| 204 | // can't verify if we're missing either param |
| 205 | if ((enrolled_password_handle_length | provided_password_length) == 0) |
| 206 | return -EINVAL; |
| 207 | |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 208 | int ret; |
| 209 | if (device) { |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 210 | const gatekeeper::password_handle_t *handle = |
| 211 | reinterpret_cast<const gatekeeper::password_handle_t *>(enrolled_password_handle); |
| 212 | if (handle->hardware_backed) { |
| 213 | ret = device->verify(device, uid, challenge, |
| 214 | enrolled_password_handle, enrolled_password_handle_length, |
| 215 | provided_password, provided_password_length, auth_token, auth_token_length, |
| 216 | request_reenroll); |
| 217 | } else { |
| 218 | // upgrade scenario, a HAL has been added to this device where there was none before |
| 219 | SoftGateKeeperDevice soft_dev; |
| 220 | ret = soft_dev.verify(uid, challenge, |
| 221 | enrolled_password_handle, enrolled_password_handle_length, |
| 222 | provided_password, provided_password_length, auth_token, auth_token_length, |
| 223 | request_reenroll); |
| 224 | |
| 225 | if (ret == 0) { |
| 226 | // success! re-enroll with HAL |
| 227 | *request_reenroll = true; |
| 228 | } |
| 229 | } |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 230 | } else { |
| 231 | ret = soft_device->verify(uid, challenge, |
| 232 | enrolled_password_handle, enrolled_password_handle_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 233 | provided_password, provided_password_length, auth_token, auth_token_length, |
| 234 | request_reenroll); |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 235 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 236 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 237 | if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 238 | // TODO: cache service? |
| 239 | sp<IServiceManager> sm = defaultServiceManager(); |
| 240 | sp<IBinder> binder = sm->getService(String16("android.security.keystore")); |
| 241 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
| 242 | if (service != NULL) { |
Andres Morales | 2ae8b4c | 2015-04-13 09:20:09 -0700 | [diff] [blame] | 243 | status_t ret = service->addAuthToken(*auth_token, *auth_token_length); |
| 244 | if (ret != ResponseCode::NO_ERROR) { |
| 245 | ALOGE("Falure sending auth token to KeyStore: %d", ret); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 246 | } |
| 247 | } else { |
| 248 | ALOGE("Unable to communicate with KeyStore"); |
| 249 | } |
| 250 | } |
| 251 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 252 | if (ret == 0) { |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 253 | maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>( |
| 254 | enrolled_password_handle)->user_id); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 257 | return ret; |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | virtual uint64_t getSecureUserId(uint32_t uid) { |
| 261 | return read_sid(uid); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 264 | virtual void clearSecureUserId(uint32_t uid) { |
| 265 | IPCThreadState* ipc = IPCThreadState::self(); |
| 266 | const int calling_pid = ipc->getCallingPid(); |
| 267 | const int calling_uid = ipc->getCallingUid(); |
| 268 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 269 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); |
| 270 | return; |
| 271 | } |
Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 272 | clear_sid(uid); |
Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame^] | 273 | |
| 274 | if (device != NULL && device->delete_user != NULL) { |
| 275 | device->delete_user(device, uid); |
| 276 | } |
Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 279 | virtual status_t dump(int fd, const Vector<String16> &) { |
| 280 | IPCThreadState* ipc = IPCThreadState::self(); |
| 281 | const int pid = ipc->getCallingPid(); |
| 282 | const int uid = ipc->getCallingUid(); |
| 283 | if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) { |
| 284 | return PERMISSION_DENIED; |
| 285 | } |
| 286 | |
| 287 | if (device == NULL) { |
| 288 | const char *result = "Device not available"; |
| 289 | write(fd, result, strlen(result) + 1); |
| 290 | } else { |
| 291 | const char *result = "OK"; |
| 292 | write(fd, result, strlen(result) + 1); |
| 293 | } |
| 294 | |
| 295 | return NO_ERROR; |
| 296 | } |
| 297 | |
| 298 | private: |
| 299 | gatekeeper_device_t *device; |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 300 | UniquePtr<SoftGateKeeperDevice> soft_device; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 301 | const hw_module_t *module; |
| 302 | }; |
| 303 | }// namespace android |
| 304 | |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 305 | int main(int argc, char* argv[]) { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 306 | ALOGI("Starting gatekeeperd..."); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 307 | if (argc < 2) { |
| 308 | ALOGE("A directory must be specified!"); |
| 309 | return 1; |
| 310 | } |
| 311 | if (chdir(argv[1]) == -1) { |
| 312 | ALOGE("chdir: %s: %s", argv[1], strerror(errno)); |
| 313 | return 1; |
| 314 | } |
| 315 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 316 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); |
| 317 | android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy(); |
| 318 | android::status_t ret = sm->addService( |
| 319 | android::String16("android.service.gatekeeper.IGateKeeperService"), proxy); |
| 320 | if (ret != android::OK) { |
| 321 | ALOGE("Couldn't register binder service!"); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * We're the only thread in existence, so we're just going to process |
| 327 | * Binder transaction as a single-threaded program. |
| 328 | */ |
| 329 | android::IPCThreadState::self()->joinThreadPool(); |
| 330 | return 0; |
| 331 | } |