blob: be2c702034e102eba87d15870fe84fd605683c9f [file] [log] [blame]
Dianne Hackborn23eb1e22015-10-08 00:35:271/*
2 * Copyright (C) 2015 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#define LOG_TAG "cmd"
18
19#include <utils/Log.h>
20#include <binder/Parcel.h>
21#include <binder/ProcessState.h>
22#include <binder/IResultReceiver.h>
23#include <binder/IServiceManager.h>
Dianne Hackborn1941a402016-08-29 19:30:4324#include <binder/IShellCallback.h>
Dianne Hackborn23eb1e22015-10-08 00:35:2725#include <binder/TextOutput.h>
Dianne Hackborn3d9eb952016-10-18 00:40:4726#include <utils/Condition.h>
27#include <utils/Mutex.h>
Dianne Hackborn23eb1e22015-10-08 00:35:2728#include <utils/Vector.h>
29
30#include <getopt.h>
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34#include <unistd.h>
Dianne Hackborn1941a402016-08-29 19:30:4335#include <fcntl.h>
Dianne Hackborn23eb1e22015-10-08 00:35:2736#include <sys/time.h>
Dianne Hackborn1941a402016-08-29 19:30:4337#include <errno.h>
Jiyong Park4e7d18a2017-08-07 11:48:3238#include <memory>
Dianne Hackborn1941a402016-08-29 19:30:4339
40#include "selinux/selinux.h"
41#include "selinux/android.h"
42
Alex Buynytskyya39d87a2018-12-13 01:40:5243#include "cmd.h"
44
Dianne Hackborn3d9eb952016-10-18 00:40:4745#define DEBUG 0
46
Dianne Hackborn23eb1e22015-10-08 00:35:2747using namespace android;
48
49static int sort_func(const String16* lhs, const String16* rhs)
50{
51 return lhs->compare(*rhs);
52}
53
Dianne Hackborn1941a402016-08-29 19:30:4354struct SecurityContext_Delete {
55 void operator()(security_context_t p) const {
56 freecon(p);
57 }
58};
Jiyong Park4e7d18a2017-08-07 11:48:3259typedef std::unique_ptr<char[], SecurityContext_Delete> Unique_SecurityContext;
Dianne Hackborn1941a402016-08-29 19:30:4360
61class MyShellCallback : public BnShellCallback
62{
63public:
Alex Buynytskyya39d87a2018-12-13 01:40:5264 TextOutput& mErrorLog;
Dianne Hackborne5ed1992016-10-10 23:35:4565 bool mActive = true;
66
Alex Buynytskyya39d87a2018-12-13 01:40:5267 MyShellCallback(TextOutput& errorLog) : mErrorLog(errorLog) {}
68
Dianne Hackborn4217f8e2017-10-30 21:31:4169 virtual int openFile(const String16& path, const String16& seLinuxContext,
70 const String16& mode) {
Dianne Hackborn1941a402016-08-29 19:30:4371 String8 path8(path);
72 char cwd[256];
73 getcwd(cwd, 256);
74 String8 fullPath(cwd);
75 fullPath.appendPath(path8);
Dianne Hackborne5ed1992016-10-10 23:35:4576 if (!mActive) {
Alex Buynytskyya39d87a2018-12-13 01:40:5277 mErrorLog << "Open attempt after active for: " << fullPath << endl;
Dianne Hackborne5ed1992016-10-10 23:35:4578 return -EPERM;
79 }
Dianne Hackborn228f2f62017-11-14 19:18:0880#if DEBUG
81 ALOGD("openFile: %s, full=%s", path8.string(), fullPath.string());
82#endif
Dianne Hackborn4217f8e2017-10-30 21:31:4183 int flags = 0;
84 bool checkRead = false;
85 bool checkWrite = false;
Alex Buynytskyya39d87a2018-12-13 01:40:5286 if (mode == u"w") {
Dianne Hackborn4217f8e2017-10-30 21:31:4187 flags = O_WRONLY|O_CREAT|O_TRUNC;
88 checkWrite = true;
Alex Buynytskyya39d87a2018-12-13 01:40:5289 } else if (mode == u"w+") {
Dianne Hackborn4217f8e2017-10-30 21:31:4190 flags = O_RDWR|O_CREAT|O_TRUNC;
91 checkRead = checkWrite = true;
Alex Buynytskyya39d87a2018-12-13 01:40:5292 } else if (mode == u"r") {
Dianne Hackborn4217f8e2017-10-30 21:31:4193 flags = O_RDONLY;
94 checkRead = true;
Alex Buynytskyya39d87a2018-12-13 01:40:5295 } else if (mode == u"r+") {
Dianne Hackborn4217f8e2017-10-30 21:31:4196 flags = O_RDWR;
97 checkRead = checkWrite = true;
98 } else {
Alex Buynytskyya39d87a2018-12-13 01:40:5299 mErrorLog << "Invalid mode requested: " << mode.string() << endl;
Dianne Hackborn4217f8e2017-10-30 21:31:41100 return -EINVAL;
101 }
102 int fd = open(fullPath.string(), flags, S_IRWXU|S_IRWXG);
Dianne Hackborn228f2f62017-11-14 19:18:08103#if DEBUG
104 ALOGD("openFile: fd=%d", fd);
105#endif
Dianne Hackborn1941a402016-08-29 19:30:43106 if (fd < 0) {
107 return fd;
108 }
109 if (is_selinux_enabled() && seLinuxContext.size() > 0) {
110 String8 seLinuxContext8(seLinuxContext);
Alex Buynytskyy4ad5b4b2018-12-19 16:21:24111 security_context_t tmp = nullptr;
Chih-Hung Hsiehf463e182017-12-05 18:20:12112 getfilecon(fullPath.string(), &tmp);
Dianne Hackborn1941a402016-08-29 19:30:43113 Unique_SecurityContext context(tmp);
Dianne Hackborn4217f8e2017-10-30 21:31:41114 if (checkWrite) {
115 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
Alex Buynytskyy4ad5b4b2018-12-19 16:21:24116 "file", "write", nullptr);
Dianne Hackborn4217f8e2017-10-30 21:31:41117 if (accessGranted != 0) {
Dianne Hackborn228f2f62017-11-14 19:18:08118#if DEBUG
119 ALOGD("openFile: failed selinux write check!");
120#endif
Dianne Hackborn4217f8e2017-10-30 21:31:41121 close(fd);
Alex Buynytskyya39d87a2018-12-13 01:40:52122 mErrorLog << "System server has no access to write file context " << context.get() << " (from path " << fullPath.string() << ", context " << seLinuxContext8.string() << ")" << endl;
Dianne Hackborn4217f8e2017-10-30 21:31:41123 return -EPERM;
124 }
125 }
126 if (checkRead) {
127 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
Alex Buynytskyy4ad5b4b2018-12-19 16:21:24128 "file", "read", nullptr);
Dianne Hackborn4217f8e2017-10-30 21:31:41129 if (accessGranted != 0) {
Dianne Hackborn228f2f62017-11-14 19:18:08130#if DEBUG
131 ALOGD("openFile: failed selinux read check!");
132#endif
Dianne Hackborn4217f8e2017-10-30 21:31:41133 close(fd);
Alex Buynytskyya39d87a2018-12-13 01:40:52134 mErrorLog << "System server has no access to read file context " << context.get() << " (from path " << fullPath.string() << ", context " << seLinuxContext8.string() << ")" << endl;
Dianne Hackborn4217f8e2017-10-30 21:31:41135 return -EPERM;
136 }
Dianne Hackborn1941a402016-08-29 19:30:43137 }
138 }
139 return fd;
140 }
141};
142
Dianne Hackborn23eb1e22015-10-08 00:35:27143class MyResultReceiver : public BnResultReceiver
144{
145public:
Dianne Hackborn3d9eb952016-10-18 00:40:47146 Mutex mMutex;
147 Condition mCondition;
148 bool mHaveResult = false;
149 int32_t mResult = 0;
150
151 virtual void send(int32_t resultCode) {
152 AutoMutex _l(mMutex);
153 mResult = resultCode;
154 mHaveResult = true;
155 mCondition.signal();
156 }
157
158 int32_t waitForResult() {
159 AutoMutex _l(mMutex);
160 while (!mHaveResult) {
161 mCondition.wait(mMutex);
162 }
163 return mResult;
Dianne Hackborn23eb1e22015-10-08 00:35:27164 }
165};
166
Alex Buynytskyya39d87a2018-12-13 01:40:52167int cmdMain(const std::vector<std::string_view>& argv, TextOutput& outputLog, TextOutput& errorLog,
168 int in, int out, int err, RunMode runMode) {
Dianne Hackborn23eb1e22015-10-08 00:35:27169 sp<ProcessState> proc = ProcessState::self();
Dianne Hackborn23eb1e22015-10-08 00:35:27170 proc->startThreadPool();
171
Dianne Hackborn228f2f62017-11-14 19:18:08172#if DEBUG
173 ALOGD("cmd: starting");
174#endif
Dianne Hackborn23eb1e22015-10-08 00:35:27175 sp<IServiceManager> sm = defaultServiceManager();
Alex Buynytskyya39d87a2018-12-13 01:40:52176 if (runMode == RunMode::kStandalone) {
177 fflush(stdout);
178 }
Alex Buynytskyy4ad5b4b2018-12-19 16:21:24179 if (sm == nullptr) {
Dianne Hackborn3d9eb952016-10-18 00:40:47180 ALOGW("Unable to get default service manager!");
Alex Buynytskyya39d87a2018-12-13 01:40:52181 errorLog << "cmd: Unable to get default service manager!" << endl;
Dianne Hackborn23eb1e22015-10-08 00:35:27182 return 20;
183 }
184
Alex Buynytskyya39d87a2018-12-13 01:40:52185 int argc = argv.size();
186
187 if (argc == 0) {
Jon Spivacke82eaa82020-02-14 02:10:45188 errorLog << "cmd: No service specified; use -l to list all running services. Use -w to start and wait for a service." << endl;
Dianne Hackborn23eb1e22015-10-08 00:35:27189 return 20;
190 }
191
Alex Buynytskyya39d87a2018-12-13 01:40:52192 if ((argc == 1) && (argv[0] == "-l")) {
Dianne Hackborn23eb1e22015-10-08 00:35:27193 Vector<String16> services = sm->listServices();
194 services.sort(sort_func);
Alex Buynytskyya39d87a2018-12-13 01:40:52195 outputLog << "Currently running services:" << endl;
Dianne Hackborn23eb1e22015-10-08 00:35:27196
197 for (size_t i=0; i<services.size(); i++) {
198 sp<IBinder> service = sm->checkService(services[i]);
Alex Buynytskyy4ad5b4b2018-12-19 16:21:24199 if (service != nullptr) {
Alex Buynytskyya39d87a2018-12-13 01:40:52200 outputLog << " " << services[i] << endl;
Dianne Hackborn23eb1e22015-10-08 00:35:27201 }
202 }
203 return 0;
204 }
205
Jon Spivacke82eaa82020-02-14 02:10:45206 bool waitForService = ((argc > 1) && (argv[0] == "-w"));
207 int serviceIdx = (waitForService) ? 1 : 0;
208 const auto cmd = argv[serviceIdx];
Alex Buynytskyya39d87a2018-12-13 01:40:52209
Dianne Hackborn23eb1e22015-10-08 00:35:27210 Vector<String16> args;
Alex Buynytskyya39d87a2018-12-13 01:40:52211 String16 serviceName = String16(cmd.data(), cmd.size());
Jon Spivacke82eaa82020-02-14 02:10:45212 for (int i = serviceIdx + 1; i < argc; i++) {
Alex Buynytskyya39d87a2018-12-13 01:40:52213 args.add(String16(argv[i].data(), argv[i].size()));
Dianne Hackborn23eb1e22015-10-08 00:35:27214 }
Jon Spivacke82eaa82020-02-14 02:10:45215 sp<IBinder> service;
216 if(waitForService) {
217 service = sm->waitForService(serviceName);
218 } else {
219 service = sm->checkService(serviceName);
220 }
221
Alex Buynytskyy4ad5b4b2018-12-19 16:21:24222 if (service == nullptr) {
Alex Buynytskyya39d87a2018-12-13 01:40:52223 if (runMode == RunMode::kStandalone) {
224 ALOGW("Can't find service %.*s", static_cast<int>(cmd.size()), cmd.data());
225 }
226 errorLog << "cmd: Can't find service: " << cmd << endl;
Dianne Hackborn23eb1e22015-10-08 00:35:27227 return 20;
228 }
229
Alex Buynytskyya39d87a2018-12-13 01:40:52230 sp<MyShellCallback> cb = new MyShellCallback(errorLog);
Dianne Hackborn3d9eb952016-10-18 00:40:47231 sp<MyResultReceiver> result = new MyResultReceiver();
232
233#if DEBUG
Dominic Lemire3abc51c2020-01-23 20:24:36234 ALOGD("cmd: Invoking %.*s in=%d, out=%d, err=%d",
235 static_cast<int>(cmd.size()), cmd.data(), in, out, err);
Dianne Hackborn3d9eb952016-10-18 00:40:47236#endif
Dianne Hackborne5ed1992016-10-10 23:35:45237
Dianne Hackborn23eb1e22015-10-08 00:35:27238 // TODO: block until a result is returned to MyResultReceiver.
Alex Buynytskyya39d87a2018-12-13 01:40:52239 status_t error = IBinder::shellCommand(service, in, out, err, args, cb, result);
240 if (error < 0) {
Dianne Hackborn3d9eb952016-10-18 00:40:47241 const char* errstr;
Alex Buynytskyya39d87a2018-12-13 01:40:52242 switch (error) {
Dianne Hackborn3d9eb952016-10-18 00:40:47243 case BAD_TYPE: errstr = "Bad type"; break;
244 case FAILED_TRANSACTION: errstr = "Failed transaction"; break;
245 case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break;
246 case UNEXPECTED_NULL: errstr = "Unexpected null"; break;
Alex Buynytskyya39d87a2018-12-13 01:40:52247 default: errstr = strerror(-error); break;
Dianne Hackborn3d9eb952016-10-18 00:40:47248 }
Alex Buynytskyya39d87a2018-12-13 01:40:52249 if (runMode == RunMode::kStandalone) {
250 ALOGW("Failure calling service %.*s: %s (%d)", static_cast<int>(cmd.size()), cmd.data(),
251 errstr, -error);
252 }
253 outputLog << "cmd: Failure calling service " << cmd << ": " << errstr << " (" << (-error)
254 << ")" << endl;
255 return error;
Dianne Hackborn3d9eb952016-10-18 00:40:47256 }
Dianne Hackborne5ed1992016-10-10 23:35:45257
258 cb->mActive = false;
Dianne Hackborn3d9eb952016-10-18 00:40:47259 status_t res = result->waitForResult();
260#if DEBUG
261 ALOGD("result=%d", (int)res);
262#endif
263 return res;
Dianne Hackborn23eb1e22015-10-08 00:35:27264}