blob: 4314dae0bb008e4b0ca5c702822436f04f4c1e3e [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
21#include <errno.h>
22#include <signal.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <getopt.h>
26#include <sys/prctl.h>
27
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080028#include <memory>
29
Elliott Hughes4f713192015-12-04 22:00:26 -080030#include <android-base/logging.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040031#include <android-base/macros.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070032#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080033#include <android-base/stringprintf.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080034#include <libminijail.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070035#include <log/log_properties.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040036#include <scoped_minijail.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080037
Mark Salyzyn97787a02016-03-28 15:52:13 -070038#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070039#include "debuggerd/handler.h"
Tom Cherry38cd57a2015-12-11 14:28:09 -080040#include "selinux/android.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070041
42#include "adb.h"
43#include "adb_auth.h"
44#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080045#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070046#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070047
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070048#include "mdns.h"
49
Dan Albertc89e0cc2015-05-08 16:13:53 -070050static const char* root_seclabel = nullptr;
51
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080052static void drop_capabilities_bounding_set_if_needed(struct minijail *j) {
Bowgo Tsai0603ec42017-08-31 06:26:47 +000053#if defined(ALLOW_ADBD_ROOT)
54 if (__android_log_is_debuggable()) {
55 return;
Dan Albertc89e0cc2015-05-08 16:13:53 -070056 }
Bowgo Tsai0603ec42017-08-31 06:26:47 +000057#endif
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080058 minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
Dan Albertc89e0cc2015-05-08 16:13:53 -070059}
60
61static bool should_drop_privileges() {
Bowgo Tsai0603ec42017-08-31 06:26:47 +000062#if defined(ALLOW_ADBD_ROOT)
Dan Albertc89e0cc2015-05-08 16:13:53 -070063 // The properties that affect `adb root` and `adb unroot` are ro.secure and
64 // ro.debuggable. In this context the names don't make the expected behavior
65 // particularly obvious.
66 //
67 // ro.debuggable:
68 // Allowed to become root, but not necessarily the default. Set to 1 on
69 // eng and userdebug builds.
70 //
71 // ro.secure:
72 // Drop privileges by default. Set to 1 on userdebug and user builds.
Elliott Hughesffdec182016-09-23 15:40:03 -070073 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzyn97787a02016-03-28 15:52:13 -070074 bool ro_debuggable = __android_log_is_debuggable();
Dan Albertc89e0cc2015-05-08 16:13:53 -070075
76 // Drop privileges if ro.secure is set...
77 bool drop = ro_secure;
78
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080079 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughesffdec182016-09-23 15:40:03 -070080 std::string prop = android::base::GetProperty("service.adb.root", "");
81 bool adb_root = (prop == "1");
82 bool adb_unroot = (prop == "0");
Dan Albertc89e0cc2015-05-08 16:13:53 -070083 if (ro_debuggable && adb_root) {
84 drop = false;
85 }
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080086 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -070087 if (adb_unroot) {
88 drop = true;
89 }
90
91 return drop;
Bowgo Tsai0603ec42017-08-31 06:26:47 +000092#else
93 return true; // "adb root" not allowed, always drop privileges.
94#endif // ALLOW_ADBD_ROOT
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()) {
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -0800119 drop_capabilities_bounding_set_if_needed(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700120
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800121 minijail_change_gid(jail.get(), AID_SHELL);
122 minijail_change_uid(jail.get(), AID_SHELL);
123 // minijail_enter() will abort if any priv-dropping step fails.
124 minijail_enter(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700125
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700126 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700127 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800128 // minijail_enter() will abort if any priv-dropping step fails.
129 minijail_enter(jail.get());
130
Nick Kralevich4d870952015-06-12 22:03:50 -0700131 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800132 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800133 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700134 }
135 }
Spencer Low5200c662015-07-30 23:07:55 -0700136 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700137 std::string local_name =
138 android::base::StringPrintf("tcp:%d", server_port);
David Purselleaae97e2016-04-07 11:25:48 -0700139 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
140 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700141 }
142 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500143}
144
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700145static void setup_port(int port) {
146 local_init(port);
147 setup_mdns(port);
148}
149
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500150int adbd_main(int server_port) {
151 umask(0);
152
153 signal(SIGPIPE, SIG_IGN);
154
155 init_transport_registration();
156
157 // We need to call this even if auth isn't enabled because the file
158 // descriptor will always be open.
159 adbd_cloexec_auth_socket();
160
Josh Gao27768452018-01-02 12:01:43 -0800161#if defined(ALLOW_ADBD_NO_AUTH)
Josh Gao6eda1842018-03-06 13:37:45 -0800162 // If ro.adb.secure is unset, default to no authentication required.
163 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Josh Gao27768452018-01-02 12:01:43 -0800164#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500165
166 adbd_auth_init();
167
168 // Our external storage path may be different than apps, since
169 // we aren't able to bind mount after dropping root.
170 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
171 if (adb_external_storage != nullptr) {
172 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
173 } else {
174 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
175 " unchanged.\n");
176 }
177
178 drop_privileges(server_port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700179
180 bool is_usb = false;
Josh Gao183b73e2017-01-11 14:39:19 -0800181 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700182 // Listen on USB.
183 usb_init();
184 is_usb = true;
185 }
186
187 // If one of these properties is set, also listen on that port.
188 // If one of the properties isn't set and we couldn't listen on usb, listen
189 // on the default port.
Elliott Hughesffdec182016-09-23 15:40:03 -0700190 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
191 if (prop_port.empty()) {
192 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700193 }
194
195 int port;
Elliott Hughesffdec182016-09-23 15:40:03 -0700196 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700197 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700198 // Listen on TCP port specified by service.adb.tcp.port property.
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700199 setup_port(port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700200 } else if (!is_usb) {
201 // Listen on default port.
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700202 setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700203 }
204
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700205 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700206 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700207 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700208
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700209 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700210 fdevent_loop();
211
212 return 0;
213}
214
Dan Albertc89e0cc2015-05-08 16:13:53 -0700215int main(int argc, char** argv) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700216 while (true) {
217 static struct option opts[] = {
218 {"root_seclabel", required_argument, nullptr, 's'},
219 {"device_banner", required_argument, nullptr, 'b'},
220 {"version", no_argument, nullptr, 'v'},
221 };
222
223 int option_index = 0;
224 int c = getopt_long(argc, argv, "", opts, &option_index);
225 if (c == -1) {
226 break;
227 }
228
229 switch (c) {
230 case 's':
231 root_seclabel = optarg;
232 break;
233 case 'b':
234 adb_device_banner = optarg;
235 break;
236 case 'v':
Elliott Hughes6b970212017-08-15 07:58:20 -0700237 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
238 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700239 return 0;
240 default:
241 // getopt already prints "adbd: invalid option -- %c" for us.
242 return 1;
243 }
244 }
245
246 close_stdin();
247
Josh Gao809607a2016-06-15 18:33:11 -0700248 debuggerd_init(nullptr);
Dan Albert9313c0d2015-05-21 13:58:50 -0700249 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700250
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700251 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700252 return adbd_main(DEFAULT_ADB_PORT);
253}