blob: 61c880409e1d410ba2d16bad076e02bb88b294d8 [file] [log] [blame]
Andres Morales2d08dce2015-04-03 16:40:15 -07001/*
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 Morales6a49c2f2015-04-16 13:16:24 -070021#include <errno.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070022#include <fcntl.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070023#include <inttypes.h>
24#include <stdint.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070025#include <unistd.h>
Justin Yun68b0ec62017-08-16 18:54:20 +090026#include <memory>
Andres Morales6a49c2f2015-04-16 13:16:24 -070027
Andres Morales2d08dce2015-04-03 16:40:15 -070028#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
30#include <binder/PermissionCache.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070031#include <gatekeeper/password_handle.h> // for password_handle_t
Andres Morales2d08dce2015-04-03 16:40:15 -070032#include <hardware/gatekeeper.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070033#include <hardware/hw_auth_token.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070034#include <keystore/IKeystoreService.h>
35#include <keystore/keystore.h> // For error code
Mark Salyzyn30f991f2017-01-10 13:19:54 -080036#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070037#include <utils/Log.h>
38#include <utils/String16.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070039
Andres Morales33dfdc72015-05-12 15:37:20 -070040#include "SoftGateKeeperDevice.h"
41
Alexey Polyudov275aece2016-10-18 13:59:26 -070042#include <hidl/HidlSupport.h>
43#include <android/hardware/gatekeeper/1.0/IGatekeeper.h>
44
45using android::sp;
46using android::hardware::gatekeeper::V1_0::IGatekeeper;
47using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode;
48using android::hardware::gatekeeper::V1_0::GatekeeperResponse;
49using android::hardware::Return;
50
Andres Morales2d08dce2015-04-03 16:40:15 -070051namespace android {
52
53static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
54static const String16 DUMP_PERMISSION("android.permission.DUMP");
55
56class GateKeeperProxy : public BnGateKeeperService {
57public:
58 GateKeeperProxy() {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070059 clear_state_if_needed_done = false;
Chris Phoenixa84ce0c2017-01-24 13:09:39 -080060 hw_device = IGatekeeper::getService();
Andres Moralesfef908e2015-07-07 10:28:15 -070061
Alexey Polyudov275aece2016-10-18 13:59:26 -070062 if (hw_device == nullptr) {
Andres Morales33dfdc72015-05-12 15:37:20 -070063 ALOGW("falling back to software GateKeeper");
64 soft_device.reset(new SoftGateKeeperDevice());
Andres Morales33dfdc72015-05-12 15:37:20 -070065 }
Andres Morales2d08dce2015-04-03 16:40:15 -070066 }
67
68 virtual ~GateKeeperProxy() {
Andres Morales2d08dce2015-04-03 16:40:15 -070069 }
70
Andres Morales6a49c2f2015-04-16 13:16:24 -070071 void store_sid(uint32_t uid, uint64_t sid) {
72 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -080073 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -070074 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
75 if (fd < 0) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -070076 ALOGE("could not open file: %s: %s", filename, strerror(errno));
Andres Morales6a49c2f2015-04-16 13:16:24 -070077 return;
78 }
79 write(fd, &sid, sizeof(sid));
80 close(fd);
81 }
82
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070083 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 Morales3c2086d2015-06-24 10:21:16 -070098 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 Morales6a49c2f2015-04-16 13:16:24 -0700112 void maybe_store_sid(uint32_t uid, uint64_t sid) {
113 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800114 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700115 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 IVe7aa2b22016-03-02 14:02:55 -0800123 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700124 int fd = open(filename, O_RDONLY);
125 if (fd < 0) return 0;
126 read(fd, &sid, sizeof(sid));
Andres Morales0b0435e2015-07-10 09:47:09 -0700127 close(fd);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700128 return sid;
129 }
130
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700131 void clear_sid(uint32_t uid) {
132 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800133 snprintf(filename, sizeof(filename), "%u", uid);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700134 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 Moralesae242922015-05-18 09:26:19 -0700140 virtual int enroll(uint32_t uid,
Andres Morales2d08dce2015-04-03 16:40:15 -0700141 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 Rooscb4ed1b2017-04-12 13:03:04 -0700152 // 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 Morales2d08dce2015-04-03 16:40:15 -0700156 // need a desired password to enroll
157 if (desired_password_length == 0) return -EINVAL;
Andres Morales33dfdc72015-05-12 15:37:20 -0700158
159 int ret;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700160 if (hw_device != nullptr) {
Andres Morales835d96e2015-06-03 15:06:24 -0700161 const gatekeeper::password_handle_t *handle =
162 reinterpret_cast<const gatekeeper::password_handle_t *>(current_password_handle);
163
Andres Morales7f6dcf62015-06-24 18:40:24 -0700164 if (handle != NULL && handle->version != 0 && !handle->hardware_backed) {
Andres Morales835d96e2015-06-03 15:06:24 -0700165 // 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 Polyudov275aece2016-10-18 13:59:26 -0700173 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 Moreland81330932017-01-03 17:01:27 -0800200 if (!hwRes.isOk()) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700201 ALOGE("enroll transaction failed\n");
202 ret = -1;
203 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700204 } 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 Polyudov8c635362016-09-07 18:51:28 -0700212 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 Morales6a49c2f2015-04-16 13:16:24 -0700220 gatekeeper::password_handle_t *handle =
221 reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle);
222 store_sid(uid, handle->user_id);
Andres Morales531e3e82015-06-01 17:23:04 -0700223 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 Morales6a49c2f2015-04-16 13:16:24 -0700229 }
Andres Moralesae242922015-05-18 09:26:19 -0700230
231 return ret;
Andres Morales2d08dce2015-04-03 16:40:15 -0700232 }
233
Andres Moralesae242922015-05-18 09:26:19 -0700234 virtual int verify(uint32_t uid,
Andres Morales2d08dce2015-04-03 16:40:15 -0700235 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
Andres Moralesae242922015-05-18 09:26:19 -0700236 const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) {
Andres Moralesc828ae82015-04-10 21:03:07 -0700237 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 Moralesae242922015-05-18 09:26:19 -0700241 &auth_token, &auth_token_length, request_reenroll);
Andres Moralesc828ae82015-04-10 21:03:07 -0700242 }
243
Andres Moralesae242922015-05-18 09:26:19 -0700244 virtual int verifyChallenge(uint32_t uid, uint64_t challenge,
Andres Moralesc828ae82015-04-10 21:03:07 -0700245 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
246 const uint8_t *provided_password, uint32_t provided_password_length,
Andres Moralesae242922015-05-18 09:26:19 -0700247 uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700248 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 Morales33dfdc72015-05-12 15:37:20 -0700259 int ret;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700260 if (hw_device != nullptr) {
Andres Morales835d96e2015-06-03 15:06:24 -0700261 const gatekeeper::password_handle_t *handle =
262 reinterpret_cast<const gatekeeper::password_handle_t *>(enrolled_password_handle);
Andres Morales7f6dcf62015-06-24 18:40:24 -0700263 // 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 Polyudov275aece2016-10-18 13:59:26 -0700266 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 Moreland81330932017-01-03 17:01:27 -0800290 if (!hwRes.isOk()) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700291 ALOGE("verify transaction failed\n");
292 ret = -1;
293 }
Andres Morales835d96e2015-06-03 15:06:24 -0700294 } 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 Morales33dfdc72015-05-12 15:37:20 -0700307 } else {
308 ret = soft_device->verify(uid, challenge,
309 enrolled_password_handle, enrolled_password_handle_length,
Andres Moralesae242922015-05-18 09:26:19 -0700310 provided_password, provided_password_length, auth_token, auth_token_length,
311 request_reenroll);
Andres Morales33dfdc72015-05-12 15:37:20 -0700312 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700313
Andres Moralesae242922015-05-18 09:26:19 -0700314 if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700315 // 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 Danisevskis72030fb2016-10-24 14:47:00 +0100320 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 Morales2d08dce2015-04-03 16:40:15 -0700323 }
324 } else {
325 ALOGE("Unable to communicate with KeyStore");
326 }
327 }
328
Andres Moralesae242922015-05-18 09:26:19 -0700329 if (ret == 0) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700330 maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>(
331 enrolled_password_handle)->user_id);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700332 }
333
Andres Moralesae242922015-05-18 09:26:19 -0700334 return ret;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700335 }
336
Pavel Grafov9890f892017-06-28 19:03:58 +0100337 virtual uint64_t getSecureUserId(uint32_t uid) { return read_sid(uid); }
Andres Morales2d08dce2015-04-03 16:40:15 -0700338
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700339 virtual void clearSecureUserId(uint32_t uid) {
340 IPCThreadState* ipc = IPCThreadState::self();
341 const int calling_pid = ipc->getCallingPid();
342 const int calling_uid = ipc->getCallingUid();
343 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
344 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
345 return;
346 }
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700347 clear_sid(uid);
Andres Morales3c2086d2015-06-24 10:21:16 -0700348
Alexey Polyudov275aece2016-10-18 13:59:26 -0700349 if (hw_device != nullptr) {
350 hw_device->deleteUser(uid, [] (const GatekeeperResponse &){});
Andres Morales3c2086d2015-06-24 10:21:16 -0700351 }
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700352 }
353
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700354 virtual void reportDeviceSetupComplete() {
355 IPCThreadState* ipc = IPCThreadState::self();
356 const int calling_pid = ipc->getCallingPid();
357 const int calling_uid = ipc->getCallingUid();
358 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
359 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
360 return;
361 }
362
363 clear_state_if_needed();
364 }
365
Andres Morales2d08dce2015-04-03 16:40:15 -0700366 virtual status_t dump(int fd, const Vector<String16> &) {
367 IPCThreadState* ipc = IPCThreadState::self();
368 const int pid = ipc->getCallingPid();
369 const int uid = ipc->getCallingUid();
370 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
371 return PERMISSION_DENIED;
372 }
373
Alexey Polyudov275aece2016-10-18 13:59:26 -0700374 if (hw_device == NULL) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700375 const char *result = "Device not available";
376 write(fd, result, strlen(result) + 1);
377 } else {
378 const char *result = "OK";
379 write(fd, result, strlen(result) + 1);
380 }
381
382 return NO_ERROR;
383 }
384
385private:
Alexey Polyudov275aece2016-10-18 13:59:26 -0700386 sp<IGatekeeper> hw_device;
Justin Yun68b0ec62017-08-16 18:54:20 +0900387 std::unique_ptr<SoftGateKeeperDevice> soft_device;
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700388
389 bool clear_state_if_needed_done;
Andres Morales2d08dce2015-04-03 16:40:15 -0700390};
391}// namespace android
392
Andres Morales6a49c2f2015-04-16 13:16:24 -0700393int main(int argc, char* argv[]) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700394 ALOGI("Starting gatekeeperd...");
Andres Morales6a49c2f2015-04-16 13:16:24 -0700395 if (argc < 2) {
396 ALOGE("A directory must be specified!");
397 return 1;
398 }
399 if (chdir(argv[1]) == -1) {
400 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
401 return 1;
402 }
403
Andres Morales2d08dce2015-04-03 16:40:15 -0700404 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
405 android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
406 android::status_t ret = sm->addService(
407 android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
408 if (ret != android::OK) {
409 ALOGE("Couldn't register binder service!");
410 return -1;
411 }
412
413 /*
414 * We're the only thread in existence, so we're just going to process
415 * Binder transaction as a single-threaded program.
416 */
417 android::IPCThreadState::self()->joinThreadPool();
418 return 0;
419}