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