blob: 218c1d0bbcffd8150f624d1745dea3f40880f1dd [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);
Nick Kralevich9f75a032015-12-15 16:51:49 -080057 if (err < 0) {
Dan Albertc89e0cc2015-05-08 16:13:53 -070058 PLOG(FATAL) << "Could not drop capabilities";
59 }
60 }
61}
62
63static bool should_drop_privileges() {
64#if defined(ALLOW_ADBD_ROOT)
65 char value[PROPERTY_VALUE_MAX];
66
Dan Albertc89e0cc2015-05-08 16:13:53 -070067 // The properties that affect `adb root` and `adb unroot` are ro.secure and
68 // ro.debuggable. In this context the names don't make the expected behavior
69 // particularly obvious.
70 //
71 // ro.debuggable:
72 // Allowed to become root, but not necessarily the default. Set to 1 on
73 // eng and userdebug builds.
74 //
75 // ro.secure:
76 // Drop privileges by default. Set to 1 on userdebug and user builds.
77 property_get("ro.secure", value, "1");
78 bool ro_secure = (strcmp(value, "1") == 0);
79
80 property_get("ro.debuggable", value, "");
81 bool ro_debuggable = (strcmp(value, "1") == 0);
82
83 // Drop privileges if ro.secure is set...
84 bool drop = ro_secure;
85
86 property_get("service.adb.root", value, "");
87 bool adb_root = (strcmp(value, "1") == 0);
88 bool adb_unroot = (strcmp(value, "0") == 0);
89
90 // ...except "adb root" lets you keep privileges in a debuggable build.
91 if (ro_debuggable && adb_root) {
92 drop = false;
93 }
94
95 // ...and "adb unroot" lets you explicitly drop privileges.
96 if (adb_unroot) {
97 drop = true;
98 }
99
100 return drop;
101#else
102 return true; // "adb root" not allowed, always drop privileges.
103#endif // ALLOW_ADBD_ROOT
104}
105
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500106static void drop_privileges(int server_port) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700107 // Add extra groups:
108 // AID_ADB to access the USB driver
109 // AID_LOG to read system logs (adb logcat)
110 // AID_INPUT to diagnose input issues (getevent)
111 // AID_INET to diagnose network issues (ping)
112 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
113 // AID_SDCARD_R to allow reading from the SD card
114 // AID_SDCARD_RW to allow writing to the SD card
115 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800116 // AID_READPROC for reading /proc entries across UID boundaries
Dan Albertc89e0cc2015-05-08 16:13:53 -0700117 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT,
118 AID_INET, AID_NET_BT, AID_NET_BT_ADMIN,
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800119 AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS,
120 AID_READPROC };
Dan Albertc89e0cc2015-05-08 16:13:53 -0700121 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800122 PLOG(FATAL) << "Could not set supplemental groups";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700123 }
124
125 /* don't listen on a port (default 5037) if running in secure mode */
126 /* don't run as root if we are running in secure mode */
127 if (should_drop_privileges()) {
128 drop_capabilities_bounding_set_if_needed();
129
130 /* then switch user and group to "shell" */
131 if (setgid(AID_SHELL) != 0) {
132 PLOG(FATAL) << "Could not setgid";
133 }
134 if (setuid(AID_SHELL) != 0) {
135 PLOG(FATAL) << "Could not setuid";
136 }
137
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700138 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700139 } else {
Nick Kralevich4d870952015-06-12 22:03:50 -0700140 if (root_seclabel != nullptr) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700141 if (setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800142 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700143 }
144 }
Spencer Low5200c662015-07-30 23:07:55 -0700145 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700146 std::string local_name =
147 android::base::StringPrintf("tcp:%d", server_port);
Spencer Low5200c662015-07-30 23:07:55 -0700148 if (install_listener(local_name, "*smartsocket*", nullptr, 0,
149 &error)) {
150 LOG(FATAL) << "Could not install *smartsocket* listener: "
151 << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700152 }
153 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500154}
155
156int adbd_main(int server_port) {
157 umask(0);
158
159 signal(SIGPIPE, SIG_IGN);
160
161 init_transport_registration();
162
163 // We need to call this even if auth isn't enabled because the file
164 // descriptor will always be open.
165 adbd_cloexec_auth_socket();
166
167 if (ALLOW_ADBD_NO_AUTH && property_get_bool("ro.adb.secure", 0) == 0) {
168 auth_required = false;
169 }
170
171 adbd_auth_init();
172
173 // Our external storage path may be different than apps, since
174 // we aren't able to bind mount after dropping root.
175 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
176 if (adb_external_storage != nullptr) {
177 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
178 } else {
179 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
180 " unchanged.\n");
181 }
182
183 drop_privileges(server_port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700184
185 bool is_usb = false;
186 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
187 // Listen on USB.
188 usb_init();
189 is_usb = true;
190 }
191
192 // If one of these properties is set, also listen on that port.
193 // If one of the properties isn't set and we couldn't listen on usb, listen
194 // on the default port.
195 char prop_port[PROPERTY_VALUE_MAX];
196 property_get("service.adb.tcp.port", prop_port, "");
197 if (prop_port[0] == '\0') {
198 property_get("persist.adb.tcp.port", prop_port, "");
199 }
200
201 int port;
202 if (sscanf(prop_port, "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700203 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700204 // Listen on TCP port specified by service.adb.tcp.port property.
205 local_init(port);
206 } else if (!is_usb) {
207 // Listen on default port.
208 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
209 }
210
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700211 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700212 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700213 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700214
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700215 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700216 fdevent_loop();
217
218 return 0;
219}
220
Dan Albertc89e0cc2015-05-08 16:13:53 -0700221int main(int argc, char** argv) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700222 while (true) {
223 static struct option opts[] = {
224 {"root_seclabel", required_argument, nullptr, 's'},
225 {"device_banner", required_argument, nullptr, 'b'},
226 {"version", no_argument, nullptr, 'v'},
227 };
228
229 int option_index = 0;
230 int c = getopt_long(argc, argv, "", opts, &option_index);
231 if (c == -1) {
232 break;
233 }
234
235 switch (c) {
236 case 's':
237 root_seclabel = optarg;
238 break;
239 case 'b':
240 adb_device_banner = optarg;
241 break;
242 case 'v':
243 printf("Android Debug Bridge Daemon version %d.%d.%d %s\n",
244 ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
245 ADB_REVISION);
246 return 0;
247 default:
248 // getopt already prints "adbd: invalid option -- %c" for us.
249 return 1;
250 }
251 }
252
253 close_stdin();
254
Dan Albert9313c0d2015-05-21 13:58:50 -0700255 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700256
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700257 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700258 return adbd_main(DEFAULT_ADB_PORT);
259}