Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | #ifdef VENDORSERVICEMANAGER |
| 30 | constexpr bool kIsVendor = true; |
| 31 | #else |
| 32 | constexpr bool kIsVendor = false; |
| 33 | #endif |
| 34 | |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 35 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 36 | static std::string getPidcon(pid_t pid) { |
Steven Moreland | 8a9bb84 | 2024-12-05 01:02:53 +0000 | [diff] [blame] | 37 | CHECK_EQ(nullptr, IPCThreadState::self()->getServingStackPointer()) |
| 38 | << "Did not get context from PID " << pid |
| 39 | << ". We should always get contexts from other processes."; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 40 | |
| 41 | char* lookup = nullptr; |
| 42 | if (getpidcon(pid, &lookup) < 0) { |
| 43 | LOG(ERROR) << "SELinux: getpidcon(pid=" << pid << ") failed to retrieve pid context"; |
| 44 | return ""; |
| 45 | } |
| 46 | std::string result = lookup; |
| 47 | freecon(lookup); |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | static struct selabel_handle* getSehandle() { |
| 52 | static struct selabel_handle* gSehandle = nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 53 | if (gSehandle != nullptr && selinux_status_updated()) { |
| 54 | selabel_close(gSehandle); |
| 55 | gSehandle = nullptr; |
| 56 | } |
| 57 | |
| 58 | if (gSehandle == nullptr) { |
| 59 | gSehandle = kIsVendor |
| 60 | ? selinux_android_vendor_service_context_handle() |
| 61 | : selinux_android_service_context_handle(); |
| 62 | } |
| 63 | |
| 64 | CHECK(gSehandle != nullptr); |
| 65 | return gSehandle; |
| 66 | } |
| 67 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 68 | struct AuditCallbackData { |
| 69 | const Access::CallingContext* context; |
| 70 | const std::string* tname; |
| 71 | }; |
| 72 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 73 | static int auditCallback(void *data, security_class_t /*cls*/, char *buf, size_t len) { |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 74 | const AuditCallbackData* ad = reinterpret_cast<AuditCallbackData*>(data); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 75 | |
| 76 | if (!ad) { |
| 77 | LOG(ERROR) << "No service manager audit data"; |
| 78 | return 0; |
| 79 | } |
| 80 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 81 | snprintf(buf, len, "pid=%d uid=%d name=%s", ad->context->debugPid, ad->context->uid, |
| 82 | ad->tname->c_str()); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | } |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 85 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 86 | |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame] | 87 | std::string Access::CallingContext::toDebugString() const { |
| 88 | std::stringstream ss; |
| 89 | ss << "Caller(pid=" << debugPid << ",uid=" << uid << ",sid=" << sid << ")"; |
| 90 | return ss.str(); |
| 91 | } |
| 92 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 93 | Access::Access() { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 94 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 95 | union selinux_callback cb; |
| 96 | |
| 97 | cb.func_audit = auditCallback; |
| 98 | selinux_set_callback(SELINUX_CB_AUDIT, cb); |
| 99 | |
| 100 | cb.func_log = kIsVendor ? selinux_vendor_log_callback : selinux_log_callback; |
| 101 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 102 | |
| 103 | CHECK(selinux_status_open(true /*fallback*/) >= 0); |
| 104 | |
| 105 | CHECK(getcon(&mThisProcessContext) == 0); |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 106 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | Access::~Access() { |
| 110 | freecon(mThisProcessContext); |
| 111 | } |
| 112 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 113 | Access::CallingContext Access::getCallingContext() { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 114 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 115 | IPCThreadState* ipc = IPCThreadState::self(); |
| 116 | |
| 117 | const char* callingSid = ipc->getCallingSid(); |
| 118 | pid_t callingPid = ipc->getCallingPid(); |
| 119 | |
| 120 | return CallingContext { |
| 121 | .debugPid = callingPid, |
| 122 | .uid = ipc->getCallingUid(), |
| 123 | .sid = callingSid ? std::string(callingSid) : getPidcon(callingPid), |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 124 | }; |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 125 | #else |
| 126 | return CallingContext(); |
| 127 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 130 | bool Access::canFind(const CallingContext& ctx,const std::string& name) { |
| 131 | return actionAllowedFromLookup(ctx, name, "find"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 134 | bool Access::canAdd(const CallingContext& ctx, const std::string& name) { |
| 135 | return actionAllowedFromLookup(ctx, name, "add"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool Access::canList(const CallingContext& ctx) { |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 139 | return actionAllowed(ctx, mThisProcessContext, "list", "service_manager"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 142 | bool Access::actionAllowed(const CallingContext& sctx, const char* tctx, const char* perm, |
| 143 | const std::string& tname) { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 144 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 145 | const char* tclass = "service_manager"; |
| 146 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 147 | AuditCallbackData data = { |
| 148 | .context = &sctx, |
| 149 | .tname = &tname, |
| 150 | }; |
| 151 | |
| 152 | return 0 == selinux_check_access(sctx.sid.c_str(), tctx, tclass, perm, |
| 153 | reinterpret_cast<void*>(&data)); |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 154 | #else |
| 155 | (void)sctx; |
| 156 | (void)tctx; |
| 157 | (void)perm; |
| 158 | (void)tname; |
| 159 | |
| 160 | return true; |
| 161 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 164 | bool Access::actionAllowedFromLookup(const CallingContext& sctx, const std::string& name, const char *perm) { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 165 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 166 | char *tctx = nullptr; |
Tri Vo | 6ea2698 | 2019-10-04 12:34:53 -0700 | [diff] [blame] | 167 | if (selabel_lookup(getSehandle(), &tctx, name.c_str(), SELABEL_CTX_ANDROID_SERVICE) != 0) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 168 | LOG(ERROR) << "SELinux: No match for " << name << " in service_contexts.\n"; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 172 | bool allowed = actionAllowed(sctx, tctx, perm, name); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 173 | freecon(tctx); |
| 174 | return allowed; |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 175 | #else |
| 176 | (void)sctx; |
| 177 | (void)name; |
| 178 | (void)perm; |
| 179 | (void)kIsVendor; |
| 180 | |
| 181 | return true; |
| 182 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // android |