blob: 0616adda61ba8b19ca3342c46e3500fcc7181c48 [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
Dianne Hackborn3d9eb952016-10-17 17:40:47 -070043#define DEBUG 0
44
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070045using namespace android;
46
47static int sort_func(const String16* lhs, const String16* rhs)
48{
49 return lhs->compare(*rhs);
50}
51
Dianne Hackborn1941a402016-08-29 12:30:43 -070052struct SecurityContext_Delete {
53 void operator()(security_context_t p) const {
54 freecon(p);
55 }
56};
Jiyong Park4e7d18a2017-08-07 20:48:32 +090057typedef std::unique_ptr<char[], SecurityContext_Delete> Unique_SecurityContext;
Dianne Hackborn1941a402016-08-29 12:30:43 -070058
59class MyShellCallback : public BnShellCallback
60{
61public:
Dianne Hackborne5ed1992016-10-10 16:35:45 -070062 bool mActive = true;
63
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070064 virtual int openFile(const String16& path, const String16& seLinuxContext,
65 const String16& mode) {
Dianne Hackborn1941a402016-08-29 12:30:43 -070066 String8 path8(path);
67 char cwd[256];
68 getcwd(cwd, 256);
69 String8 fullPath(cwd);
70 fullPath.appendPath(path8);
Dianne Hackborne5ed1992016-10-10 16:35:45 -070071 if (!mActive) {
72 aerr << "Open attempt after active for: " << fullPath << endl;
73 return -EPERM;
74 }
Dianne Hackborn228f2f62017-11-14 11:18:08 -080075#if DEBUG
76 ALOGD("openFile: %s, full=%s", path8.string(), fullPath.string());
77#endif
Dianne Hackborn4217f8e2017-10-30 14:31:41 -070078 int flags = 0;
79 bool checkRead = false;
80 bool checkWrite = false;
81 if (mode == String16("w")) {
82 flags = O_WRONLY|O_CREAT|O_TRUNC;
83 checkWrite = true;
84 } else if (mode == String16("w+")) {
85 flags = O_RDWR|O_CREAT|O_TRUNC;
86 checkRead = checkWrite = true;
87 } else if (mode == String16("r")) {
88 flags = O_RDONLY;
89 checkRead = true;
90 } else if (mode == String16("r+")) {
91 flags = O_RDWR;
92 checkRead = checkWrite = true;
93 } else {
94 aerr << "Invalid mode requested: " << mode.string() << endl;
95 return -EINVAL;
96 }
97 int fd = open(fullPath.string(), flags, S_IRWXU|S_IRWXG);
Dianne Hackborn228f2f62017-11-14 11:18:08 -080098#if DEBUG
99 ALOGD("openFile: fd=%d", fd);
100#endif
Dianne Hackborn1941a402016-08-29 12:30:43 -0700101 if (fd < 0) {
102 return fd;
103 }
104 if (is_selinux_enabled() && seLinuxContext.size() > 0) {
105 String8 seLinuxContext8(seLinuxContext);
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800106 security_context_t tmp = nullptr;
Chih-Hung Hsiehf463e182017-12-05 10:20:12 -0800107 getfilecon(fullPath.string(), &tmp);
Dianne Hackborn1941a402016-08-29 12:30:43 -0700108 Unique_SecurityContext context(tmp);
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700109 if (checkWrite) {
110 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800111 "file", "write", nullptr);
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700112 if (accessGranted != 0) {
Dianne Hackborn228f2f62017-11-14 11:18:08 -0800113#if DEBUG
114 ALOGD("openFile: failed selinux write check!");
115#endif
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700116 close(fd);
117 aerr << "System server has no access to write file context " << context.get()
118 << " (from path " << fullPath.string() << ", context "
119 << seLinuxContext8.string() << ")" << endl;
120 return -EPERM;
121 }
122 }
123 if (checkRead) {
124 int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(),
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800125 "file", "read", nullptr);
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700126 if (accessGranted != 0) {
Dianne Hackborn228f2f62017-11-14 11:18:08 -0800127#if DEBUG
128 ALOGD("openFile: failed selinux read check!");
129#endif
Dianne Hackborn4217f8e2017-10-30 14:31:41 -0700130 close(fd);
131 aerr << "System server has no access to read file context " << context.get()
132 << " (from path " << fullPath.string() << ", context "
133 << seLinuxContext8.string() << ")" << endl;
134 return -EPERM;
135 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700136 }
137 }
138 return fd;
139 }
140};
141
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700142class MyResultReceiver : public BnResultReceiver
143{
144public:
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700145 Mutex mMutex;
146 Condition mCondition;
147 bool mHaveResult = false;
148 int32_t mResult = 0;
149
150 virtual void send(int32_t resultCode) {
151 AutoMutex _l(mMutex);
152 mResult = resultCode;
153 mHaveResult = true;
154 mCondition.signal();
155 }
156
157 int32_t waitForResult() {
158 AutoMutex _l(mMutex);
159 while (!mHaveResult) {
160 mCondition.wait(mMutex);
161 }
162 return mResult;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700163 }
164};
165
166int main(int argc, char* const argv[])
167{
168 signal(SIGPIPE, SIG_IGN);
169 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();
176 fflush(stdout);
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800177 if (sm == nullptr) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700178 ALOGW("Unable to get default service manager!");
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700179 aerr << "cmd: Unable to get default service manager!" << endl;
180 return 20;
181 }
182
183 if (argc == 1) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700184 aerr << "cmd: No service specified; use -l to list all services" << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700185 return 20;
186 }
187
188 if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {
189 Vector<String16> services = sm->listServices();
190 services.sort(sort_func);
191 aout << "Currently running services:" << endl;
192
193 for (size_t i=0; i<services.size(); i++) {
194 sp<IBinder> service = sm->checkService(services[i]);
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800195 if (service != nullptr) {
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700196 aout << " " << services[i] << endl;
197 }
198 }
199 return 0;
200 }
201
202 Vector<String16> args;
203 for (int i=2; i<argc; i++) {
204 args.add(String16(argv[i]));
205 }
206 String16 cmd = String16(argv[1]);
207 sp<IBinder> service = sm->checkService(cmd);
Alex Buynytskyy4ad5b4b2018-12-19 08:21:24 -0800208 if (service == nullptr) {
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700209 ALOGW("Can't find service %s", argv[1]);
210 aerr << "cmd: Can't find service: " << argv[1] << endl;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700211 return 20;
212 }
213
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700214 sp<MyShellCallback> cb = new MyShellCallback();
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700215 sp<MyResultReceiver> result = new MyResultReceiver();
216
217#if DEBUG
218 ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", argv[1], STDIN_FILENO, STDOUT_FILENO,
219 STDERR_FILENO);
220#endif
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700221
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700222 // TODO: block until a result is returned to MyResultReceiver.
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700223 status_t err = IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args,
224 cb, result);
225 if (err < 0) {
226 const char* errstr;
227 switch (err) {
228 case BAD_TYPE: errstr = "Bad type"; break;
229 case FAILED_TRANSACTION: errstr = "Failed transaction"; break;
230 case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break;
231 case UNEXPECTED_NULL: errstr = "Unexpected null"; break;
232 default: errstr = strerror(-err); break;
233 }
234 ALOGW("Failure calling service %s: %s (%d)", argv[1], errstr, -err);
235 aout << "cmd: Failure calling service " << argv[1] << ": " << errstr << " ("
236 << (-err) << ")" << endl;
237 return err;
238 }
Dianne Hackborne5ed1992016-10-10 16:35:45 -0700239
240 cb->mActive = false;
Dianne Hackborn3d9eb952016-10-17 17:40:47 -0700241 status_t res = result->waitForResult();
242#if DEBUG
243 ALOGD("result=%d", (int)res);
244#endif
245 return res;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700246}