| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 24 | #include <binder/IShellCallback.h> | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 25 | #include <binder/TextOutput.h> | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 26 | #include <utils/Condition.h> | 
|  | 27 | #include <utils/Mutex.h> | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 28 | #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 Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 35 | #include <fcntl.h> | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 36 | #include <sys/time.h> | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 37 | #include <errno.h> | 
| Jiyong Park | 4e7d18a | 2017-08-07 20:48:32 +0900 | [diff] [blame] | 38 | #include <memory> | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 39 |  | 
|  | 40 | #include "selinux/selinux.h" | 
|  | 41 | #include "selinux/android.h" | 
|  | 42 |  | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 43 | #include "cmd.h" | 
|  | 44 |  | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 45 | #define DEBUG 0 | 
|  | 46 |  | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 47 | using namespace android; | 
|  | 48 |  | 
|  | 49 | static int sort_func(const String16* lhs, const String16* rhs) | 
|  | 50 | { | 
|  | 51 | return lhs->compare(*rhs); | 
|  | 52 | } | 
|  | 53 |  | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 54 | struct SecurityContext_Delete { | 
|  | 55 | void operator()(security_context_t p) const { | 
|  | 56 | freecon(p); | 
|  | 57 | } | 
|  | 58 | }; | 
| Jiyong Park | 4e7d18a | 2017-08-07 20:48:32 +0900 | [diff] [blame] | 59 | typedef std::unique_ptr<char[], SecurityContext_Delete> Unique_SecurityContext; | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | class MyShellCallback : public BnShellCallback | 
|  | 62 | { | 
|  | 63 | public: | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 64 | TextOutput& mErrorLog; | 
| Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 65 | bool mActive = true; | 
|  | 66 |  | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 67 | MyShellCallback(TextOutput& errorLog) : mErrorLog(errorLog) {} | 
|  | 68 |  | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 69 | virtual int openFile(const String16& path, const String16& seLinuxContext, | 
|  | 70 | const String16& mode) { | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 71 | String8 path8(path); | 
|  | 72 | char cwd[256]; | 
|  | 73 | getcwd(cwd, 256); | 
|  | 74 | String8 fullPath(cwd); | 
|  | 75 | fullPath.appendPath(path8); | 
| Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 76 | if (!mActive) { | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 77 | mErrorLog << "Open attempt after active for: " << fullPath << endl; | 
| Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 78 | return -EPERM; | 
|  | 79 | } | 
| Dianne Hackborn | 228f2f6 | 2017-11-14 11:18:08 -0800 | [diff] [blame] | 80 | #if DEBUG | 
|  | 81 | ALOGD("openFile: %s, full=%s", path8.string(), fullPath.string()); | 
|  | 82 | #endif | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 83 | int flags = 0; | 
|  | 84 | bool checkRead = false; | 
|  | 85 | bool checkWrite = false; | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 86 | if (mode == u"w") { | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 87 | flags = O_WRONLY|O_CREAT|O_TRUNC; | 
|  | 88 | checkWrite = true; | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 89 | } else if (mode == u"w+") { | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 90 | flags = O_RDWR|O_CREAT|O_TRUNC; | 
|  | 91 | checkRead = checkWrite = true; | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 92 | } else if (mode == u"r") { | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 93 | flags = O_RDONLY; | 
|  | 94 | checkRead = true; | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 95 | } else if (mode == u"r+") { | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 96 | flags = O_RDWR; | 
|  | 97 | checkRead = checkWrite = true; | 
|  | 98 | } else { | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 99 | mErrorLog << "Invalid mode requested: " << mode.string() << endl; | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 100 | return -EINVAL; | 
|  | 101 | } | 
|  | 102 | int fd = open(fullPath.string(), flags, S_IRWXU|S_IRWXG); | 
| Dianne Hackborn | 228f2f6 | 2017-11-14 11:18:08 -0800 | [diff] [blame] | 103 | #if DEBUG | 
|  | 104 | ALOGD("openFile: fd=%d", fd); | 
|  | 105 | #endif | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 106 | if (fd < 0) { | 
|  | 107 | return fd; | 
|  | 108 | } | 
|  | 109 | if (is_selinux_enabled() && seLinuxContext.size() > 0) { | 
|  | 110 | String8 seLinuxContext8(seLinuxContext); | 
| Alex Buynytskyy | 4ad5b4b | 2018-12-19 08:21:24 -0800 | [diff] [blame] | 111 | security_context_t tmp = nullptr; | 
| Chih-Hung Hsieh | f463e18 | 2017-12-05 10:20:12 -0800 | [diff] [blame] | 112 | getfilecon(fullPath.string(), &tmp); | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 113 | Unique_SecurityContext context(tmp); | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 114 | if (checkWrite) { | 
|  | 115 | int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(), | 
| Alex Buynytskyy | 4ad5b4b | 2018-12-19 08:21:24 -0800 | [diff] [blame] | 116 | "file", "write", nullptr); | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 117 | if (accessGranted != 0) { | 
| Dianne Hackborn | 228f2f6 | 2017-11-14 11:18:08 -0800 | [diff] [blame] | 118 | #if DEBUG | 
|  | 119 | ALOGD("openFile: failed selinux write check!"); | 
|  | 120 | #endif | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 121 | close(fd); | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 122 | mErrorLog << "System server has no access to write file context " << context.get() << " (from path " << fullPath.string() << ", context " << seLinuxContext8.string() << ")" << endl; | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 123 | return -EPERM; | 
|  | 124 | } | 
|  | 125 | } | 
|  | 126 | if (checkRead) { | 
|  | 127 | int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(), | 
| Alex Buynytskyy | 4ad5b4b | 2018-12-19 08:21:24 -0800 | [diff] [blame] | 128 | "file", "read", nullptr); | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 129 | if (accessGranted != 0) { | 
| Dianne Hackborn | 228f2f6 | 2017-11-14 11:18:08 -0800 | [diff] [blame] | 130 | #if DEBUG | 
|  | 131 | ALOGD("openFile: failed selinux read check!"); | 
|  | 132 | #endif | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 133 | close(fd); | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 134 | mErrorLog << "System server has no access to read file context " << context.get() << " (from path " << fullPath.string() << ", context " << seLinuxContext8.string() << ")" << endl; | 
| Dianne Hackborn | 4217f8e | 2017-10-30 14:31:41 -0700 | [diff] [blame] | 135 | return -EPERM; | 
|  | 136 | } | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 137 | } | 
|  | 138 | } | 
|  | 139 | return fd; | 
|  | 140 | } | 
|  | 141 | }; | 
|  | 142 |  | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 143 | class MyResultReceiver : public BnResultReceiver | 
|  | 144 | { | 
|  | 145 | public: | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 146 | 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 Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 164 | } | 
|  | 165 | }; | 
|  | 166 |  | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 167 | int cmdMain(const std::vector<std::string_view>& argv, TextOutput& outputLog, TextOutput& errorLog, | 
|  | 168 | int in, int out, int err, RunMode runMode) { | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 169 | sp<ProcessState> proc = ProcessState::self(); | 
|  | 170 | proc->startThreadPool(); | 
|  | 171 |  | 
| Dianne Hackborn | 228f2f6 | 2017-11-14 11:18:08 -0800 | [diff] [blame] | 172 | #if DEBUG | 
|  | 173 | ALOGD("cmd: starting"); | 
|  | 174 | #endif | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 175 | sp<IServiceManager> sm = defaultServiceManager(); | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 176 | if (runMode == RunMode::kStandalone) { | 
|  | 177 | fflush(stdout); | 
|  | 178 | } | 
| Alex Buynytskyy | 4ad5b4b | 2018-12-19 08:21:24 -0800 | [diff] [blame] | 179 | if (sm == nullptr) { | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 180 | ALOGW("Unable to get default service manager!"); | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 181 | errorLog << "cmd: Unable to get default service manager!" << endl; | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 182 | return 20; | 
|  | 183 | } | 
|  | 184 |  | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 185 | int argc = argv.size(); | 
|  | 186 |  | 
|  | 187 | if (argc == 0) { | 
|  | 188 | errorLog << "cmd: No service specified; use -l to list all services" << endl; | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 189 | return 20; | 
|  | 190 | } | 
|  | 191 |  | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 192 | if ((argc == 1) && (argv[0] == "-l")) { | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 193 | Vector<String16> services = sm->listServices(); | 
|  | 194 | services.sort(sort_func); | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 195 | outputLog << "Currently running services:" << endl; | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 196 |  | 
|  | 197 | for (size_t i=0; i<services.size(); i++) { | 
|  | 198 | sp<IBinder> service = sm->checkService(services[i]); | 
| Alex Buynytskyy | 4ad5b4b | 2018-12-19 08:21:24 -0800 | [diff] [blame] | 199 | if (service != nullptr) { | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 200 | outputLog << "  " << services[i] << endl; | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 201 | } | 
|  | 202 | } | 
|  | 203 | return 0; | 
|  | 204 | } | 
|  | 205 |  | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 206 | const auto cmd = argv[0]; | 
|  | 207 |  | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 208 | Vector<String16> args; | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 209 | String16 serviceName = String16(cmd.data(), cmd.size()); | 
|  | 210 | for (int i = 1; i < argc; i++) { | 
|  | 211 | args.add(String16(argv[i].data(), argv[i].size())); | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 212 | } | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 213 | sp<IBinder> service = sm->checkService(serviceName); | 
| Alex Buynytskyy | 4ad5b4b | 2018-12-19 08:21:24 -0800 | [diff] [blame] | 214 | if (service == nullptr) { | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 215 | if (runMode == RunMode::kStandalone) { | 
|  | 216 | ALOGW("Can't find service %.*s", static_cast<int>(cmd.size()), cmd.data()); | 
|  | 217 | } | 
|  | 218 | errorLog << "cmd: Can't find service: " << cmd << endl; | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 219 | return 20; | 
|  | 220 | } | 
|  | 221 |  | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 222 | sp<MyShellCallback> cb = new MyShellCallback(errorLog); | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 223 | sp<MyResultReceiver> result = new MyResultReceiver(); | 
|  | 224 |  | 
|  | 225 | #if DEBUG | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 226 | ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", cmd, in, out, err); | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 227 | #endif | 
| Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 228 |  | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 229 | // TODO: block until a result is returned to MyResultReceiver. | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 230 | status_t error = IBinder::shellCommand(service, in, out, err, args, cb, result); | 
|  | 231 | if (error < 0) { | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 232 | const char* errstr; | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 233 | switch (error) { | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 234 | case BAD_TYPE: errstr = "Bad type"; break; | 
|  | 235 | case FAILED_TRANSACTION: errstr = "Failed transaction"; break; | 
|  | 236 | case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break; | 
|  | 237 | case UNEXPECTED_NULL: errstr = "Unexpected null"; break; | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 238 | default: errstr = strerror(-error); break; | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 239 | } | 
| Alex Buynytskyy | a39d87a | 2018-12-12 17:40:52 -0800 | [diff] [blame] | 240 | if (runMode == RunMode::kStandalone) { | 
|  | 241 | ALOGW("Failure calling service %.*s: %s (%d)", static_cast<int>(cmd.size()), cmd.data(), | 
|  | 242 | errstr, -error); | 
|  | 243 | } | 
|  | 244 | outputLog << "cmd: Failure calling service " << cmd << ": " << errstr << " (" << (-error) | 
|  | 245 | << ")" << endl; | 
|  | 246 | return error; | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 247 | } | 
| Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 248 |  | 
|  | 249 | cb->mActive = false; | 
| Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 250 | status_t res = result->waitForResult(); | 
|  | 251 | #if DEBUG | 
|  | 252 | ALOGD("result=%d", (int)res); | 
|  | 253 | #endif | 
|  | 254 | return res; | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 255 | } |