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