blob: f35f360decb1d85bb1926cc2a91ed251ffcafee3 [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001/*
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
23using ::android::binder::Status;
24
25namespace android {
26
27ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {}
28
29Status 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
34Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Steven Morelanda9fe4742019-07-18 14:45:20 -070035 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070036
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 Morelanda9fe4742019-07-18 14:45:20 -070055 if (!mAccess->canFind(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -070056 // 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 Moreland905e2e82019-07-17 11:05:45 -070065bool 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 Moreland80e1e6d2019-06-21 12:35:59 -070080Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -070081 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070082
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 Morelanda9fe4742019-07-18 14:45:20 -070088 if (!mAccess->canAdd(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -070089 return Status::fromExceptionCode(Status::EX_SECURITY);
90 }
91
92 if (binder == nullptr) {
93 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
94 }
95
Steven Moreland905e2e82019-07-17 11:05:45 -070096 if (!isValidServiceName(name)) {
97 LOG(ERROR) << "Invalid service name: " << name;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070098 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
99 }
100
Steven Moreland88860b02019-08-12 14:24:14 -0700101 // implicitly unlinked when the binder is removed
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700102 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 Moreland80e1e6d2019-06-21 12:35:59 -0700107 mNameToService[name] = Service {
108 .binder = binder,
109 .allowIsolated = allowIsolated,
110 .dumpPriority = dumpPriority,
111 };
112
113 return Status::ok();
114}
115
116Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700117 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700118 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
142void 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