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) { |
Yifan Hong | ba7fcc3 | 2020-06-03 16:53:07 +0000 | [diff] [blame] | 37 | android_errorWriteLog(0x534e4554, "121035042"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 38 | |
| 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 | |
| 49 | static struct selabel_handle* getSehandle() { |
| 50 | static struct selabel_handle* gSehandle = nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 51 | 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 Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 66 | struct AuditCallbackData { |
| 67 | const Access::CallingContext* context; |
| 68 | const std::string* tname; |
| 69 | }; |
| 70 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 71 | 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] | 72 | const AuditCallbackData* ad = reinterpret_cast<AuditCallbackData*>(data); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 73 | |
| 74 | if (!ad) { |
| 75 | LOG(ERROR) << "No service manager audit data"; |
| 76 | return 0; |
| 77 | } |
| 78 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 79 | snprintf(buf, len, "pid=%d uid=%d name=%s", ad->context->debugPid, ad->context->uid, |
| 80 | ad->tname->c_str()); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | } |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 83 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 84 | |
Steven Moreland | 5759db0 | 2024-03-27 00:03:05 +0000 | [diff] [blame^] | 85 | std::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 Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 91 | Access::Access() { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 92 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 93 | 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); |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 104 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | Access::~Access() { |
| 108 | freecon(mThisProcessContext); |
| 109 | } |
| 110 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 111 | Access::CallingContext Access::getCallingContext() { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 112 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 113 | 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 Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 122 | }; |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 123 | #else |
| 124 | return CallingContext(); |
| 125 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 128 | bool Access::canFind(const CallingContext& ctx,const std::string& name) { |
| 129 | return actionAllowedFromLookup(ctx, name, "find"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 132 | bool Access::canAdd(const CallingContext& ctx, const std::string& name) { |
| 133 | return actionAllowedFromLookup(ctx, name, "add"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool Access::canList(const CallingContext& ctx) { |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 137 | return actionAllowed(ctx, mThisProcessContext, "list", "service_manager"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 140 | bool Access::actionAllowed(const CallingContext& sctx, const char* tctx, const char* perm, |
| 141 | const std::string& tname) { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 142 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 143 | const char* tclass = "service_manager"; |
| 144 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 145 | 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)); |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 152 | #else |
| 153 | (void)sctx; |
| 154 | (void)tctx; |
| 155 | (void)perm; |
| 156 | (void)tname; |
| 157 | |
| 158 | return true; |
| 159 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 162 | bool Access::actionAllowedFromLookup(const CallingContext& sctx, const std::string& name, const char *perm) { |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 163 | #ifdef __ANDROID__ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 164 | char *tctx = nullptr; |
Tri Vo | 6ea2698 | 2019-10-04 12:34:53 -0700 | [diff] [blame] | 165 | 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] | 166 | LOG(ERROR) << "SELinux: No match for " << name << " in service_contexts.\n"; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
Steven Moreland | 68039a1 | 2019-07-31 19:44:16 -0700 | [diff] [blame] | 170 | bool allowed = actionAllowed(sctx, tctx, perm, name); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 171 | freecon(tctx); |
| 172 | return allowed; |
Pawan | ab56a47 | 2022-07-26 17:06:25 +0000 | [diff] [blame] | 173 | #else |
| 174 | (void)sctx; |
| 175 | (void)name; |
| 176 | (void)perm; |
| 177 | (void)kIsVendor; |
| 178 | |
| 179 | return true; |
| 180 | #endif |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | } // android |