2015-04-04 01:40:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "gatekeeperd"
|
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
#include <android/service/gatekeeper/BnGateKeeperService.h>
|
|
|
|
#include <gatekeeper/GateKeeperResponse.h>
|
2015-04-04 01:40:15 +02:00
|
|
|
|
2020-12-08 22:08:13 +01:00
|
|
|
#include <endian.h>
|
2015-04-16 22:16:24 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2017-08-16 11:54:20 +02:00
|
|
|
#include <memory>
|
2015-04-16 22:16:24 +02:00
|
|
|
|
2019-02-16 00:59:39 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/properties.h>
|
2020-12-08 22:08:13 +01:00
|
|
|
#include <android/binder_ibinder.h>
|
|
|
|
#include <android/binder_manager.h>
|
|
|
|
#include <android/security/keystore/IKeystoreService.h>
|
2015-04-04 01:40:15 +02:00
|
|
|
#include <binder/IPCThreadState.h>
|
|
|
|
#include <binder/IServiceManager.h>
|
|
|
|
#include <binder/PermissionCache.h>
|
2020-12-08 22:08:13 +01:00
|
|
|
#include <gatekeeper/password_handle.h> // for password_handle_t
|
2015-04-16 22:16:24 +02:00
|
|
|
#include <hardware/hw_auth_token.h>
|
2017-11-14 21:53:39 +01:00
|
|
|
#include <keystore/keystore_return_types.h>
|
2019-02-16 00:59:39 +01:00
|
|
|
#include <libgsi/libgsi.h>
|
2017-01-10 22:19:54 +01:00
|
|
|
#include <log/log.h>
|
2016-09-28 19:07:20 +02:00
|
|
|
#include <utils/String16.h>
|
2015-04-04 01:40:15 +02:00
|
|
|
|
2020-12-08 22:08:13 +01:00
|
|
|
#include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
|
|
|
|
#include <aidl/android/security/authorization/IKeystoreAuthorization.h>
|
2021-01-18 11:01:12 +01:00
|
|
|
#include <android/hardware/gatekeeper/1.0/IGatekeeper.h>
|
2020-12-08 22:08:13 +01:00
|
|
|
#include <hidl/HidlSupport.h>
|
2016-10-18 22:59:26 +02:00
|
|
|
|
|
|
|
using android::sp;
|
2021-01-18 11:01:12 +01:00
|
|
|
using android::hardware::Return;
|
2020-12-08 22:08:13 +01:00
|
|
|
using android::hardware::gatekeeper::V1_0::GatekeeperResponse;
|
|
|
|
using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode;
|
|
|
|
using android::hardware::gatekeeper::V1_0::IGatekeeper;
|
2016-10-18 22:59:26 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
using ::android::binder::Status;
|
|
|
|
using ::android::service::gatekeeper::BnGateKeeperService;
|
|
|
|
using GKResponse = ::android::service::gatekeeper::GateKeeperResponse;
|
|
|
|
using GKResponseCode = ::android::service::gatekeeper::ResponseCode;
|
2020-12-08 22:08:13 +01:00
|
|
|
using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType;
|
|
|
|
using ::aidl::android::hardware::security::keymint::HardwareAuthToken;
|
|
|
|
using ::aidl::android::security::authorization::IKeystoreAuthorization;
|
2019-03-29 19:14:31 +01:00
|
|
|
|
2015-04-04 01:40:15 +02:00
|
|
|
namespace android {
|
|
|
|
|
|
|
|
static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
|
|
|
|
static const String16 DUMP_PERMISSION("android.permission.DUMP");
|
|
|
|
|
|
|
|
class GateKeeperProxy : public BnGateKeeperService {
|
2020-12-08 22:08:13 +01:00
|
|
|
public:
|
2015-04-04 01:40:15 +02:00
|
|
|
GateKeeperProxy() {
|
2017-04-12 22:03:04 +02:00
|
|
|
clear_state_if_needed_done = false;
|
2017-01-24 22:09:39 +01:00
|
|
|
hw_device = IGatekeeper::getService();
|
2019-02-16 00:59:39 +01:00
|
|
|
is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false);
|
2015-07-07 19:28:15 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
if (!hw_device) {
|
|
|
|
LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad.";
|
2015-05-13 00:37:20 +02:00
|
|
|
}
|
2015-04-04 01:40:15 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:08:13 +01:00
|
|
|
virtual ~GateKeeperProxy() {}
|
2015-04-04 01:40:15 +02:00
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
void store_sid(uint32_t userId, uint64_t sid) {
|
2015-04-16 22:16:24 +02:00
|
|
|
char filename[21];
|
2020-09-24 02:00:20 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%u", userId);
|
2015-04-16 22:16:24 +02:00
|
|
|
int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd < 0) {
|
2015-04-17 18:00:28 +02:00
|
|
|
ALOGE("could not open file: %s: %s", filename, strerror(errno));
|
2015-04-16 22:16:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
write(fd, &sid, sizeof(sid));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-04-12 22:03:04 +02:00
|
|
|
void clear_state_if_needed() {
|
|
|
|
if (clear_state_if_needed_done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-16 00:59:39 +01:00
|
|
|
if (mark_cold_boot() && !is_running_gsi) {
|
2017-04-12 22:03:04 +02:00
|
|
|
ALOGI("cold boot: clearing state");
|
2019-03-29 19:14:31 +01:00
|
|
|
if (hw_device) {
|
2020-12-08 22:08:13 +01:00
|
|
|
hw_device->deleteAllUsers([](const GatekeeperResponse&) {});
|
2017-04-12 22:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_state_if_needed_done = true;
|
|
|
|
}
|
|
|
|
|
2015-06-24 19:21:16 +02:00
|
|
|
bool mark_cold_boot() {
|
2020-12-08 22:08:13 +01:00
|
|
|
const char* filename = ".coldboot";
|
2015-06-24 19:21:16 +02:00
|
|
|
if (access(filename, F_OK) == -1) {
|
|
|
|
int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd < 0) {
|
|
|
|
ALOGE("could not open file: %s : %s", filename, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
void maybe_store_sid(uint32_t userId, uint64_t sid) {
|
2015-04-16 22:16:24 +02:00
|
|
|
char filename[21];
|
2020-09-24 02:00:20 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%u", userId);
|
2015-04-16 22:16:24 +02:00
|
|
|
if (access(filename, F_OK) == -1) {
|
2020-09-24 02:00:20 +02:00
|
|
|
store_sid(userId, sid);
|
2015-04-16 22:16:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
uint64_t read_sid(uint32_t userId) {
|
2015-04-16 22:16:24 +02:00
|
|
|
char filename[21];
|
|
|
|
uint64_t sid;
|
2020-09-24 02:00:20 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%u", userId);
|
2015-04-16 22:16:24 +02:00
|
|
|
int fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) return 0;
|
|
|
|
read(fd, &sid, sizeof(sid));
|
2015-07-10 18:47:09 +02:00
|
|
|
close(fd);
|
2015-04-16 22:16:24 +02:00
|
|
|
return sid;
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
void clear_sid(uint32_t userId) {
|
2015-04-17 18:00:28 +02:00
|
|
|
char filename[21];
|
2020-09-24 02:00:20 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%u", userId);
|
2015-04-17 18:00:28 +02:00
|
|
|
if (remove(filename) < 0) {
|
|
|
|
ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
|
2020-09-24 02:00:20 +02:00
|
|
|
store_sid(userId, 0);
|
2015-04-17 18:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
// This should only be called on userIds being passed to the GateKeeper HAL. It ensures that
|
2019-02-16 00:59:39 +01:00
|
|
|
// secure storage shared across a GSI image and a host image will not overlap.
|
2020-09-24 02:00:20 +02:00
|
|
|
uint32_t adjust_userId(uint32_t userId) {
|
2019-02-16 00:59:39 +01:00
|
|
|
static constexpr uint32_t kGsiOffset = 1000000;
|
2020-09-24 02:00:20 +02:00
|
|
|
CHECK(userId < kGsiOffset);
|
2019-02-16 00:59:39 +01:00
|
|
|
CHECK(hw_device != nullptr);
|
|
|
|
if (is_running_gsi) {
|
2020-09-24 02:00:20 +02:00
|
|
|
return userId + kGsiOffset;
|
2019-02-16 00:59:39 +01:00
|
|
|
}
|
2020-09-24 02:00:20 +02:00
|
|
|
return userId;
|
2019-02-16 00:59:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
#define GK_ERROR *gkResponse = GKResponse::error(), Status::ok()
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
Status enroll(int32_t userId, const std::optional<std::vector<uint8_t>>& currentPasswordHandle,
|
2020-01-23 05:28:36 +01:00
|
|
|
const std::optional<std::vector<uint8_t>>& currentPassword,
|
2019-03-29 19:14:31 +01:00
|
|
|
const std::vector<uint8_t>& desiredPassword, GKResponse* gkResponse) override {
|
2015-04-04 01:40:15 +02:00
|
|
|
IPCThreadState* ipc = IPCThreadState::self();
|
|
|
|
const int calling_pid = ipc->getCallingPid();
|
|
|
|
const int calling_uid = ipc->getCallingUid();
|
|
|
|
if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
|
2019-03-29 19:14:31 +01:00
|
|
|
return GK_ERROR;
|
2015-04-04 01:40:15 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 22:03:04 +02:00
|
|
|
// Make sure to clear any state from before factory reset as soon as a credential is
|
|
|
|
// enrolled (which may happen during device setup).
|
|
|
|
clear_state_if_needed();
|
|
|
|
|
2015-04-04 01:40:15 +02:00
|
|
|
// need a desired password to enroll
|
2019-03-29 19:14:31 +01:00
|
|
|
if (desiredPassword.size() == 0) return GK_ERROR;
|
2015-06-04 00:06:24 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
if (!hw_device) {
|
|
|
|
LOG(ERROR) << "has no HAL to talk to";
|
|
|
|
return GK_ERROR;
|
|
|
|
}
|
2016-10-18 22:59:26 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
android::hardware::hidl_vec<uint8_t> curPwdHandle;
|
|
|
|
android::hardware::hidl_vec<uint8_t> curPwd;
|
|
|
|
|
|
|
|
if (currentPasswordHandle && currentPassword) {
|
|
|
|
if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) {
|
|
|
|
LOG(INFO) << "Password handle has wrong length";
|
|
|
|
return GK_ERROR;
|
2016-10-18 22:59:26 +02:00
|
|
|
}
|
2019-03-29 19:14:31 +01:00
|
|
|
curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()),
|
|
|
|
currentPasswordHandle->size());
|
|
|
|
curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()),
|
|
|
|
currentPassword->size());
|
2015-05-13 00:37:20 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
android::hardware::hidl_vec<uint8_t> newPwd;
|
|
|
|
newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size());
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
uint32_t hw_userId = adjust_userId(userId);
|
2019-03-29 19:14:31 +01:00
|
|
|
Return<void> hwRes = hw_device->enroll(
|
2020-09-24 02:00:20 +02:00
|
|
|
hw_userId, curPwdHandle, curPwd, newPwd,
|
|
|
|
[&gkResponse](const GatekeeperResponse& rsp) {
|
2019-03-29 19:14:31 +01:00
|
|
|
if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
|
|
|
|
*gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
|
|
|
|
} else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
|
|
|
|
rsp.timeout > 0) {
|
|
|
|
*gkResponse = GKResponse::retry(rsp.timeout);
|
|
|
|
} else {
|
|
|
|
*gkResponse = GKResponse::error();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!hwRes.isOk()) {
|
|
|
|
LOG(ERROR) << "enroll transaction failed";
|
|
|
|
return GK_ERROR;
|
2016-09-08 03:51:28 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) {
|
|
|
|
if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) {
|
|
|
|
LOG(ERROR) << "HAL returned password handle of invalid length "
|
|
|
|
<< gkResponse->payload().size();
|
|
|
|
return GK_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gatekeeper::password_handle_t* handle =
|
|
|
|
reinterpret_cast<const gatekeeper::password_handle_t*>(
|
|
|
|
gkResponse->payload().data());
|
2020-09-24 02:00:20 +02:00
|
|
|
store_sid(userId, handle->user_id);
|
2015-06-02 02:23:04 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
GKResponse verifyResponse;
|
2015-06-02 02:23:04 +02:00
|
|
|
// immediately verify this password so we don't ask the user to enter it again
|
|
|
|
// if they just created it.
|
2020-09-24 02:00:20 +02:00
|
|
|
auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse);
|
2019-03-29 19:14:31 +01:00
|
|
|
if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) {
|
|
|
|
LOG(ERROR) << "Failed to verify password after enrolling";
|
|
|
|
}
|
2015-04-16 22:16:24 +02:00
|
|
|
}
|
2015-05-18 18:26:19 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
return Status::ok();
|
2015-04-04 01:40:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
Status verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle,
|
2019-03-29 19:14:31 +01:00
|
|
|
const ::std::vector<uint8_t>& providedPassword, GKResponse* gkResponse) override {
|
2020-09-24 02:00:20 +02:00
|
|
|
return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword,
|
2019-03-29 19:14:31 +01:00
|
|
|
gkResponse);
|
2015-04-11 06:03:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
Status verifyChallenge(int32_t userId, int64_t challenge,
|
2019-03-29 19:14:31 +01:00
|
|
|
const std::vector<uint8_t>& enrolledPasswordHandle,
|
|
|
|
const std::vector<uint8_t>& providedPassword,
|
|
|
|
GKResponse* gkResponse) override {
|
2015-04-04 01:40:15 +02:00
|
|
|
IPCThreadState* ipc = IPCThreadState::self();
|
|
|
|
const int calling_pid = ipc->getCallingPid();
|
|
|
|
const int calling_uid = ipc->getCallingUid();
|
|
|
|
if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
|
2019-03-29 19:14:31 +01:00
|
|
|
return GK_ERROR;
|
2015-04-04 01:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// can't verify if we're missing either param
|
2019-03-29 19:14:31 +01:00
|
|
|
if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR;
|
|
|
|
|
|
|
|
if (!hw_device) return GK_ERROR;
|
|
|
|
|
|
|
|
if (enrolledPasswordHandle.size() != sizeof(gatekeeper::password_handle_t)) {
|
|
|
|
LOG(INFO) << "Password handle has wrong length";
|
|
|
|
return GK_ERROR;
|
|
|
|
}
|
|
|
|
const gatekeeper::password_handle_t* handle =
|
|
|
|
reinterpret_cast<const gatekeeper::password_handle_t*>(
|
|
|
|
enrolledPasswordHandle.data());
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
uint32_t hw_userId = adjust_userId(userId);
|
2019-03-29 19:14:31 +01:00
|
|
|
android::hardware::hidl_vec<uint8_t> curPwdHandle;
|
|
|
|
curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()),
|
|
|
|
enrolledPasswordHandle.size());
|
|
|
|
android::hardware::hidl_vec<uint8_t> enteredPwd;
|
|
|
|
enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()),
|
|
|
|
providedPassword.size());
|
|
|
|
|
|
|
|
Return<void> hwRes = hw_device->verify(
|
2020-09-24 02:00:20 +02:00
|
|
|
hw_userId, challenge, curPwdHandle, enteredPwd,
|
2019-03-29 19:14:31 +01:00
|
|
|
[&gkResponse](const GatekeeperResponse& rsp) {
|
|
|
|
if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
|
|
|
|
*gkResponse = GKResponse::ok(
|
|
|
|
{rsp.data.begin(), rsp.data.end()},
|
|
|
|
rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */);
|
|
|
|
} else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) {
|
|
|
|
*gkResponse = GKResponse::retry(rsp.timeout);
|
|
|
|
} else {
|
|
|
|
*gkResponse = GKResponse::error();
|
2016-10-18 22:59:26 +02:00
|
|
|
}
|
|
|
|
});
|
2019-03-29 19:14:31 +01:00
|
|
|
|
|
|
|
if (!hwRes.isOk()) {
|
|
|
|
LOG(ERROR) << "verify transaction failed";
|
|
|
|
return GK_ERROR;
|
2015-05-13 00:37:20 +02:00
|
|
|
}
|
2015-04-04 01:40:15 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
if (gkResponse->response_code() == GKResponseCode::OK) {
|
|
|
|
if (gkResponse->payload().size() != 0) {
|
2020-12-08 22:08:13 +01:00
|
|
|
// try to connect to IKeystoreAuthorization AIDL service first.
|
|
|
|
AIBinder* authzAIBinder =
|
|
|
|
AServiceManager_checkService("android.security.authorization");
|
|
|
|
::ndk::SpAIBinder authzBinder(authzAIBinder);
|
|
|
|
auto authzService = IKeystoreAuthorization::fromBinder(authzBinder);
|
|
|
|
if (authzService) {
|
|
|
|
if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) {
|
|
|
|
LOG(ERROR) << "Incorrect size of AuthToken payload.";
|
|
|
|
return GK_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hw_auth_token_t* hwAuthToken =
|
|
|
|
reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data());
|
|
|
|
HardwareAuthToken authToken;
|
|
|
|
|
|
|
|
authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp);
|
|
|
|
authToken.challenge = hwAuthToken->challenge;
|
2021-02-12 08:16:58 +01:00
|
|
|
authToken.userId = hwAuthToken->user_id;
|
2020-12-08 22:08:13 +01:00
|
|
|
authToken.authenticatorId = hwAuthToken->authenticator_id;
|
|
|
|
authToken.authenticatorType = static_cast<HardwareAuthenticatorType>(
|
|
|
|
betoh32(hwAuthToken->authenticator_type));
|
|
|
|
authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]);
|
|
|
|
auto result = authzService->addAuthToken(authToken);
|
|
|
|
if (!result.isOk()) {
|
|
|
|
LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService.";
|
|
|
|
return GK_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 19:14:31 +01:00
|
|
|
sp<IServiceManager> sm = defaultServiceManager();
|
2020-12-08 22:08:13 +01:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
|
|
|
|
sp<security::keystore::IKeystoreService> service =
|
|
|
|
interface_cast<security::keystore::IKeystoreService>(binder);
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
int result = 0;
|
|
|
|
auto binder_result = service->addAuthToken(gkResponse->payload(), &result);
|
|
|
|
if (!binder_result.isOk() ||
|
|
|
|
!keystore::KeyStoreServiceReturnCode(result).isOk()) {
|
|
|
|
LOG(ERROR) << "Failure sending auth token to KeyStore: " << result;
|
2020-12-08 22:08:13 +01:00
|
|
|
return GK_ERROR;
|
2019-03-29 19:14:31 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-08 22:08:13 +01:00
|
|
|
LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with "
|
|
|
|
"Keystore.";
|
|
|
|
return GK_ERROR;
|
2015-04-04 01:40:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
maybe_store_sid(userId, handle->user_id);
|
2015-04-16 22:16:24 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
return Status::ok();
|
2015-04-16 22:16:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
Status getSecureUserId(int32_t userId, int64_t* sid) override {
|
|
|
|
*sid = read_sid(userId);
|
2019-03-29 19:14:31 +01:00
|
|
|
return Status::ok();
|
|
|
|
}
|
2015-04-04 01:40:15 +02:00
|
|
|
|
2020-09-24 02:00:20 +02:00
|
|
|
Status clearSecureUserId(int32_t userId) override {
|
2015-04-17 00:57:17 +02:00
|
|
|
IPCThreadState* ipc = IPCThreadState::self();
|
|
|
|
const int calling_pid = ipc->getCallingPid();
|
|
|
|
const int calling_uid = ipc->getCallingUid();
|
|
|
|
if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
|
|
|
|
ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
|
2019-03-29 19:14:31 +01:00
|
|
|
return Status::ok();
|
2015-04-17 00:57:17 +02:00
|
|
|
}
|
2020-09-24 02:00:20 +02:00
|
|
|
clear_sid(userId);
|
2015-06-24 19:21:16 +02:00
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
if (hw_device) {
|
2020-09-24 02:00:20 +02:00
|
|
|
uint32_t hw_userId = adjust_userId(userId);
|
|
|
|
hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {});
|
2015-06-24 19:21:16 +02:00
|
|
|
}
|
2019-03-29 19:14:31 +01:00
|
|
|
return Status::ok();
|
2015-04-17 00:57:17 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
Status reportDeviceSetupComplete() override {
|
2017-04-12 22:03:04 +02:00
|
|
|
IPCThreadState* ipc = IPCThreadState::self();
|
|
|
|
const int calling_pid = ipc->getCallingPid();
|
|
|
|
const int calling_uid = ipc->getCallingUid();
|
|
|
|
if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
|
|
|
|
ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
|
2019-03-29 19:14:31 +01:00
|
|
|
return Status::ok();
|
2017-04-12 22:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
clear_state_if_needed();
|
2019-03-29 19:14:31 +01:00
|
|
|
return Status::ok();
|
2017-04-12 22:03:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:14:31 +01:00
|
|
|
status_t dump(int fd, const Vector<String16>&) override {
|
2015-04-04 01:40:15 +02:00
|
|
|
IPCThreadState* ipc = IPCThreadState::self();
|
|
|
|
const int pid = ipc->getCallingPid();
|
|
|
|
const int uid = ipc->getCallingUid();
|
|
|
|
if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
2016-10-18 22:59:26 +02:00
|
|
|
if (hw_device == NULL) {
|
2020-12-08 22:08:13 +01:00
|
|
|
const char* result = "Device not available";
|
2015-04-04 01:40:15 +02:00
|
|
|
write(fd, result, strlen(result) + 1);
|
|
|
|
} else {
|
2020-12-08 22:08:13 +01:00
|
|
|
const char* result = "OK";
|
2015-04-04 01:40:15 +02:00
|
|
|
write(fd, result, strlen(result) + 1);
|
|
|
|
}
|
|
|
|
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2015-04-04 01:40:15 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:08:13 +01:00
|
|
|
private:
|
2016-10-18 22:59:26 +02:00
|
|
|
sp<IGatekeeper> hw_device;
|
2017-04-12 22:03:04 +02:00
|
|
|
|
|
|
|
bool clear_state_if_needed_done;
|
2019-02-16 00:59:39 +01:00
|
|
|
bool is_running_gsi;
|
2015-04-04 01:40:15 +02:00
|
|
|
};
|
2020-12-08 22:08:13 +01:00
|
|
|
} // namespace android
|
2015-04-04 01:40:15 +02:00
|
|
|
|
2015-04-16 22:16:24 +02:00
|
|
|
int main(int argc, char* argv[]) {
|
2015-04-04 01:40:15 +02:00
|
|
|
ALOGI("Starting gatekeeperd...");
|
2015-04-16 22:16:24 +02:00
|
|
|
if (argc < 2) {
|
|
|
|
ALOGE("A directory must be specified!");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (chdir(argv[1]) == -1) {
|
|
|
|
ALOGE("chdir: %s: %s", argv[1], strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-04 01:40:15 +02:00
|
|
|
android::sp<android::IServiceManager> sm = android::defaultServiceManager();
|
|
|
|
android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
|
|
|
|
android::status_t ret = sm->addService(
|
|
|
|
android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
|
|
|
|
if (ret != android::OK) {
|
|
|
|
ALOGE("Couldn't register binder service!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're the only thread in existence, so we're just going to process
|
|
|
|
* Binder transaction as a single-threaded program.
|
|
|
|
*/
|
|
|
|
android::IPCThreadState::self()->joinThreadPool();
|
|
|
|
return 0;
|
|
|
|
}
|