blob: feea7a3a83a6ec9fce6be8c4b8c9f31d7d187613 [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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albertc89e0cc2015-05-08 16:13:53 -070018
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
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include <android-base/logging.h>
29#include <android-base/stringprintf.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070030#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"
Josh Gao7d586072015-11-20 15:37:31 -080037#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070038#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070039
40static const char* root_seclabel = nullptr;
41
42static 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
67static bool should_drop_privileges() {
68#if defined(ALLOW_ADBD_ROOT)
69 char value[PROPERTY_VALUE_MAX];
70
Dan Albertc89e0cc2015-05-08 16:13:53 -070071 // The properties that affect `adb root` and `adb unroot` are ro.secure and
72 // ro.debuggable. In this context the names don't make the expected behavior
73 // particularly obvious.
74 //
75 // ro.debuggable:
76 // Allowed to become root, but not necessarily the default. Set to 1 on
77 // eng and userdebug builds.
78 //
79 // ro.secure:
80 // Drop privileges by default. Set to 1 on userdebug and user builds.
81 property_get("ro.secure", value, "1");
82 bool ro_secure = (strcmp(value, "1") == 0);
83
84 property_get("ro.debuggable", value, "");
85 bool ro_debuggable = (strcmp(value, "1") == 0);
86
87 // Drop privileges if ro.secure is set...
88 bool drop = ro_secure;
89
90 property_get("service.adb.root", value, "");
91 bool adb_root = (strcmp(value, "1") == 0);
92 bool adb_unroot = (strcmp(value, "0") == 0);
93
94 // ...except "adb root" lets you keep privileges in a debuggable build.
95 if (ro_debuggable && adb_root) {
96 drop = false;
97 }
98
99 // ...and "adb unroot" lets you explicitly drop privileges.
100 if (adb_unroot) {
101 drop = true;
102 }
103
104 return drop;
105#else
106 return true; // "adb root" not allowed, always drop privileges.
107#endif // ALLOW_ADBD_ROOT
108}
109
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500110static void drop_privileges(int server_port) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700111 // Add extra groups:
112 // AID_ADB to access the USB driver
113 // AID_LOG to read system logs (adb logcat)
114 // AID_INPUT to diagnose input issues (getevent)
115 // AID_INET to diagnose network issues (ping)
116 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
117 // AID_SDCARD_R to allow reading from the SD card
118 // AID_SDCARD_RW to allow writing to the SD card
119 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800120 // AID_READPROC for reading /proc entries across UID boundaries
Dan Albertc89e0cc2015-05-08 16:13:53 -0700121 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT,
122 AID_INET, AID_NET_BT, AID_NET_BT_ADMIN,
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800123 AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS,
124 AID_READPROC };
Dan Albertc89e0cc2015-05-08 16:13:53 -0700125 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800126 PLOG(FATAL) << "Could not set supplemental groups";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700127 }
128
129 /* don't listen on a port (default 5037) if running in secure mode */
130 /* don't run as root if we are running in secure mode */
131 if (should_drop_privileges()) {
132 drop_capabilities_bounding_set_if_needed();
133
134 /* then switch user and group to "shell" */
135 if (setgid(AID_SHELL) != 0) {
136 PLOG(FATAL) << "Could not setgid";
137 }
138 if (setuid(AID_SHELL) != 0) {
139 PLOG(FATAL) << "Could not setuid";
140 }
141
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700142 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700143 } else {
Nick Kralevich4d870952015-06-12 22:03:50 -0700144 if (root_seclabel != nullptr) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700145 if (setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800146 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700147 }
148 }
Spencer Low5200c662015-07-30 23:07:55 -0700149 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700150 std::string local_name =
151 android::base::StringPrintf("tcp:%d", server_port);
Spencer Low5200c662015-07-30 23:07:55 -0700152 if (install_listener(local_name, "*smartsocket*", nullptr, 0,
153 &error)) {
154 LOG(FATAL) << "Could not install *smartsocket* listener: "
155 << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700156 }
157 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500158}
159
160int adbd_main(int server_port) {
161 umask(0);
162
163 signal(SIGPIPE, SIG_IGN);
164
165 init_transport_registration();
166
167 // We need to call this even if auth isn't enabled because the file
168 // descriptor will always be open.
169 adbd_cloexec_auth_socket();
170
171 if (ALLOW_ADBD_NO_AUTH && property_get_bool("ro.adb.secure", 0) == 0) {
172 auth_required = false;
173 }
174
175 adbd_auth_init();
176
177 // Our external storage path may be different than apps, since
178 // we aren't able to bind mount after dropping root.
179 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
180 if (adb_external_storage != nullptr) {
181 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
182 } else {
183 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
184 " unchanged.\n");
185 }
186
187 drop_privileges(server_port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700188
189 bool is_usb = false;
190 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
191 // Listen on USB.
192 usb_init();
193 is_usb = true;
194 }
195
196 // If one of these properties is set, also listen on that port.
197 // If one of the properties isn't set and we couldn't listen on usb, listen
198 // on the default port.
199 char prop_port[PROPERTY_VALUE_MAX];
200 property_get("service.adb.tcp.port", prop_port, "");
201 if (prop_port[0] == '\0') {
202 property_get("persist.adb.tcp.port", prop_port, "");
203 }
204
205 int port;
206 if (sscanf(prop_port, "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700207 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700208 // Listen on TCP port specified by service.adb.tcp.port property.
209 local_init(port);
210 } else if (!is_usb) {
211 // Listen on default port.
212 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
213 }
214
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700215 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700216 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700217 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700218
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700219 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700220 fdevent_loop();
221
222 return 0;
223}
224
Dan Albertc89e0cc2015-05-08 16:13:53 -0700225int main(int argc, char** argv) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700226 while (true) {
227 static struct option opts[] = {
228 {"root_seclabel", required_argument, nullptr, 's'},
229 {"device_banner", required_argument, nullptr, 'b'},
230 {"version", no_argument, nullptr, 'v'},
231 };
232
233 int option_index = 0;
234 int c = getopt_long(argc, argv, "", opts, &option_index);
235 if (c == -1) {
236 break;
237 }
238
239 switch (c) {
240 case 's':
241 root_seclabel = optarg;
242 break;
243 case 'b':
244 adb_device_banner = optarg;
245 break;
246 case 'v':
247 printf("Android Debug Bridge Daemon version %d.%d.%d %s\n",
248 ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
249 ADB_REVISION);
250 return 0;
251 default:
252 // getopt already prints "adbd: invalid option -- %c" for us.
253 return 1;
254 }
255 }
256
257 close_stdin();
258
Dan Albert9313c0d2015-05-21 13:58:50 -0700259 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700260
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700261 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700262 return adbd_main(DEFAULT_ADB_PORT);
263}