Dan Albert | 53a3744 | 2015-05-08 16:13:53 -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 TRACE_TAG TRACE_ADB |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <signal.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <getopt.h> |
| 26 | #include <sys/prctl.h> |
| 27 | |
| 28 | #include "base/logging.h" |
| 29 | #include "base/stringprintf.h" |
| 30 | #include "cutils/properties.h" |
| 31 | #include "private/android_filesystem_config.h" |
| 32 | #include "selinux/selinux.h" |
| 33 | |
| 34 | #include "adb.h" |
| 35 | #include "adb_auth.h" |
| 36 | #include "adb_listeners.h" |
| 37 | #include "transport.h" |
| 38 | #include "qemu_tracing.h" |
| 39 | |
| 40 | static const char* root_seclabel = nullptr; |
| 41 | |
| 42 | static void drop_capabilities_bounding_set_if_needed() { |
| 43 | #ifdef ALLOW_ADBD_ROOT |
| 44 | char value[PROPERTY_VALUE_MAX]; |
| 45 | property_get("ro.debuggable", value, ""); |
| 46 | if (strcmp(value, "1") == 0) { |
| 47 | return; |
| 48 | } |
| 49 | #endif |
| 50 | for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { |
| 51 | if (i == CAP_SETUID || i == CAP_SETGID) { |
| 52 | // CAP_SETUID CAP_SETGID needed by /system/bin/run-as |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); |
| 57 | |
| 58 | // Some kernels don't have file capabilities compiled in, and |
| 59 | // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically |
| 60 | // die when we see such misconfigured kernels. |
| 61 | if ((err < 0) && (errno != EINVAL)) { |
| 62 | PLOG(FATAL) << "Could not drop capabilities"; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static bool should_drop_privileges() { |
| 68 | #if defined(ALLOW_ADBD_ROOT) |
| 69 | char value[PROPERTY_VALUE_MAX]; |
| 70 | |
| 71 | // The emulator is never secure, so don't drop privileges there. |
| 72 | // TODO: this seems like a bug --- shouldn't the emulator behave like a device? |
| 73 | property_get("ro.kernel.qemu", value, ""); |
| 74 | if (strcmp(value, "1") == 0) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | // The properties that affect `adb root` and `adb unroot` are ro.secure and |
| 79 | // ro.debuggable. In this context the names don't make the expected behavior |
| 80 | // particularly obvious. |
| 81 | // |
| 82 | // ro.debuggable: |
| 83 | // Allowed to become root, but not necessarily the default. Set to 1 on |
| 84 | // eng and userdebug builds. |
| 85 | // |
| 86 | // ro.secure: |
| 87 | // Drop privileges by default. Set to 1 on userdebug and user builds. |
| 88 | property_get("ro.secure", value, "1"); |
| 89 | bool ro_secure = (strcmp(value, "1") == 0); |
| 90 | |
| 91 | property_get("ro.debuggable", value, ""); |
| 92 | bool ro_debuggable = (strcmp(value, "1") == 0); |
| 93 | |
| 94 | // Drop privileges if ro.secure is set... |
| 95 | bool drop = ro_secure; |
| 96 | |
| 97 | property_get("service.adb.root", value, ""); |
| 98 | bool adb_root = (strcmp(value, "1") == 0); |
| 99 | bool adb_unroot = (strcmp(value, "0") == 0); |
| 100 | |
| 101 | // ...except "adb root" lets you keep privileges in a debuggable build. |
| 102 | if (ro_debuggable && adb_root) { |
| 103 | drop = false; |
| 104 | } |
| 105 | |
| 106 | // ...and "adb unroot" lets you explicitly drop privileges. |
| 107 | if (adb_unroot) { |
| 108 | drop = true; |
| 109 | } |
| 110 | |
| 111 | return drop; |
| 112 | #else |
| 113 | return true; // "adb root" not allowed, always drop privileges. |
| 114 | #endif // ALLOW_ADBD_ROOT |
| 115 | } |
| 116 | |
| 117 | int adbd_main(int server_port) { |
| 118 | umask(0); |
| 119 | |
| 120 | signal(SIGPIPE, SIG_IGN); |
| 121 | |
| 122 | init_transport_registration(); |
| 123 | |
| 124 | // We need to call this even if auth isn't enabled because the file |
| 125 | // descriptor will always be open. |
| 126 | adbd_cloexec_auth_socket(); |
| 127 | |
| 128 | auth_enabled = property_get_bool("ro.adb.secure", 0) != 0; |
| 129 | if (auth_enabled) { |
| 130 | adbd_auth_init(); |
| 131 | } |
| 132 | |
| 133 | // Our external storage path may be different than apps, since |
| 134 | // we aren't able to bind mount after dropping root. |
| 135 | const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE"); |
| 136 | if (adb_external_storage != nullptr) { |
| 137 | setenv("EXTERNAL_STORAGE", adb_external_storage, 1); |
| 138 | } else { |
| 139 | D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE" |
| 140 | " unchanged.\n"); |
| 141 | } |
| 142 | |
| 143 | // Add extra groups: |
| 144 | // AID_ADB to access the USB driver |
| 145 | // AID_LOG to read system logs (adb logcat) |
| 146 | // AID_INPUT to diagnose input issues (getevent) |
| 147 | // AID_INET to diagnose network issues (ping) |
| 148 | // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump) |
| 149 | // AID_SDCARD_R to allow reading from the SD card |
| 150 | // AID_SDCARD_RW to allow writing to the SD card |
| 151 | // AID_NET_BW_STATS to read out qtaguid statistics |
| 152 | gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, |
| 153 | AID_INET, AID_NET_BT, AID_NET_BT_ADMIN, |
| 154 | AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS}; |
| 155 | if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0) { |
| 156 | PLOG(FATAL) << "Could not set supplental groups"; |
| 157 | } |
| 158 | |
| 159 | /* don't listen on a port (default 5037) if running in secure mode */ |
| 160 | /* don't run as root if we are running in secure mode */ |
| 161 | if (should_drop_privileges()) { |
| 162 | drop_capabilities_bounding_set_if_needed(); |
| 163 | |
| 164 | /* then switch user and group to "shell" */ |
| 165 | if (setgid(AID_SHELL) != 0) { |
| 166 | PLOG(FATAL) << "Could not setgid"; |
| 167 | } |
| 168 | if (setuid(AID_SHELL) != 0) { |
| 169 | PLOG(FATAL) << "Could not setuid"; |
| 170 | } |
| 171 | |
| 172 | D("Local port disabled\n"); |
| 173 | } else { |
| 174 | if ((root_seclabel != nullptr) && (is_selinux_enabled() > 0)) { |
| 175 | if (setcon(root_seclabel) < 0) { |
| 176 | LOG(FATAL) << "Could not set selinux context"; |
| 177 | } |
| 178 | } |
| 179 | std::string local_name = |
| 180 | android::base::StringPrintf("tcp:%d", server_port); |
| 181 | if (install_listener(local_name, "*smartsocket*", nullptr, 0)) { |
| 182 | LOG(FATAL) << "Could not install *smartsocket* listener"; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | bool is_usb = false; |
| 187 | if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) { |
| 188 | // Listen on USB. |
| 189 | usb_init(); |
| 190 | is_usb = true; |
| 191 | } |
| 192 | |
| 193 | // If one of these properties is set, also listen on that port. |
| 194 | // If one of the properties isn't set and we couldn't listen on usb, listen |
| 195 | // on the default port. |
| 196 | char prop_port[PROPERTY_VALUE_MAX]; |
| 197 | property_get("service.adb.tcp.port", prop_port, ""); |
| 198 | if (prop_port[0] == '\0') { |
| 199 | property_get("persist.adb.tcp.port", prop_port, ""); |
| 200 | } |
| 201 | |
| 202 | int port; |
| 203 | if (sscanf(prop_port, "%d", &port) == 1 && port > 0) { |
| 204 | printf("using port=%d\n", port); |
| 205 | // Listen on TCP port specified by service.adb.tcp.port property. |
| 206 | local_init(port); |
| 207 | } else if (!is_usb) { |
| 208 | // Listen on default port. |
| 209 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 210 | } |
| 211 | |
| 212 | D("adbd_main(): pre init_jdwp()\n"); |
| 213 | init_jdwp(); |
| 214 | D("adbd_main(): post init_jdwp()\n"); |
| 215 | |
| 216 | D("Event loop starting\n"); |
| 217 | fdevent_loop(); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void close_stdin() { |
| 223 | int fd = unix_open("/dev/null", O_RDONLY); |
| 224 | if (fd == -1) { |
| 225 | perror("failed to open /dev/null, stdin will remain open"); |
| 226 | return; |
| 227 | } |
| 228 | dup2(fd, STDIN_FILENO); |
| 229 | adb_close(fd); |
| 230 | } |
| 231 | |
| 232 | int main(int argc, char** argv) { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 233 | while (true) { |
| 234 | static struct option opts[] = { |
| 235 | {"root_seclabel", required_argument, nullptr, 's'}, |
| 236 | {"device_banner", required_argument, nullptr, 'b'}, |
| 237 | {"version", no_argument, nullptr, 'v'}, |
| 238 | }; |
| 239 | |
| 240 | int option_index = 0; |
| 241 | int c = getopt_long(argc, argv, "", opts, &option_index); |
| 242 | if (c == -1) { |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | switch (c) { |
| 247 | case 's': |
| 248 | root_seclabel = optarg; |
| 249 | break; |
| 250 | case 'b': |
| 251 | adb_device_banner = optarg; |
| 252 | break; |
| 253 | case 'v': |
| 254 | printf("Android Debug Bridge Daemon version %d.%d.%d %s\n", |
| 255 | ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION, |
| 256 | ADB_REVISION); |
| 257 | return 0; |
| 258 | default: |
| 259 | // getopt already prints "adbd: invalid option -- %c" for us. |
| 260 | return 1; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | close_stdin(); |
| 265 | |
Dan Albert | 08d552b | 2015-05-21 13:58:50 -0700 | [diff] [blame^] | 266 | adb_trace_init(argv); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 267 | |
| 268 | /* If adbd runs inside the emulator this will enable adb tracing via |
| 269 | * adb-debug qemud service in the emulator. */ |
| 270 | adb_qemu_trace_init(); |
| 271 | |
| 272 | D("Handling main()\n"); |
| 273 | return adbd_main(DEFAULT_ADB_PORT); |
| 274 | } |