blob: 941f8c243e56e510260b055358dafc1a88618223 [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
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070019#include <android/service/gatekeeper/BnGateKeeperService.h>
20#include <gatekeeper/GateKeeperResponse.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070021
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000022#include <endian.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070023#include <errno.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070024#include <fcntl.h>
25#include <unistd.h>
Justin Yun68b0ec62017-08-16 18:54:20 +090026#include <memory>
Andres Morales6a49c2f2015-04-16 13:16:24 -070027
David Anderson97400bd2019-02-15 15:59:39 -080028#include <android-base/logging.h>
29#include <android-base/properties.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000030#include <android/binder_ibinder.h>
31#include <android/binder_manager.h>
32#include <android/security/keystore/IKeystoreService.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070033#include <binder/IPCThreadState.h>
34#include <binder/IServiceManager.h>
35#include <binder/PermissionCache.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000036#include <gatekeeper/password_handle.h> // for password_handle_t
Andres Morales6a49c2f2015-04-16 13:16:24 -070037#include <hardware/hw_auth_token.h>
Dmitry Dementyev0dd259c2017-11-14 12:53:39 -080038#include <keystore/keystore_return_types.h>
David Anderson97400bd2019-02-15 15:59:39 -080039#include <libgsi/libgsi.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080040#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070041#include <utils/String16.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070042
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000043#include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
44#include <aidl/android/security/authorization/IKeystoreAuthorization.h>
Louis Chang4c66b8a2021-01-18 10:01:12 +000045#include <android/hardware/gatekeeper/1.0/IGatekeeper.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000046#include <hidl/HidlSupport.h>
Alexey Polyudov275aece2016-10-18 13:59:26 -070047
48using android::sp;
Louis Chang4c66b8a2021-01-18 10:01:12 +000049using android::hardware::Return;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000050using android::hardware::gatekeeper::V1_0::GatekeeperResponse;
51using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode;
52using android::hardware::gatekeeper::V1_0::IGatekeeper;
Alexey Polyudov275aece2016-10-18 13:59:26 -070053
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070054using ::android::binder::Status;
55using ::android::service::gatekeeper::BnGateKeeperService;
56using GKResponse = ::android::service::gatekeeper::GateKeeperResponse;
57using GKResponseCode = ::android::service::gatekeeper::ResponseCode;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000058using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType;
59using ::aidl::android::hardware::security::keymint::HardwareAuthToken;
60using ::aidl::android::security::authorization::IKeystoreAuthorization;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070061
Andres Morales2d08dce2015-04-03 16:40:15 -070062namespace android {
63
64static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
65static const String16 DUMP_PERMISSION("android.permission.DUMP");
66
67class GateKeeperProxy : public BnGateKeeperService {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000068 public:
Andres Morales2d08dce2015-04-03 16:40:15 -070069 GateKeeperProxy() {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070070 clear_state_if_needed_done = false;
Chris Phoenixa84ce0c2017-01-24 13:09:39 -080071 hw_device = IGatekeeper::getService();
David Anderson97400bd2019-02-15 15:59:39 -080072 is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false);
Andres Moralesfef908e2015-07-07 10:28:15 -070073
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070074 if (!hw_device) {
75 LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad.";
Andres Morales33dfdc72015-05-12 15:37:20 -070076 }
Andres Morales2d08dce2015-04-03 16:40:15 -070077 }
78
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000079 virtual ~GateKeeperProxy() {}
Andres Morales2d08dce2015-04-03 16:40:15 -070080
Janis Danisevskis0a738d92020-09-23 17:00:20 -070081 void store_sid(uint32_t userId, uint64_t sid) {
Andres Morales6a49c2f2015-04-16 13:16:24 -070082 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -070083 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -070084 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
85 if (fd < 0) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -070086 ALOGE("could not open file: %s: %s", filename, strerror(errno));
Andres Morales6a49c2f2015-04-16 13:16:24 -070087 return;
88 }
89 write(fd, &sid, sizeof(sid));
90 close(fd);
91 }
92
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070093 void clear_state_if_needed() {
94 if (clear_state_if_needed_done) {
95 return;
96 }
97
David Anderson97400bd2019-02-15 15:59:39 -080098 if (mark_cold_boot() && !is_running_gsi) {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070099 ALOGI("cold boot: clearing state");
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700100 if (hw_device) {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000101 hw_device->deleteAllUsers([](const GatekeeperResponse&) {});
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700102 }
103 }
104
105 clear_state_if_needed_done = true;
106 }
107
Andres Morales3c2086d2015-06-24 10:21:16 -0700108 bool mark_cold_boot() {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000109 const char* filename = ".coldboot";
Andres Morales3c2086d2015-06-24 10:21:16 -0700110 if (access(filename, F_OK) == -1) {
111 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
112 if (fd < 0) {
113 ALOGE("could not open file: %s : %s", filename, strerror(errno));
114 return false;
115 }
116 close(fd);
117 return true;
118 }
119 return false;
120 }
121
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700122 void maybe_store_sid(uint32_t userId, uint64_t sid) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700123 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700124 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700125 if (access(filename, F_OK) == -1) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700126 store_sid(userId, sid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700127 }
128 }
129
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700130 uint64_t read_sid(uint32_t userId) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700131 char filename[21];
132 uint64_t sid;
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700133 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700134 int fd = open(filename, O_RDONLY);
135 if (fd < 0) return 0;
136 read(fd, &sid, sizeof(sid));
Andres Morales0b0435e2015-07-10 09:47:09 -0700137 close(fd);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700138 return sid;
139 }
140
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700141 void clear_sid(uint32_t userId) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700142 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700143 snprintf(filename, sizeof(filename), "%u", userId);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700144 if (remove(filename) < 0) {
145 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700146 store_sid(userId, 0);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700147 }
148 }
149
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700150 // This should only be called on userIds being passed to the GateKeeper HAL. It ensures that
David Anderson97400bd2019-02-15 15:59:39 -0800151 // secure storage shared across a GSI image and a host image will not overlap.
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700152 uint32_t adjust_userId(uint32_t userId) {
David Anderson97400bd2019-02-15 15:59:39 -0800153 static constexpr uint32_t kGsiOffset = 1000000;
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700154 CHECK(userId < kGsiOffset);
David Anderson97400bd2019-02-15 15:59:39 -0800155 CHECK(hw_device != nullptr);
156 if (is_running_gsi) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700157 return userId + kGsiOffset;
David Anderson97400bd2019-02-15 15:59:39 -0800158 }
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700159 return userId;
David Anderson97400bd2019-02-15 15:59:39 -0800160 }
161
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700162#define GK_ERROR *gkResponse = GKResponse::error(), Status::ok()
163
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700164 Status enroll(int32_t userId, const std::optional<std::vector<uint8_t>>& currentPasswordHandle,
Jooyung Han57110a42020-01-23 13:28:36 +0900165 const std::optional<std::vector<uint8_t>>& currentPassword,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700166 const std::vector<uint8_t>& desiredPassword, GKResponse* gkResponse) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700167 IPCThreadState* ipc = IPCThreadState::self();
168 const int calling_pid = ipc->getCallingPid();
169 const int calling_uid = ipc->getCallingUid();
170 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700171 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700172 }
173
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700174 // Make sure to clear any state from before factory reset as soon as a credential is
175 // enrolled (which may happen during device setup).
176 clear_state_if_needed();
177
Andres Morales2d08dce2015-04-03 16:40:15 -0700178 // need a desired password to enroll
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700179 if (desiredPassword.size() == 0) return GK_ERROR;
Andres Morales33dfdc72015-05-12 15:37:20 -0700180
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700181 if (!hw_device) {
182 LOG(ERROR) << "has no HAL to talk to";
183 return GK_ERROR;
184 }
Andres Morales835d96e2015-06-03 15:06:24 -0700185
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700186 android::hardware::hidl_vec<uint8_t> curPwdHandle;
187 android::hardware::hidl_vec<uint8_t> curPwd;
188
189 if (currentPasswordHandle && currentPassword) {
190 if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) {
191 LOG(INFO) << "Password handle has wrong length";
192 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700193 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700194 curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()),
195 currentPasswordHandle->size());
196 curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()),
197 currentPassword->size());
198 }
Andres Morales835d96e2015-06-03 15:06:24 -0700199
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700200 android::hardware::hidl_vec<uint8_t> newPwd;
201 newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size());
Alexey Polyudov275aece2016-10-18 13:59:26 -0700202
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700203 uint32_t hw_userId = adjust_userId(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700204 Return<void> hwRes = hw_device->enroll(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700205 hw_userId, curPwdHandle, curPwd, newPwd,
206 [&gkResponse](const GatekeeperResponse& rsp) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700207 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
208 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
209 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
210 rsp.timeout > 0) {
211 *gkResponse = GKResponse::retry(rsp.timeout);
212 } else {
213 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700214 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700215 });
216 if (!hwRes.isOk()) {
217 LOG(ERROR) << "enroll transaction failed";
218 return GK_ERROR;
219 }
220
221 if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) {
222 if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) {
223 LOG(ERROR) << "HAL returned password handle of invalid length "
224 << gkResponse->payload().size();
225 return GK_ERROR;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700226 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700227
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700228 const gatekeeper::password_handle_t* handle =
229 reinterpret_cast<const gatekeeper::password_handle_t*>(
230 gkResponse->payload().data());
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700231 store_sid(userId, handle->user_id);
Andres Morales531e3e82015-06-01 17:23:04 -0700232
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700233 GKResponse verifyResponse;
Andres Morales531e3e82015-06-01 17:23:04 -0700234 // immediately verify this password so we don't ask the user to enter it again
235 // if they just created it.
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700236 auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700237 if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) {
238 LOG(ERROR) << "Failed to verify password after enrolling";
239 }
Andres Morales6a49c2f2015-04-16 13:16:24 -0700240 }
Andres Moralesae242922015-05-18 09:26:19 -0700241
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700242 return Status::ok();
Andres Morales2d08dce2015-04-03 16:40:15 -0700243 }
244
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700245 Status verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700246 const ::std::vector<uint8_t>& providedPassword, GKResponse* gkResponse) override {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700247 return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700248 gkResponse);
Andres Moralesc828ae82015-04-10 21:03:07 -0700249 }
250
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700251 Status verifyChallenge(int32_t userId, int64_t challenge,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700252 const std::vector<uint8_t>& enrolledPasswordHandle,
253 const std::vector<uint8_t>& providedPassword,
254 GKResponse* gkResponse) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700255 IPCThreadState* ipc = IPCThreadState::self();
256 const int calling_pid = ipc->getCallingPid();
257 const int calling_uid = ipc->getCallingUid();
258 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700259 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700260 }
261
262 // can't verify if we're missing either param
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700263 if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700264
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700265 if (!hw_device) return GK_ERROR;
266
267 if (enrolledPasswordHandle.size() != sizeof(gatekeeper::password_handle_t)) {
268 LOG(INFO) << "Password handle has wrong length";
269 return GK_ERROR;
270 }
271 const gatekeeper::password_handle_t* handle =
272 reinterpret_cast<const gatekeeper::password_handle_t*>(
273 enrolledPasswordHandle.data());
274
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700275 uint32_t hw_userId = adjust_userId(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700276 android::hardware::hidl_vec<uint8_t> curPwdHandle;
277 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()),
278 enrolledPasswordHandle.size());
279 android::hardware::hidl_vec<uint8_t> enteredPwd;
280 enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()),
281 providedPassword.size());
282
283 Return<void> hwRes = hw_device->verify(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700284 hw_userId, challenge, curPwdHandle, enteredPwd,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700285 [&gkResponse](const GatekeeperResponse& rsp) {
286 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
287 *gkResponse = GKResponse::ok(
288 {rsp.data.begin(), rsp.data.end()},
289 rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */);
290 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) {
291 *gkResponse = GKResponse::retry(rsp.timeout);
292 } else {
293 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700294 }
295 });
Andres Morales835d96e2015-06-03 15:06:24 -0700296
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700297 if (!hwRes.isOk()) {
298 LOG(ERROR) << "verify transaction failed";
299 return GK_ERROR;
300 }
301
302 if (gkResponse->response_code() == GKResponseCode::OK) {
303 if (gkResponse->payload().size() != 0) {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000304 // try to connect to IKeystoreAuthorization AIDL service first.
305 AIBinder* authzAIBinder =
306 AServiceManager_checkService("android.security.authorization");
307 ::ndk::SpAIBinder authzBinder(authzAIBinder);
308 auto authzService = IKeystoreAuthorization::fromBinder(authzBinder);
309 if (authzService) {
310 if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) {
311 LOG(ERROR) << "Incorrect size of AuthToken payload.";
312 return GK_ERROR;
313 }
314
315 const hw_auth_token_t* hwAuthToken =
316 reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data());
317 HardwareAuthToken authToken;
318
319 authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp);
320 authToken.challenge = hwAuthToken->challenge;
321 authToken.authenticatorId = hwAuthToken->authenticator_id;
322 authToken.authenticatorType = static_cast<HardwareAuthenticatorType>(
323 betoh32(hwAuthToken->authenticator_type));
324 authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]);
325 auto result = authzService->addAuthToken(authToken);
326 if (!result.isOk()) {
327 LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService.";
328 return GK_ERROR;
329 }
330 AIBinder_decStrong(authzAIBinder);
331 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700332 sp<IServiceManager> sm = defaultServiceManager();
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000333
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700334 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
335 sp<security::keystore::IKeystoreService> service =
336 interface_cast<security::keystore::IKeystoreService>(binder);
337
338 if (service) {
339 int result = 0;
340 auto binder_result = service->addAuthToken(gkResponse->payload(), &result);
341 if (!binder_result.isOk() ||
342 !keystore::KeyStoreServiceReturnCode(result).isOk()) {
343 LOG(ERROR) << "Failure sending auth token to KeyStore: " << result;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000344 return GK_ERROR;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700345 }
346 } else {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000347 LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with "
348 "Keystore.";
349 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700350 }
351 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700352
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700353 maybe_store_sid(userId, handle->user_id);
Andres Morales33dfdc72015-05-12 15:37:20 -0700354 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700355
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700356 return Status::ok();
Andres Morales6a49c2f2015-04-16 13:16:24 -0700357 }
358
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700359 Status getSecureUserId(int32_t userId, int64_t* sid) override {
360 *sid = read_sid(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700361 return Status::ok();
362 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700363
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700364 Status clearSecureUserId(int32_t userId) override {
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700365 IPCThreadState* ipc = IPCThreadState::self();
366 const int calling_pid = ipc->getCallingPid();
367 const int calling_uid = ipc->getCallingUid();
368 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
369 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700370 return Status::ok();
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700371 }
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700372 clear_sid(userId);
Andres Morales3c2086d2015-06-24 10:21:16 -0700373
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700374 if (hw_device) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700375 uint32_t hw_userId = adjust_userId(userId);
376 hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {});
Andres Morales3c2086d2015-06-24 10:21:16 -0700377 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700378 return Status::ok();
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700379 }
380
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700381 Status reportDeviceSetupComplete() override {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700382 IPCThreadState* ipc = IPCThreadState::self();
383 const int calling_pid = ipc->getCallingPid();
384 const int calling_uid = ipc->getCallingUid();
385 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
386 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700387 return Status::ok();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700388 }
389
390 clear_state_if_needed();
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700391 return Status::ok();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700392 }
393
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700394 status_t dump(int fd, const Vector<String16>&) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700395 IPCThreadState* ipc = IPCThreadState::self();
396 const int pid = ipc->getCallingPid();
397 const int uid = ipc->getCallingUid();
398 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
399 return PERMISSION_DENIED;
400 }
401
Alexey Polyudov275aece2016-10-18 13:59:26 -0700402 if (hw_device == NULL) {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000403 const char* result = "Device not available";
Andres Morales2d08dce2015-04-03 16:40:15 -0700404 write(fd, result, strlen(result) + 1);
405 } else {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000406 const char* result = "OK";
Andres Morales2d08dce2015-04-03 16:40:15 -0700407 write(fd, result, strlen(result) + 1);
408 }
409
Elliott Hughes643268f2018-10-08 11:10:11 -0700410 return OK;
Andres Morales2d08dce2015-04-03 16:40:15 -0700411 }
412
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000413 private:
Alexey Polyudov275aece2016-10-18 13:59:26 -0700414 sp<IGatekeeper> hw_device;
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700415
416 bool clear_state_if_needed_done;
David Anderson97400bd2019-02-15 15:59:39 -0800417 bool is_running_gsi;
Andres Morales2d08dce2015-04-03 16:40:15 -0700418};
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000419} // namespace android
Andres Morales2d08dce2015-04-03 16:40:15 -0700420
Andres Morales6a49c2f2015-04-16 13:16:24 -0700421int main(int argc, char* argv[]) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700422 ALOGI("Starting gatekeeperd...");
Andres Morales6a49c2f2015-04-16 13:16:24 -0700423 if (argc < 2) {
424 ALOGE("A directory must be specified!");
425 return 1;
426 }
427 if (chdir(argv[1]) == -1) {
428 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
429 return 1;
430 }
431
Andres Morales2d08dce2015-04-03 16:40:15 -0700432 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
433 android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
434 android::status_t ret = sm->addService(
435 android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
436 if (ret != android::OK) {
437 ALOGE("Couldn't register binder service!");
438 return -1;
439 }
440
441 /*
442 * We're the only thread in existence, so we're just going to process
443 * Binder transaction as a single-threaded program.
444 */
445 android::IPCThreadState::self()->joinThreadPool();
446 return 0;
447}