blob: d97e8f9de1b8857002b0857cbe24e718c9846f32 [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
Josh Gao0560feb2019-01-22 19:36:15 -080021#if defined(__BIONIC__)
Josh Gao41da1002018-07-25 18:13:44 -070022#include <android/fdsan.h>
Josh Gao0560feb2019-01-22 19:36:15 -080023#endif
24
Dan Albert53a37442015-05-08 16:13:53 -070025#include <errno.h>
Josh Gao358aca22018-04-04 17:53:54 -070026#include <getopt.h>
27#include <malloc.h>
Dan Albert53a37442015-05-08 16:13:53 -070028#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
Luis Hector Chaveze4528e12018-04-04 15:59:59 -070031#include <sys/capability.h>
Dan Albert53a37442015-05-08 16:13:53 -070032#include <sys/prctl.h>
33
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080034#include <memory>
Jason Jeremy Iman84613872019-07-19 12:44:39 +090035#include <vector>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080036
Elliott Hughesf55ead92015-12-04 22:00:26 -080037#include <android-base/logging.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040038#include <android-base/macros.h>
Elliott Hughes8b249d22016-09-23 15:40:03 -070039#include <android-base/properties.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080040#include <android-base/stringprintf.h>
Jason Jeremy Iman84613872019-07-19 12:44:39 +090041#include <android-base/strings.h>
Josh Gao0560feb2019-01-22 19:36:15 -080042
43#if defined(__ANDROID__)
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080044#include <libminijail.h>
Steven Morelandb087d302017-04-13 23:48:57 -070045#include <log/log_properties.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040046#include <scoped_minijail.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080047
Mark Salyzync75f65f2016-03-28 15:52:13 -070048#include <private/android_filesystem_config.h>
Tom Cherry46845222015-12-11 14:28:09 -080049#include "selinux/android.h"
Josh Gao0560feb2019-01-22 19:36:15 -080050#endif
Dan Albert53a37442015-05-08 16:13:53 -070051
52#include "adb.h"
53#include "adb_auth.h"
54#include "adb_listeners.h"
Josh Gao1eef4782015-11-20 15:37:31 -080055#include "adb_utils.h"
Joshua Duong290ccb52019-11-20 14:18:43 -080056#include "adb_wifi.h"
Jason Jeremy Iman84613872019-07-19 12:44:39 +090057#include "socket_spec.h"
Dan Albert53a37442015-05-08 16:13:53 -070058#include "transport.h"
Dan Albert53a37442015-05-08 16:13:53 -070059
Josh Gao2dc61962021-02-03 22:11:45 -080060#include "daemon/mdns.h"
61#include "daemon/watchdog.h"
Casey Dahlin10ad15f2016-05-06 16:19:13 -070062
Josh Gao0560feb2019-01-22 19:36:15 -080063#if defined(__ANDROID__)
Dan Albert53a37442015-05-08 16:13:53 -070064static const char* root_seclabel = nullptr;
65
Dan Albert53a37442015-05-08 16:13:53 -070066static bool should_drop_privileges() {
Dan Albert53a37442015-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.
Elliott Hughes8b249d22016-09-23 15:40:03 -070077 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzync75f65f2016-03-28 15:52:13 -070078 bool ro_debuggable = __android_log_is_debuggable();
Dan Albert53a37442015-05-08 16:13:53 -070079
80 // Drop privileges if ro.secure is set...
81 bool drop = ro_secure;
82
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080083 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughes8b249d22016-09-23 15:40:03 -070084 std::string prop = android::base::GetProperty("service.adb.root", "");
85 bool adb_root = (prop == "1");
86 bool adb_unroot = (prop == "0");
Dan Albert53a37442015-05-08 16:13:53 -070087 if (ro_debuggable && adb_root) {
88 drop = false;
89 }
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080090 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albert53a37442015-05-08 16:13:53 -070091 if (adb_unroot) {
92 drop = true;
93 }
94
95 return drop;
Dan Albert53a37442015-05-08 16:13:53 -070096}
97
Mike Frysinger2e114072015-12-08 18:57:47 -050098static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040099 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800100
Dan Albert53a37442015-05-08 16:13:53 -0700101 // Add extra groups:
102 // AID_ADB to access the USB driver
103 // AID_LOG to read system logs (adb logcat)
104 // AID_INPUT to diagnose input issues (getevent)
105 // AID_INET to diagnose network issues (ping)
106 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
107 // AID_SDCARD_R to allow reading from the SD card
108 // AID_SDCARD_RW to allow writing to the SD card
109 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevich8a1b4b32015-11-07 16:52:17 -0800110 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou209c27b2017-05-08 15:50:55 -0700111 // AID_UHID for using 'hid' command to read/write to /dev/uhid
Martijn Coenen223bcc72020-08-18 15:34:15 +0200112 // AID_EXT_DATA_RW for writing to /sdcard/Android/data (devices without sdcardfs)
113 // AID_EXT_OBB_RW for writing to /sdcard/Android/obb (devices without sdcardfs)
Siarhei Vishniakou209c27b2017-05-08 15:50:55 -0700114 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
115 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
Martijn Coenen223bcc72020-08-18 15:34:15 +0200116 AID_NET_BW_STATS, AID_READPROC, AID_UHID, AID_EXT_DATA_RW,
117 AID_EXT_OBB_RW};
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -0400118 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albert53a37442015-05-08 16:13:53 -0700119
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800120 // Don't listen on a port (default 5037) if running in secure mode.
121 // Don't run as root if running in secure mode.
Dan Albert53a37442015-05-08 16:13:53 -0700122 if (should_drop_privileges()) {
Josh Gaoc5ca6382020-06-04 17:58:48 -0700123 const bool should_drop_caps = !__android_log_is_debuggable();
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700124
125 if (should_drop_caps) {
126 minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
127 }
Dan Albert53a37442015-05-08 16:13:53 -0700128
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800129 minijail_change_gid(jail.get(), AID_SHELL);
130 minijail_change_uid(jail.get(), AID_SHELL);
131 // minijail_enter() will abort if any priv-dropping step fails.
132 minijail_enter(jail.get());
Dan Albert53a37442015-05-08 16:13:53 -0700133
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700134 // Whenever ambient capabilities are being used, minijail cannot
135 // simultaneously drop the bounding capability set to just
136 // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
137 // and permitted sets. So we need to do that in two steps.
138 using ScopedCaps =
139 std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
140 ScopedCaps caps(cap_get_proc(), &cap_free);
141 if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
142 PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
143 }
144 if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
145 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
146 }
147 if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
148 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
149 }
150 if (cap_set_proc(caps.get()) != 0) {
151 PLOG(FATAL) << "cap_set_proc() failed";
152 }
153
Yabin Cui815ad882015-09-02 17:44:28 -0700154 D("Local port disabled");
Dan Albert53a37442015-05-08 16:13:53 -0700155 } else {
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800156 // minijail_enter() will abort if any priv-dropping step fails.
157 minijail_enter(jail.get());
158
Nick Kralevich11f73a12015-06-12 22:03:50 -0700159 if (root_seclabel != nullptr) {
Tom Cherry46845222015-12-11 14:28:09 -0800160 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800161 LOG(FATAL) << "Could not set SELinux context";
Dan Albert53a37442015-05-08 16:13:53 -0700162 }
163 }
Dan Albert53a37442015-05-08 16:13:53 -0700164 }
Mike Frysinger2e114072015-12-08 18:57:47 -0500165}
Josh Gao0560feb2019-01-22 19:36:15 -0800166#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500167
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900168static void setup_adb(const std::vector<std::string>& addrs) {
Josh Gao0560feb2019-01-22 19:36:15 -0800169#if defined(__ANDROID__)
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900170 // Get the first valid port from addrs and setup mDNS.
171 int port = -1;
172 std::string error;
173 for (const auto& addr : addrs) {
174 port = get_host_socket_spec_port(addr, &error);
175 if (port != -1) {
176 break;
177 }
178 }
179 if (port == -1) {
180 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
181 }
Joshua Duong290ccb52019-11-20 14:18:43 -0800182 LOG(INFO) << "Setup mdns on port= " << port;
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700183 setup_mdns(port);
Josh Gao0560feb2019-01-22 19:36:15 -0800184#endif
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900185 for (const auto& addr : addrs) {
186 LOG(INFO) << "adbd listening on " << addr;
187 local_init(addr);
188 }
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700189}
190
Mike Frysinger2e114072015-12-08 18:57:47 -0500191int adbd_main(int server_port) {
192 umask(0);
193
194 signal(SIGPIPE, SIG_IGN);
195
Josh Gao0560feb2019-01-22 19:36:15 -0800196#if defined(__BIONIC__)
Josh Gao41da1002018-07-25 18:13:44 -0700197 auto fdsan_level = android_fdsan_get_error_level();
198 if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
199 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
200 }
Josh Gao0560feb2019-01-22 19:36:15 -0800201#endif
Josh Gao41da1002018-07-25 18:13:44 -0700202
Mike Frysinger2e114072015-12-08 18:57:47 -0500203 init_transport_registration();
204
205 // We need to call this even if auth isn't enabled because the file
206 // descriptor will always be open.
207 adbd_cloexec_auth_socket();
208
Josh Gaoc5ca6382020-06-04 17:58:48 -0700209#if defined(__ANDROID__)
Shaju Mathew390e67a2021-10-15 12:01:49 -0700210 bool device_unlocked = android::base::GetProperty("ro.boot.verifiedbootstate", "") == "orange";
211 if (device_unlocked || __android_log_is_debuggable()) {
212#if defined(__ANDROID_RECOVERY__)
213 auth_required = false; // Bypass authorization when the device transitions to
214 // fastbootd (from recovery). A corrupt userdata image can potentially
215 // result in the device falling into rescue, and a subsequent fastboot
216 // state should not require authorization - otherwise, it will force the
217 // need for manual intervention(b/188703874).
218#else
219 // If we're on userdebug/eng or the device is unlocked, permit no-authentication.
Bowgo Tsai35b817b2019-03-12 04:25:33 +0800220 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Shaju Mathew390e67a2021-10-15 12:01:49 -0700221#endif
Bowgo Tsai35b817b2019-03-12 04:25:33 +0800222 }
Josh Gao361148b2018-01-02 12:01:43 -0800223#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500224
Mike Frysinger2e114072015-12-08 18:57:47 -0500225 // Our external storage path may be different than apps, since
226 // we aren't able to bind mount after dropping root.
227 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
228 if (adb_external_storage != nullptr) {
229 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
230 } else {
231 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
232 " unchanged.\n");
233 }
234
Josh Gao0560feb2019-01-22 19:36:15 -0800235#if defined(__ANDROID__)
Mike Frysinger2e114072015-12-08 18:57:47 -0500236 drop_privileges(server_port);
Josh Gao0560feb2019-01-22 19:36:15 -0800237#endif
Dan Albert53a37442015-05-08 16:13:53 -0700238
Josh Gao2dc61962021-02-03 22:11:45 -0800239#if defined(__ANDROID__)
240 // A thread gets spawned as a side-effect of initializing the watchdog, so it needs to happen
241 // after we drop privileges.
242 watchdog::Initialize();
243#endif
244
Josh Gao7cac88a2019-10-22 12:30:39 -0700245 // adbd_auth_init will spawn a thread, so we need to defer it until after selinux transitions.
246 adbd_auth_init();
247
Dan Albert53a37442015-05-08 16:13:53 -0700248 bool is_usb = false;
Josh Gao0560feb2019-01-22 19:36:15 -0800249
250#if defined(__ANDROID__)
Josh Gao0dde9c82017-01-11 14:39:19 -0800251 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albert53a37442015-05-08 16:13:53 -0700252 // Listen on USB.
253 usb_init();
254 is_usb = true;
255 }
Josh Gao0560feb2019-01-22 19:36:15 -0800256#endif
Dan Albert53a37442015-05-08 16:13:53 -0700257
258 // If one of these properties is set, also listen on that port.
259 // If one of the properties isn't set and we couldn't listen on usb, listen
260 // on the default port.
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900261 std::vector<std::string> addrs;
262 std::string prop_addr = android::base::GetProperty("service.adb.listen_addrs", "");
263 if (prop_addr.empty()) {
264 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
265 if (prop_port.empty()) {
266 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
267 }
Dan Albert53a37442015-05-08 16:13:53 -0700268
Josh Gao79d122a2019-09-17 17:34:41 +0800269#if !defined(__ANDROID__)
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900270 if (prop_port.empty() && getenv("ADBD_PORT")) {
271 prop_port = getenv("ADBD_PORT");
272 }
Josh Gao79d122a2019-09-17 17:34:41 +0800273#endif
274
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900275 int port;
276 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
277 D("using tcp port=%d", port);
278 // Listen on TCP and VSOCK port specified by service.adb.tcp.port property.
279 addrs.push_back(android::base::StringPrintf("tcp:%d", port));
280 addrs.push_back(android::base::StringPrintf("vsock:%d", port));
281 setup_adb(addrs);
282 } else if (!is_usb) {
283 // Listen on default port.
284 addrs.push_back(
285 android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
286 addrs.push_back(
287 android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
288 setup_adb(addrs);
289 }
290 } else {
291 addrs = android::base::Split(prop_addr, ",");
292 setup_adb(addrs);
Dan Albert53a37442015-05-08 16:13:53 -0700293 }
294
Josh Gaob2bcd752020-04-20 16:45:55 -0700295 LOG(INFO) << "adbd started";
296
Yabin Cui815ad882015-09-02 17:44:28 -0700297 D("adbd_main(): pre init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700298 init_jdwp();
Yabin Cui815ad882015-09-02 17:44:28 -0700299 D("adbd_main(): post init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700300
Yabin Cui815ad882015-09-02 17:44:28 -0700301 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700302 fdevent_loop();
303
304 return 0;
305}
306
Dan Albert53a37442015-05-08 16:13:53 -0700307int main(int argc, char** argv) {
Josh Gao0560feb2019-01-22 19:36:15 -0800308#if defined(__BIONIC__)
Josh Gao358aca22018-04-04 17:53:54 -0700309 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
310 mallopt(M_DECAY_TIME, 1);
Josh Gao0560feb2019-01-22 19:36:15 -0800311#endif
Josh Gao358aca22018-04-04 17:53:54 -0700312
Dan Albert53a37442015-05-08 16:13:53 -0700313 while (true) {
314 static struct option opts[] = {
Joshua Duong290ccb52019-11-20 14:18:43 -0800315 {"root_seclabel", required_argument, nullptr, 's'},
316 {"device_banner", required_argument, nullptr, 'b'},
317 {"version", no_argument, nullptr, 'v'},
318 {"logpostfsdata", no_argument, nullptr, 'l'},
Dan Albert53a37442015-05-08 16:13:53 -0700319 };
320
321 int option_index = 0;
322 int c = getopt_long(argc, argv, "", opts, &option_index);
323 if (c == -1) {
324 break;
325 }
326
327 switch (c) {
Josh Gao0560feb2019-01-22 19:36:15 -0800328#if defined(__ANDROID__)
329 case 's':
330 root_seclabel = optarg;
331 break;
332#endif
333 case 'b':
334 adb_device_banner = optarg;
335 break;
336 case 'v':
337 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
338 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
339 return 0;
Joshua Duong290ccb52019-11-20 14:18:43 -0800340 case 'l':
341 LOG(ERROR) << "post-fs-data triggered";
342 return 0;
Josh Gao0560feb2019-01-22 19:36:15 -0800343 default:
344 // getopt already prints "adbd: invalid option -- %c" for us.
345 return 1;
Dan Albert53a37442015-05-08 16:13:53 -0700346 }
347 }
348
349 close_stdin();
350
Dan Albert08d552b2015-05-21 13:58:50 -0700351 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700352
Yabin Cui815ad882015-09-02 17:44:28 -0700353 D("Handling main()");
Dan Albert53a37442015-05-08 16:13:53 -0700354 return adbd_main(DEFAULT_ADB_PORT);
355}