blob: 5f7dc25d594ba4909c5450c7fb68664612df8b28 [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
101 if (OK != binder->linkToDeath(this)) {
102 LOG(ERROR) << "Could not linkToDeath when adding " << name;
103 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
104 }
105
106 auto it = mNameToService.find(name);
107 if (it != mNameToService.end()) {
108 if (OK != it->second.binder->unlinkToDeath(this)) {
109 LOG(WARNING) << "Could not unlinkToDeath when adding " << name;
110 }
111 }
112
113 mNameToService[name] = Service {
114 .binder = binder,
115 .allowIsolated = allowIsolated,
116 .dumpPriority = dumpPriority,
117 };
118
119 return Status::ok();
120}
121
122Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700123 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700124 return Status::fromExceptionCode(Status::EX_SECURITY);
125 }
126
127 size_t toReserve = 0;
128 for (auto const& [name, service] : mNameToService) {
129 (void) name;
130
131 if (service.dumpPriority & dumpPriority) ++toReserve;
132 }
133
134 CHECK(outList->empty());
135
136 outList->reserve(toReserve);
137 for (auto const& [name, service] : mNameToService) {
138 (void) service;
139
140 if (service.dumpPriority & dumpPriority) {
141 outList->push_back(name);
142 }
143 }
144
145 return Status::ok();
146}
147
148void ServiceManager::binderDied(const wp<IBinder>& who) {
149 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
150 if (who == it->second.binder) {
151 it = mNameToService.erase(it);
152 } else {
153 ++it;
154 }
155 }
156}
157
158} // namespace android