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 | |
| 31 | for (const auto& [name, service] : mNameToService) { |
| 32 | CHECK(service.binder != nullptr) << name; |
| 33 | } |
| 34 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 35 | |
| 36 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { |
| 37 | // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons. |
| 38 | return checkService(name, outBinder); |
| 39 | } |
| 40 | |
| 41 | Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 42 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 43 | |
| 44 | auto it = mNameToService.find(name); |
| 45 | if (it == mNameToService.end()) { |
| 46 | *outBinder = nullptr; |
| 47 | return Status::ok(); |
| 48 | } |
| 49 | |
| 50 | const Service& service = it->second; |
| 51 | |
| 52 | if (!service.allowIsolated) { |
| 53 | uid_t appid = multiuser_get_app_id(ctx.uid); |
| 54 | bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END; |
| 55 | |
| 56 | if (isIsolated) { |
| 57 | *outBinder = nullptr; |
| 58 | return Status::ok(); |
| 59 | } |
| 60 | } |
| 61 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 62 | if (!mAccess->canFind(ctx, name)) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 63 | // returns ok and null for legacy reasons |
| 64 | *outBinder = nullptr; |
| 65 | return Status::ok(); |
| 66 | } |
| 67 | |
| 68 | *outBinder = service.binder; |
| 69 | return Status::ok(); |
| 70 | } |
| 71 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 72 | bool isValidServiceName(const std::string& name) { |
| 73 | if (name.size() == 0) return false; |
| 74 | if (name.size() > 127) return false; |
| 75 | |
| 76 | for (char c : name) { |
Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 77 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 78 | if (c >= 'a' && c <= 'z') continue; |
| 79 | if (c >= 'A' && c <= 'Z') continue; |
| 80 | if (c >= '0' && c <= '9') continue; |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 87 | 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] | 88 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 89 | |
| 90 | // apps cannot add services |
| 91 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { |
| 92 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 93 | } |
| 94 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 95 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 96 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 97 | } |
| 98 | |
| 99 | if (binder == nullptr) { |
| 100 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 101 | } |
| 102 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 103 | if (!isValidServiceName(name)) { |
| 104 | LOG(ERROR) << "Invalid service name: " << name; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 105 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 106 | } |
| 107 | |
Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 108 | // implicitly unlinked when the binder is removed |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 109 | if (OK != binder->linkToDeath(this)) { |
| 110 | LOG(ERROR) << "Could not linkToDeath when adding " << name; |
| 111 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 112 | } |
| 113 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 114 | mNameToService[name] = Service { |
| 115 | .binder = binder, |
| 116 | .allowIsolated = allowIsolated, |
| 117 | .dumpPriority = dumpPriority, |
| 118 | }; |
| 119 | |
| 120 | return Status::ok(); |
| 121 | } |
| 122 | |
| 123 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 124 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 125 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 126 | } |
| 127 | |
| 128 | size_t toReserve = 0; |
| 129 | for (auto const& [name, service] : mNameToService) { |
| 130 | (void) name; |
| 131 | |
| 132 | if (service.dumpPriority & dumpPriority) ++toReserve; |
| 133 | } |
| 134 | |
| 135 | CHECK(outList->empty()); |
| 136 | |
| 137 | outList->reserve(toReserve); |
| 138 | for (auto const& [name, service] : mNameToService) { |
| 139 | (void) service; |
| 140 | |
| 141 | if (service.dumpPriority & dumpPriority) { |
| 142 | outList->push_back(name); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return Status::ok(); |
| 147 | } |
| 148 | |
| 149 | void ServiceManager::binderDied(const wp<IBinder>& who) { |
| 150 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { |
| 151 | if (who == it->second.binder) { |
| 152 | it = mNameToService.erase(it); |
| 153 | } else { |
| 154 | ++it; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | } // namespace android |