blob: f6f1acc31c4d708759669d82e336a974ee75fbf7 [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 Gao41da1002018-07-25 18:13:44 -070021#include <android/fdsan.h>
Dan Albert53a37442015-05-08 16:13:53 -070022#include <errno.h>
Josh Gao358aca22018-04-04 17:53:54 -070023#include <getopt.h>
24#include <malloc.h>
Dan Albert53a37442015-05-08 16:13:53 -070025#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
Luis Hector Chaveze4528e12018-04-04 15:59:59 -070028#include <sys/capability.h>
Dan Albert53a37442015-05-08 16:13:53 -070029#include <sys/prctl.h>
30
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080031#include <memory>
32
Elliott Hughesf55ead92015-12-04 22:00:26 -080033#include <android-base/logging.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040034#include <android-base/macros.h>
Elliott Hughes8b249d22016-09-23 15:40:03 -070035#include <android-base/properties.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080036#include <android-base/stringprintf.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080037#include <libminijail.h>
Steven Morelandb087d302017-04-13 23:48:57 -070038#include <log/log_properties.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040039#include <scoped_minijail.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080040
Mark Salyzync75f65f2016-03-28 15:52:13 -070041#include <private/android_filesystem_config.h>
Tom Cherry46845222015-12-11 14:28:09 -080042#include "selinux/android.h"
Dan Albert53a37442015-05-08 16:13:53 -070043
44#include "adb.h"
45#include "adb_auth.h"
46#include "adb_listeners.h"
Josh Gao1eef4782015-11-20 15:37:31 -080047#include "adb_utils.h"
Dan Albert53a37442015-05-08 16:13:53 -070048#include "transport.h"
Dan Albert53a37442015-05-08 16:13:53 -070049
Casey Dahlin10ad15f2016-05-06 16:19:13 -070050#include "mdns.h"
51
Dan Albert53a37442015-05-08 16:13:53 -070052static const char* root_seclabel = nullptr;
53
Luis Hector Chaveze4528e12018-04-04 15:59:59 -070054static bool should_drop_capabilities_bounding_set() {
Bowgo Tsai71e154f2017-08-31 06:26:47 +000055#if defined(ALLOW_ADBD_ROOT)
56 if (__android_log_is_debuggable()) {
Luis Hector Chaveze4528e12018-04-04 15:59:59 -070057 return false;
Dan Albert53a37442015-05-08 16:13:53 -070058 }
Bowgo Tsai71e154f2017-08-31 06:26:47 +000059#endif
Luis Hector Chaveze4528e12018-04-04 15:59:59 -070060 return true;
Dan Albert53a37442015-05-08 16:13:53 -070061}
62
63static bool should_drop_privileges() {
Bowgo Tsai71e154f2017-08-31 06:26:47 +000064#if defined(ALLOW_ADBD_ROOT)
Dan Albert53a37442015-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 Hughes8b249d22016-09-23 15:40:03 -070075 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzync75f65f2016-03-28 15:52:13 -070076 bool ro_debuggable = __android_log_is_debuggable();
Dan Albert53a37442015-05-08 16:13:53 -070077
78 // Drop privileges if ro.secure is set...
79 bool drop = ro_secure;
80
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080081 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughes8b249d22016-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 Albert53a37442015-05-08 16:13:53 -070085 if (ro_debuggable && adb_root) {
86 drop = false;
87 }
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080088 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albert53a37442015-05-08 16:13:53 -070089 if (adb_unroot) {
90 drop = true;
91 }
92
93 return drop;
Bowgo Tsai71e154f2017-08-31 06:26:47 +000094#else
95 return true; // "adb root" not allowed, always drop privileges.
96#endif // ALLOW_ADBD_ROOT
Dan Albert53a37442015-05-08 16:13:53 -070097}
98
Mike Frysinger2e114072015-12-08 18:57:47 -050099static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -0400100 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800101
Dan Albert53a37442015-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 Kralevich8a1b4b32015-11-07 16:52:17 -0800111 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou209c27b2017-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 Obesbd75c672016-07-18 13:46:42 -0400116 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albert53a37442015-05-08 16:13:53 -0700117
Jorge Lucangeli Obes83e28512015-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 Albert53a37442015-05-08 16:13:53 -0700120 if (should_drop_privileges()) {
Luis Hector Chaveze4528e12018-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 Albert53a37442015-05-08 16:13:53 -0700126
Jorge Lucangeli Obes83e28512015-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 Albert53a37442015-05-08 16:13:53 -0700131
Luis Hector Chaveze4528e12018-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 Cui815ad882015-09-02 17:44:28 -0700152 D("Local port disabled");
Dan Albert53a37442015-05-08 16:13:53 -0700153 } else {
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800154 // minijail_enter() will abort if any priv-dropping step fails.
155 minijail_enter(jail.get());
156
Nick Kralevich11f73a12015-06-12 22:03:50 -0700157 if (root_seclabel != nullptr) {
Tom Cherry46845222015-12-11 14:28:09 -0800158 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800159 LOG(FATAL) << "Could not set SELinux context";
Dan Albert53a37442015-05-08 16:13:53 -0700160 }
161 }
Spencer Low753d4852015-07-30 23:07:55 -0700162 std::string error;
Dan Albert53a37442015-05-08 16:13:53 -0700163 std::string local_name =
164 android::base::StringPrintf("tcp:%d", server_port);
David Pursell19d0c232016-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 Albert53a37442015-05-08 16:13:53 -0700167 }
168 }
Mike Frysinger2e114072015-12-08 18:57:47 -0500169}
170
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700171static void setup_port(int port) {
172 local_init(port);
173 setup_mdns(port);
174}
175
Mike Frysinger2e114072015-12-08 18:57:47 -0500176int adbd_main(int server_port) {
177 umask(0);
178
179 signal(SIGPIPE, SIG_IGN);
180
Josh Gao41da1002018-07-25 18:13:44 -0700181 auto fdsan_level = android_fdsan_get_error_level();
182 if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
183 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
184 }
185
Mike Frysinger2e114072015-12-08 18:57:47 -0500186 init_transport_registration();
187
188 // We need to call this even if auth isn't enabled because the file
189 // descriptor will always be open.
190 adbd_cloexec_auth_socket();
191
Josh Gao361148b2018-01-02 12:01:43 -0800192#if defined(ALLOW_ADBD_NO_AUTH)
Josh Gao7dbe8b72018-03-06 13:37:45 -0800193 // If ro.adb.secure is unset, default to no authentication required.
194 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Josh Gao361148b2018-01-02 12:01:43 -0800195#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500196
197 adbd_auth_init();
198
199 // Our external storage path may be different than apps, since
200 // we aren't able to bind mount after dropping root.
201 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
202 if (adb_external_storage != nullptr) {
203 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
204 } else {
205 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
206 " unchanged.\n");
207 }
208
209 drop_privileges(server_port);
Dan Albert53a37442015-05-08 16:13:53 -0700210
211 bool is_usb = false;
Josh Gao0dde9c82017-01-11 14:39:19 -0800212 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albert53a37442015-05-08 16:13:53 -0700213 // Listen on USB.
214 usb_init();
215 is_usb = true;
216 }
217
218 // If one of these properties is set, also listen on that port.
219 // If one of the properties isn't set and we couldn't listen on usb, listen
220 // on the default port.
Elliott Hughes8b249d22016-09-23 15:40:03 -0700221 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
222 if (prop_port.empty()) {
223 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
Dan Albert53a37442015-05-08 16:13:53 -0700224 }
225
226 int port;
Elliott Hughes8b249d22016-09-23 15:40:03 -0700227 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700228 D("using port=%d", port);
Dan Albert53a37442015-05-08 16:13:53 -0700229 // Listen on TCP port specified by service.adb.tcp.port property.
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700230 setup_port(port);
Dan Albert53a37442015-05-08 16:13:53 -0700231 } else if (!is_usb) {
232 // Listen on default port.
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700233 setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Dan Albert53a37442015-05-08 16:13:53 -0700234 }
235
Yabin Cui815ad882015-09-02 17:44:28 -0700236 D("adbd_main(): pre init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700237 init_jdwp();
Yabin Cui815ad882015-09-02 17:44:28 -0700238 D("adbd_main(): post init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700239
Yabin Cui815ad882015-09-02 17:44:28 -0700240 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700241 fdevent_loop();
242
243 return 0;
244}
245
Dan Albert53a37442015-05-08 16:13:53 -0700246int main(int argc, char** argv) {
Josh Gao358aca22018-04-04 17:53:54 -0700247 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
248 mallopt(M_DECAY_TIME, 1);
249
Dan Albert53a37442015-05-08 16:13:53 -0700250 while (true) {
251 static struct option opts[] = {
252 {"root_seclabel", required_argument, nullptr, 's'},
253 {"device_banner", required_argument, nullptr, 'b'},
254 {"version", no_argument, nullptr, 'v'},
255 };
256
257 int option_index = 0;
258 int c = getopt_long(argc, argv, "", opts, &option_index);
259 if (c == -1) {
260 break;
261 }
262
263 switch (c) {
264 case 's':
265 root_seclabel = optarg;
266 break;
267 case 'b':
268 adb_device_banner = optarg;
269 break;
270 case 'v':
Elliott Hughese32a8262017-08-15 07:58:20 -0700271 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
272 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
Dan Albert53a37442015-05-08 16:13:53 -0700273 return 0;
274 default:
275 // getopt already prints "adbd: invalid option -- %c" for us.
276 return 1;
277 }
278 }
279
280 close_stdin();
281
Dan Albert08d552b2015-05-21 13:58:50 -0700282 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700283
Yabin Cui815ad882015-09-02 17:44:28 -0700284 D("Handling main()");
Dan Albert53a37442015-05-08 16:13:53 -0700285 return adbd_main(DEFAULT_ADB_PORT);
286}