blob: 809872417de7ed78825ae6d16270825104775974 [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001/*
2 * Copyright (C) 2019 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#include "Access.h"
18
19#include <android-base/logging.h>
20#include <binder/IPCThreadState.h>
21#include <log/log_safetynet.h>
22#include <selinux/android.h>
23#include <selinux/avc.h>
24
Steven Moreland5759db02024-03-27 00:03:05 +000025#include <sstream>
26
Steven Moreland80e1e6d2019-06-21 12:35:59 -070027namespace android {
28
29#ifdef VENDORSERVICEMANAGER
30constexpr bool kIsVendor = true;
31#else
32constexpr bool kIsVendor = false;
33#endif
34
Pawanab56a472022-07-26 17:06:25 +000035#ifdef __ANDROID__
Steven Moreland80e1e6d2019-06-21 12:35:59 -070036static std::string getPidcon(pid_t pid) {
Yifan Hongba7fcc32020-06-03 16:53:07 +000037 android_errorWriteLog(0x534e4554, "121035042");
Steven Moreland80e1e6d2019-06-21 12:35:59 -070038
39 char* lookup = nullptr;
40 if (getpidcon(pid, &lookup) < 0) {
41 LOG(ERROR) << "SELinux: getpidcon(pid=" << pid << ") failed to retrieve pid context";
42 return "";
43 }
44 std::string result = lookup;
45 freecon(lookup);
46 return result;
47}
48
49static struct selabel_handle* getSehandle() {
50 static struct selabel_handle* gSehandle = nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070051 if (gSehandle != nullptr && selinux_status_updated()) {
52 selabel_close(gSehandle);
53 gSehandle = nullptr;
54 }
55
56 if (gSehandle == nullptr) {
57 gSehandle = kIsVendor
58 ? selinux_android_vendor_service_context_handle()
59 : selinux_android_service_context_handle();
60 }
61
62 CHECK(gSehandle != nullptr);
63 return gSehandle;
64}
65
Steven Moreland68039a12019-07-31 19:44:16 -070066struct AuditCallbackData {
67 const Access::CallingContext* context;
68 const std::string* tname;
69};
70
Steven Moreland80e1e6d2019-06-21 12:35:59 -070071static int auditCallback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
Steven Moreland68039a12019-07-31 19:44:16 -070072 const AuditCallbackData* ad = reinterpret_cast<AuditCallbackData*>(data);
Steven Moreland80e1e6d2019-06-21 12:35:59 -070073
74 if (!ad) {
75 LOG(ERROR) << "No service manager audit data";
76 return 0;
77 }
78
Steven Moreland68039a12019-07-31 19:44:16 -070079 snprintf(buf, len, "pid=%d uid=%d name=%s", ad->context->debugPid, ad->context->uid,
80 ad->tname->c_str());
Steven Moreland80e1e6d2019-06-21 12:35:59 -070081 return 0;
82}
Pawanab56a472022-07-26 17:06:25 +000083#endif
Steven Moreland80e1e6d2019-06-21 12:35:59 -070084
Steven Moreland5759db02024-03-27 00:03:05 +000085std::string Access::CallingContext::toDebugString() const {
86 std::stringstream ss;
87 ss << "Caller(pid=" << debugPid << ",uid=" << uid << ",sid=" << sid << ")";
88 return ss.str();
89}
90
Steven Moreland80e1e6d2019-06-21 12:35:59 -070091Access::Access() {
Pawanab56a472022-07-26 17:06:25 +000092#ifdef __ANDROID__
Steven Moreland80e1e6d2019-06-21 12:35:59 -070093 union selinux_callback cb;
94
95 cb.func_audit = auditCallback;
96 selinux_set_callback(SELINUX_CB_AUDIT, cb);
97
98 cb.func_log = kIsVendor ? selinux_vendor_log_callback : selinux_log_callback;
99 selinux_set_callback(SELINUX_CB_LOG, cb);
100
101 CHECK(selinux_status_open(true /*fallback*/) >= 0);
102
103 CHECK(getcon(&mThisProcessContext) == 0);
Pawanab56a472022-07-26 17:06:25 +0000104#endif
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700105}
106
107Access::~Access() {
108 freecon(mThisProcessContext);
109}
110
Steven Morelanda9fe4742019-07-18 14:45:20 -0700111Access::CallingContext Access::getCallingContext() {
Pawanab56a472022-07-26 17:06:25 +0000112#ifdef __ANDROID__
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700113 IPCThreadState* ipc = IPCThreadState::self();
114
115 const char* callingSid = ipc->getCallingSid();
116 pid_t callingPid = ipc->getCallingPid();
117
118 return CallingContext {
119 .debugPid = callingPid,
120 .uid = ipc->getCallingUid(),
121 .sid = callingSid ? std::string(callingSid) : getPidcon(callingPid),
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700122 };
Pawanab56a472022-07-26 17:06:25 +0000123#else
124 return CallingContext();
125#endif
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700126}
127
Steven Morelanda9fe4742019-07-18 14:45:20 -0700128bool Access::canFind(const CallingContext& ctx,const std::string& name) {
129 return actionAllowedFromLookup(ctx, name, "find");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700130}
131
Steven Morelanda9fe4742019-07-18 14:45:20 -0700132bool Access::canAdd(const CallingContext& ctx, const std::string& name) {
133 return actionAllowedFromLookup(ctx, name, "add");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700134}
135
136bool Access::canList(const CallingContext& ctx) {
Steven Moreland68039a12019-07-31 19:44:16 -0700137 return actionAllowed(ctx, mThisProcessContext, "list", "service_manager");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700138}
139
Steven Moreland68039a12019-07-31 19:44:16 -0700140bool Access::actionAllowed(const CallingContext& sctx, const char* tctx, const char* perm,
141 const std::string& tname) {
Pawanab56a472022-07-26 17:06:25 +0000142#ifdef __ANDROID__
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700143 const char* tclass = "service_manager";
144
Steven Moreland68039a12019-07-31 19:44:16 -0700145 AuditCallbackData data = {
146 .context = &sctx,
147 .tname = &tname,
148 };
149
150 return 0 == selinux_check_access(sctx.sid.c_str(), tctx, tclass, perm,
151 reinterpret_cast<void*>(&data));
Pawanab56a472022-07-26 17:06:25 +0000152#else
153 (void)sctx;
154 (void)tctx;
155 (void)perm;
156 (void)tname;
157
158 return true;
159#endif
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700160}
161
Steven Morelanda9fe4742019-07-18 14:45:20 -0700162bool Access::actionAllowedFromLookup(const CallingContext& sctx, const std::string& name, const char *perm) {
Pawanab56a472022-07-26 17:06:25 +0000163#ifdef __ANDROID__
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700164 char *tctx = nullptr;
Tri Vo6ea26982019-10-04 12:34:53 -0700165 if (selabel_lookup(getSehandle(), &tctx, name.c_str(), SELABEL_CTX_ANDROID_SERVICE) != 0) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700166 LOG(ERROR) << "SELinux: No match for " << name << " in service_contexts.\n";
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700167 return false;
168 }
169
Steven Moreland68039a12019-07-31 19:44:16 -0700170 bool allowed = actionAllowed(sctx, tctx, perm, name);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700171 freecon(tctx);
172 return allowed;
Pawanab56a472022-07-26 17:06:25 +0000173#else
174 (void)sctx;
175 (void)name;
176 (void)perm;
177 (void)kIsVendor;
178
179 return true;
180#endif
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700181}
182
183} // android