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