blob: ced757345728353be017c8958ffd098fc9cf3a22 [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 Gunasinghe701fbca2020-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 Gunasinghe701fbca2020-12-08 21:08:13 +000030#include <android/binder_manager.h>
31#include <android/security/keystore/IKeystoreService.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
34#include <binder/PermissionCache.h>
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +000035#include <gatekeeper/password_handle.h> // for password_handle_t
Andres Morales6a49c2f2015-04-16 13:16:24 -070036#include <hardware/hw_auth_token.h>
Dmitry Dementyev0dd259c2017-11-14 12:53:39 -080037#include <keystore/keystore_return_types.h>
David Anderson97400bd2019-02-15 15:59:39 -080038#include <libgsi/libgsi.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080039#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070040#include <utils/String16.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070041
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +000042#include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
43#include <aidl/android/security/authorization/IKeystoreAuthorization.h>
Alexey Polyudov275aece2016-10-18 13:59:26 -070044#include <android/hardware/gatekeeper/1.0/IGatekeeper.h>
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +000045#include <hidl/HidlSupport.h>
Alexey Polyudov275aece2016-10-18 13:59:26 -070046
47using android::sp;
Alexey Polyudov275aece2016-10-18 13:59:26 -070048using android::hardware::Return;
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +000049using android::hardware::gatekeeper::V1_0::GatekeeperResponse;
50using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode;
51using android::hardware::gatekeeper::V1_0::IGatekeeper;
Alexey Polyudov275aece2016-10-18 13:59:26 -070052
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070053using ::android::binder::Status;
54using ::android::service::gatekeeper::BnGateKeeperService;
55using GKResponse = ::android::service::gatekeeper::GateKeeperResponse;
56using GKResponseCode = ::android::service::gatekeeper::ResponseCode;
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +000057using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType;
58using ::aidl::android::hardware::security::keymint::HardwareAuthToken;
59using ::aidl::android::security::authorization::IKeystoreAuthorization;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070060
Andres Morales2d08dce2015-04-03 16:40:15 -070061namespace android {
62
63static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
64static const String16 DUMP_PERMISSION("android.permission.DUMP");
65
66class GateKeeperProxy : public BnGateKeeperService {
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +000067 public:
Andres Morales2d08dce2015-04-03 16:40:15 -070068 GateKeeperProxy() {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070069 clear_state_if_needed_done = false;
Chris Phoenixa84ce0c2017-01-24 13:09:39 -080070 hw_device = IGatekeeper::getService();
David Anderson97400bd2019-02-15 15:59:39 -080071 is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false);
Andres Moralesfef908e2015-07-07 10:28:15 -070072
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070073 if (!hw_device) {
74 LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad.";
Andres Morales33dfdc72015-05-12 15:37:20 -070075 }
Andres Morales2d08dce2015-04-03 16:40:15 -070076 }
77
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +000078 virtual ~GateKeeperProxy() {}
Andres Morales2d08dce2015-04-03 16:40:15 -070079
Janis Danisevskis0a738d92020-09-23 17:00:20 -070080 void store_sid(uint32_t userId, uint64_t sid) {
Andres Morales6a49c2f2015-04-16 13:16:24 -070081 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -070082 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -070083 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
84 if (fd < 0) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -070085 ALOGE("could not open file: %s: %s", filename, strerror(errno));
Andres Morales6a49c2f2015-04-16 13:16:24 -070086 return;
87 }
88 write(fd, &sid, sizeof(sid));
89 close(fd);
90 }
91
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070092 void clear_state_if_needed() {
93 if (clear_state_if_needed_done) {
94 return;
95 }
96
David Anderson97400bd2019-02-15 15:59:39 -080097 if (mark_cold_boot() && !is_running_gsi) {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070098 ALOGI("cold boot: clearing state");
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070099 if (hw_device) {
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000100 hw_device->deleteAllUsers([](const GatekeeperResponse&) {});
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700101 }
102 }
103
104 clear_state_if_needed_done = true;
105 }
106
Andres Morales3c2086d2015-06-24 10:21:16 -0700107 bool mark_cold_boot() {
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000108 const char* filename = ".coldboot";
Andres Morales3c2086d2015-06-24 10:21:16 -0700109 if (access(filename, F_OK) == -1) {
110 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
111 if (fd < 0) {
112 ALOGE("could not open file: %s : %s", filename, strerror(errno));
113 return false;
114 }
115 close(fd);
116 return true;
117 }
118 return false;
119 }
120
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700121 void maybe_store_sid(uint32_t userId, uint64_t sid) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700122 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700123 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700124 if (access(filename, F_OK) == -1) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700125 store_sid(userId, sid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700126 }
127 }
128
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700129 uint64_t read_sid(uint32_t userId) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700130 char filename[21];
131 uint64_t sid;
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700132 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700133 int fd = open(filename, O_RDONLY);
134 if (fd < 0) return 0;
135 read(fd, &sid, sizeof(sid));
Andres Morales0b0435e2015-07-10 09:47:09 -0700136 close(fd);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700137 return sid;
138 }
139
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700140 void clear_sid(uint32_t userId) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700141 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700142 snprintf(filename, sizeof(filename), "%u", userId);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700143 if (remove(filename) < 0) {
144 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700145 store_sid(userId, 0);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700146 }
147 }
148
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700149 // This should only be called on userIds being passed to the GateKeeper HAL. It ensures that
David Anderson97400bd2019-02-15 15:59:39 -0800150 // secure storage shared across a GSI image and a host image will not overlap.
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700151 uint32_t adjust_userId(uint32_t userId) {
David Anderson97400bd2019-02-15 15:59:39 -0800152 static constexpr uint32_t kGsiOffset = 1000000;
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700153 CHECK(userId < kGsiOffset);
David Anderson97400bd2019-02-15 15:59:39 -0800154 CHECK(hw_device != nullptr);
155 if (is_running_gsi) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700156 return userId + kGsiOffset;
David Anderson97400bd2019-02-15 15:59:39 -0800157 }
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700158 return userId;
David Anderson97400bd2019-02-15 15:59:39 -0800159 }
160
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700161#define GK_ERROR *gkResponse = GKResponse::error(), Status::ok()
162
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700163 Status enroll(int32_t userId, const std::optional<std::vector<uint8_t>>& currentPasswordHandle,
Jooyung Han57110a42020-01-23 13:28:36 +0900164 const std::optional<std::vector<uint8_t>>& currentPassword,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700165 const std::vector<uint8_t>& desiredPassword, GKResponse* gkResponse) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700166 IPCThreadState* ipc = IPCThreadState::self();
167 const int calling_pid = ipc->getCallingPid();
168 const int calling_uid = ipc->getCallingUid();
169 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700170 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700171 }
172
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700173 // Make sure to clear any state from before factory reset as soon as a credential is
174 // enrolled (which may happen during device setup).
175 clear_state_if_needed();
176
Andres Morales2d08dce2015-04-03 16:40:15 -0700177 // need a desired password to enroll
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700178 if (desiredPassword.size() == 0) return GK_ERROR;
Andres Morales33dfdc72015-05-12 15:37:20 -0700179
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700180 if (!hw_device) {
181 LOG(ERROR) << "has no HAL to talk to";
182 return GK_ERROR;
183 }
Andres Morales835d96e2015-06-03 15:06:24 -0700184
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700185 android::hardware::hidl_vec<uint8_t> curPwdHandle;
186 android::hardware::hidl_vec<uint8_t> curPwd;
187
188 if (currentPasswordHandle && currentPassword) {
189 if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) {
190 LOG(INFO) << "Password handle has wrong length";
191 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700192 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700193 curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()),
194 currentPasswordHandle->size());
195 curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()),
196 currentPassword->size());
197 }
Andres Morales835d96e2015-06-03 15:06:24 -0700198
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700199 android::hardware::hidl_vec<uint8_t> newPwd;
200 newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size());
Alexey Polyudov275aece2016-10-18 13:59:26 -0700201
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700202 uint32_t hw_userId = adjust_userId(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700203 Return<void> hwRes = hw_device->enroll(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700204 hw_userId, curPwdHandle, curPwd, newPwd,
205 [&gkResponse](const GatekeeperResponse& rsp) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700206 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
207 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
208 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
209 rsp.timeout > 0) {
210 *gkResponse = GKResponse::retry(rsp.timeout);
211 } else {
212 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700213 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700214 });
215 if (!hwRes.isOk()) {
216 LOG(ERROR) << "enroll transaction failed";
217 return GK_ERROR;
218 }
219
220 if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) {
221 if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) {
222 LOG(ERROR) << "HAL returned password handle of invalid length "
223 << gkResponse->payload().size();
224 return GK_ERROR;
Alexey Polyudov275aece2016-10-18 13:59:26 -0700225 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700226
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700227 const gatekeeper::password_handle_t* handle =
228 reinterpret_cast<const gatekeeper::password_handle_t*>(
229 gkResponse->payload().data());
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700230 store_sid(userId, handle->user_id);
Andres Morales531e3e82015-06-01 17:23:04 -0700231
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700232 GKResponse verifyResponse;
Andres Morales531e3e82015-06-01 17:23:04 -0700233 // immediately verify this password so we don't ask the user to enter it again
234 // if they just created it.
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700235 auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700236 if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) {
237 LOG(ERROR) << "Failed to verify password after enrolling";
238 }
Andres Morales6a49c2f2015-04-16 13:16:24 -0700239 }
Andres Moralesae242922015-05-18 09:26:19 -0700240
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700241 return Status::ok();
Andres Morales2d08dce2015-04-03 16:40:15 -0700242 }
243
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700244 Status verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700245 const ::std::vector<uint8_t>& providedPassword, GKResponse* gkResponse) override {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700246 return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700247 gkResponse);
Andres Moralesc828ae82015-04-10 21:03:07 -0700248 }
249
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700250 Status verifyChallenge(int32_t userId, int64_t challenge,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700251 const std::vector<uint8_t>& enrolledPasswordHandle,
252 const std::vector<uint8_t>& providedPassword,
253 GKResponse* gkResponse) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700254 IPCThreadState* ipc = IPCThreadState::self();
255 const int calling_pid = ipc->getCallingPid();
256 const int calling_uid = ipc->getCallingUid();
257 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700258 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700259 }
260
261 // can't verify if we're missing either param
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700262 if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700263
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700264 if (!hw_device) return GK_ERROR;
265
266 if (enrolledPasswordHandle.size() != sizeof(gatekeeper::password_handle_t)) {
267 LOG(INFO) << "Password handle has wrong length";
268 return GK_ERROR;
269 }
270 const gatekeeper::password_handle_t* handle =
271 reinterpret_cast<const gatekeeper::password_handle_t*>(
272 enrolledPasswordHandle.data());
273
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700274 uint32_t hw_userId = adjust_userId(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700275 android::hardware::hidl_vec<uint8_t> curPwdHandle;
276 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()),
277 enrolledPasswordHandle.size());
278 android::hardware::hidl_vec<uint8_t> enteredPwd;
279 enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()),
280 providedPassword.size());
281
282 Return<void> hwRes = hw_device->verify(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700283 hw_userId, challenge, curPwdHandle, enteredPwd,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700284 [&gkResponse](const GatekeeperResponse& rsp) {
285 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
286 *gkResponse = GKResponse::ok(
287 {rsp.data.begin(), rsp.data.end()},
288 rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */);
289 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) {
290 *gkResponse = GKResponse::retry(rsp.timeout);
291 } else {
292 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700293 }
294 });
Andres Morales835d96e2015-06-03 15:06:24 -0700295
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700296 if (!hwRes.isOk()) {
297 LOG(ERROR) << "verify transaction failed";
298 return GK_ERROR;
299 }
300
301 if (gkResponse->response_code() == GKResponseCode::OK) {
302 if (gkResponse->payload().size() != 0) {
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000303 // try to connect to IKeystoreAuthorization AIDL service first.
304 ::ndk::SpAIBinder authzBinder(
305 AServiceManager_getService("android.security.authorization"));
306 auto authzService = IKeystoreAuthorization::fromBinder(authzBinder);
307 if (authzService) {
308 if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) {
309 LOG(ERROR) << "Incorrect size of AuthToken payload.";
310 return GK_ERROR;
311 }
312
313 const hw_auth_token_t* hwAuthToken =
314 reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data());
315 HardwareAuthToken authToken;
316
317 authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp);
318 authToken.challenge = hwAuthToken->challenge;
319 authToken.authenticatorId = hwAuthToken->authenticator_id;
320 authToken.authenticatorType = static_cast<HardwareAuthenticatorType>(
321 betoh32(hwAuthToken->authenticator_type));
322 authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]);
323 auto result = authzService->addAuthToken(authToken);
324 if (!result.isOk()) {
325 LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService.";
326 return GK_ERROR;
327 }
328 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700329 sp<IServiceManager> sm = defaultServiceManager();
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000330
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700331 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
332 sp<security::keystore::IKeystoreService> service =
333 interface_cast<security::keystore::IKeystoreService>(binder);
334
335 if (service) {
336 int result = 0;
337 auto binder_result = service->addAuthToken(gkResponse->payload(), &result);
338 if (!binder_result.isOk() ||
339 !keystore::KeyStoreServiceReturnCode(result).isOk()) {
340 LOG(ERROR) << "Failure sending auth token to KeyStore: " << result;
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000341 return GK_ERROR;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700342 }
343 } else {
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000344 LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with "
345 "Keystore.";
346 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700347 }
348 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700349
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700350 maybe_store_sid(userId, handle->user_id);
Andres Morales33dfdc72015-05-12 15:37:20 -0700351 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700352
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700353 return Status::ok();
Andres Morales6a49c2f2015-04-16 13:16:24 -0700354 }
355
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700356 Status getSecureUserId(int32_t userId, int64_t* sid) override {
357 *sid = read_sid(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700358 return Status::ok();
359 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700360
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700361 Status clearSecureUserId(int32_t userId) override {
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700362 IPCThreadState* ipc = IPCThreadState::self();
363 const int calling_pid = ipc->getCallingPid();
364 const int calling_uid = ipc->getCallingUid();
365 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
366 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700367 return Status::ok();
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700368 }
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700369 clear_sid(userId);
Andres Morales3c2086d2015-06-24 10:21:16 -0700370
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700371 if (hw_device) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700372 uint32_t hw_userId = adjust_userId(userId);
373 hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {});
Andres Morales3c2086d2015-06-24 10:21:16 -0700374 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700375 return Status::ok();
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700376 }
377
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700378 Status reportDeviceSetupComplete() override {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700379 IPCThreadState* ipc = IPCThreadState::self();
380 const int calling_pid = ipc->getCallingPid();
381 const int calling_uid = ipc->getCallingUid();
382 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
383 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700384 return Status::ok();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700385 }
386
387 clear_state_if_needed();
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700388 return Status::ok();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700389 }
390
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700391 status_t dump(int fd, const Vector<String16>&) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700392 IPCThreadState* ipc = IPCThreadState::self();
393 const int pid = ipc->getCallingPid();
394 const int uid = ipc->getCallingUid();
395 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
396 return PERMISSION_DENIED;
397 }
398
Alexey Polyudov275aece2016-10-18 13:59:26 -0700399 if (hw_device == NULL) {
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000400 const char* result = "Device not available";
Andres Morales2d08dce2015-04-03 16:40:15 -0700401 write(fd, result, strlen(result) + 1);
402 } else {
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000403 const char* result = "OK";
Andres Morales2d08dce2015-04-03 16:40:15 -0700404 write(fd, result, strlen(result) + 1);
405 }
406
Elliott Hughes643268f2018-10-08 11:10:11 -0700407 return OK;
Andres Morales2d08dce2015-04-03 16:40:15 -0700408 }
409
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000410 private:
Alexey Polyudov275aece2016-10-18 13:59:26 -0700411 sp<IGatekeeper> hw_device;
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700412
413 bool clear_state_if_needed_done;
David Anderson97400bd2019-02-15 15:59:39 -0800414 bool is_running_gsi;
Andres Morales2d08dce2015-04-03 16:40:15 -0700415};
Hasini Gunasinghe701fbca2020-12-08 21:08:13 +0000416} // namespace android
Andres Morales2d08dce2015-04-03 16:40:15 -0700417
Andres Morales6a49c2f2015-04-16 13:16:24 -0700418int main(int argc, char* argv[]) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700419 ALOGI("Starting gatekeeperd...");
Andres Morales6a49c2f2015-04-16 13:16:24 -0700420 if (argc < 2) {
421 ALOGE("A directory must be specified!");
422 return 1;
423 }
424 if (chdir(argv[1]) == -1) {
425 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
426 return 1;
427 }
428
Andres Morales2d08dce2015-04-03 16:40:15 -0700429 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
430 android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
431 android::status_t ret = sm->addService(
432 android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
433 if (ret != android::OK) {
434 ALOGE("Couldn't register binder service!");
435 return -1;
436 }
437
438 /*
439 * We're the only thread in existence, so we're just going to process
440 * Binder transaction as a single-threaded program.
441 */
442 android::IPCThreadState::self()->joinThreadPool();
443 return 0;
444}