blob: 8c41c5e2d3806b4078ca2620d17fc61dd757a099 [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
Casey Dahlin10ad15f2016-05-06 16:19:13 -070060#include "mdns.h"
61
Josh Gao0560feb2019-01-22 19:36:15 -080062#if defined(__ANDROID__)
Dan Albert53a37442015-05-08 16:13:53 -070063static const char* root_seclabel = nullptr;
64
Dan Albert53a37442015-05-08 16:13:53 -070065static bool should_drop_privileges() {
Dan Albert53a37442015-05-08 16:13:53 -070066 // The properties that affect `adb root` and `adb unroot` are ro.secure and
67 // ro.debuggable. In this context the names don't make the expected behavior
68 // particularly obvious.
69 //
70 // ro.debuggable:
71 // Allowed to become root, but not necessarily the default. Set to 1 on
72 // eng and userdebug builds.
73 //
74 // ro.secure:
75 // Drop privileges by default. Set to 1 on userdebug and user builds.
Elliott Hughes8b249d22016-09-23 15:40:03 -070076 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzync75f65f2016-03-28 15:52:13 -070077 bool ro_debuggable = __android_log_is_debuggable();
Dan Albert53a37442015-05-08 16:13:53 -070078
79 // Drop privileges if ro.secure is set...
80 bool drop = ro_secure;
81
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080082 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughes8b249d22016-09-23 15:40:03 -070083 std::string prop = android::base::GetProperty("service.adb.root", "");
84 bool adb_root = (prop == "1");
85 bool adb_unroot = (prop == "0");
Dan Albert53a37442015-05-08 16:13:53 -070086 if (ro_debuggable && adb_root) {
87 drop = false;
88 }
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080089 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albert53a37442015-05-08 16:13:53 -070090 if (adb_unroot) {
91 drop = true;
92 }
93
94 return drop;
Dan Albert53a37442015-05-08 16:13:53 -070095}
96
Mike Frysinger2e114072015-12-08 18:57:47 -050097static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040098 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080099
Dan Albert53a37442015-05-08 16:13:53 -0700100 // Add extra groups:
101 // AID_ADB to access the USB driver
102 // AID_LOG to read system logs (adb logcat)
103 // AID_INPUT to diagnose input issues (getevent)
104 // AID_INET to diagnose network issues (ping)
105 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
106 // AID_SDCARD_R to allow reading from the SD card
107 // AID_SDCARD_RW to allow writing to the SD card
108 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevich8a1b4b32015-11-07 16:52:17 -0800109 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou209c27b2017-05-08 15:50:55 -0700110 // AID_UHID for using 'hid' command to read/write to /dev/uhid
Martijn Coenen223bcc72020-08-18 15:34:15 +0200111 // AID_EXT_DATA_RW for writing to /sdcard/Android/data (devices without sdcardfs)
112 // AID_EXT_OBB_RW for writing to /sdcard/Android/obb (devices without sdcardfs)
Siarhei Vishniakou209c27b2017-05-08 15:50:55 -0700113 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
114 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
Martijn Coenen223bcc72020-08-18 15:34:15 +0200115 AID_NET_BW_STATS, AID_READPROC, AID_UHID, AID_EXT_DATA_RW,
116 AID_EXT_OBB_RW};
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -0400117 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albert53a37442015-05-08 16:13:53 -0700118
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800119 // Don't listen on a port (default 5037) if running in secure mode.
120 // Don't run as root if running in secure mode.
Dan Albert53a37442015-05-08 16:13:53 -0700121 if (should_drop_privileges()) {
Josh Gaoc5ca6382020-06-04 17:58:48 -0700122 const bool should_drop_caps = !__android_log_is_debuggable();
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700123
124 if (should_drop_caps) {
125 minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
126 }
Dan Albert53a37442015-05-08 16:13:53 -0700127
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800128 minijail_change_gid(jail.get(), AID_SHELL);
129 minijail_change_uid(jail.get(), AID_SHELL);
130 // minijail_enter() will abort if any priv-dropping step fails.
131 minijail_enter(jail.get());
Dan Albert53a37442015-05-08 16:13:53 -0700132
Luis Hector Chaveze4528e12018-04-04 15:59:59 -0700133 // Whenever ambient capabilities are being used, minijail cannot
134 // simultaneously drop the bounding capability set to just
135 // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
136 // and permitted sets. So we need to do that in two steps.
137 using ScopedCaps =
138 std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
139 ScopedCaps caps(cap_get_proc(), &cap_free);
140 if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
141 PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
142 }
143 if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
144 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
145 }
146 if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
147 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
148 }
149 if (cap_set_proc(caps.get()) != 0) {
150 PLOG(FATAL) << "cap_set_proc() failed";
151 }
152
Yabin Cui815ad882015-09-02 17:44:28 -0700153 D("Local port disabled");
Dan Albert53a37442015-05-08 16:13:53 -0700154 } else {
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800155 // minijail_enter() will abort if any priv-dropping step fails.
156 minijail_enter(jail.get());
157
Nick Kralevich11f73a12015-06-12 22:03:50 -0700158 if (root_seclabel != nullptr) {
Tom Cherry46845222015-12-11 14:28:09 -0800159 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800160 LOG(FATAL) << "Could not set SELinux context";
Dan Albert53a37442015-05-08 16:13:53 -0700161 }
162 }
Dan Albert53a37442015-05-08 16:13:53 -0700163 }
Mike Frysinger2e114072015-12-08 18:57:47 -0500164}
Josh Gao0560feb2019-01-22 19:36:15 -0800165#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500166
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900167static void setup_adb(const std::vector<std::string>& addrs) {
Josh Gao0560feb2019-01-22 19:36:15 -0800168#if defined(__ANDROID__)
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900169 // Get the first valid port from addrs and setup mDNS.
170 int port = -1;
171 std::string error;
172 for (const auto& addr : addrs) {
173 port = get_host_socket_spec_port(addr, &error);
174 if (port != -1) {
175 break;
176 }
177 }
178 if (port == -1) {
179 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
180 }
Joshua Duong290ccb52019-11-20 14:18:43 -0800181 LOG(INFO) << "Setup mdns on port= " << port;
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700182 setup_mdns(port);
Josh Gao0560feb2019-01-22 19:36:15 -0800183#endif
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900184 for (const auto& addr : addrs) {
185 LOG(INFO) << "adbd listening on " << addr;
186 local_init(addr);
187 }
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700188}
189
Mike Frysinger2e114072015-12-08 18:57:47 -0500190int adbd_main(int server_port) {
191 umask(0);
192
193 signal(SIGPIPE, SIG_IGN);
194
Josh Gao0560feb2019-01-22 19:36:15 -0800195#if defined(__BIONIC__)
Josh Gao41da1002018-07-25 18:13:44 -0700196 auto fdsan_level = android_fdsan_get_error_level();
197 if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
198 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
199 }
Josh Gao0560feb2019-01-22 19:36:15 -0800200#endif
Josh Gao41da1002018-07-25 18:13:44 -0700201
Mike Frysinger2e114072015-12-08 18:57:47 -0500202 init_transport_registration();
203
204 // We need to call this even if auth isn't enabled because the file
205 // descriptor will always be open.
206 adbd_cloexec_auth_socket();
207
Josh Gaoc5ca6382020-06-04 17:58:48 -0700208#if defined(__ANDROID__)
209 // If we're on userdebug/eng or the device is unlocked, permit no-authentication.
210 bool device_unlocked = "orange" == android::base::GetProperty("ro.boot.verifiedbootstate", "");
211 if (__android_log_is_debuggable() || device_unlocked) {
Bowgo Tsai35b817b2019-03-12 04:25:33 +0800212 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
213 }
Josh Gao361148b2018-01-02 12:01:43 -0800214#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500215
Mike Frysinger2e114072015-12-08 18:57:47 -0500216 // Our external storage path may be different than apps, since
217 // we aren't able to bind mount after dropping root.
218 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
219 if (adb_external_storage != nullptr) {
220 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
221 } else {
222 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
223 " unchanged.\n");
224 }
225
Josh Gao0560feb2019-01-22 19:36:15 -0800226#if defined(__ANDROID__)
Mike Frysinger2e114072015-12-08 18:57:47 -0500227 drop_privileges(server_port);
Josh Gao0560feb2019-01-22 19:36:15 -0800228#endif
Dan Albert53a37442015-05-08 16:13:53 -0700229
Josh Gao7cac88a2019-10-22 12:30:39 -0700230 // adbd_auth_init will spawn a thread, so we need to defer it until after selinux transitions.
231 adbd_auth_init();
232
Dan Albert53a37442015-05-08 16:13:53 -0700233 bool is_usb = false;
Josh Gao0560feb2019-01-22 19:36:15 -0800234
235#if defined(__ANDROID__)
Josh Gao0dde9c82017-01-11 14:39:19 -0800236 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albert53a37442015-05-08 16:13:53 -0700237 // Listen on USB.
238 usb_init();
239 is_usb = true;
240 }
Josh Gao0560feb2019-01-22 19:36:15 -0800241#endif
Dan Albert53a37442015-05-08 16:13:53 -0700242
243 // If one of these properties is set, also listen on that port.
244 // If one of the properties isn't set and we couldn't listen on usb, listen
245 // on the default port.
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900246 std::vector<std::string> addrs;
247 std::string prop_addr = android::base::GetProperty("service.adb.listen_addrs", "");
248 if (prop_addr.empty()) {
249 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
250 if (prop_port.empty()) {
251 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
252 }
Dan Albert53a37442015-05-08 16:13:53 -0700253
Josh Gao79d122a2019-09-17 17:34:41 +0800254#if !defined(__ANDROID__)
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900255 if (prop_port.empty() && getenv("ADBD_PORT")) {
256 prop_port = getenv("ADBD_PORT");
257 }
Josh Gao79d122a2019-09-17 17:34:41 +0800258#endif
259
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900260 int port;
261 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
262 D("using tcp port=%d", port);
263 // Listen on TCP and VSOCK port specified by service.adb.tcp.port property.
264 addrs.push_back(android::base::StringPrintf("tcp:%d", port));
265 addrs.push_back(android::base::StringPrintf("vsock:%d", port));
266 setup_adb(addrs);
267 } else if (!is_usb) {
268 // Listen on default port.
269 addrs.push_back(
270 android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
271 addrs.push_back(
272 android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
273 setup_adb(addrs);
274 }
275 } else {
276 addrs = android::base::Split(prop_addr, ",");
277 setup_adb(addrs);
Dan Albert53a37442015-05-08 16:13:53 -0700278 }
279
Josh Gaob2bcd752020-04-20 16:45:55 -0700280 LOG(INFO) << "adbd started";
281
Yabin Cui815ad882015-09-02 17:44:28 -0700282 D("adbd_main(): pre init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700283 init_jdwp();
Yabin Cui815ad882015-09-02 17:44:28 -0700284 D("adbd_main(): post init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700285
Yabin Cui815ad882015-09-02 17:44:28 -0700286 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700287 fdevent_loop();
288
289 return 0;
290}
291
Dan Albert53a37442015-05-08 16:13:53 -0700292int main(int argc, char** argv) {
Josh Gao0560feb2019-01-22 19:36:15 -0800293#if defined(__BIONIC__)
Josh Gao358aca22018-04-04 17:53:54 -0700294 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
295 mallopt(M_DECAY_TIME, 1);
Josh Gao0560feb2019-01-22 19:36:15 -0800296#endif
Josh Gao358aca22018-04-04 17:53:54 -0700297
Dan Albert53a37442015-05-08 16:13:53 -0700298 while (true) {
299 static struct option opts[] = {
Joshua Duong290ccb52019-11-20 14:18:43 -0800300 {"root_seclabel", required_argument, nullptr, 's'},
301 {"device_banner", required_argument, nullptr, 'b'},
302 {"version", no_argument, nullptr, 'v'},
303 {"logpostfsdata", no_argument, nullptr, 'l'},
Dan Albert53a37442015-05-08 16:13:53 -0700304 };
305
306 int option_index = 0;
307 int c = getopt_long(argc, argv, "", opts, &option_index);
308 if (c == -1) {
309 break;
310 }
311
312 switch (c) {
Josh Gao0560feb2019-01-22 19:36:15 -0800313#if defined(__ANDROID__)
314 case 's':
315 root_seclabel = optarg;
316 break;
317#endif
318 case 'b':
319 adb_device_banner = optarg;
320 break;
321 case 'v':
322 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
323 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
324 return 0;
Joshua Duong290ccb52019-11-20 14:18:43 -0800325 case 'l':
326 LOG(ERROR) << "post-fs-data triggered";
327 return 0;
Josh Gao0560feb2019-01-22 19:36:15 -0800328 default:
329 // getopt already prints "adbd: invalid option -- %c" for us.
330 return 1;
Dan Albert53a37442015-05-08 16:13:53 -0700331 }
332 }
333
334 close_stdin();
335
Dan Albert08d552b2015-05-21 13:58:50 -0700336 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700337
Yabin Cui815ad882015-09-02 17:44:28 -0700338 D("Handling main()");
Dan Albert53a37442015-05-08 16:13:53 -0700339 return adbd_main(DEFAULT_ADB_PORT);
340}