Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [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 <errno.h> |
| 20 | #include <signal.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 23 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 24 | #include "sysdeps.h" |
| 25 | |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 26 | #include "adb.h" |
| 27 | #include "adb_auth.h" |
| 28 | #include "adb_listeners.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 29 | #include "transport.h" |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 30 | |
| 31 | #if !ADB_HOST |
| 32 | #include <getopt.h> |
Dan Albert | 6795cd8 | 2015-02-19 11:36:53 -0800 | [diff] [blame] | 33 | #include <sys/prctl.h> |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 34 | |
| 35 | #include "cutils/properties.h" |
| 36 | #include "private/android_filesystem_config.h" |
| 37 | #include "selinux/selinux.h" |
| 38 | |
| 39 | #include "qemu_tracing.h" |
| 40 | #endif |
| 41 | |
| 42 | static void adb_cleanup(void) |
| 43 | { |
| 44 | usb_cleanup(); |
| 45 | } |
| 46 | |
Dan Albert | 6795cd8 | 2015-02-19 11:36:53 -0800 | [diff] [blame] | 47 | #if defined(_WIN32) |
| 48 | static BOOL WINAPI ctrlc_handler(DWORD type) |
| 49 | { |
| 50 | exit(STATUS_CONTROL_C_EXIT); |
| 51 | return TRUE; |
| 52 | } |
| 53 | #endif |
| 54 | |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 55 | #if ADB_HOST |
| 56 | #ifdef WORKAROUND_BUG6558362 |
| 57 | #include <sched.h> |
| 58 | #define AFFINITY_ENVVAR "ADB_CPU_AFFINITY_BUG6558362" |
| 59 | void adb_set_affinity(void) |
| 60 | { |
| 61 | cpu_set_t cpu_set; |
| 62 | const char* cpunum_str = getenv(AFFINITY_ENVVAR); |
| 63 | char* strtol_res; |
| 64 | int cpu_num; |
| 65 | |
| 66 | if (!cpunum_str || !*cpunum_str) |
| 67 | return; |
| 68 | cpu_num = strtol(cpunum_str, &strtol_res, 0); |
| 69 | if (*strtol_res != '\0') |
| 70 | fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, AFFINITY_ENVVAR); |
| 71 | |
| 72 | sched_getaffinity(0, sizeof(cpu_set), &cpu_set); |
| 73 | D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]); |
| 74 | CPU_ZERO(&cpu_set); |
| 75 | CPU_SET(cpu_num, &cpu_set); |
| 76 | sched_setaffinity(0, sizeof(cpu_set), &cpu_set); |
| 77 | sched_getaffinity(0, sizeof(cpu_set), &cpu_set); |
| 78 | D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]); |
| 79 | } |
| 80 | #endif |
| 81 | #else /* ADB_HOST */ |
| 82 | static const char *root_seclabel = NULL; |
| 83 | |
| 84 | static void drop_capabilities_bounding_set_if_needed() { |
| 85 | #ifdef ALLOW_ADBD_ROOT |
| 86 | char value[PROPERTY_VALUE_MAX]; |
| 87 | property_get("ro.debuggable", value, ""); |
| 88 | if (strcmp(value, "1") == 0) { |
| 89 | return; |
| 90 | } |
| 91 | #endif |
| 92 | int i; |
| 93 | for (i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { |
| 94 | if (i == CAP_SETUID || i == CAP_SETGID) { |
| 95 | // CAP_SETUID CAP_SETGID needed by /system/bin/run-as |
| 96 | continue; |
| 97 | } |
| 98 | int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); |
| 99 | |
| 100 | // Some kernels don't have file capabilities compiled in, and |
| 101 | // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically |
| 102 | // die when we see such misconfigured kernels. |
| 103 | if ((err < 0) && (errno != EINVAL)) { |
| 104 | exit(1); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static bool should_drop_privileges() { |
| 110 | #if defined(ALLOW_ADBD_ROOT) |
| 111 | char value[PROPERTY_VALUE_MAX]; |
| 112 | |
Dan Albert | 13f9c40 | 2015-02-19 11:03:26 -0800 | [diff] [blame] | 113 | // The properties that affect `adb root` and `adb unroot` are ro.secure and |
| 114 | // ro.debuggable. In this context the names don't make the expected behavior |
| 115 | // particularly obvious. |
| 116 | // |
| 117 | // ro.debuggable: |
| 118 | // Allowed to become root, but not necessarily the default. Set to 1 on |
| 119 | // eng and userdebug builds. |
| 120 | // |
| 121 | // ro.secure: |
| 122 | // Drop privileges by default. Set to 1 on userdebug and user builds. |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 123 | property_get("ro.secure", value, "1"); |
| 124 | bool ro_secure = (strcmp(value, "1") == 0); |
| 125 | |
Dan Albert | 13f9c40 | 2015-02-19 11:03:26 -0800 | [diff] [blame] | 126 | property_get("ro.debuggable", value, ""); |
| 127 | bool ro_debuggable = (strcmp(value, "1") == 0); |
| 128 | |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 129 | // Drop privileges if ro.secure is set... |
| 130 | bool drop = ro_secure; |
| 131 | |
Dan Albert | bd0b750 | 2015-02-18 18:22:45 -0800 | [diff] [blame] | 132 | property_get("service.adb.root", value, ""); |
| 133 | bool adb_root = (strcmp(value, "1") == 0); |
| 134 | bool adb_unroot = (strcmp(value, "0") == 0); |
| 135 | |
| 136 | // ...except "adb root" lets you keep privileges in a debuggable build. |
| 137 | if (ro_debuggable && adb_root) { |
| 138 | drop = false; |
| 139 | } |
| 140 | |
| 141 | // ...and "adb unroot" lets you explicitly drop privileges. |
| 142 | if (adb_unroot) { |
| 143 | drop = true; |
| 144 | } |
| 145 | |
| 146 | return drop; |
| 147 | #else |
| 148 | return true; // "adb root" not allowed, always drop privileges. |
| 149 | #endif /* ALLOW_ADBD_ROOT */ |
| 150 | } |
| 151 | |
| 152 | void start_device_log(void) |
| 153 | { |
| 154 | int fd; |
| 155 | char path[PATH_MAX]; |
| 156 | struct tm now; |
| 157 | time_t t; |
| 158 | char value[PROPERTY_VALUE_MAX]; |
| 159 | |
| 160 | // read the trace mask from persistent property persist.adb.trace_mask |
| 161 | // give up if the property is not set or cannot be parsed |
| 162 | property_get("persist.adb.trace_mask", value, ""); |
| 163 | if (sscanf(value, "%x", &adb_trace_mask) != 1) |
| 164 | return; |
| 165 | |
| 166 | adb_mkdir("/data/adb", 0775); |
| 167 | tzset(); |
| 168 | time(&t); |
| 169 | localtime_r(&t, &now); |
| 170 | strftime(path, sizeof(path), |
| 171 | "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt", |
| 172 | &now); |
| 173 | fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640); |
| 174 | if (fd < 0) |
| 175 | return; |
| 176 | |
| 177 | // redirect stdout and stderr to the log file |
| 178 | dup2(fd, 1); |
| 179 | dup2(fd, 2); |
| 180 | fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); |
| 181 | adb_close(fd); |
| 182 | |
| 183 | fd = unix_open("/dev/null", O_RDONLY); |
| 184 | dup2(fd, 0); |
| 185 | adb_close(fd); |
| 186 | } |
| 187 | #endif /* ADB_HOST */ |
| 188 | |
| 189 | /* Constructs a local name of form tcp:port. |
| 190 | * target_str points to the target string, it's content will be overwritten. |
| 191 | * target_size is the capacity of the target string. |
| 192 | * server_port is the port number to use for the local name. |
| 193 | */ |
| 194 | void build_local_name(char* target_str, size_t target_size, int server_port) |
| 195 | { |
| 196 | snprintf(target_str, target_size, "tcp:%d", server_port); |
| 197 | } |
| 198 | |
| 199 | void start_logging(void) |
| 200 | { |
| 201 | #if defined(_WIN32) |
| 202 | char temp[ MAX_PATH ]; |
| 203 | FILE* fnul; |
| 204 | FILE* flog; |
| 205 | |
| 206 | GetTempPath( sizeof(temp) - 8, temp ); |
| 207 | strcat( temp, "adb.log" ); |
| 208 | |
| 209 | /* Win32 specific redirections */ |
| 210 | fnul = fopen( "NUL", "rt" ); |
| 211 | if (fnul != NULL) |
| 212 | stdin[0] = fnul[0]; |
| 213 | |
| 214 | flog = fopen( temp, "at" ); |
| 215 | if (flog == NULL) |
| 216 | flog = fnul; |
| 217 | |
| 218 | setvbuf( flog, NULL, _IONBF, 0 ); |
| 219 | |
| 220 | stdout[0] = flog[0]; |
| 221 | stderr[0] = flog[0]; |
| 222 | fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); |
| 223 | #else |
| 224 | int fd; |
| 225 | |
| 226 | fd = unix_open("/dev/null", O_RDONLY); |
| 227 | dup2(fd, 0); |
| 228 | adb_close(fd); |
| 229 | |
| 230 | fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640); |
| 231 | if(fd < 0) { |
| 232 | fd = unix_open("/dev/null", O_WRONLY); |
| 233 | } |
| 234 | dup2(fd, 1); |
| 235 | dup2(fd, 2); |
| 236 | adb_close(fd); |
| 237 | fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid()); |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | int adb_main(int is_daemon, int server_port) |
| 242 | { |
| 243 | #if !ADB_HOST |
| 244 | int port; |
| 245 | char value[PROPERTY_VALUE_MAX]; |
| 246 | |
| 247 | umask(000); |
| 248 | #endif |
| 249 | |
| 250 | atexit(adb_cleanup); |
| 251 | #if defined(_WIN32) |
| 252 | SetConsoleCtrlHandler( ctrlc_handler, TRUE ); |
| 253 | #else |
| 254 | // No SIGCHLD. Let the service subproc handle its children. |
| 255 | signal(SIGPIPE, SIG_IGN); |
| 256 | #endif |
| 257 | |
| 258 | init_transport_registration(); |
| 259 | |
| 260 | #if ADB_HOST |
| 261 | HOST = 1; |
| 262 | |
| 263 | #ifdef WORKAROUND_BUG6558362 |
| 264 | if(is_daemon) adb_set_affinity(); |
| 265 | #endif |
| 266 | usb_init(); |
| 267 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 268 | adb_auth_init(); |
| 269 | |
| 270 | char local_name[30]; |
| 271 | build_local_name(local_name, sizeof(local_name), server_port); |
| 272 | if(install_listener(local_name, "*smartsocket*", NULL, 0)) { |
| 273 | exit(1); |
| 274 | } |
| 275 | #else |
| 276 | property_get("ro.adb.secure", value, "0"); |
| 277 | auth_enabled = !strcmp(value, "1"); |
| 278 | if (auth_enabled) |
| 279 | adb_auth_init(); |
| 280 | |
| 281 | // Our external storage path may be different than apps, since |
| 282 | // we aren't able to bind mount after dropping root. |
| 283 | const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE"); |
| 284 | if (NULL != adb_external_storage) { |
| 285 | setenv("EXTERNAL_STORAGE", adb_external_storage, 1); |
| 286 | } else { |
| 287 | D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE" |
| 288 | " unchanged.\n"); |
| 289 | } |
| 290 | |
| 291 | /* add extra groups: |
| 292 | ** AID_ADB to access the USB driver |
| 293 | ** AID_LOG to read system logs (adb logcat) |
| 294 | ** AID_INPUT to diagnose input issues (getevent) |
| 295 | ** AID_INET to diagnose network issues (ping) |
| 296 | ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump) |
| 297 | ** AID_SDCARD_R to allow reading from the SD card |
| 298 | ** AID_SDCARD_RW to allow writing to the SD card |
| 299 | ** AID_NET_BW_STATS to read out qtaguid statistics |
| 300 | */ |
| 301 | gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT, |
| 302 | AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW, |
| 303 | AID_NET_BW_STATS }; |
| 304 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 305 | exit(1); |
| 306 | } |
| 307 | |
| 308 | /* don't listen on a port (default 5037) if running in secure mode */ |
| 309 | /* don't run as root if we are running in secure mode */ |
| 310 | if (should_drop_privileges()) { |
| 311 | drop_capabilities_bounding_set_if_needed(); |
| 312 | |
| 313 | /* then switch user and group to "shell" */ |
| 314 | if (setgid(AID_SHELL) != 0) { |
| 315 | exit(1); |
| 316 | } |
| 317 | if (setuid(AID_SHELL) != 0) { |
| 318 | exit(1); |
| 319 | } |
| 320 | |
| 321 | D("Local port disabled\n"); |
| 322 | } else { |
| 323 | char local_name[30]; |
| 324 | if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) { |
| 325 | // b/12587913: fix setcon to allow const pointers |
| 326 | if (setcon((char *)root_seclabel) < 0) { |
| 327 | exit(1); |
| 328 | } |
| 329 | } |
| 330 | build_local_name(local_name, sizeof(local_name), server_port); |
| 331 | if(install_listener(local_name, "*smartsocket*", NULL, 0)) { |
| 332 | exit(1); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | int usb = 0; |
| 337 | if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) { |
| 338 | // listen on USB |
| 339 | usb_init(); |
| 340 | usb = 1; |
| 341 | } |
| 342 | |
| 343 | // If one of these properties is set, also listen on that port |
| 344 | // If one of the properties isn't set and we couldn't listen on usb, |
| 345 | // listen on the default port. |
| 346 | property_get("service.adb.tcp.port", value, ""); |
| 347 | if (!value[0]) { |
| 348 | property_get("persist.adb.tcp.port", value, ""); |
| 349 | } |
| 350 | if (sscanf(value, "%d", &port) == 1 && port > 0) { |
| 351 | printf("using port=%d\n", port); |
| 352 | // listen on TCP port specified by service.adb.tcp.port property |
| 353 | local_init(port); |
| 354 | } else if (!usb) { |
| 355 | // listen on default port |
| 356 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 357 | } |
| 358 | |
| 359 | D("adb_main(): pre init_jdwp()\n"); |
| 360 | init_jdwp(); |
| 361 | D("adb_main(): post init_jdwp()\n"); |
| 362 | #endif |
| 363 | |
| 364 | if (is_daemon) |
| 365 | { |
| 366 | // inform our parent that we are up and running. |
| 367 | #if defined(_WIN32) |
| 368 | DWORD count; |
| 369 | WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL ); |
| 370 | #else |
| 371 | fprintf(stderr, "OK\n"); |
| 372 | #endif |
| 373 | start_logging(); |
| 374 | } |
| 375 | D("Event loop starting\n"); |
| 376 | |
| 377 | fdevent_loop(); |
| 378 | |
| 379 | usb_cleanup(); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | int main(int argc, char **argv) |
| 385 | { |
| 386 | #if ADB_HOST |
| 387 | adb_sysdeps_init(); |
| 388 | adb_trace_init(); |
| 389 | D("Handling commandline()\n"); |
| 390 | return adb_commandline(argc - 1, argv + 1); |
| 391 | #else |
| 392 | /* If adbd runs inside the emulator this will enable adb tracing via |
| 393 | * adb-debug qemud service in the emulator. */ |
| 394 | adb_qemu_trace_init(); |
| 395 | while(1) { |
| 396 | int c; |
| 397 | int option_index = 0; |
| 398 | static struct option opts[] = { |
| 399 | {"root_seclabel", required_argument, 0, 's' }, |
| 400 | {"device_banner", required_argument, 0, 'b' } |
| 401 | }; |
| 402 | c = getopt_long(argc, argv, "", opts, &option_index); |
| 403 | if (c == -1) |
| 404 | break; |
| 405 | switch (c) { |
| 406 | case 's': |
| 407 | root_seclabel = optarg; |
| 408 | break; |
| 409 | case 'b': |
| 410 | adb_device_banner = optarg; |
| 411 | break; |
| 412 | default: |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | start_device_log(); |
| 418 | D("Handling main()\n"); |
| 419 | return adb_main(0, DEFAULT_ADB_PORT); |
| 420 | #endif |
| 421 | } |