blob: db8f07ba4cee552dd294ac88ca4e3b08a374a9b6 [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
Josh Gao776c2ec2019-01-22 19:36:15 -080021#if defined(__BIONIC__)
Josh Gao954adcc2018-07-25 18:13:44 -070022#include <android/fdsan.h>
Josh Gao776c2ec2019-01-22 19:36:15 -080023#endif
24
Dan Albertc89e0cc2015-05-08 16:13:53 -070025#include <errno.h>
Josh Gao218f7662018-04-04 17:53:54 -070026#include <getopt.h>
27#include <malloc.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070028#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070031#include <sys/capability.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070032#include <sys/prctl.h>
33
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080034#include <memory>
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +090035#include <vector>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080036
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/logging.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040038#include <android-base/macros.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070039#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080040#include <android-base/stringprintf.h>
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +090041#include <android-base/strings.h>
Josh Gao776c2ec2019-01-22 19:36:15 -080042
43#if defined(__ANDROID__)
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080044#include <libminijail.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070045#include <log/log_properties.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040046#include <scoped_minijail.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080047
Mark Salyzyn97787a02016-03-28 15:52:13 -070048#include <private/android_filesystem_config.h>
Tom Cherry38cd57a2015-12-11 14:28:09 -080049#include "selinux/android.h"
Josh Gao776c2ec2019-01-22 19:36:15 -080050#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -070051
52#include "adb.h"
53#include "adb_auth.h"
54#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080055#include "adb_utils.h"
Joshua Duongd85f5c02019-11-20 14:18:43 -080056#include "adb_wifi.h"
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +090057#include "socket_spec.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070058#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070059
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070060#include "mdns.h"
61
Josh Gao776c2ec2019-01-22 19:36:15 -080062#if defined(__ANDROID__)
Dan Albertc89e0cc2015-05-08 16:13:53 -070063static const char* root_seclabel = nullptr;
64
Bowgo Tsai9b30c0a2019-03-12 04:25:33 +080065static inline bool is_device_unlocked() {
66 return "orange" == android::base::GetProperty("ro.boot.verifiedbootstate", "");
67}
68
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070069static bool should_drop_capabilities_bounding_set() {
Bowgo Tsai9b30c0a2019-03-12 04:25:33 +080070 if (ALLOW_ADBD_ROOT || is_device_unlocked()) {
71 if (__android_log_is_debuggable()) {
72 return false;
73 }
Dan Albertc89e0cc2015-05-08 16:13:53 -070074 }
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070075 return true;
Dan Albertc89e0cc2015-05-08 16:13:53 -070076}
77
78static bool should_drop_privileges() {
Bowgo Tsai9b30c0a2019-03-12 04:25:33 +080079 // "adb root" not allowed, always drop privileges.
80 if (!ALLOW_ADBD_ROOT && !is_device_unlocked()) return true;
81
Dan Albertc89e0cc2015-05-08 16:13:53 -070082 // The properties that affect `adb root` and `adb unroot` are ro.secure and
83 // ro.debuggable. In this context the names don't make the expected behavior
84 // particularly obvious.
85 //
86 // ro.debuggable:
87 // Allowed to become root, but not necessarily the default. Set to 1 on
88 // eng and userdebug builds.
89 //
90 // ro.secure:
91 // Drop privileges by default. Set to 1 on userdebug and user builds.
Elliott Hughesffdec182016-09-23 15:40:03 -070092 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzyn97787a02016-03-28 15:52:13 -070093 bool ro_debuggable = __android_log_is_debuggable();
Dan Albertc89e0cc2015-05-08 16:13:53 -070094
95 // Drop privileges if ro.secure is set...
96 bool drop = ro_secure;
97
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080098 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughesffdec182016-09-23 15:40:03 -070099 std::string prop = android::base::GetProperty("service.adb.root", "");
100 bool adb_root = (prop == "1");
101 bool adb_unroot = (prop == "0");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700102 if (ro_debuggable && adb_root) {
103 drop = false;
104 }
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800105 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700106 if (adb_unroot) {
107 drop = true;
108 }
109
110 return drop;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700111}
112
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500113static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400114 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800115
Dan Albertc89e0cc2015-05-08 16:13:53 -0700116 // Add extra groups:
117 // AID_ADB to access the USB driver
118 // AID_LOG to read system logs (adb logcat)
119 // AID_INPUT to diagnose input issues (getevent)
120 // AID_INET to diagnose network issues (ping)
121 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
122 // AID_SDCARD_R to allow reading from the SD card
123 // AID_SDCARD_RW to allow writing to the SD card
124 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800125 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou0729dd12017-05-08 15:50:55 -0700126 // AID_UHID for using 'hid' command to read/write to /dev/uhid
127 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
128 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
129 AID_NET_BW_STATS, AID_READPROC, AID_UHID};
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400130 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700131
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800132 // Don't listen on a port (default 5037) if running in secure mode.
133 // Don't run as root if running in secure mode.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700134 if (should_drop_privileges()) {
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700135 const bool should_drop_caps = should_drop_capabilities_bounding_set();
136
137 if (should_drop_caps) {
138 minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
139 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700140
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800141 minijail_change_gid(jail.get(), AID_SHELL);
142 minijail_change_uid(jail.get(), AID_SHELL);
143 // minijail_enter() will abort if any priv-dropping step fails.
144 minijail_enter(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700145
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700146 // Whenever ambient capabilities are being used, minijail cannot
147 // simultaneously drop the bounding capability set to just
148 // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
149 // and permitted sets. So we need to do that in two steps.
150 using ScopedCaps =
151 std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
152 ScopedCaps caps(cap_get_proc(), &cap_free);
153 if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
154 PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
155 }
156 if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
157 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
158 }
159 if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
160 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
161 }
162 if (cap_set_proc(caps.get()) != 0) {
163 PLOG(FATAL) << "cap_set_proc() failed";
164 }
165
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700166 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700167 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800168 // minijail_enter() will abort if any priv-dropping step fails.
169 minijail_enter(jail.get());
170
Nick Kralevich4d870952015-06-12 22:03:50 -0700171 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800172 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800173 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700174 }
175 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700176 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500177}
Josh Gao776c2ec2019-01-22 19:36:15 -0800178#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500179
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900180static void setup_adb(const std::vector<std::string>& addrs) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800181#if defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900182 // Get the first valid port from addrs and setup mDNS.
183 int port = -1;
184 std::string error;
185 for (const auto& addr : addrs) {
186 port = get_host_socket_spec_port(addr, &error);
187 if (port != -1) {
188 break;
189 }
190 }
191 if (port == -1) {
192 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
193 }
Joshua Duongd85f5c02019-11-20 14:18:43 -0800194 LOG(INFO) << "Setup mdns on port= " << port;
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700195 setup_mdns(port);
Josh Gao776c2ec2019-01-22 19:36:15 -0800196#endif
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900197 for (const auto& addr : addrs) {
198 LOG(INFO) << "adbd listening on " << addr;
199 local_init(addr);
200 }
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700201}
202
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500203int adbd_main(int server_port) {
204 umask(0);
205
206 signal(SIGPIPE, SIG_IGN);
207
Josh Gao776c2ec2019-01-22 19:36:15 -0800208#if defined(__BIONIC__)
Josh Gao954adcc2018-07-25 18:13:44 -0700209 auto fdsan_level = android_fdsan_get_error_level();
210 if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
211 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
212 }
Josh Gao776c2ec2019-01-22 19:36:15 -0800213#endif
Josh Gao954adcc2018-07-25 18:13:44 -0700214
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500215 init_transport_registration();
216
217 // We need to call this even if auth isn't enabled because the file
218 // descriptor will always be open.
219 adbd_cloexec_auth_socket();
220
Tianjie Xua933b432019-10-30 10:37:32 -0700221#if defined(__ANDROID_RECOVERY__)
222 if (is_device_unlocked() || __android_log_is_debuggable()) {
223 auth_required = false;
224 }
225#elif defined(ALLOW_ADBD_NO_AUTH)
Josh Gao6eda1842018-03-06 13:37:45 -0800226 // If ro.adb.secure is unset, default to no authentication required.
227 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Bowgo Tsai9b30c0a2019-03-12 04:25:33 +0800228#elif defined(__ANDROID__)
229 if (is_device_unlocked()) { // allows no authentication when the device is unlocked.
230 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
231 }
Josh Gao27768452018-01-02 12:01:43 -0800232#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500233
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500234 // Our external storage path may be different than apps, since
235 // we aren't able to bind mount after dropping root.
236 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
237 if (adb_external_storage != nullptr) {
238 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
239 } else {
240 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
241 " unchanged.\n");
242 }
243
Josh Gao776c2ec2019-01-22 19:36:15 -0800244#if defined(__ANDROID__)
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500245 drop_privileges(server_port);
Josh Gao776c2ec2019-01-22 19:36:15 -0800246#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700247
Josh Gao27523262019-10-22 12:30:39 -0700248 // adbd_auth_init will spawn a thread, so we need to defer it until after selinux transitions.
249 adbd_auth_init();
250
Dan Albertc89e0cc2015-05-08 16:13:53 -0700251 bool is_usb = false;
Josh Gao776c2ec2019-01-22 19:36:15 -0800252
253#if defined(__ANDROID__)
Josh Gao183b73e2017-01-11 14:39:19 -0800254 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700255 // Listen on USB.
256 usb_init();
257 is_usb = true;
258 }
Josh Gao776c2ec2019-01-22 19:36:15 -0800259#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700260
261 // If one of these properties is set, also listen on that port.
262 // If one of the properties isn't set and we couldn't listen on usb, listen
263 // on the default port.
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900264 std::vector<std::string> addrs;
265 std::string prop_addr = android::base::GetProperty("service.adb.listen_addrs", "");
266 if (prop_addr.empty()) {
267 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
268 if (prop_port.empty()) {
269 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
270 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700271
Josh Gao06e7cb52019-09-17 17:34:41 +0800272#if !defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900273 if (prop_port.empty() && getenv("ADBD_PORT")) {
274 prop_port = getenv("ADBD_PORT");
275 }
Josh Gao06e7cb52019-09-17 17:34:41 +0800276#endif
277
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900278 int port;
279 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
280 D("using tcp port=%d", port);
281 // Listen on TCP and VSOCK port specified by service.adb.tcp.port property.
282 addrs.push_back(android::base::StringPrintf("tcp:%d", port));
283 addrs.push_back(android::base::StringPrintf("vsock:%d", port));
284 setup_adb(addrs);
285 } else if (!is_usb) {
286 // Listen on default port.
287 addrs.push_back(
288 android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
289 addrs.push_back(
290 android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
291 setup_adb(addrs);
292 }
293 } else {
294 addrs = android::base::Split(prop_addr, ",");
295 setup_adb(addrs);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700296 }
297
Josh Gao98419f72020-04-20 16:45:55 -0700298 LOG(INFO) << "adbd started";
299
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700300 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700301 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700302 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700303
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700304 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700305 fdevent_loop();
306
307 return 0;
308}
309
Dan Albertc89e0cc2015-05-08 16:13:53 -0700310int main(int argc, char** argv) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800311#if defined(__BIONIC__)
Josh Gao218f7662018-04-04 17:53:54 -0700312 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
313 mallopt(M_DECAY_TIME, 1);
Josh Gao776c2ec2019-01-22 19:36:15 -0800314#endif
Josh Gao218f7662018-04-04 17:53:54 -0700315
Dan Albertc89e0cc2015-05-08 16:13:53 -0700316 while (true) {
317 static struct option opts[] = {
Joshua Duongd85f5c02019-11-20 14:18:43 -0800318 {"root_seclabel", required_argument, nullptr, 's'},
319 {"device_banner", required_argument, nullptr, 'b'},
320 {"version", no_argument, nullptr, 'v'},
321 {"logpostfsdata", no_argument, nullptr, 'l'},
Dan Albertc89e0cc2015-05-08 16:13:53 -0700322 };
323
324 int option_index = 0;
325 int c = getopt_long(argc, argv, "", opts, &option_index);
326 if (c == -1) {
327 break;
328 }
329
330 switch (c) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800331#if defined(__ANDROID__)
332 case 's':
333 root_seclabel = optarg;
334 break;
335#endif
336 case 'b':
337 adb_device_banner = optarg;
338 break;
339 case 'v':
340 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
341 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
342 return 0;
Joshua Duongd85f5c02019-11-20 14:18:43 -0800343 case 'l':
344 LOG(ERROR) << "post-fs-data triggered";
345 return 0;
Josh Gao776c2ec2019-01-22 19:36:15 -0800346 default:
347 // getopt already prints "adbd: invalid option -- %c" for us.
348 return 1;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700349 }
350 }
351
352 close_stdin();
353
Dan Albert9313c0d2015-05-21 13:58:50 -0700354 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700355
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700356 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700357 return adbd_main(DEFAULT_ADB_PORT);
358}