blob: a10167f4e16a65d7cbde97196a1cb628c67f979c [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>
26
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070027#include <android/log.h>
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
36#include <utils/Log.h>
37#include <utils/String16.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070038
Andres Morales33dfdc72015-05-12 15:37:20 -070039#include "SoftGateKeeperDevice.h"
Andres Morales1cf7d252015-08-04 16:57:12 -070040#include "IUserManager.h"
Andres Morales33dfdc72015-05-12 15:37:20 -070041
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() {
Alexey Polyudov275aece2016-10-18 13:59:26 -070059 hw_device = IGatekeeper::getService("gatekeeper");
Andres Moralesfef908e2015-07-07 10:28:15 -070060
Alexey Polyudov275aece2016-10-18 13:59:26 -070061 if (hw_device == nullptr) {
Andres Morales33dfdc72015-05-12 15:37:20 -070062 ALOGW("falling back to software GateKeeper");
63 soft_device.reset(new SoftGateKeeperDevice());
Andres Morales33dfdc72015-05-12 15:37:20 -070064 }
Andres Morales3c2086d2015-06-24 10:21:16 -070065
66 if (mark_cold_boot()) {
67 ALOGI("cold boot: clearing state");
Alexey Polyudov275aece2016-10-18 13:59:26 -070068 if (hw_device != nullptr) {
69 hw_device->deleteAllUsers([](const GatekeeperResponse &){});
Andres Morales3c2086d2015-06-24 10:21:16 -070070 }
71 }
Andres Morales2d08dce2015-04-03 16:40:15 -070072 }
73
74 virtual ~GateKeeperProxy() {
Andres Morales2d08dce2015-04-03 16:40:15 -070075 }
76
Andres Morales6a49c2f2015-04-16 13:16:24 -070077 void store_sid(uint32_t uid, uint64_t sid) {
78 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -080079 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -070080 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
81 if (fd < 0) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -070082 ALOGE("could not open file: %s: %s", filename, strerror(errno));
Andres Morales6a49c2f2015-04-16 13:16:24 -070083 return;
84 }
85 write(fd, &sid, sizeof(sid));
86 close(fd);
87 }
88
Andres Morales3c2086d2015-06-24 10:21:16 -070089 bool mark_cold_boot() {
90 const char *filename = ".coldboot";
91 if (access(filename, F_OK) == -1) {
92 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
93 if (fd < 0) {
94 ALOGE("could not open file: %s : %s", filename, strerror(errno));
95 return false;
96 }
97 close(fd);
98 return true;
99 }
100 return false;
101 }
102
Andres Morales6a49c2f2015-04-16 13:16:24 -0700103 void maybe_store_sid(uint32_t uid, uint64_t sid) {
104 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800105 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700106 if (access(filename, F_OK) == -1) {
107 store_sid(uid, sid);
108 }
109 }
110
111 uint64_t read_sid(uint32_t uid) {
112 char filename[21];
113 uint64_t sid;
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800114 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700115 int fd = open(filename, O_RDONLY);
116 if (fd < 0) return 0;
117 read(fd, &sid, sizeof(sid));
Andres Morales0b0435e2015-07-10 09:47:09 -0700118 close(fd);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700119 return sid;
120 }
121
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700122 void clear_sid(uint32_t uid) {
123 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800124 snprintf(filename, sizeof(filename), "%u", uid);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700125 if (remove(filename) < 0) {
126 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
127 store_sid(uid, 0);
128 }
129 }
130
Andres Moralesae242922015-05-18 09:26:19 -0700131 virtual int enroll(uint32_t uid,
Andres Morales2d08dce2015-04-03 16:40:15 -0700132 const uint8_t *current_password_handle, uint32_t current_password_handle_length,
133 const uint8_t *current_password, uint32_t current_password_length,
134 const uint8_t *desired_password, uint32_t desired_password_length,
135 uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) {
136 IPCThreadState* ipc = IPCThreadState::self();
137 const int calling_pid = ipc->getCallingPid();
138 const int calling_uid = ipc->getCallingUid();
139 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
140 return PERMISSION_DENIED;
141 }
142
143 // need a desired password to enroll
144 if (desired_password_length == 0) return -EINVAL;
Andres Morales33dfdc72015-05-12 15:37:20 -0700145
146 int ret;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700147 if (hw_device != nullptr) {
Andres Morales835d96e2015-06-03 15:06:24 -0700148 const gatekeeper::password_handle_t *handle =
149 reinterpret_cast<const gatekeeper::password_handle_t *>(current_password_handle);
150
Andres Morales7f6dcf62015-06-24 18:40:24 -0700151 if (handle != NULL && handle->version != 0 && !handle->hardware_backed) {
Andres Morales835d96e2015-06-03 15:06:24 -0700152 // handle is being re-enrolled from a software version. HAL probably won't accept
153 // the handle as valid, so we nullify it and enroll from scratch
154 current_password_handle = NULL;
155 current_password_handle_length = 0;
156 current_password = NULL;
157 current_password_length = 0;
158 }
159
Alexey Polyudov275aece2016-10-18 13:59:26 -0700160 android::hardware::hidl_vec<uint8_t> curPwdHandle;
161 curPwdHandle.setToExternal(const_cast<uint8_t*>(current_password_handle),
162 current_password_handle_length);
163 android::hardware::hidl_vec<uint8_t> curPwd;
164 curPwd.setToExternal(const_cast<uint8_t*>(current_password),
165 current_password_length);
166 android::hardware::hidl_vec<uint8_t> newPwd;
167 newPwd.setToExternal(const_cast<uint8_t*>(desired_password),
168 desired_password_length);
169
170 Return<void> hwRes = hw_device->enroll(uid, curPwdHandle, curPwd, newPwd,
171 [&ret, enrolled_password_handle, enrolled_password_handle_length]
172 (const GatekeeperResponse &rsp) {
173 ret = static_cast<int>(rsp.code); // propagate errors
174 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
175 if (enrolled_password_handle != nullptr &&
176 enrolled_password_handle_length != nullptr) {
177 *enrolled_password_handle = new uint8_t[rsp.data.size()];
178 *enrolled_password_handle_length = rsp.data.size();
179 memcpy(*enrolled_password_handle, rsp.data.data(),
180 *enrolled_password_handle_length);
181 }
182 ret = 0; // all success states are reported as 0
183 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && rsp.timeout > 0) {
184 ret = rsp.timeout;
185 }
186 });
Steven Moreland81330932017-01-03 17:01:27 -0800187 if (!hwRes.isOk()) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700188 ALOGE("enroll transaction failed\n");
189 ret = -1;
190 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700191 } else {
192 ret = soft_device->enroll(uid,
193 current_password_handle, current_password_handle_length,
194 current_password, current_password_length,
195 desired_password, desired_password_length,
196 enrolled_password_handle, enrolled_password_handle_length);
197 }
198
Andres Moralesae242922015-05-18 09:26:19 -0700199 if (ret == 0) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700200 gatekeeper::password_handle_t *handle =
201 reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle);
202 store_sid(uid, handle->user_id);
Andres Morales531e3e82015-06-01 17:23:04 -0700203 bool rr;
204
205 // immediately verify this password so we don't ask the user to enter it again
206 // if they just created it.
207 verify(uid, *enrolled_password_handle, sizeof(password_handle_t), desired_password,
208 desired_password_length, &rr);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700209 }
Andres Moralesae242922015-05-18 09:26:19 -0700210
211 return ret;
Andres Morales2d08dce2015-04-03 16:40:15 -0700212 }
213
Andres Moralesae242922015-05-18 09:26:19 -0700214 virtual int verify(uint32_t uid,
Andres Morales2d08dce2015-04-03 16:40:15 -0700215 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
Andres Moralesae242922015-05-18 09:26:19 -0700216 const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) {
Andres Moralesc828ae82015-04-10 21:03:07 -0700217 uint8_t *auth_token;
218 uint32_t auth_token_length;
219 return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length,
220 provided_password, provided_password_length,
Andres Moralesae242922015-05-18 09:26:19 -0700221 &auth_token, &auth_token_length, request_reenroll);
Andres Moralesc828ae82015-04-10 21:03:07 -0700222 }
223
Andres Moralesae242922015-05-18 09:26:19 -0700224 virtual int verifyChallenge(uint32_t uid, uint64_t challenge,
Andres Moralesc828ae82015-04-10 21:03:07 -0700225 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
226 const uint8_t *provided_password, uint32_t provided_password_length,
Andres Moralesae242922015-05-18 09:26:19 -0700227 uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700228 IPCThreadState* ipc = IPCThreadState::self();
229 const int calling_pid = ipc->getCallingPid();
230 const int calling_uid = ipc->getCallingUid();
231 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
232 return PERMISSION_DENIED;
233 }
234
235 // can't verify if we're missing either param
236 if ((enrolled_password_handle_length | provided_password_length) == 0)
237 return -EINVAL;
238
Andres Morales33dfdc72015-05-12 15:37:20 -0700239 int ret;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700240 if (hw_device != nullptr) {
Andres Morales835d96e2015-06-03 15:06:24 -0700241 const gatekeeper::password_handle_t *handle =
242 reinterpret_cast<const gatekeeper::password_handle_t *>(enrolled_password_handle);
Andres Morales7f6dcf62015-06-24 18:40:24 -0700243 // handle version 0 does not have hardware backed flag, and thus cannot be upgraded to
244 // a HAL if there was none before
245 if (handle->version == 0 || handle->hardware_backed) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700246 android::hardware::hidl_vec<uint8_t> curPwdHandle;
247 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolled_password_handle),
248 enrolled_password_handle_length);
249 android::hardware::hidl_vec<uint8_t> enteredPwd;
250 enteredPwd.setToExternal(const_cast<uint8_t*>(provided_password),
251 provided_password_length);
252 Return<void> hwRes = hw_device->verify(uid, challenge, curPwdHandle, enteredPwd,
253 [&ret, request_reenroll, auth_token, auth_token_length]
254 (const GatekeeperResponse &rsp) {
255 ret = static_cast<int>(rsp.code); // propagate errors
256 if (auth_token != nullptr && auth_token_length != nullptr &&
257 rsp.code >= GatekeeperStatusCode::STATUS_OK) {
258 *auth_token = new uint8_t[rsp.data.size()];
259 *auth_token_length = rsp.data.size();
260 memcpy(*auth_token, rsp.data.data(), *auth_token_length);
261 if (request_reenroll != nullptr) {
262 *request_reenroll = (rsp.code == GatekeeperStatusCode::STATUS_REENROLL);
263 }
264 ret = 0; // all success states are reported as 0
265 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
266 rsp.timeout > 0) {
267 ret = rsp.timeout;
268 }
269 });
Steven Moreland81330932017-01-03 17:01:27 -0800270 if (!hwRes.isOk()) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700271 ALOGE("verify transaction failed\n");
272 ret = -1;
273 }
Andres Morales835d96e2015-06-03 15:06:24 -0700274 } else {
275 // upgrade scenario, a HAL has been added to this device where there was none before
276 SoftGateKeeperDevice soft_dev;
277 ret = soft_dev.verify(uid, challenge,
278 enrolled_password_handle, enrolled_password_handle_length,
279 provided_password, provided_password_length, auth_token, auth_token_length,
280 request_reenroll);
281
282 if (ret == 0) {
283 // success! re-enroll with HAL
284 *request_reenroll = true;
285 }
286 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700287 } else {
288 ret = soft_device->verify(uid, challenge,
289 enrolled_password_handle, enrolled_password_handle_length,
Andres Moralesae242922015-05-18 09:26:19 -0700290 provided_password, provided_password_length, auth_token, auth_token_length,
291 request_reenroll);
Andres Morales33dfdc72015-05-12 15:37:20 -0700292 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700293
Andres Moralesae242922015-05-18 09:26:19 -0700294 if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700295 // TODO: cache service?
296 sp<IServiceManager> sm = defaultServiceManager();
297 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
298 sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
299 if (service != NULL) {
Andres Morales2ae8b4c2015-04-13 09:20:09 -0700300 status_t ret = service->addAuthToken(*auth_token, *auth_token_length);
301 if (ret != ResponseCode::NO_ERROR) {
302 ALOGE("Falure sending auth token to KeyStore: %d", ret);
Andres Morales2d08dce2015-04-03 16:40:15 -0700303 }
304 } else {
305 ALOGE("Unable to communicate with KeyStore");
306 }
307 }
308
Andres Moralesae242922015-05-18 09:26:19 -0700309 if (ret == 0) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700310 maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>(
311 enrolled_password_handle)->user_id);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700312 }
313
Andres Moralesae242922015-05-18 09:26:19 -0700314 return ret;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700315 }
316
317 virtual uint64_t getSecureUserId(uint32_t uid) {
Andres Morales1cf7d252015-08-04 16:57:12 -0700318 uint64_t sid = read_sid(uid);
319 if (sid == 0) {
320 // might be a work profile, look up the parent
321 sp<IServiceManager> sm = defaultServiceManager();
322 sp<IBinder> binder = sm->getService(String16("user"));
323 sp<IUserManager> um = interface_cast<IUserManager>(binder);
324 int32_t parent = um->getCredentialOwnerProfile(uid);
325 if (parent < 0) {
326 return 0;
327 } else if (parent != (int32_t) uid) {
328 return read_sid(parent);
329 }
330 }
331 return sid;
332
Andres Morales2d08dce2015-04-03 16:40:15 -0700333 }
334
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700335 virtual void clearSecureUserId(uint32_t uid) {
336 IPCThreadState* ipc = IPCThreadState::self();
337 const int calling_pid = ipc->getCallingPid();
338 const int calling_uid = ipc->getCallingUid();
339 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
340 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
341 return;
342 }
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700343 clear_sid(uid);
Andres Morales3c2086d2015-06-24 10:21:16 -0700344
Alexey Polyudov275aece2016-10-18 13:59:26 -0700345 if (hw_device != nullptr) {
346 hw_device->deleteUser(uid, [] (const GatekeeperResponse &){});
Andres Morales3c2086d2015-06-24 10:21:16 -0700347 }
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700348 }
349
Andres Morales2d08dce2015-04-03 16:40:15 -0700350 virtual status_t dump(int fd, const Vector<String16> &) {
351 IPCThreadState* ipc = IPCThreadState::self();
352 const int pid = ipc->getCallingPid();
353 const int uid = ipc->getCallingUid();
354 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
355 return PERMISSION_DENIED;
356 }
357
Alexey Polyudov275aece2016-10-18 13:59:26 -0700358 if (hw_device == NULL) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700359 const char *result = "Device not available";
360 write(fd, result, strlen(result) + 1);
361 } else {
362 const char *result = "OK";
363 write(fd, result, strlen(result) + 1);
364 }
365
366 return NO_ERROR;
367 }
368
369private:
Alexey Polyudov275aece2016-10-18 13:59:26 -0700370 sp<IGatekeeper> hw_device;
Andres Morales33dfdc72015-05-12 15:37:20 -0700371 UniquePtr<SoftGateKeeperDevice> soft_device;
Andres Morales2d08dce2015-04-03 16:40:15 -0700372};
373}// namespace android
374
Andres Morales6a49c2f2015-04-16 13:16:24 -0700375int main(int argc, char* argv[]) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700376 ALOGI("Starting gatekeeperd...");
Andres Morales6a49c2f2015-04-16 13:16:24 -0700377 if (argc < 2) {
378 ALOGE("A directory must be specified!");
379 return 1;
380 }
381 if (chdir(argv[1]) == -1) {
382 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
383 return 1;
384 }
385
Andres Morales2d08dce2015-04-03 16:40:15 -0700386 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
387 android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
388 android::status_t ret = sm->addService(
389 android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
390 if (ret != android::OK) {
391 ALOGE("Couldn't register binder service!");
392 return -1;
393 }
394
395 /*
396 * We're the only thread in existence, so we're just going to process
397 * Binder transaction as a single-threaded program.
398 */
399 android::IPCThreadState::self()->joinThreadPool();
400 return 0;
401}