blob: 77e535af5b76fe62ab58f2637364a390d561d32f [file] [log] [blame]
Dan Albert53a37442015-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert53a37442015-05-08 16:13:53 -070018
19#include "sysdeps.h"
20
21#include <errno.h>
Josh Gao358aca22018-04-04 17:53:54 -070022#include <getopt.h>
23#include <malloc.h>
Dan Albert53a37442015-05-08 16:13:53 -070024#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
Luis Hector Chaveze4528e12018-04-04 15:59:59 -070027#include <sys/capability.h>
Dan Albert53a37442015-05-08 16:13:53 -070028#include <sys/prctl.h>
29
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080030#include <memory>
Jason Jeremy Iman84613872019-07-19 12:44:39 +090031#include <vector>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080032
Elliott Hughesf55ead92015-12-04 22:00:26 -080033#include <android-base/logging.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040034#include <android-base/macros.h>
Elliott Hughes8b249d22016-09-23 15:40:03 -070035#include <android-base/properties.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080036#include <android-base/stringprintf.h>
Jason Jeremy Iman84613872019-07-19 12:44:39 +090037#include <android-base/strings.h>
Josh Gao0560feb2019-01-22 19:36:15 -080038
39#if defined(__ANDROID__)
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080040#include <libminijail.h>
Steven Morelandb087d302017-04-13 23:48:57 -070041#include <log/log_properties.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040042#include <scoped_minijail.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080043
Mark Salyzync75f65f2016-03-28 15:52:13 -070044#include <private/android_filesystem_config.h>
Tom Cherry46845222015-12-11 14:28:09 -080045#include "selinux/android.h"
Josh Gao0560feb2019-01-22 19:36:15 -080046#endif
Dan Albert53a37442015-05-08 16:13:53 -070047
48#include "adb.h"
49#include "adb_auth.h"
50#include "adb_listeners.h"
Josh Gao1eef4782015-11-20 15:37:31 -080051#include "adb_utils.h"
Joshua Duong290ccb52019-11-20 14:18:43 -080052#include "adb_wifi.h"
Jason Jeremy Iman84613872019-07-19 12:44:39 +090053#include "socket_spec.h"
Dan Albert53a37442015-05-08 16:13:53 -070054#include "transport.h"
Dan Albert53a37442015-05-08 16:13:53 -070055
Fabien Sanglard3331d892022-12-21 01:43:43 +000056#include "daemon/jdwp_service.h"
Josh Gao2dc61962021-02-03 22:11:45 -080057#include "daemon/mdns.h"
58#include "daemon/watchdog.h"
Casey Dahlin10ad15f2016-05-06 16:19:13 -070059
Josh Gao0560feb2019-01-22 19:36:15 -080060#if defined(__ANDROID__)
Dan Albert53a37442015-05-08 16:13:53 -070061static const char* root_seclabel = nullptr;
62
Dan Albert53a37442015-05-08 16:13:53 -070063static bool should_drop_privileges() {
Dan Albert53a37442015-05-08 16:13:53 -070064 // The properties that affect `adb root` and `adb unroot` are ro.secure and
65 // ro.debuggable. In this context the names don't make the expected behavior
66 // particularly obvious.
67 //
68 // ro.debuggable:
69 // Allowed to become root, but not necessarily the default. Set to 1 on
70 // eng and userdebug builds.
71 //
72 // ro.secure:
73 // Drop privileges by default. Set to 1 on userdebug and user builds.
Elliott Hughes8b249d22016-09-23 15:40:03 -070074 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzync75f65f2016-03-28 15:52:13 -070075 bool ro_debuggable = __android_log_is_debuggable();
Dan Albert53a37442015-05-08 16:13:53 -070076
77 // Drop privileges if ro.secure is set...
78 bool drop = ro_secure;
79
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080080 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughes8b249d22016-09-23 15:40:03 -070081 std::string prop = android::base::GetProperty("service.adb.root", "");
82 bool adb_root = (prop == "1");
83 bool adb_unroot = (prop == "0");
Dan Albert53a37442015-05-08 16:13:53 -070084 if (ro_debuggable && adb_root) {
85 drop = false;
86 }
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080087 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albert53a37442015-05-08 16:13:53 -070088 if (adb_unroot) {
89 drop = true;
90 }
91
92 return drop;
Dan Albert53a37442015-05-08 16:13:53 -070093}
94
Mike Frysinger2e114072015-12-08 18:57:47 -050095static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040096 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080097
Dan Albert53a37442015-05-08 16:13:53 -070098 // Add extra groups:
99 // AID_ADB to access the USB driver
100 // AID_LOG to read system logs (adb logcat)
101 // AID_INPUT to diagnose input issues (getevent)
102 // AID_INET to diagnose network issues (ping)
103 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
104 // AID_SDCARD_R to allow reading from the SD card
105 // AID_SDCARD_RW to allow writing to the SD card
106 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevich8a1b4b32015-11-07 16:52:17 -0800107 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou209c27b2017-05-08 15:50:55 -0700108 // AID_UHID for using 'hid' command to read/write to /dev/uhid
Martijn Coenen223bcc72020-08-18 15:34:15 +0200109 // AID_EXT_DATA_RW for writing to /sdcard/Android/data (devices without sdcardfs)
110 // AID_EXT_OBB_RW for writing to /sdcard/Android/obb (devices without sdcardfs)
Kalesh Singh7cb519d2021-12-06 16:19:58 -0800111 // AID_READTRACEFS for reading tracefs entries
Siarhei Vishniakou209c27b2017-05-08 15:50:55 -0700112 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
113 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
Martijn Coenen223bcc72020-08-18 15:34:15 +0200114 AID_NET_BW_STATS, AID_READPROC, AID_UHID, AID_EXT_DATA_RW,
Kalesh Singh7cb519d2021-12-06 16:19:58 -0800115 AID_EXT_OBB_RW, AID_READTRACEFS};
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -0400116 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albert53a37442015-05-08 16:13:53 -0700117
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800118 // Don't listen on a port (default 5037) if running in secure mode.
119 // Don't run as root if running in secure mode.
Dan Albert53a37442015-05-08 16:13:53 -0700120 if (should_drop_privileges()) {
Josh Gaoc5ca6382020-06-04 17:58:48 -0700121 const bool should_drop_caps = !__android_log_is_debuggable();
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700122
123 if (should_drop_caps) {
Nikita Ioffe89a9afc2024-04-19 00:38:49 +0000124 // CAP_SETUI and CAP_SETGID are required for change_uid and change_gid calls below.
125 // CAP_SYS_NICE needs to be in the bounding set of adbd for sh spawned from `adb shell`
126 // to also have it in the bounding set. This in turn is required to be able to launch
127 // VMs from shell (e.g. adb shell /apex/com.android.virt/bin/vm run-microdroid).
128 // Full fork+execve chain looks like this:
129 // adbd (CapBnd: CAP_SYS_NICE) -> /system/bin/sh (CapBnd: CAP_SYS_NICE) ->
130 // /apex/com.android.virt/bin/vm (CapBnd: CAP_SYS_NICE) ->
131 // virtmngr (CapBnd: CAP_SYS_NICE) -> crosvm (CapEff: CAP_SYS_NICE).
Nikita Ioffe8b970562024-04-19 17:23:47 +0000132 // Note: the adbd will drop it's effective capabilities several lines below, while the
133 // /system/bin/sh process spawned from adbd will run as non-root uid, hence won't be
134 // able to use the CAP_SYS_NICE capability in the first place.
Nikita Ioffe89a9afc2024-04-19 00:38:49 +0000135 minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID) |
136 CAP_TO_MASK(CAP_SYS_NICE));
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700137 }
Dan Albert53a37442015-05-08 16:13:53 -0700138
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800139 minijail_change_gid(jail.get(), AID_SHELL);
140 minijail_change_uid(jail.get(), AID_SHELL);
141 // minijail_enter() will abort if any priv-dropping step fails.
142 minijail_enter(jail.get());
Dan Albert53a37442015-05-08 16:13:53 -0700143
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700144 // Whenever ambient capabilities are being used, minijail cannot
145 // simultaneously drop the bounding capability set to just
146 // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
147 // and permitted sets. So we need to do that in two steps.
148 using ScopedCaps =
149 std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
150 ScopedCaps caps(cap_get_proc(), &cap_free);
151 if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
152 PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
153 }
154 if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
Nikita Ioffe8b970562024-04-19 17:23:47 +0000155 PLOG(FATAL) << "cap_clear_flag(EFFECTIVE) failed";
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700156 }
157 if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
158 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
159 }
160 if (cap_set_proc(caps.get()) != 0) {
161 PLOG(FATAL) << "cap_set_proc() failed";
162 }
163
Yabin Cui815ad882015-09-02 17:44:28 -0700164 D("Local port disabled");
Dan Albert53a37442015-05-08 16:13:53 -0700165 } else {
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800166 // minijail_enter() will abort if any priv-dropping step fails.
167 minijail_enter(jail.get());
168
Nick Kralevich11f73a12015-06-12 22:03:50 -0700169 if (root_seclabel != nullptr) {
Tom Cherry46845222015-12-11 14:28:09 -0800170 if (selinux_android_setcon(root_seclabel) < 0) {
Yi-Yo Chiang50d4f792022-08-06 14:32:51 +0800171 // If we failed to become root, don't try again to avoid a
172 // restart loop.
173 android::base::SetProperty("service.adb.root", "0");
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800174 LOG(FATAL) << "Could not set SELinux context";
Dan Albert53a37442015-05-08 16:13:53 -0700175 }
176 }
Dan Albert53a37442015-05-08 16:13:53 -0700177 }
Mike Frysinger2e114072015-12-08 18:57:47 -0500178}
Josh Gao0560feb2019-01-22 19:36:15 -0800179#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500180
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900181static void setup_adb(const std::vector<std::string>& addrs) {
Josh Gao0560feb2019-01-22 19:36:15 -0800182#if defined(__ANDROID__)
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900183 // Get the first valid port from addrs and setup mDNS.
184 int port = -1;
185 std::string error;
186 for (const auto& addr : addrs) {
187 port = get_host_socket_spec_port(addr, &error);
188 if (port != -1) {
189 break;
190 }
191 }
192 if (port == -1) {
193 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
194 }
Joshua Duong290ccb52019-11-20 14:18:43 -0800195 LOG(INFO) << "Setup mdns on port= " << port;
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700196 setup_mdns(port);
Josh Gao0560feb2019-01-22 19:36:15 -0800197#endif
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900198 for (const auto& addr : addrs) {
199 LOG(INFO) << "adbd listening on " << addr;
200 local_init(addr);
201 }
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700202}
203
Mike Frysinger2e114072015-12-08 18:57:47 -0500204int adbd_main(int server_port) {
205 umask(0);
206
207 signal(SIGPIPE, SIG_IGN);
208
Mike Frysinger2e114072015-12-08 18:57:47 -0500209 // We need to call this even if auth isn't enabled because the file
210 // descriptor will always be open.
211 adbd_cloexec_auth_socket();
212
Josh Gaoc5ca6382020-06-04 17:58:48 -0700213#if defined(__ANDROID__)
Shaju Mathew390e67a2021-10-15 12:01:49 -0700214 bool device_unlocked = android::base::GetProperty("ro.boot.verifiedbootstate", "") == "orange";
215 if (device_unlocked || __android_log_is_debuggable()) {
216#if defined(__ANDROID_RECOVERY__)
217 auth_required = false; // Bypass authorization when the device transitions to
218 // fastbootd (from recovery). A corrupt userdata image can potentially
219 // result in the device falling into rescue, and a subsequent fastboot
220 // state should not require authorization - otherwise, it will force the
221 // need for manual intervention(b/188703874).
222#else
223 // If we're on userdebug/eng or the device is unlocked, permit no-authentication.
Bowgo Tsai35b817b2019-03-12 04:25:33 +0800224 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Shaju Mathew390e67a2021-10-15 12:01:49 -0700225#endif
Bowgo Tsai35b817b2019-03-12 04:25:33 +0800226 }
Josh Gao361148b2018-01-02 12:01:43 -0800227#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500228
Mike Frysinger2e114072015-12-08 18:57:47 -0500229 // Our external storage path may be different than apps, since
230 // we aren't able to bind mount after dropping root.
231 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
232 if (adb_external_storage != nullptr) {
233 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
234 } else {
235 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
236 " unchanged.\n");
237 }
238
Josh Gao0560feb2019-01-22 19:36:15 -0800239#if defined(__ANDROID__)
Mike Frysinger2e114072015-12-08 18:57:47 -0500240 drop_privileges(server_port);
Josh Gao0560feb2019-01-22 19:36:15 -0800241#endif
Dan Albert53a37442015-05-08 16:13:53 -0700242
Josh Gao2dc61962021-02-03 22:11:45 -0800243#if defined(__ANDROID__)
244 // A thread gets spawned as a side-effect of initializing the watchdog, so it needs to happen
245 // after we drop privileges.
246 watchdog::Initialize();
247#endif
248
Josh Gao7cac88a2019-10-22 12:30:39 -0700249 // adbd_auth_init will spawn a thread, so we need to defer it until after selinux transitions.
250 adbd_auth_init();
251
Dan Albert53a37442015-05-08 16:13:53 -0700252 bool is_usb = false;
Josh Gao0560feb2019-01-22 19:36:15 -0800253
254#if defined(__ANDROID__)
Josh Gao0dde9c82017-01-11 14:39:19 -0800255 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albert53a37442015-05-08 16:13:53 -0700256 // Listen on USB.
257 usb_init();
258 is_usb = true;
259 }
Josh Gao0560feb2019-01-22 19:36:15 -0800260#endif
Dan Albert53a37442015-05-08 16:13:53 -0700261
262 // If one of these properties is set, also listen on that port.
263 // If one of the properties isn't set and we couldn't listen on usb, listen
264 // on the default port.
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900265 std::vector<std::string> addrs;
266 std::string prop_addr = android::base::GetProperty("service.adb.listen_addrs", "");
267 if (prop_addr.empty()) {
268 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
269 if (prop_port.empty()) {
270 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
271 }
Dan Albert53a37442015-05-08 16:13:53 -0700272
Josh Gao79d122a2019-09-17 17:34:41 +0800273#if !defined(__ANDROID__)
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900274 if (prop_port.empty() && getenv("ADBD_PORT")) {
275 prop_port = getenv("ADBD_PORT");
276 }
Josh Gao79d122a2019-09-17 17:34:41 +0800277#endif
278
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900279 int port;
280 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
281 D("using tcp port=%d", port);
282 // Listen on TCP and VSOCK port specified by service.adb.tcp.port property.
283 addrs.push_back(android::base::StringPrintf("tcp:%d", port));
284 addrs.push_back(android::base::StringPrintf("vsock:%d", port));
285 setup_adb(addrs);
286 } else if (!is_usb) {
287 // Listen on default port.
288 addrs.push_back(
289 android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
290 addrs.push_back(
291 android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
292 setup_adb(addrs);
293 }
294 } else {
295 addrs = android::base::Split(prop_addr, ",");
296 setup_adb(addrs);
Dan Albert53a37442015-05-08 16:13:53 -0700297 }
298
Josh Gaob2bcd752020-04-20 16:45:55 -0700299 LOG(INFO) << "adbd started";
300
Yabin Cui815ad882015-09-02 17:44:28 -0700301 D("adbd_main(): pre init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700302 init_jdwp();
Yabin Cui815ad882015-09-02 17:44:28 -0700303 D("adbd_main(): post init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700304
Yabin Cui815ad882015-09-02 17:44:28 -0700305 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700306 fdevent_loop();
307
308 return 0;
309}
310
Dan Albert53a37442015-05-08 16:13:53 -0700311int main(int argc, char** argv) {
Josh Gao0560feb2019-01-22 19:36:15 -0800312#if defined(__BIONIC__)
Josh Gao358aca22018-04-04 17:53:54 -0700313 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
314 mallopt(M_DECAY_TIME, 1);
Josh Gao0560feb2019-01-22 19:36:15 -0800315#endif
Josh Gao358aca22018-04-04 17:53:54 -0700316
Dan Albert53a37442015-05-08 16:13:53 -0700317 while (true) {
318 static struct option opts[] = {
Joshua Duong290ccb52019-11-20 14:18:43 -0800319 {"root_seclabel", required_argument, nullptr, 's'},
320 {"device_banner", required_argument, nullptr, 'b'},
321 {"version", no_argument, nullptr, 'v'},
322 {"logpostfsdata", no_argument, nullptr, 'l'},
Arnaud Ferraris9edb6ce2022-10-05 18:28:49 +0200323 {nullptr, no_argument, nullptr, 0},
Dan Albert53a37442015-05-08 16:13:53 -0700324 };
325
326 int option_index = 0;
327 int c = getopt_long(argc, argv, "", opts, &option_index);
328 if (c == -1) {
329 break;
330 }
331
332 switch (c) {
Josh Gao0560feb2019-01-22 19:36:15 -0800333#if defined(__ANDROID__)
334 case 's':
335 root_seclabel = optarg;
336 break;
337#endif
338 case 'b':
339 adb_device_banner = optarg;
340 break;
341 case 'v':
342 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
343 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
344 return 0;
Joshua Duong290ccb52019-11-20 14:18:43 -0800345 case 'l':
346 LOG(ERROR) << "post-fs-data triggered";
347 return 0;
Josh Gao0560feb2019-01-22 19:36:15 -0800348 default:
349 // getopt already prints "adbd: invalid option -- %c" for us.
350 return 1;
Dan Albert53a37442015-05-08 16:13:53 -0700351 }
352 }
353
354 close_stdin();
355
Dan Albert08d552b2015-05-21 13:58:50 -0700356 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700357
Yabin Cui815ad882015-09-02 17:44:28 -0700358 D("Handling main()");
Dan Albert53a37442015-05-08 16:13:53 -0700359 return adbd_main(DEFAULT_ADB_PORT);
360}