blob: 8f1c01ab2cf86bc1b6a8184437cb7730a05ce719 [file] [log] [blame]
Dianne Hackborn23eb1e22015-10-07 17:35:27 -07001/*
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 12:30:43 -070024#include <binder/IShellCallback.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070025#include <binder/TextOutput.h>
Dianne Hackborn3d9eb952016-10-17 17:40:47 -070026#include <utils/Condition.h>
27#include <utils/Mutex.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070028#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 12:30:43 -070035#include <fcntl.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070036#include <sys/time.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070037#include <errno.h>
Jiyong Park4e7d18a2017-08-07 20:48:32 +090038#include <memory>
Dianne Hackborn1941a402016-08-29 12:30:43 -070039
40#include "selinux/selinux.h"
41#include "selinux/android.h"
42
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080043#include "cmd.h"
44
Dianne Hackborn3d9eb952016-10-17 17:40:47 -070045#define DEBUG 0
46
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070047using 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 12:30:43 -070054struct SecurityContext_Delete {
ThiƩbaud Weksteen146ed6e2021-09-15 20:06:13 +020055 void operator()(char* p) const {
Dianne Hackborn1941a402016-08-29 12:30:43 -070056 freecon(p);
57 }
58};
Jiyong Park4e7d18a2017-08-07 20:48:32 +090059typedef std::unique_ptr<char[], SecurityContext_Delete> Unique_SecurityContext;
Dianne Hackborn1941a402016-08-29 12:30:43 -070060
61class MyShellCallback : public BnShellCallback
62{
63public:
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080064 TextOutput& mErrorLog;
Dianne Hackborne5ed1992016-10-10 16:35:45 -070065 bool mActive = true;
66
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080067 MyShellCallback(TextOutput& errorLog) : mErrorLog(errorLog) {}
68
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070069 virtual int openFile(const String16& path, const String16& seLinuxContext,
70 const String16& mode) {
Dianne Hackborn1941a402016-08-29 12:30:43 -070071 String8 path8(path);
72 char cwd[256];
73 getcwd(cwd, 256);
74 String8 fullPath(cwd);
75 fullPath.appendPath(path8);
Dianne Hackborne5ed1992016-10-10 16:35:45 -070076 if (!mActive) {
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080077 mErrorLog << "Open attempt after active for: " << fullPath << endl;
Dianne Hackborne5ed1992016-10-10 16:35:45 -070078 return -EPERM;
79 }
Dianne Hackborn228f2f62017-11-14 11:18:08 -080080#if DEBUG
81 ALOGD("openFile: %s, full=%s", path8.string(), fullPath.string());
82#endif
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070083 int flags = 0;
84 bool checkRead = false;
85 bool checkWrite = false;
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080086 if (mode == u"w") {
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070087 flags = O_WRONLY|O_CREAT|O_TRUNC;
88 checkWrite = true;
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080089 } else if (mode == u"w+") {
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070090 flags = O_RDWR|O_CREAT|O_TRUNC;
91 checkRead = checkWrite = true;
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080092 } else if (mode == u"r") {
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070093 flags = O_RDONLY;
94 checkRead = true;
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080095 } else if (mode == u"r+") {
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070096 flags = O_RDWR;
97 checkRead = checkWrite = true;
98 } else {
Alex Buynytskyya39d87a2018-12-12 17:40:52 -080099 mErrorLog << "Invalid mode requested: " << mode.string() << endl;
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700100 return -EINVAL;
101 }
102 int fd = open(fullPath.string(), flags, S_IRWXU|S_IRWXG);
Dianne Hackborn228f2f62017-11-14 11:18:08 -0800103#if DEBUG
104 ALOGD("openFile: fd=%d", fd);
105#endif
Dianne Hackborn1941a402016-08-29 12:30:43 -0700106 if (fd < 0) {
107 return fd;
108 }
109 if (is_selinux_enabled() && seLinuxContext.size() > 0) {
110 String8 seLinuxContext8(seLinuxContext);
ThiƩbaud Weksteen146ed6e2021-09-15 20:06:13 +0200111 char* tmp = nullptr;
Chih-Hung Hsiehf463e182017-12-05 10:20:12 -0800112 getfilecon(fullPath.string(), &tmp);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700113 Unique_SecurityContext context(tmp);
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700114 if (checkWrite) {
115 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800116 "file", "write", nullptr);
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700117 if (accessGranted != 0) {
Dianne Hackborn228f2f62017-11-14 11:18:08 -0800118#if DEBUG
119 ALOGD("openFile: failed selinux write check!");
120#endif
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700121 close(fd);
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800122 mErrorLog << "System server has no access to write file context " << context.get() << " (from path " << fullPath.string() << ", context " << seLinuxContext8.string() << ")" << endl;
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700123 return -EPERM;
124 }
125 }
126 if (checkRead) {
127 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800128 "file", "read", nullptr);
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700129 if (accessGranted != 0) {
Dianne Hackborn228f2f62017-11-14 11:18:08 -0800130#if DEBUG
131 ALOGD("openFile: failed selinux read check!");
132#endif
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700133 close(fd);
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800134 mErrorLog << "System server has no access to read file context " << context.get() << " (from path " << fullPath.string() << ", context " << seLinuxContext8.string() << ")" << endl;
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700135 return -EPERM;
136 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700137 }
138 }
139 return fd;
140 }
141};
142
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700143class MyResultReceiver : public BnResultReceiver
144{
145public:
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700146 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-07 17:35:27 -0700164 }
165};
166
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800167int cmdMain(const std::vector<std::string_view>& argv, TextOutput& outputLog, TextOutput& errorLog,
168 int in, int out, int err, RunMode runMode) {
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700169 sp<ProcessState> proc = ProcessState::self();
170 proc->startThreadPool();
171
Dianne Hackborn228f2f62017-11-14 11:18:08 -0800172#if DEBUG
173 ALOGD("cmd: starting");
174#endif
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700175 sp<IServiceManager> sm = defaultServiceManager();
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800176 if (runMode == RunMode::kStandalone) {
177 fflush(stdout);
178 }
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800179 if (sm == nullptr) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700180 ALOGW("Unable to get default service manager!");
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800181 errorLog << "cmd: Unable to get default service manager!" << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700182 return 20;
183 }
184
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800185 int argc = argv.size();
186
187 if (argc == 0) {
Jon Spivacke82eaa82020-02-13 18:10:45 -0800188 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-07 17:35:27 -0700189 return 20;
190 }
191
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800192 if ((argc == 1) && (argv[0] == "-l")) {
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700193 Vector<String16> services = sm->listServices();
194 services.sort(sort_func);
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800195 outputLog << "Currently running services:" << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700196
197 for (size_t i=0; i<services.size(); i++) {
198 sp<IBinder> service = sm->checkService(services[i]);
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800199 if (service != nullptr) {
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800200 outputLog << " " << services[i] << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700201 }
202 }
203 return 0;
204 }
205
Jon Spivacke82eaa82020-02-13 18:10:45 -0800206 bool waitForService = ((argc > 1) && (argv[0] == "-w"));
207 int serviceIdx = (waitForService) ? 1 : 0;
208 const auto cmd = argv[serviceIdx];
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800209
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700210 Vector<String16> args;
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800211 String16 serviceName = String16(cmd.data(), cmd.size());
Jon Spivacke82eaa82020-02-13 18:10:45 -0800212 for (int i = serviceIdx + 1; i < argc; i++) {
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800213 args.add(String16(argv[i].data(), argv[i].size()));
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700214 }
Jon Spivacke82eaa82020-02-13 18:10:45 -0800215 sp<IBinder> service;
216 if(waitForService) {
217 service = sm->waitForService(serviceName);
218 } else {
219 service = sm->checkService(serviceName);
220 }
221
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800222 if (service == nullptr) {
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800223 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-07 17:35:27 -0700227 return 20;
228 }
229
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800230 sp<MyShellCallback> cb = new MyShellCallback(errorLog);
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700231 sp<MyResultReceiver> result = new MyResultReceiver();
232
233#if DEBUG
Dominic Lemire3abc51c2020-01-23 12:24:36 -0800234 ALOGD("cmd: Invoking %.*s in=%d, out=%d, err=%d",
235 static_cast<int>(cmd.size()), cmd.data(), in, out, err);
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700236#endif
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700237
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700238 // TODO: block until a result is returned to MyResultReceiver.
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800239 status_t error = IBinder::shellCommand(service, in, out, err, args, cb, result);
240 if (error < 0) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700241 const char* errstr;
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800242 switch (error) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700243 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-12 17:40:52 -0800247 default: errstr = strerror(-error); break;
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700248 }
Alex Buynytskyya39d87a2018-12-12 17:40:52 -0800249 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-17 17:40:47 -0700256 }
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700257
258 cb->mActive = false;
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700259 status_t res = result->waitForResult();
260#if DEBUG
261 ALOGD("result=%d", (int)res);
262#endif
263 return res;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700264}