blob: fce3a4fb51e063f4262ea483a0b3fe49ebcd54b9 [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>
35
Elliott Hughes4f713192015-12-04 22:00:26 -080036#include <android-base/logging.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040037#include <android-base/macros.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070038#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080039#include <android-base/stringprintf.h>
Josh Gao776c2ec2019-01-22 19:36:15 -080040
41#if defined(__ANDROID__)
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080042#include <libminijail.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070043#include <log/log_properties.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040044#include <scoped_minijail.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080045
Mark Salyzyn97787a02016-03-28 15:52:13 -070046#include <private/android_filesystem_config.h>
Tom Cherry38cd57a2015-12-11 14:28:09 -080047#include "selinux/android.h"
Josh Gao776c2ec2019-01-22 19:36:15 -080048#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -070049
50#include "adb.h"
51#include "adb_auth.h"
52#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080053#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070054#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070055
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070056#include "mdns.h"
57
Josh Gao776c2ec2019-01-22 19:36:15 -080058#if defined(__ANDROID__)
Dan Albertc89e0cc2015-05-08 16:13:53 -070059static const char* root_seclabel = nullptr;
60
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070061static bool should_drop_capabilities_bounding_set() {
Bowgo Tsai0603ec42017-08-31 06:26:47 +000062#if defined(ALLOW_ADBD_ROOT)
63 if (__android_log_is_debuggable()) {
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070064 return false;
Dan Albertc89e0cc2015-05-08 16:13:53 -070065 }
Bowgo Tsai0603ec42017-08-31 06:26:47 +000066#endif
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070067 return true;
Dan Albertc89e0cc2015-05-08 16:13:53 -070068}
69
70static bool should_drop_privileges() {
Bowgo Tsai0603ec42017-08-31 06:26:47 +000071#if defined(ALLOW_ADBD_ROOT)
Dan Albertc89e0cc2015-05-08 16:13:53 -070072 // The properties that affect `adb root` and `adb unroot` are ro.secure and
73 // ro.debuggable. In this context the names don't make the expected behavior
74 // particularly obvious.
75 //
76 // ro.debuggable:
77 // Allowed to become root, but not necessarily the default. Set to 1 on
78 // eng and userdebug builds.
79 //
80 // ro.secure:
81 // Drop privileges by default. Set to 1 on userdebug and user builds.
Elliott Hughesffdec182016-09-23 15:40:03 -070082 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzyn97787a02016-03-28 15:52:13 -070083 bool ro_debuggable = __android_log_is_debuggable();
Dan Albertc89e0cc2015-05-08 16:13:53 -070084
85 // Drop privileges if ro.secure is set...
86 bool drop = ro_secure;
87
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080088 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughesffdec182016-09-23 15:40:03 -070089 std::string prop = android::base::GetProperty("service.adb.root", "");
90 bool adb_root = (prop == "1");
91 bool adb_unroot = (prop == "0");
Dan Albertc89e0cc2015-05-08 16:13:53 -070092 if (ro_debuggable && adb_root) {
93 drop = false;
94 }
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080095 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -070096 if (adb_unroot) {
97 drop = true;
98 }
99
100 return drop;
Bowgo Tsai0603ec42017-08-31 06:26:47 +0000101#else
102 return true; // "adb root" not allowed, always drop privileges.
103#endif // ALLOW_ADBD_ROOT
Dan Albertc89e0cc2015-05-08 16:13:53 -0700104}
105
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500106static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400107 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800108
Dan Albertc89e0cc2015-05-08 16:13:53 -0700109 // Add extra groups:
110 // AID_ADB to access the USB driver
111 // AID_LOG to read system logs (adb logcat)
112 // AID_INPUT to diagnose input issues (getevent)
113 // AID_INET to diagnose network issues (ping)
114 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
115 // AID_SDCARD_R to allow reading from the SD card
116 // AID_SDCARD_RW to allow writing to the SD card
117 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800118 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou0729dd12017-05-08 15:50:55 -0700119 // AID_UHID for using 'hid' command to read/write to /dev/uhid
120 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
121 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
122 AID_NET_BW_STATS, AID_READPROC, AID_UHID};
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400123 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700124
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800125 // Don't listen on a port (default 5037) if running in secure mode.
126 // Don't run as root if running in secure mode.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700127 if (should_drop_privileges()) {
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700128 const bool should_drop_caps = should_drop_capabilities_bounding_set();
129
130 if (should_drop_caps) {
131 minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
132 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700133
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800134 minijail_change_gid(jail.get(), AID_SHELL);
135 minijail_change_uid(jail.get(), AID_SHELL);
136 // minijail_enter() will abort if any priv-dropping step fails.
137 minijail_enter(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700138
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700139 // Whenever ambient capabilities are being used, minijail cannot
140 // simultaneously drop the bounding capability set to just
141 // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
142 // and permitted sets. So we need to do that in two steps.
143 using ScopedCaps =
144 std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
145 ScopedCaps caps(cap_get_proc(), &cap_free);
146 if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
147 PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
148 }
149 if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
150 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
151 }
152 if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
153 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
154 }
155 if (cap_set_proc(caps.get()) != 0) {
156 PLOG(FATAL) << "cap_set_proc() failed";
157 }
158
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700159 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700160 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800161 // minijail_enter() will abort if any priv-dropping step fails.
162 minijail_enter(jail.get());
163
Nick Kralevich4d870952015-06-12 22:03:50 -0700164 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800165 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800166 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700167 }
168 }
Spencer Low5200c662015-07-30 23:07:55 -0700169 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700170 std::string local_name =
171 android::base::StringPrintf("tcp:%d", server_port);
David Purselleaae97e2016-04-07 11:25:48 -0700172 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
173 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700174 }
175 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500176}
Josh Gao776c2ec2019-01-22 19:36:15 -0800177#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500178
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700179static void setup_port(int port) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800180 LOG(INFO) << "adbd listening on port " << port;
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700181 local_init(port);
Josh Gao776c2ec2019-01-22 19:36:15 -0800182#if defined(__ANDROID__)
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700183 setup_mdns(port);
Josh Gao776c2ec2019-01-22 19:36:15 -0800184#endif
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700185}
186
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500187int adbd_main(int server_port) {
188 umask(0);
189
190 signal(SIGPIPE, SIG_IGN);
191
Josh Gao776c2ec2019-01-22 19:36:15 -0800192#if defined(__BIONIC__)
Josh Gao954adcc2018-07-25 18:13:44 -0700193 auto fdsan_level = android_fdsan_get_error_level();
194 if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
195 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
196 }
Josh Gao776c2ec2019-01-22 19:36:15 -0800197#endif
Josh Gao954adcc2018-07-25 18:13:44 -0700198
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500199 init_transport_registration();
200
201 // We need to call this even if auth isn't enabled because the file
202 // descriptor will always be open.
203 adbd_cloexec_auth_socket();
204
Josh Gao27768452018-01-02 12:01:43 -0800205#if defined(ALLOW_ADBD_NO_AUTH)
Josh Gao6eda1842018-03-06 13:37:45 -0800206 // If ro.adb.secure is unset, default to no authentication required.
207 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Josh Gao27768452018-01-02 12:01:43 -0800208#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500209
210 adbd_auth_init();
211
212 // Our external storage path may be different than apps, since
213 // we aren't able to bind mount after dropping root.
214 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
215 if (adb_external_storage != nullptr) {
216 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
217 } else {
218 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
219 " unchanged.\n");
220 }
221
Josh Gao776c2ec2019-01-22 19:36:15 -0800222#if defined(__ANDROID__)
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500223 drop_privileges(server_port);
Josh Gao776c2ec2019-01-22 19:36:15 -0800224#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700225
226 bool is_usb = false;
Josh Gao776c2ec2019-01-22 19:36:15 -0800227
228#if defined(__ANDROID__)
Josh Gao183b73e2017-01-11 14:39:19 -0800229 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700230 // Listen on USB.
231 usb_init();
232 is_usb = true;
233 }
Josh Gao776c2ec2019-01-22 19:36:15 -0800234#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700235
236 // If one of these properties is set, also listen on that port.
237 // If one of the properties isn't set and we couldn't listen on usb, listen
238 // on the default port.
Elliott Hughesffdec182016-09-23 15:40:03 -0700239 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
240 if (prop_port.empty()) {
241 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700242 }
243
244 int port;
Elliott Hughesffdec182016-09-23 15:40:03 -0700245 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700246 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700247 // Listen on TCP port specified by service.adb.tcp.port property.
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700248 setup_port(port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700249 } else if (!is_usb) {
250 // Listen on default port.
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700251 setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700252 }
253
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700254 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700255 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700256 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700257
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700258 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700259 fdevent_loop();
260
261 return 0;
262}
263
Dan Albertc89e0cc2015-05-08 16:13:53 -0700264int main(int argc, char** argv) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800265#if defined(__BIONIC__)
Josh Gao218f7662018-04-04 17:53:54 -0700266 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
267 mallopt(M_DECAY_TIME, 1);
Josh Gao776c2ec2019-01-22 19:36:15 -0800268#endif
Josh Gao218f7662018-04-04 17:53:54 -0700269
Dan Albertc89e0cc2015-05-08 16:13:53 -0700270 while (true) {
271 static struct option opts[] = {
272 {"root_seclabel", required_argument, nullptr, 's'},
273 {"device_banner", required_argument, nullptr, 'b'},
274 {"version", no_argument, nullptr, 'v'},
275 };
276
277 int option_index = 0;
278 int c = getopt_long(argc, argv, "", opts, &option_index);
279 if (c == -1) {
280 break;
281 }
282
283 switch (c) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800284#if defined(__ANDROID__)
285 case 's':
286 root_seclabel = optarg;
287 break;
288#endif
289 case 'b':
290 adb_device_banner = optarg;
291 break;
292 case 'v':
293 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
294 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
295 return 0;
296 default:
297 // getopt already prints "adbd: invalid option -- %c" for us.
298 return 1;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700299 }
300 }
301
302 close_stdin();
303
Dan Albert9313c0d2015-05-21 13:58:50 -0700304 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700305
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700306 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700307 return adbd_main(DEFAULT_ADB_PORT);
308}