blob: 232d9c5fd254b46a034b2f2b18ec9b7c2e6852fc [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>
Josh Gao218f7662018-04-04 17:53:54 -070022#include <getopt.h>
23#include <malloc.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070024#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070027#include <sys/capability.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070028#include <sys/prctl.h>
29
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080030#include <memory>
31
Elliott Hughes4f713192015-12-04 22:00:26 -080032#include <android-base/logging.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040033#include <android-base/macros.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070034#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/stringprintf.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080036#include <libminijail.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070037#include <log/log_properties.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040038#include <scoped_minijail.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080039
Mark Salyzyn97787a02016-03-28 15:52:13 -070040#include <private/android_filesystem_config.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070041#include "debuggerd/handler.h"
Tom Cherry38cd57a2015-12-11 14:28:09 -080042#include "selinux/android.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070043
44#include "adb.h"
45#include "adb_auth.h"
46#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080047#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070048#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070049
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -070050#include "mdns.h"
51
Dan Albertc89e0cc2015-05-08 16:13:53 -070052static const char* root_seclabel = nullptr;
53
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070054static bool should_drop_capabilities_bounding_set() {
Bowgo Tsai0603ec42017-08-31 06:26:47 +000055#if defined(ALLOW_ADBD_ROOT)
56 if (__android_log_is_debuggable()) {
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070057 return false;
Dan Albertc89e0cc2015-05-08 16:13:53 -070058 }
Bowgo Tsai0603ec42017-08-31 06:26:47 +000059#endif
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -070060 return true;
Dan Albertc89e0cc2015-05-08 16:13:53 -070061}
62
63static bool should_drop_privileges() {
Bowgo Tsai0603ec42017-08-31 06:26:47 +000064#if defined(ALLOW_ADBD_ROOT)
Dan Albertc89e0cc2015-05-08 16:13:53 -070065 // The properties that affect `adb root` and `adb unroot` are ro.secure and
66 // ro.debuggable. In this context the names don't make the expected behavior
67 // particularly obvious.
68 //
69 // ro.debuggable:
70 // Allowed to become root, but not necessarily the default. Set to 1 on
71 // eng and userdebug builds.
72 //
73 // ro.secure:
74 // Drop privileges by default. Set to 1 on userdebug and user builds.
Elliott Hughesffdec182016-09-23 15:40:03 -070075 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzyn97787a02016-03-28 15:52:13 -070076 bool ro_debuggable = __android_log_is_debuggable();
Dan Albertc89e0cc2015-05-08 16:13:53 -070077
78 // Drop privileges if ro.secure is set...
79 bool drop = ro_secure;
80
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080081 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughesffdec182016-09-23 15:40:03 -070082 std::string prop = android::base::GetProperty("service.adb.root", "");
83 bool adb_root = (prop == "1");
84 bool adb_unroot = (prop == "0");
Dan Albertc89e0cc2015-05-08 16:13:53 -070085 if (ro_debuggable && adb_root) {
86 drop = false;
87 }
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080088 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -070089 if (adb_unroot) {
90 drop = true;
91 }
92
93 return drop;
Bowgo Tsai0603ec42017-08-31 06:26:47 +000094#else
95 return true; // "adb root" not allowed, always drop privileges.
96#endif // ALLOW_ADBD_ROOT
Dan Albertc89e0cc2015-05-08 16:13:53 -070097}
98
Mike Frysinger4120ebc2015-12-08 18:57:47 -050099static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400100 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800101
Dan Albertc89e0cc2015-05-08 16:13:53 -0700102 // Add extra groups:
103 // AID_ADB to access the USB driver
104 // AID_LOG to read system logs (adb logcat)
105 // AID_INPUT to diagnose input issues (getevent)
106 // AID_INET to diagnose network issues (ping)
107 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
108 // AID_SDCARD_R to allow reading from the SD card
109 // AID_SDCARD_RW to allow writing to the SD card
110 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800111 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou0729dd12017-05-08 15:50:55 -0700112 // AID_UHID for using 'hid' command to read/write to /dev/uhid
113 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,
115 AID_NET_BW_STATS, AID_READPROC, AID_UHID};
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400116 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700117
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800118 // Don't listen on a port (default 5037) if running in secure mode.
119 // Don't run as root if running in secure mode.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700120 if (should_drop_privileges()) {
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700121 const bool should_drop_caps = should_drop_capabilities_bounding_set();
122
123 if (should_drop_caps) {
124 minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
125 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700126
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800127 minijail_change_gid(jail.get(), AID_SHELL);
128 minijail_change_uid(jail.get(), AID_SHELL);
129 // minijail_enter() will abort if any priv-dropping step fails.
130 minijail_enter(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700131
Luis Hector Chavezdaacf4f2018-04-04 15:59:59 -0700132 // Whenever ambient capabilities are being used, minijail cannot
133 // simultaneously drop the bounding capability set to just
134 // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
135 // and permitted sets. So we need to do that in two steps.
136 using ScopedCaps =
137 std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
138 ScopedCaps caps(cap_get_proc(), &cap_free);
139 if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
140 PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
141 }
142 if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
143 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
144 }
145 if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
146 PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
147 }
148 if (cap_set_proc(caps.get()) != 0) {
149 PLOG(FATAL) << "cap_set_proc() failed";
150 }
151
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700152 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700153 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800154 // minijail_enter() will abort if any priv-dropping step fails.
155 minijail_enter(jail.get());
156
Nick Kralevich4d870952015-06-12 22:03:50 -0700157 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800158 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800159 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700160 }
161 }
Spencer Low5200c662015-07-30 23:07:55 -0700162 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700163 std::string local_name =
164 android::base::StringPrintf("tcp:%d", server_port);
David Purselleaae97e2016-04-07 11:25:48 -0700165 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
166 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700167 }
168 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500169}
170
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700171static void setup_port(int port) {
172 local_init(port);
173 setup_mdns(port);
174}
175
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500176int adbd_main(int server_port) {
177 umask(0);
178
179 signal(SIGPIPE, SIG_IGN);
180
181 init_transport_registration();
182
183 // We need to call this even if auth isn't enabled because the file
184 // descriptor will always be open.
185 adbd_cloexec_auth_socket();
186
Josh Gao27768452018-01-02 12:01:43 -0800187#if defined(ALLOW_ADBD_NO_AUTH)
Josh Gao6eda1842018-03-06 13:37:45 -0800188 // If ro.adb.secure is unset, default to no authentication required.
189 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Josh Gao27768452018-01-02 12:01:43 -0800190#endif
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500191
192 adbd_auth_init();
193
194 // Our external storage path may be different than apps, since
195 // we aren't able to bind mount after dropping root.
196 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
197 if (adb_external_storage != nullptr) {
198 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
199 } else {
200 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
201 " unchanged.\n");
202 }
203
204 drop_privileges(server_port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700205
206 bool is_usb = false;
Josh Gao183b73e2017-01-11 14:39:19 -0800207 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700208 // Listen on USB.
209 usb_init();
210 is_usb = true;
211 }
212
213 // If one of these properties is set, also listen on that port.
214 // If one of the properties isn't set and we couldn't listen on usb, listen
215 // on the default port.
Elliott Hughesffdec182016-09-23 15:40:03 -0700216 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
217 if (prop_port.empty()) {
218 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700219 }
220
221 int port;
Elliott Hughesffdec182016-09-23 15:40:03 -0700222 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700223 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700224 // Listen on TCP port specified by service.adb.tcp.port property.
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700225 setup_port(port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700226 } else if (!is_usb) {
227 // Listen on default port.
Casey Dahlin6cd5e0b2016-05-06 16:19:13 -0700228 setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700229 }
230
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700231 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700232 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700233 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700234
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700235 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700236 fdevent_loop();
237
238 return 0;
239}
240
Dan Albertc89e0cc2015-05-08 16:13:53 -0700241int main(int argc, char** argv) {
Josh Gao218f7662018-04-04 17:53:54 -0700242 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
243 mallopt(M_DECAY_TIME, 1);
244
Dan Albertc89e0cc2015-05-08 16:13:53 -0700245 while (true) {
246 static struct option opts[] = {
247 {"root_seclabel", required_argument, nullptr, 's'},
248 {"device_banner", required_argument, nullptr, 'b'},
249 {"version", no_argument, nullptr, 'v'},
250 };
251
252 int option_index = 0;
253 int c = getopt_long(argc, argv, "", opts, &option_index);
254 if (c == -1) {
255 break;
256 }
257
258 switch (c) {
259 case 's':
260 root_seclabel = optarg;
261 break;
262 case 'b':
263 adb_device_banner = optarg;
264 break;
265 case 'v':
Elliott Hughes6b970212017-08-15 07:58:20 -0700266 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
267 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700268 return 0;
269 default:
270 // getopt already prints "adbd: invalid option -- %c" for us.
271 return 1;
272 }
273 }
274
275 close_stdin();
276
Josh Gao809607a2016-06-15 18:33:11 -0700277 debuggerd_init(nullptr);
Dan Albert9313c0d2015-05-21 13:58:50 -0700278 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700279
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700280 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700281 return adbd_main(DEFAULT_ADB_PORT);
282}