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