blob: 658e24456778d20ea2be66251711cdfe1265f437 [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
Dan Albertc89e0cc2015-05-08 16:13:53 -070065static bool should_drop_privileges() {
Dan Albertc89e0cc2015-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 Hughesffdec182016-09-23 15:40:03 -070076 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzyn97787a02016-03-28 15:52:13 -070077 bool ro_debuggable = __android_log_is_debuggable();
Dan Albertc89e0cc2015-05-08 16:13:53 -070078
79 // Drop privileges if ro.secure is set...
80 bool drop = ro_secure;
81
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080082 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughesffdec182016-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 Albertc89e0cc2015-05-08 16:13:53 -070086 if (ro_debuggable && adb_root) {
87 drop = false;
88 }
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080089 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -070090 if (adb_unroot) {
91 drop = true;
92 }
93
94 return drop;
Dan Albertc89e0cc2015-05-08 16:13:53 -070095}
96
Mike Frysinger4120ebc2015-12-08 18:57:47 -050097static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040098 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080099
Dan Albertc89e0cc2015-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 Kralevichc39ba5a2015-11-07 16:52:17 -0800109 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou0729dd12017-05-08 15:50:55 -0700110 // AID_UHID for using 'hid' command to read/write to /dev/uhid
111 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
112 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
113 AID_NET_BW_STATS, AID_READPROC, AID_UHID};
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400114 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700115
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800116 // Don't listen on a port (default 5037) if running in secure mode.
117 // Don't run as root if running in secure mode.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700118 if (should_drop_privileges()) {
Josh Gaob6b70c22020-06-04 17:58:48 -0700119 const bool should_drop_caps = !__android_log_is_debuggable();
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700120
121 if (should_drop_caps) {
122 minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
123 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700124
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800125 minijail_change_gid(jail.get(), AID_SHELL);
126 minijail_change_uid(jail.get(), AID_SHELL);
127 // minijail_enter() will abort if any priv-dropping step fails.
128 minijail_enter(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700129
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700130 // Whenever ambient capabilities are being used, minijail cannot
131 // simultaneously drop the bounding capability set to just
132 // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
133 // and permitted sets. So we need to do that in two steps.
134 using ScopedCaps =
135 std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
136 ScopedCaps caps(cap_get_proc(), &cap_free);
137 if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
138 PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
139 }
140 if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
141 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
142 }
143 if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
144 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
145 }
146 if (cap_set_proc(caps.get()) != 0) {
147 PLOG(FATAL) << "cap_set_proc() failed";
148 }
149
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700150 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700151 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800152 // minijail_enter() will abort if any priv-dropping step fails.
153 minijail_enter(jail.get());
154
Nick Kralevich4d870952015-06-12 22:03:50 -0700155 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800156 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800157 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700158 }
159 }
Spencer Low5200c662015-07-30 23:07:55 -0700160 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700161 std::string local_name =
162 android::base::StringPrintf("tcp:%d", server_port);
David Purselleaae97e2016-04-07 11:25:48 -0700163 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
164 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700165 }
166 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500167}
Josh Gao776c2ec2019-01-22 19:36:15 -0800168#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500169
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900170static void setup_adb(const std::vector<std::string>& addrs) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800171#if defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900172 // Get the first valid port from addrs and setup mDNS.
173 int port = -1;
174 std::string error;
175 for (const auto& addr : addrs) {
176 port = get_host_socket_spec_port(addr, &error);
177 if (port != -1) {
178 break;
179 }
180 }
181 if (port == -1) {
182 port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
183 }
Joshua Duongd85f5c02019-11-20 14:18:43 -0800184 LOG(INFO) << "Setup mdns on port= " << port;
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700185 setup_mdns(port);
Josh Gao776c2ec2019-01-22 19:36:15 -0800186#endif
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900187 for (const auto& addr : addrs) {
188 LOG(INFO) << "adbd listening on " << addr;
189 local_init(addr);
190 }
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700191}
192
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500193int adbd_main(int server_port) {
194 umask(0);
195
196 signal(SIGPIPE, SIG_IGN);
197
Josh Gao776c2ec2019-01-22 19:36:15 -0800198#if defined(__BIONIC__)
Josh Gao954adcc2018-07-25 18:13:44 -0700199 auto fdsan_level = android_fdsan_get_error_level();
200 if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
201 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
202 }
Josh Gao776c2ec2019-01-22 19:36:15 -0800203#endif
Josh Gao954adcc2018-07-25 18:13:44 -0700204
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500205 init_transport_registration();
206
207 // We need to call this even if auth isn't enabled because the file
208 // descriptor will always be open.
209 adbd_cloexec_auth_socket();
210
Josh Gaob6b70c22020-06-04 17:58:48 -0700211#if defined(__ANDROID__)
212 // If we're on userdebug/eng or the device is unlocked, permit no-authentication.
213 bool device_unlocked = "orange" == android::base::GetProperty("ro.boot.verifiedbootstate", "");
214 if (__android_log_is_debuggable() || device_unlocked) {
Bowgo Tsai9b30c0a2019-03-12 04:25:33 +0800215 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
216 }
Josh Gao27768452018-01-02 12:01:43 -0800217#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500218
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500219 // Our external storage path may be different than apps, since
220 // we aren't able to bind mount after dropping root.
221 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
222 if (adb_external_storage != nullptr) {
223 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
224 } else {
225 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
226 " unchanged.\n");
227 }
228
Josh Gao776c2ec2019-01-22 19:36:15 -0800229#if defined(__ANDROID__)
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500230 drop_privileges(server_port);
Josh Gao776c2ec2019-01-22 19:36:15 -0800231#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700232
Josh Gao27523262019-10-22 12:30:39 -0700233 // adbd_auth_init will spawn a thread, so we need to defer it until after selinux transitions.
234 adbd_auth_init();
235
Dan Albertc89e0cc2015-05-08 16:13:53 -0700236 bool is_usb = false;
Josh Gao776c2ec2019-01-22 19:36:15 -0800237
238#if defined(__ANDROID__)
Josh Gao183b73e2017-01-11 14:39:19 -0800239 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700240 // Listen on USB.
241 usb_init();
242 is_usb = true;
243 }
Josh Gao776c2ec2019-01-22 19:36:15 -0800244#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700245
246 // If one of these properties is set, also listen on that port.
247 // If one of the properties isn't set and we couldn't listen on usb, listen
248 // on the default port.
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900249 std::vector<std::string> addrs;
250 std::string prop_addr = android::base::GetProperty("service.adb.listen_addrs", "");
251 if (prop_addr.empty()) {
252 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
253 if (prop_port.empty()) {
254 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
255 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700256
Josh Gao06e7cb52019-09-17 17:34:41 +0800257#if !defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900258 if (prop_port.empty() && getenv("ADBD_PORT")) {
259 prop_port = getenv("ADBD_PORT");
260 }
Josh Gao06e7cb52019-09-17 17:34:41 +0800261#endif
262
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900263 int port;
264 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
265 D("using tcp port=%d", port);
266 // Listen on TCP and VSOCK port specified by service.adb.tcp.port property.
267 addrs.push_back(android::base::StringPrintf("tcp:%d", port));
268 addrs.push_back(android::base::StringPrintf("vsock:%d", port));
269 setup_adb(addrs);
270 } else if (!is_usb) {
271 // Listen on default port.
272 addrs.push_back(
273 android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
274 addrs.push_back(
275 android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
276 setup_adb(addrs);
277 }
278 } else {
279 addrs = android::base::Split(prop_addr, ",");
280 setup_adb(addrs);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700281 }
282
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700283 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700284 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700285 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700286
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700287 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700288 fdevent_loop();
289
290 return 0;
291}
292
Dan Albertc89e0cc2015-05-08 16:13:53 -0700293int main(int argc, char** argv) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800294#if defined(__BIONIC__)
Josh Gao218f7662018-04-04 17:53:54 -0700295 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
296 mallopt(M_DECAY_TIME, 1);
Josh Gao776c2ec2019-01-22 19:36:15 -0800297#endif
Josh Gao218f7662018-04-04 17:53:54 -0700298
Dan Albertc89e0cc2015-05-08 16:13:53 -0700299 while (true) {
300 static struct option opts[] = {
Joshua Duongd85f5c02019-11-20 14:18:43 -0800301 {"root_seclabel", required_argument, nullptr, 's'},
302 {"device_banner", required_argument, nullptr, 'b'},
303 {"version", no_argument, nullptr, 'v'},
304 {"logpostfsdata", no_argument, nullptr, 'l'},
Dan Albertc89e0cc2015-05-08 16:13:53 -0700305 };
306
307 int option_index = 0;
308 int c = getopt_long(argc, argv, "", opts, &option_index);
309 if (c == -1) {
310 break;
311 }
312
313 switch (c) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800314#if defined(__ANDROID__)
315 case 's':
316 root_seclabel = optarg;
317 break;
318#endif
319 case 'b':
320 adb_device_banner = optarg;
321 break;
322 case 'v':
323 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
324 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
325 return 0;
Joshua Duongd85f5c02019-11-20 14:18:43 -0800326 case 'l':
327 LOG(ERROR) << "post-fs-data triggered";
328 return 0;
Josh Gao776c2ec2019-01-22 19:36:15 -0800329 default:
330 // getopt already prints "adbd: invalid option -- %c" for us.
331 return 1;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700332 }
333 }
334
335 close_stdin();
336
Dan Albert9313c0d2015-05-21 13:58:50 -0700337 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700338
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700339 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700340 return adbd_main(DEFAULT_ADB_PORT);
341}