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 "ServiceManager.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 21 | #include <binder/Stability.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 22 | #include <cutils/android_filesystem_config.h> |
| 23 | #include <cutils/multiuser.h> |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 24 | #include <thread> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 25 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 26 | #ifndef VENDORSERVICEMANAGER |
| 27 | #include <vintf/VintfObject.h> |
| 28 | #include <vintf/constants.h> |
| 29 | #endif // !VENDORSERVICEMANAGER |
| 30 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 31 | using ::android::binder::Status; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 32 | using ::android::internal::Stability; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 36 | #ifndef VENDORSERVICEMANAGER |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame^] | 37 | static bool isVintfDeclared(const std::string& name) { |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 38 | size_t firstSlash = name.find('/'); |
| 39 | size_t lastDot = name.rfind('.', firstSlash); |
| 40 | if (firstSlash == std::string::npos || lastDot == std::string::npos) { |
| 41 | LOG(ERROR) << "VINTF HALs require names in the format type/instance (e.g. " |
| 42 | << "some.package.foo.IFoo/default) but got: " << name; |
| 43 | return false; |
| 44 | } |
| 45 | const std::string package = name.substr(0, lastDot); |
| 46 | const std::string iface = name.substr(lastDot+1, firstSlash-lastDot-1); |
| 47 | const std::string instance = name.substr(firstSlash+1); |
| 48 | |
| 49 | for (const auto& manifest : { |
| 50 | vintf::VintfObject::GetDeviceHalManifest(), |
| 51 | vintf::VintfObject::GetFrameworkHalManifest() |
| 52 | }) { |
| 53 | if (manifest->hasAidlInstance(package, iface, instance)) { |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | LOG(ERROR) << "Could not find " << package << "." << iface << "/" << instance |
| 58 | << " in the VINTF manifest."; |
| 59 | return false; |
| 60 | } |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame^] | 61 | |
| 62 | static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) { |
| 63 | if (!Stability::requiresVintfDeclaration(binder)) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | return isVintfDeclared(name); |
| 68 | } |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 69 | #endif // !VENDORSERVICEMANAGER |
| 70 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 71 | ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {} |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 72 | ServiceManager::~ServiceManager() { |
| 73 | // this should only happen in tests |
| 74 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 75 | for (const auto& [name, callbacks] : mNameToCallback) { |
| 76 | CHECK(!callbacks.empty()) << name; |
| 77 | for (const auto& callback : callbacks) { |
| 78 | CHECK(callback != nullptr) << name; |
| 79 | } |
| 80 | } |
| 81 | |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 82 | for (const auto& [name, service] : mNameToService) { |
| 83 | CHECK(service.binder != nullptr) << name; |
| 84 | } |
| 85 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 86 | |
| 87 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 88 | *outBinder = tryGetService(name, true); |
| 89 | // returns ok regardless of result for legacy reasons |
| 90 | return Status::ok(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 94 | *outBinder = tryGetService(name, false); |
| 95 | // returns ok regardless of result for legacy reasons |
| 96 | return Status::ok(); |
| 97 | } |
| 98 | |
| 99 | sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 100 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 101 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 102 | sp<IBinder> out; |
| 103 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 104 | const Service& service = it->second; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 105 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 106 | if (!service.allowIsolated) { |
| 107 | uid_t appid = multiuser_get_app_id(ctx.uid); |
| 108 | bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 109 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 110 | if (isIsolated) { |
| 111 | return nullptr; |
| 112 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 113 | } |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 114 | out = service.binder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 117 | if (!mAccess->canFind(ctx, name)) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 118 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 121 | if (!out && startIfNotFound) { |
| 122 | tryStartService(name); |
| 123 | } |
| 124 | |
| 125 | return out; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 128 | bool isValidServiceName(const std::string& name) { |
| 129 | if (name.size() == 0) return false; |
| 130 | if (name.size() > 127) return false; |
| 131 | |
| 132 | for (char c : name) { |
Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 133 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 134 | if (c >= 'a' && c <= 'z') continue; |
| 135 | if (c >= 'A' && c <= 'Z') continue; |
| 136 | if (c >= '0' && c <= '9') continue; |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 143 | Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 144 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 145 | |
| 146 | // apps cannot add services |
| 147 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { |
| 148 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 149 | } |
| 150 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 151 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 152 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 153 | } |
| 154 | |
| 155 | if (binder == nullptr) { |
| 156 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 157 | } |
| 158 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 159 | if (!isValidServiceName(name)) { |
| 160 | LOG(ERROR) << "Invalid service name: " << name; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 161 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 162 | } |
| 163 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 164 | #ifndef VENDORSERVICEMANAGER |
| 165 | if (!meetsDeclarationRequirements(binder, name)) { |
| 166 | // already logged |
| 167 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 168 | } |
| 169 | #endif // !VENDORSERVICEMANAGER |
| 170 | |
Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 171 | // implicitly unlinked when the binder is removed |
Steven Moreland | 46f380b | 2019-10-16 16:28:21 -0700 | [diff] [blame] | 172 | if (binder->remoteBinder() != nullptr && binder->linkToDeath(this) != OK) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 173 | LOG(ERROR) << "Could not linkToDeath when adding " << name; |
| 174 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 175 | } |
| 176 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 177 | mNameToService[name] = Service { |
| 178 | .binder = binder, |
| 179 | .allowIsolated = allowIsolated, |
| 180 | .dumpPriority = dumpPriority, |
| 181 | }; |
| 182 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 183 | auto it = mNameToCallback.find(name); |
| 184 | if (it != mNameToCallback.end()) { |
| 185 | for (const sp<IServiceCallback>& cb : it->second) { |
| 186 | // permission checked in registerForNotifications |
| 187 | cb->onRegistration(name, binder); |
| 188 | } |
| 189 | } |
| 190 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 191 | return Status::ok(); |
| 192 | } |
| 193 | |
| 194 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 195 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 196 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 197 | } |
| 198 | |
| 199 | size_t toReserve = 0; |
| 200 | for (auto const& [name, service] : mNameToService) { |
| 201 | (void) name; |
| 202 | |
| 203 | if (service.dumpPriority & dumpPriority) ++toReserve; |
| 204 | } |
| 205 | |
| 206 | CHECK(outList->empty()); |
| 207 | |
| 208 | outList->reserve(toReserve); |
| 209 | for (auto const& [name, service] : mNameToService) { |
| 210 | (void) service; |
| 211 | |
| 212 | if (service.dumpPriority & dumpPriority) { |
| 213 | outList->push_back(name); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return Status::ok(); |
| 218 | } |
| 219 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 220 | Status ServiceManager::registerForNotifications( |
| 221 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 222 | auto ctx = mAccess->getCallingContext(); |
| 223 | |
| 224 | if (!mAccess->canFind(ctx, name)) { |
| 225 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 226 | } |
| 227 | |
| 228 | if (!isValidServiceName(name)) { |
| 229 | LOG(ERROR) << "Invalid service name: " << name; |
| 230 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 231 | } |
| 232 | |
| 233 | if (callback == nullptr) { |
| 234 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 235 | } |
| 236 | |
| 237 | if (OK != IInterface::asBinder(callback)->linkToDeath(this)) { |
| 238 | LOG(ERROR) << "Could not linkToDeath when adding " << name; |
| 239 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 240 | } |
| 241 | |
| 242 | mNameToCallback[name].push_back(callback); |
| 243 | |
| 244 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 245 | const sp<IBinder>& binder = it->second.binder; |
| 246 | |
| 247 | // never null if an entry exists |
| 248 | CHECK(binder != nullptr) << name; |
| 249 | callback->onRegistration(name, binder); |
| 250 | } |
| 251 | |
| 252 | return Status::ok(); |
| 253 | } |
| 254 | Status ServiceManager::unregisterForNotifications( |
| 255 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 256 | auto ctx = mAccess->getCallingContext(); |
| 257 | |
| 258 | if (!mAccess->canFind(ctx, name)) { |
| 259 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 260 | } |
| 261 | |
| 262 | bool found = false; |
| 263 | |
| 264 | auto it = mNameToCallback.find(name); |
| 265 | if (it != mNameToCallback.end()) { |
| 266 | removeCallback(IInterface::asBinder(callback), &it, &found); |
| 267 | } |
| 268 | |
| 269 | if (!found) { |
| 270 | LOG(ERROR) << "Trying to unregister callback, but none exists " << name; |
| 271 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 272 | } |
| 273 | |
| 274 | return Status::ok(); |
| 275 | } |
| 276 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame^] | 277 | Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) { |
| 278 | auto ctx = mAccess->getCallingContext(); |
| 279 | |
| 280 | if (!mAccess->canFind(ctx, name)) { |
| 281 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 282 | } |
| 283 | |
| 284 | *outReturn = false; |
| 285 | |
| 286 | #ifndef VENDORSERVICEMANAGER |
| 287 | *outReturn = isVintfDeclared(name); |
| 288 | #endif |
| 289 | return Status::ok(); |
| 290 | } |
| 291 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 292 | void ServiceManager::removeCallback(const wp<IBinder>& who, |
| 293 | CallbackMap::iterator* it, |
| 294 | bool* found) { |
| 295 | std::vector<sp<IServiceCallback>>& listeners = (*it)->second; |
| 296 | |
| 297 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 298 | if (IInterface::asBinder(*lit) == who) { |
| 299 | if(found) *found = true; |
| 300 | lit = listeners.erase(lit); |
| 301 | } else { |
| 302 | ++lit; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (listeners.empty()) { |
| 307 | *it = mNameToCallback.erase(*it); |
| 308 | } else { |
| 309 | it++; |
| 310 | } |
| 311 | } |
| 312 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 313 | void ServiceManager::binderDied(const wp<IBinder>& who) { |
| 314 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { |
| 315 | if (who == it->second.binder) { |
| 316 | it = mNameToService.erase(it); |
| 317 | } else { |
| 318 | ++it; |
| 319 | } |
| 320 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 321 | |
| 322 | for (auto it = mNameToCallback.begin(); it != mNameToCallback.end();) { |
| 323 | removeCallback(who, &it, nullptr /*found*/); |
| 324 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 327 | void ServiceManager::tryStartService(const std::string& name) { |
| 328 | ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service", |
| 329 | name.c_str()); |
| 330 | |
| 331 | std::thread([=] { |
| 332 | (void)base::SetProperty("ctl.interface_start", "aidl/" + name); |
| 333 | }).detach(); |
| 334 | } |
| 335 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 336 | } // namespace android |