blob: c8366ca65c0878bdddbe1159ffb3f94edb61c9f7 [file] [log] [blame]
Steven Morelandfe66b732019-02-01 14:29:45 -08001/*
2 * Copyright (C) 2017 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
Martijn Coenen39247422017-09-25 15:29:06 +020017#define LOG_TAG "hwservicemanager"
18
Martijn Coenen7ce83be2017-04-07 16:19:32 -070019#include <android-base/logging.h>
20#include <hidl-util/FQName.h>
21#include <log/log.h>
22
23#include "AccessControl.h"
24
25namespace android {
26
27static const char *kPermissionAdd = "add";
28static const char *kPermissionGet = "find";
29static const char *kPermissionList = "list";
30
31struct audit_data {
32 const char* interfaceName;
Steven Moreland7185a892019-01-09 18:00:05 -080033 const char* sid;
Martijn Coenen7ce83be2017-04-07 16:19:32 -070034 pid_t pid;
35};
36
37using android::FQName;
38
39AccessControl::AccessControl() {
40 mSeHandle = selinux_android_hw_service_context_handle();
Yi Kong00892ee2018-07-31 16:50:28 -070041 LOG_ALWAYS_FATAL_IF(mSeHandle == nullptr, "Failed to acquire SELinux handle.");
Martijn Coenen7ce83be2017-04-07 16:19:32 -070042
43 if (getcon(&mSeContext) != 0) {
44 LOG_ALWAYS_FATAL("Failed to acquire hwservicemanager context.");
45 }
46
47 selinux_status_open(true);
48
49 mSeCallbacks.func_audit = AccessControl::auditCallback;
50 selinux_set_callback(SELINUX_CB_AUDIT, mSeCallbacks);
51
52 mSeCallbacks.func_log = selinux_log_callback; /* defined in libselinux */
53 selinux_set_callback(SELINUX_CB_LOG, mSeCallbacks);
54}
55
Steven Moreland7185a892019-01-09 18:00:05 -080056bool AccessControl::canAdd(const std::string& fqName, const CallingContext& callingContext) {
Steven Moreland0cd38f02018-03-06 15:09:22 -080057 FQName fqIface;
Martijn Coenen7ce83be2017-04-07 16:19:32 -070058
Steven Moreland0cd38f02018-03-06 15:09:22 -080059 if (!FQName::parse(fqName, &fqIface)) {
Martijn Coenen7ce83be2017-04-07 16:19:32 -070060 return false;
61 }
62 const std::string checkName = fqIface.package() + "::" + fqIface.name();
63
Steven Moreland7185a892019-01-09 18:00:05 -080064 return checkPermission(callingContext, kPermissionAdd, checkName.c_str());
Martijn Coenen7ce83be2017-04-07 16:19:32 -070065}
66
Steven Moreland7185a892019-01-09 18:00:05 -080067bool AccessControl::canGet(const std::string& fqName, const CallingContext& callingContext) {
Steven Moreland0cd38f02018-03-06 15:09:22 -080068 FQName fqIface;
Martijn Coenen7ce83be2017-04-07 16:19:32 -070069
Steven Moreland0cd38f02018-03-06 15:09:22 -080070 if (!FQName::parse(fqName, &fqIface)) {
Martijn Coenen7ce83be2017-04-07 16:19:32 -070071 return false;
72 }
73 const std::string checkName = fqIface.package() + "::" + fqIface.name();
74
Steven Moreland7185a892019-01-09 18:00:05 -080075 return checkPermission(callingContext, kPermissionGet, checkName.c_str());
Martijn Coenen7ce83be2017-04-07 16:19:32 -070076}
77
Steven Moreland7185a892019-01-09 18:00:05 -080078bool AccessControl::canList(const CallingContext& callingContext) {
79 return checkPermission(callingContext, mSeContext, kPermissionList, nullptr);
Martijn Coenen7ce83be2017-04-07 16:19:32 -070080}
81
Steven Moreland7185a892019-01-09 18:00:05 -080082AccessControl::CallingContext AccessControl::getCallingContext(pid_t sourcePid) {
Yi Kong00892ee2018-07-31 16:50:28 -070083 char *sourceContext = nullptr;
Martijn Coenen7ce83be2017-04-07 16:19:32 -070084
85 if (getpidcon(sourcePid, &sourceContext) < 0) {
Martijn Coenen39247422017-09-25 15:29:06 +020086 ALOGE("SELinux: failed to retrieve process context for pid %d", sourcePid);
Steven Moreland7185a892019-01-09 18:00:05 -080087 return { false, "", sourcePid };
Steven Moreland4034c1c2017-11-03 17:42:27 -070088 }
89
Steven Moreland7185a892019-01-09 18:00:05 -080090 std::string context = sourceContext;
91 freecon(sourceContext);
92 return { true, context, sourcePid };
Steven Moreland4034c1c2017-11-03 17:42:27 -070093}
94
Steven Moreland7185a892019-01-09 18:00:05 -080095bool AccessControl::checkPermission(const CallingContext& source, const char *targetContext, const char *perm, const char *interface) {
96 if (!source.sidPresent) {
Martijn Coenen7ce83be2017-04-07 16:19:32 -070097 return false;
98 }
99
Steven Moreland4034c1c2017-11-03 17:42:27 -0700100 bool allowed = false;
Steven Moreland4034c1c2017-11-03 17:42:27 -0700101
Steven Moreland7185a892019-01-09 18:00:05 -0800102 struct audit_data ad;
103 ad.pid = source.pid;
104 ad.sid = source.sid.c_str();
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700105 ad.interfaceName = interface;
106
Steven Moreland7185a892019-01-09 18:00:05 -0800107 allowed = (selinux_check_access(source.sid.c_str(), targetContext, "hwservice_manager",
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700108 perm, (void *) &ad) == 0);
109
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700110 return allowed;
111}
112
Steven Moreland7185a892019-01-09 18:00:05 -0800113bool AccessControl::checkPermission(const CallingContext& source, const char *perm, const char *interface) {
Yi Kong00892ee2018-07-31 16:50:28 -0700114 char *targetContext = nullptr;
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700115 bool allowed = false;
116
117 // Lookup service in hwservice_contexts
118 if (selabel_lookup(mSeHandle, &targetContext, interface, 0) != 0) {
119 ALOGE("No match for interface %s in hwservice_contexts", interface);
120 return false;
121 }
122
Steven Moreland7185a892019-01-09 18:00:05 -0800123 allowed = checkPermission(source, targetContext, perm, interface);
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700124
125 freecon(targetContext);
126
127 return allowed;
128}
129
130int AccessControl::auditCallback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
131 struct audit_data *ad = (struct audit_data *)data;
132
133 if (!ad || !ad->interfaceName) {
134 ALOGE("No valid hwservicemanager audit data");
135 return 0;
136 }
137
Steven Moreland7185a892019-01-09 18:00:05 -0800138 const char* sid = ad->sid ? ad->sid : "N/A";
139
140 snprintf(buf, len, "interface=%s sid=%s pid=%d", ad->interfaceName, sid, ad->pid);
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700141 return 0;
142}
143
144} // namespace android