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