blob: 527a264f62f6e8e6eeca1e3978301b9ccd4ce5ba [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
21#include <errno.h>
Josh Gao358aca22018-04-04 17:53:54 -070022#include <getopt.h>
23#include <malloc.h>
Dan Albert53a37442015-05-08 16:13:53 -070024#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
Dan Albert53a37442015-05-08 16:13:53 -070027#include <sys/prctl.h>
28
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080029#include <memory>
30
Elliott Hughesf55ead92015-12-04 22:00:26 -080031#include <android-base/logging.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040032#include <android-base/macros.h>
Elliott Hughes8b249d22016-09-23 15:40:03 -070033#include <android-base/properties.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080034#include <android-base/stringprintf.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080035#include <libminijail.h>
Steven Morelandb087d302017-04-13 23:48:57 -070036#include <log/log_properties.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040037#include <scoped_minijail.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080038
Mark Salyzync75f65f2016-03-28 15:52:13 -070039#include <private/android_filesystem_config.h>
Josh Gao690b0a92016-10-18 18:17:52 -070040#include "debuggerd/handler.h"
Tom Cherry46845222015-12-11 14:28:09 -080041#include "selinux/android.h"
Dan Albert53a37442015-05-08 16:13:53 -070042
43#include "adb.h"
44#include "adb_auth.h"
45#include "adb_listeners.h"
Josh Gao1eef4782015-11-20 15:37:31 -080046#include "adb_utils.h"
Dan Albert53a37442015-05-08 16:13:53 -070047#include "transport.h"
Dan Albert53a37442015-05-08 16:13:53 -070048
Casey Dahlin10ad15f2016-05-06 16:19:13 -070049#include "mdns.h"
50
Dan Albert53a37442015-05-08 16:13:53 -070051static const char* root_seclabel = nullptr;
52
Jorge Lucangeli Obes49061422016-02-19 15:23:28 -080053static void drop_capabilities_bounding_set_if_needed(struct minijail *j) {
Bowgo Tsai71e154f2017-08-31 06:26:47 +000054#if defined(ALLOW_ADBD_ROOT)
55 if (__android_log_is_debuggable()) {
56 return;
Dan Albert53a37442015-05-08 16:13:53 -070057 }
Bowgo Tsai71e154f2017-08-31 06:26:47 +000058#endif
Jorge Lucangeli Obes49061422016-02-19 15:23:28 -080059 minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
Dan Albert53a37442015-05-08 16:13:53 -070060}
61
62static bool should_drop_privileges() {
Bowgo Tsai71e154f2017-08-31 06:26:47 +000063#if defined(ALLOW_ADBD_ROOT)
Dan Albert53a37442015-05-08 16:13:53 -070064 // The properties that affect `adb root` and `adb unroot` are ro.secure and
65 // ro.debuggable. In this context the names don't make the expected behavior
66 // particularly obvious.
67 //
68 // ro.debuggable:
69 // Allowed to become root, but not necessarily the default. Set to 1 on
70 // eng and userdebug builds.
71 //
72 // ro.secure:
73 // Drop privileges by default. Set to 1 on userdebug and user builds.
Elliott Hughes8b249d22016-09-23 15:40:03 -070074 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzync75f65f2016-03-28 15:52:13 -070075 bool ro_debuggable = __android_log_is_debuggable();
Dan Albert53a37442015-05-08 16:13:53 -070076
77 // Drop privileges if ro.secure is set...
78 bool drop = ro_secure;
79
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080080 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughes8b249d22016-09-23 15:40:03 -070081 std::string prop = android::base::GetProperty("service.adb.root", "");
82 bool adb_root = (prop == "1");
83 bool adb_unroot = (prop == "0");
Dan Albert53a37442015-05-08 16:13:53 -070084 if (ro_debuggable && adb_root) {
85 drop = false;
86 }
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080087 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albert53a37442015-05-08 16:13:53 -070088 if (adb_unroot) {
89 drop = true;
90 }
91
92 return drop;
Bowgo Tsai71e154f2017-08-31 06:26:47 +000093#else
94 return true; // "adb root" not allowed, always drop privileges.
95#endif // ALLOW_ADBD_ROOT
Dan Albert53a37442015-05-08 16:13:53 -070096}
97
Mike Frysinger2e114072015-12-08 18:57:47 -050098static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040099 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800100
Dan Albert53a37442015-05-08 16:13:53 -0700101 // Add extra groups:
102 // AID_ADB to access the USB driver
103 // AID_LOG to read system logs (adb logcat)
104 // AID_INPUT to diagnose input issues (getevent)
105 // AID_INET to diagnose network issues (ping)
106 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
107 // AID_SDCARD_R to allow reading from the SD card
108 // AID_SDCARD_RW to allow writing to the SD card
109 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevich8a1b4b32015-11-07 16:52:17 -0800110 // AID_READPROC for reading /proc entries across UID boundaries
Siarhei Vishniakou209c27b2017-05-08 15:50:55 -0700111 // AID_UHID for using 'hid' command to read/write to /dev/uhid
112 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
113 AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
114 AID_NET_BW_STATS, AID_READPROC, AID_UHID};
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -0400115 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albert53a37442015-05-08 16:13:53 -0700116
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800117 // Don't listen on a port (default 5037) if running in secure mode.
118 // Don't run as root if running in secure mode.
Dan Albert53a37442015-05-08 16:13:53 -0700119 if (should_drop_privileges()) {
Jorge Lucangeli Obes49061422016-02-19 15:23:28 -0800120 drop_capabilities_bounding_set_if_needed(jail.get());
Dan Albert53a37442015-05-08 16:13:53 -0700121
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800122 minijail_change_gid(jail.get(), AID_SHELL);
123 minijail_change_uid(jail.get(), AID_SHELL);
124 // minijail_enter() will abort if any priv-dropping step fails.
125 minijail_enter(jail.get());
Dan Albert53a37442015-05-08 16:13:53 -0700126
Yabin Cui815ad882015-09-02 17:44:28 -0700127 D("Local port disabled");
Dan Albert53a37442015-05-08 16:13:53 -0700128 } else {
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800129 // minijail_enter() will abort if any priv-dropping step fails.
130 minijail_enter(jail.get());
131
Nick Kralevich11f73a12015-06-12 22:03:50 -0700132 if (root_seclabel != nullptr) {
Tom Cherry46845222015-12-11 14:28:09 -0800133 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800134 LOG(FATAL) << "Could not set SELinux context";
Dan Albert53a37442015-05-08 16:13:53 -0700135 }
136 }
Spencer Low753d4852015-07-30 23:07:55 -0700137 std::string error;
Dan Albert53a37442015-05-08 16:13:53 -0700138 std::string local_name =
139 android::base::StringPrintf("tcp:%d", server_port);
David Pursell19d0c232016-04-07 11:25:48 -0700140 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
141 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albert53a37442015-05-08 16:13:53 -0700142 }
143 }
Mike Frysinger2e114072015-12-08 18:57:47 -0500144}
145
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700146static void setup_port(int port) {
147 local_init(port);
148 setup_mdns(port);
149}
150
Mike Frysinger2e114072015-12-08 18:57:47 -0500151int adbd_main(int server_port) {
152 umask(0);
153
154 signal(SIGPIPE, SIG_IGN);
155
156 init_transport_registration();
157
158 // We need to call this even if auth isn't enabled because the file
159 // descriptor will always be open.
160 adbd_cloexec_auth_socket();
161
Josh Gao361148b2018-01-02 12:01:43 -0800162#if defined(ALLOW_ADBD_NO_AUTH)
Josh Gao7dbe8b72018-03-06 13:37:45 -0800163 // If ro.adb.secure is unset, default to no authentication required.
164 auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
Josh Gao361148b2018-01-02 12:01:43 -0800165#endif
Mike Frysinger2e114072015-12-08 18:57:47 -0500166
167 adbd_auth_init();
168
169 // Our external storage path may be different than apps, since
170 // we aren't able to bind mount after dropping root.
171 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
172 if (adb_external_storage != nullptr) {
173 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
174 } else {
175 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
176 " unchanged.\n");
177 }
178
179 drop_privileges(server_port);
Dan Albert53a37442015-05-08 16:13:53 -0700180
181 bool is_usb = false;
Josh Gao0dde9c82017-01-11 14:39:19 -0800182 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albert53a37442015-05-08 16:13:53 -0700183 // Listen on USB.
184 usb_init();
185 is_usb = true;
186 }
187
188 // If one of these properties is set, also listen on that port.
189 // If one of the properties isn't set and we couldn't listen on usb, listen
190 // on the default port.
Elliott Hughes8b249d22016-09-23 15:40:03 -0700191 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
192 if (prop_port.empty()) {
193 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
Dan Albert53a37442015-05-08 16:13:53 -0700194 }
195
196 int port;
Elliott Hughes8b249d22016-09-23 15:40:03 -0700197 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700198 D("using port=%d", port);
Dan Albert53a37442015-05-08 16:13:53 -0700199 // Listen on TCP port specified by service.adb.tcp.port property.
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700200 setup_port(port);
Dan Albert53a37442015-05-08 16:13:53 -0700201 } else if (!is_usb) {
202 // Listen on default port.
Casey Dahlin10ad15f2016-05-06 16:19:13 -0700203 setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Dan Albert53a37442015-05-08 16:13:53 -0700204 }
205
Yabin Cui815ad882015-09-02 17:44:28 -0700206 D("adbd_main(): pre init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700207 init_jdwp();
Yabin Cui815ad882015-09-02 17:44:28 -0700208 D("adbd_main(): post init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700209
Yabin Cui815ad882015-09-02 17:44:28 -0700210 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700211 fdevent_loop();
212
213 return 0;
214}
215
Dan Albert53a37442015-05-08 16:13:53 -0700216int main(int argc, char** argv) {
Josh Gao358aca22018-04-04 17:53:54 -0700217 // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
218 mallopt(M_DECAY_TIME, 1);
219
Dan Albert53a37442015-05-08 16:13:53 -0700220 while (true) {
221 static struct option opts[] = {
222 {"root_seclabel", required_argument, nullptr, 's'},
223 {"device_banner", required_argument, nullptr, 'b'},
224 {"version", no_argument, nullptr, 'v'},
225 };
226
227 int option_index = 0;
228 int c = getopt_long(argc, argv, "", opts, &option_index);
229 if (c == -1) {
230 break;
231 }
232
233 switch (c) {
234 case 's':
235 root_seclabel = optarg;
236 break;
237 case 'b':
238 adb_device_banner = optarg;
239 break;
240 case 'v':
Elliott Hughese32a8262017-08-15 07:58:20 -0700241 printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
242 ADB_VERSION_MINOR, ADB_SERVER_VERSION);
Dan Albert53a37442015-05-08 16:13:53 -0700243 return 0;
244 default:
245 // getopt already prints "adbd: invalid option -- %c" for us.
246 return 1;
247 }
248 }
249
250 close_stdin();
251
Josh Gaoab5449d2016-06-15 18:33:11 -0700252 debuggerd_init(nullptr);
Dan Albert08d552b2015-05-21 13:58:50 -0700253 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700254
Yabin Cui815ad882015-09-02 17:44:28 -0700255 D("Handling main()");
Dan Albert53a37442015-05-08 16:13:53 -0700256 return adbd_main(DEFAULT_ADB_PORT);
257}