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