blob: 184c6d222056e84a4efb34731cc214e42f6f4f3a [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
Andres Morales2d08dce2015-04-03 16:40:15 -070027#include <binder/IPCThreadState.h>
28#include <binder/IServiceManager.h>
29#include <binder/PermissionCache.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070030#include <gatekeeper/password_handle.h> // for password_handle_t
Andres Morales2d08dce2015-04-03 16:40:15 -070031#include <hardware/gatekeeper.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070032#include <hardware/hw_auth_token.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070033#include <keystore/IKeystoreService.h>
34#include <keystore/keystore.h> // For error code
Mark Salyzyn30f991f2017-01-10 13:19:54 -080035#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070036#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"
40
Alexey Polyudov275aece2016-10-18 13:59:26 -070041#include <hidl/HidlSupport.h>
42#include <android/hardware/gatekeeper/1.0/IGatekeeper.h>
43
44using android::sp;
45using android::hardware::gatekeeper::V1_0::IGatekeeper;
46using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode;
47using android::hardware::gatekeeper::V1_0::GatekeeperResponse;
48using android::hardware::Return;
49
Andres Morales2d08dce2015-04-03 16:40:15 -070050namespace android {
51
52static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
53static const String16 DUMP_PERMISSION("android.permission.DUMP");
54
55class GateKeeperProxy : public BnGateKeeperService {
56public:
57 GateKeeperProxy() {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070058 clear_state_if_needed_done = false;
Chris Phoenixa84ce0c2017-01-24 13:09:39 -080059 hw_device = IGatekeeper::getService();
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 Morales2d08dce2015-04-03 16:40:15 -070065 }
66
67 virtual ~GateKeeperProxy() {
Andres Morales2d08dce2015-04-03 16:40:15 -070068 }
69
Andres Morales6a49c2f2015-04-16 13:16:24 -070070 void store_sid(uint32_t uid, uint64_t sid) {
71 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -080072 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -070073 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
74 if (fd < 0) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -070075 ALOGE("could not open file: %s: %s", filename, strerror(errno));
Andres Morales6a49c2f2015-04-16 13:16:24 -070076 return;
77 }
78 write(fd, &sid, sizeof(sid));
79 close(fd);
80 }
81
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070082 void clear_state_if_needed() {
83 if (clear_state_if_needed_done) {
84 return;
85 }
86
87 if (mark_cold_boot()) {
88 ALOGI("cold boot: clearing state");
89 if (hw_device != nullptr) {
90 hw_device->deleteAllUsers([](const GatekeeperResponse &){});
91 }
92 }
93
94 clear_state_if_needed_done = true;
95 }
96
Andres Morales3c2086d2015-06-24 10:21:16 -070097 bool mark_cold_boot() {
98 const char *filename = ".coldboot";
99 if (access(filename, F_OK) == -1) {
100 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
101 if (fd < 0) {
102 ALOGE("could not open file: %s : %s", filename, strerror(errno));
103 return false;
104 }
105 close(fd);
106 return true;
107 }
108 return false;
109 }
110
Andres Morales6a49c2f2015-04-16 13:16:24 -0700111 void maybe_store_sid(uint32_t uid, uint64_t sid) {
112 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800113 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700114 if (access(filename, F_OK) == -1) {
115 store_sid(uid, sid);
116 }
117 }
118
119 uint64_t read_sid(uint32_t uid) {
120 char filename[21];
121 uint64_t sid;
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800122 snprintf(filename, sizeof(filename), "%u", uid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700123 int fd = open(filename, O_RDONLY);
124 if (fd < 0) return 0;
125 read(fd, &sid, sizeof(sid));
Andres Morales0b0435e2015-07-10 09:47:09 -0700126 close(fd);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700127 return sid;
128 }
129
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700130 void clear_sid(uint32_t uid) {
131 char filename[21];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800132 snprintf(filename, sizeof(filename), "%u", uid);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700133 if (remove(filename) < 0) {
134 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
135 store_sid(uid, 0);
136 }
137 }
138
Andres Moralesae242922015-05-18 09:26:19 -0700139 virtual int enroll(uint32_t uid,
Andres Morales2d08dce2015-04-03 16:40:15 -0700140 const uint8_t *current_password_handle, uint32_t current_password_handle_length,
141 const uint8_t *current_password, uint32_t current_password_length,
142 const uint8_t *desired_password, uint32_t desired_password_length,
143 uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) {
144 IPCThreadState* ipc = IPCThreadState::self();
145 const int calling_pid = ipc->getCallingPid();
146 const int calling_uid = ipc->getCallingUid();
147 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
148 return PERMISSION_DENIED;
149 }
150
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700151 // Make sure to clear any state from before factory reset as soon as a credential is
152 // enrolled (which may happen during device setup).
153 clear_state_if_needed();
154
Andres Morales2d08dce2015-04-03 16:40:15 -0700155 // need a desired password to enroll
156 if (desired_password_length == 0) return -EINVAL;
Andres Morales33dfdc72015-05-12 15:37:20 -0700157
158 int ret;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700159 if (hw_device != nullptr) {
Andres Morales835d96e2015-06-03 15:06:24 -0700160 const gatekeeper::password_handle_t *handle =
161 reinterpret_cast<const gatekeeper::password_handle_t *>(current_password_handle);
162
Andres Morales7f6dcf62015-06-24 18:40:24 -0700163 if (handle != NULL && handle->version != 0 && !handle->hardware_backed) {
Andres Morales835d96e2015-06-03 15:06:24 -0700164 // handle is being re-enrolled from a software version. HAL probably won't accept
165 // the handle as valid, so we nullify it and enroll from scratch
166 current_password_handle = NULL;
167 current_password_handle_length = 0;
168 current_password = NULL;
169 current_password_length = 0;
170 }
171
Alexey Polyudov275aece2016-10-18 13:59:26 -0700172 android::hardware::hidl_vec<uint8_t> curPwdHandle;
173 curPwdHandle.setToExternal(const_cast<uint8_t*>(current_password_handle),
174 current_password_handle_length);
175 android::hardware::hidl_vec<uint8_t> curPwd;
176 curPwd.setToExternal(const_cast<uint8_t*>(current_password),
177 current_password_length);
178 android::hardware::hidl_vec<uint8_t> newPwd;
179 newPwd.setToExternal(const_cast<uint8_t*>(desired_password),
180 desired_password_length);
181
182 Return<void> hwRes = hw_device->enroll(uid, curPwdHandle, curPwd, newPwd,
183 [&ret, enrolled_password_handle, enrolled_password_handle_length]
184 (const GatekeeperResponse &rsp) {
185 ret = static_cast<int>(rsp.code); // propagate errors
186 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
187 if (enrolled_password_handle != nullptr &&
188 enrolled_password_handle_length != nullptr) {
189 *enrolled_password_handle = new uint8_t[rsp.data.size()];
190 *enrolled_password_handle_length = rsp.data.size();
191 memcpy(*enrolled_password_handle, rsp.data.data(),
192 *enrolled_password_handle_length);
193 }
194 ret = 0; // all success states are reported as 0
195 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && rsp.timeout > 0) {
196 ret = rsp.timeout;
197 }
198 });
Steven Moreland81330932017-01-03 17:01:27 -0800199 if (!hwRes.isOk()) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700200 ALOGE("enroll transaction failed\n");
201 ret = -1;
202 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700203 } else {
204 ret = soft_device->enroll(uid,
205 current_password_handle, current_password_handle_length,
206 current_password, current_password_length,
207 desired_password, desired_password_length,
208 enrolled_password_handle, enrolled_password_handle_length);
209 }
210
Alexey Polyudov8c635362016-09-07 18:51:28 -0700211 if (ret == GATEKEEPER_RESPONSE_OK && (*enrolled_password_handle == nullptr ||
212 *enrolled_password_handle_length != sizeof(password_handle_t))) {
213 ret = GATEKEEPER_RESPONSE_ERROR;
214 ALOGE("HAL: password_handle=%p size_of_handle=%" PRIu32 "\n",
215 *enrolled_password_handle, *enrolled_password_handle_length);
216 }
217
218 if (ret == GATEKEEPER_RESPONSE_OK) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700219 gatekeeper::password_handle_t *handle =
220 reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle);
221 store_sid(uid, handle->user_id);
Andres Morales531e3e82015-06-01 17:23:04 -0700222 bool rr;
223
224 // immediately verify this password so we don't ask the user to enter it again
225 // if they just created it.
226 verify(uid, *enrolled_password_handle, sizeof(password_handle_t), desired_password,
227 desired_password_length, &rr);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700228 }
Andres Moralesae242922015-05-18 09:26:19 -0700229
230 return ret;
Andres Morales2d08dce2015-04-03 16:40:15 -0700231 }
232
Andres Moralesae242922015-05-18 09:26:19 -0700233 virtual int verify(uint32_t uid,
Andres Morales2d08dce2015-04-03 16:40:15 -0700234 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
Andres Moralesae242922015-05-18 09:26:19 -0700235 const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) {
Andres Moralesc828ae82015-04-10 21:03:07 -0700236 uint8_t *auth_token;
237 uint32_t auth_token_length;
238 return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length,
239 provided_password, provided_password_length,
Andres Moralesae242922015-05-18 09:26:19 -0700240 &auth_token, &auth_token_length, request_reenroll);
Andres Moralesc828ae82015-04-10 21:03:07 -0700241 }
242
Andres Moralesae242922015-05-18 09:26:19 -0700243 virtual int verifyChallenge(uint32_t uid, uint64_t challenge,
Andres Moralesc828ae82015-04-10 21:03:07 -0700244 const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
245 const uint8_t *provided_password, uint32_t provided_password_length,
Andres Moralesae242922015-05-18 09:26:19 -0700246 uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700247 IPCThreadState* ipc = IPCThreadState::self();
248 const int calling_pid = ipc->getCallingPid();
249 const int calling_uid = ipc->getCallingUid();
250 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
251 return PERMISSION_DENIED;
252 }
253
254 // can't verify if we're missing either param
255 if ((enrolled_password_handle_length | provided_password_length) == 0)
256 return -EINVAL;
257
Andres Morales33dfdc72015-05-12 15:37:20 -0700258 int ret;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700259 if (hw_device != nullptr) {
Andres Morales835d96e2015-06-03 15:06:24 -0700260 const gatekeeper::password_handle_t *handle =
261 reinterpret_cast<const gatekeeper::password_handle_t *>(enrolled_password_handle);
Andres Morales7f6dcf62015-06-24 18:40:24 -0700262 // handle version 0 does not have hardware backed flag, and thus cannot be upgraded to
263 // a HAL if there was none before
264 if (handle->version == 0 || handle->hardware_backed) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700265 android::hardware::hidl_vec<uint8_t> curPwdHandle;
266 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolled_password_handle),
267 enrolled_password_handle_length);
268 android::hardware::hidl_vec<uint8_t> enteredPwd;
269 enteredPwd.setToExternal(const_cast<uint8_t*>(provided_password),
270 provided_password_length);
271 Return<void> hwRes = hw_device->verify(uid, challenge, curPwdHandle, enteredPwd,
272 [&ret, request_reenroll, auth_token, auth_token_length]
273 (const GatekeeperResponse &rsp) {
274 ret = static_cast<int>(rsp.code); // propagate errors
275 if (auth_token != nullptr && auth_token_length != nullptr &&
276 rsp.code >= GatekeeperStatusCode::STATUS_OK) {
277 *auth_token = new uint8_t[rsp.data.size()];
278 *auth_token_length = rsp.data.size();
279 memcpy(*auth_token, rsp.data.data(), *auth_token_length);
280 if (request_reenroll != nullptr) {
281 *request_reenroll = (rsp.code == GatekeeperStatusCode::STATUS_REENROLL);
282 }
283 ret = 0; // all success states are reported as 0
284 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
285 rsp.timeout > 0) {
286 ret = rsp.timeout;
287 }
288 });
Steven Moreland81330932017-01-03 17:01:27 -0800289 if (!hwRes.isOk()) {
Alexey Polyudov275aece2016-10-18 13:59:26 -0700290 ALOGE("verify transaction failed\n");
291 ret = -1;
292 }
Andres Morales835d96e2015-06-03 15:06:24 -0700293 } else {
294 // upgrade scenario, a HAL has been added to this device where there was none before
295 SoftGateKeeperDevice soft_dev;
296 ret = soft_dev.verify(uid, challenge,
297 enrolled_password_handle, enrolled_password_handle_length,
298 provided_password, provided_password_length, auth_token, auth_token_length,
299 request_reenroll);
300
301 if (ret == 0) {
302 // success! re-enroll with HAL
303 *request_reenroll = true;
304 }
305 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700306 } else {
307 ret = soft_device->verify(uid, challenge,
308 enrolled_password_handle, enrolled_password_handle_length,
Andres Moralesae242922015-05-18 09:26:19 -0700309 provided_password, provided_password_length, auth_token, auth_token_length,
310 request_reenroll);
Andres Morales33dfdc72015-05-12 15:37:20 -0700311 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700312
Andres Moralesae242922015-05-18 09:26:19 -0700313 if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700314 // TODO: cache service?
315 sp<IServiceManager> sm = defaultServiceManager();
316 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
317 sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
318 if (service != NULL) {
Janis Danisevskis72030fb2016-10-24 14:47:00 +0100319 auto ret = service->addAuthToken(*auth_token, *auth_token_length);
320 if (!ret.isOk()) {
321 ALOGE("Failure sending auth token to KeyStore: %" PRId32, int32_t(ret));
Andres Morales2d08dce2015-04-03 16:40:15 -0700322 }
323 } else {
324 ALOGE("Unable to communicate with KeyStore");
325 }
326 }
327
Andres Moralesae242922015-05-18 09:26:19 -0700328 if (ret == 0) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700329 maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>(
330 enrolled_password_handle)->user_id);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700331 }
332
Andres Moralesae242922015-05-18 09:26:19 -0700333 return ret;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700334 }
335
Pavel Grafov9890f892017-06-28 19:03:58 +0100336 virtual uint64_t getSecureUserId(uint32_t uid) { return read_sid(uid); }
Andres Morales2d08dce2015-04-03 16:40:15 -0700337
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700338 virtual void clearSecureUserId(uint32_t uid) {
339 IPCThreadState* ipc = IPCThreadState::self();
340 const int calling_pid = ipc->getCallingPid();
341 const int calling_uid = ipc->getCallingUid();
342 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
343 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
344 return;
345 }
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700346 clear_sid(uid);
Andres Morales3c2086d2015-06-24 10:21:16 -0700347
Alexey Polyudov275aece2016-10-18 13:59:26 -0700348 if (hw_device != nullptr) {
349 hw_device->deleteUser(uid, [] (const GatekeeperResponse &){});
Andres Morales3c2086d2015-06-24 10:21:16 -0700350 }
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700351 }
352
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700353 virtual void reportDeviceSetupComplete() {
354 IPCThreadState* ipc = IPCThreadState::self();
355 const int calling_pid = ipc->getCallingPid();
356 const int calling_uid = ipc->getCallingUid();
357 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
358 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
359 return;
360 }
361
362 clear_state_if_needed();
363 }
364
Andres Morales2d08dce2015-04-03 16:40:15 -0700365 virtual status_t dump(int fd, const Vector<String16> &) {
366 IPCThreadState* ipc = IPCThreadState::self();
367 const int pid = ipc->getCallingPid();
368 const int uid = ipc->getCallingUid();
369 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
370 return PERMISSION_DENIED;
371 }
372
Alexey Polyudov275aece2016-10-18 13:59:26 -0700373 if (hw_device == NULL) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700374 const char *result = "Device not available";
375 write(fd, result, strlen(result) + 1);
376 } else {
377 const char *result = "OK";
378 write(fd, result, strlen(result) + 1);
379 }
380
381 return NO_ERROR;
382 }
383
384private:
Alexey Polyudov275aece2016-10-18 13:59:26 -0700385 sp<IGatekeeper> hw_device;
Andres Morales33dfdc72015-05-12 15:37:20 -0700386 UniquePtr<SoftGateKeeperDevice> soft_device;
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700387
388 bool clear_state_if_needed_done;
Andres Morales2d08dce2015-04-03 16:40:15 -0700389};
390}// namespace android
391
Andres Morales6a49c2f2015-04-16 13:16:24 -0700392int main(int argc, char* argv[]) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700393 ALOGI("Starting gatekeeperd...");
Andres Morales6a49c2f2015-04-16 13:16:24 -0700394 if (argc < 2) {
395 ALOGE("A directory must be specified!");
396 return 1;
397 }
398 if (chdir(argv[1]) == -1) {
399 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
400 return 1;
401 }
402
Andres Morales2d08dce2015-04-03 16:40:15 -0700403 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
404 android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
405 android::status_t ret = sm->addService(
406 android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
407 if (ret != android::OK) {
408 ALOGE("Couldn't register binder service!");
409 return -1;
410 }
411
412 /*
413 * We're the only thread in existence, so we're just going to process
414 * Binder transaction as a single-threaded program.
415 */
416 android::IPCThreadState::self()->joinThreadPool();
417 return 0;
418}