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