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 | |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 43 | #define DEBUG 0 |
| 44 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 45 | using namespace android; |
| 46 | |
| 47 | static int sort_func(const String16* lhs, const String16* rhs) |
| 48 | { |
| 49 | return lhs->compare(*rhs); |
| 50 | } |
| 51 | |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 52 | struct SecurityContext_Delete { |
| 53 | void operator()(security_context_t p) const { |
| 54 | freecon(p); |
| 55 | } |
| 56 | }; |
Jiyong Park | 4e7d18a | 2017-08-07 20:48:32 +0900 | [diff] [blame^] | 57 | typedef std::unique_ptr<char[], SecurityContext_Delete> Unique_SecurityContext; |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 58 | |
| 59 | class MyShellCallback : public BnShellCallback |
| 60 | { |
| 61 | public: |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 62 | bool mActive = true; |
| 63 | |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 64 | virtual int openOutputFile(const String16& path, const String16& seLinuxContext) { |
| 65 | String8 path8(path); |
| 66 | char cwd[256]; |
| 67 | getcwd(cwd, 256); |
| 68 | String8 fullPath(cwd); |
| 69 | fullPath.appendPath(path8); |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 70 | if (!mActive) { |
| 71 | aerr << "Open attempt after active for: " << fullPath << endl; |
| 72 | return -EPERM; |
| 73 | } |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 74 | int fd = open(fullPath.string(), O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG); |
| 75 | if (fd < 0) { |
| 76 | return fd; |
| 77 | } |
| 78 | if (is_selinux_enabled() && seLinuxContext.size() > 0) { |
| 79 | String8 seLinuxContext8(seLinuxContext); |
| 80 | security_context_t tmp = NULL; |
| 81 | int ret = getfilecon(fullPath.string(), &tmp); |
| 82 | Unique_SecurityContext context(tmp); |
| 83 | int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(), |
| 84 | "file", "write", NULL); |
| 85 | if (accessGranted != 0) { |
| 86 | close(fd); |
| 87 | aerr << "System server has no access to file context " << context.get() |
| 88 | << " (from path " << fullPath.string() << ", context " |
| 89 | << seLinuxContext8.string() << ")" << endl; |
| 90 | return -EPERM; |
| 91 | } |
| 92 | } |
| 93 | return fd; |
| 94 | } |
| 95 | }; |
| 96 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 97 | class MyResultReceiver : public BnResultReceiver |
| 98 | { |
| 99 | public: |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 100 | Mutex mMutex; |
| 101 | Condition mCondition; |
| 102 | bool mHaveResult = false; |
| 103 | int32_t mResult = 0; |
| 104 | |
| 105 | virtual void send(int32_t resultCode) { |
| 106 | AutoMutex _l(mMutex); |
| 107 | mResult = resultCode; |
| 108 | mHaveResult = true; |
| 109 | mCondition.signal(); |
| 110 | } |
| 111 | |
| 112 | int32_t waitForResult() { |
| 113 | AutoMutex _l(mMutex); |
| 114 | while (!mHaveResult) { |
| 115 | mCondition.wait(mMutex); |
| 116 | } |
| 117 | return mResult; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | |
| 121 | int main(int argc, char* const argv[]) |
| 122 | { |
| 123 | signal(SIGPIPE, SIG_IGN); |
| 124 | sp<ProcessState> proc = ProcessState::self(); |
Martijn Coenen | c21bc9a | 2017-05-11 09:45:59 -0700 | [diff] [blame] | 125 | // setThreadPoolMaxThreadCount(0) actually tells the kernel it's |
| 126 | // not allowed to spawn any additional threads, but we still spawn |
| 127 | // a binder thread from userspace when we call startThreadPool(). |
| 128 | // This is safe because we only have 2 callbacks, neither of which |
| 129 | // block. |
| 130 | // See b/36066697 for rationale |
| 131 | proc->setThreadPoolMaxThreadCount(0); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 132 | proc->startThreadPool(); |
| 133 | |
| 134 | sp<IServiceManager> sm = defaultServiceManager(); |
| 135 | fflush(stdout); |
| 136 | if (sm == NULL) { |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 137 | ALOGW("Unable to get default service manager!"); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 138 | aerr << "cmd: Unable to get default service manager!" << endl; |
| 139 | return 20; |
| 140 | } |
| 141 | |
| 142 | if (argc == 1) { |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 143 | aerr << "cmd: No service specified; use -l to list all services" << endl; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 144 | return 20; |
| 145 | } |
| 146 | |
| 147 | if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) { |
| 148 | Vector<String16> services = sm->listServices(); |
| 149 | services.sort(sort_func); |
| 150 | aout << "Currently running services:" << endl; |
| 151 | |
| 152 | for (size_t i=0; i<services.size(); i++) { |
| 153 | sp<IBinder> service = sm->checkService(services[i]); |
| 154 | if (service != NULL) { |
| 155 | aout << " " << services[i] << endl; |
| 156 | } |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | Vector<String16> args; |
| 162 | for (int i=2; i<argc; i++) { |
| 163 | args.add(String16(argv[i])); |
| 164 | } |
| 165 | String16 cmd = String16(argv[1]); |
| 166 | sp<IBinder> service = sm->checkService(cmd); |
| 167 | if (service == NULL) { |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 168 | ALOGW("Can't find service %s", argv[1]); |
| 169 | aerr << "cmd: Can't find service: " << argv[1] << endl; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 170 | return 20; |
| 171 | } |
| 172 | |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 173 | sp<MyShellCallback> cb = new MyShellCallback(); |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 174 | sp<MyResultReceiver> result = new MyResultReceiver(); |
| 175 | |
| 176 | #if DEBUG |
| 177 | ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", argv[1], STDIN_FILENO, STDOUT_FILENO, |
| 178 | STDERR_FILENO); |
| 179 | #endif |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 180 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 181 | // TODO: block until a result is returned to MyResultReceiver. |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 182 | status_t err = IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args, |
| 183 | cb, result); |
| 184 | if (err < 0) { |
| 185 | const char* errstr; |
| 186 | switch (err) { |
| 187 | case BAD_TYPE: errstr = "Bad type"; break; |
| 188 | case FAILED_TRANSACTION: errstr = "Failed transaction"; break; |
| 189 | case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break; |
| 190 | case UNEXPECTED_NULL: errstr = "Unexpected null"; break; |
| 191 | default: errstr = strerror(-err); break; |
| 192 | } |
| 193 | ALOGW("Failure calling service %s: %s (%d)", argv[1], errstr, -err); |
| 194 | aout << "cmd: Failure calling service " << argv[1] << ": " << errstr << " (" |
| 195 | << (-err) << ")" << endl; |
| 196 | return err; |
| 197 | } |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame] | 198 | |
| 199 | cb->mActive = false; |
Dianne Hackborn | 3d9eb95 | 2016-10-17 17:40:47 -0700 | [diff] [blame] | 200 | status_t res = result->waitForResult(); |
| 201 | #if DEBUG |
| 202 | ALOGD("result=%d", (int)res); |
| 203 | #endif |
| 204 | return res; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 205 | } |