Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
| 21 | #include <errno.h> |
Josh Gao | 358aca2 | 2018-04-04 17:53:54 -0700 | [diff] [blame^] | 22 | #include <getopt.h> |
| 23 | #include <malloc.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 24 | #include <signal.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 27 | #include <sys/prctl.h> |
| 28 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 29 | #include <memory> |
| 30 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 32 | #include <android-base/macros.h> |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 33 | #include <android-base/properties.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 35 | #include <libminijail.h> |
Steven Moreland | b087d30 | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 36 | #include <log/log_properties.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 37 | #include <scoped_minijail.h> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 38 | |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> |
Josh Gao | 690b0a9 | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 40 | #include "debuggerd/handler.h" |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 41 | #include "selinux/android.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 42 | |
| 43 | #include "adb.h" |
| 44 | #include "adb_auth.h" |
| 45 | #include "adb_listeners.h" |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 46 | #include "adb_utils.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 47 | #include "transport.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 48 | |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 49 | #include "mdns.h" |
| 50 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 51 | static const char* root_seclabel = nullptr; |
| 52 | |
Jorge Lucangeli Obes | 4906142 | 2016-02-19 15:23:28 -0800 | [diff] [blame] | 53 | static void drop_capabilities_bounding_set_if_needed(struct minijail *j) { |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 54 | #if defined(ALLOW_ADBD_ROOT) |
| 55 | if (__android_log_is_debuggable()) { |
| 56 | return; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 57 | } |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 58 | #endif |
Jorge Lucangeli Obes | 4906142 | 2016-02-19 15:23:28 -0800 | [diff] [blame] | 59 | minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID)); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static bool should_drop_privileges() { |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 63 | #if defined(ALLOW_ADBD_ROOT) |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 64 | // 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 Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 74 | bool ro_secure = android::base::GetBoolProperty("ro.secure", true); |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 75 | bool ro_debuggable = __android_log_is_debuggable(); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 76 | |
| 77 | // Drop privileges if ro.secure is set... |
| 78 | bool drop = ro_secure; |
| 79 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 80 | // ... except "adb root" lets you keep privileges in a debuggable build. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 81 | std::string prop = android::base::GetProperty("service.adb.root", ""); |
| 82 | bool adb_root = (prop == "1"); |
| 83 | bool adb_unroot = (prop == "0"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 84 | if (ro_debuggable && adb_root) { |
| 85 | drop = false; |
| 86 | } |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 87 | // ... and "adb unroot" lets you explicitly drop privileges. |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 88 | if (adb_unroot) { |
| 89 | drop = true; |
| 90 | } |
| 91 | |
| 92 | return drop; |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 93 | #else |
| 94 | return true; // "adb root" not allowed, always drop privileges. |
| 95 | #endif // ALLOW_ADBD_ROOT |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 98 | static void drop_privileges(int server_port) { |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 99 | ScopedMinijail jail(minijail_new()); |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 100 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 101 | // 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 Kralevich | 8a1b4b3 | 2015-11-07 16:52:17 -0800 | [diff] [blame] | 110 | // AID_READPROC for reading /proc entries across UID boundaries |
Siarhei Vishniakou | 209c27b | 2017-05-08 15:50:55 -0700 | [diff] [blame] | 111 | // 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 Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 115 | minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 116 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 117 | // 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 119 | if (should_drop_privileges()) { |
Jorge Lucangeli Obes | 4906142 | 2016-02-19 15:23:28 -0800 | [diff] [blame] | 120 | drop_capabilities_bounding_set_if_needed(jail.get()); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 121 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 122 | 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 126 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 127 | D("Local port disabled"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 128 | } else { |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 129 | // minijail_enter() will abort if any priv-dropping step fails. |
| 130 | minijail_enter(jail.get()); |
| 131 | |
Nick Kralevich | 11f73a1 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 132 | if (root_seclabel != nullptr) { |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 133 | if (selinux_android_setcon(root_seclabel) < 0) { |
Jorge Lucangeli Obes | 116b8b9 | 2015-11-11 11:33:19 -0800 | [diff] [blame] | 134 | LOG(FATAL) << "Could not set SELinux context"; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 137 | std::string error; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 138 | std::string local_name = |
| 139 | android::base::StringPrintf("tcp:%d", server_port); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 140 | if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) { |
| 141 | LOG(FATAL) << "Could not install *smartsocket* listener: " << error; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 146 | static void setup_port(int port) { |
| 147 | local_init(port); |
| 148 | setup_mdns(port); |
| 149 | } |
| 150 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 151 | int 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 Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 162 | #if defined(ALLOW_ADBD_NO_AUTH) |
Josh Gao | 7dbe8b7 | 2018-03-06 13:37:45 -0800 | [diff] [blame] | 163 | // If ro.adb.secure is unset, default to no authentication required. |
| 164 | auth_required = android::base::GetBoolProperty("ro.adb.secure", false); |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 165 | #endif |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 166 | |
| 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 180 | |
| 181 | bool is_usb = false; |
Josh Gao | 0dde9c8 | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 182 | if (access(USB_FFS_ADB_EP0, F_OK) == 0) { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 183 | // 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 Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 191 | 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | int port; |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 197 | if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 198 | D("using port=%d", port); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 199 | // Listen on TCP port specified by service.adb.tcp.port property. |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 200 | setup_port(port); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 201 | } else if (!is_usb) { |
| 202 | // Listen on default port. |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 203 | setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 206 | D("adbd_main(): pre init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 207 | init_jdwp(); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 208 | D("adbd_main(): post init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 209 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 210 | D("Event loop starting"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 211 | fdevent_loop(); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 216 | int main(int argc, char** argv) { |
Josh Gao | 358aca2 | 2018-04-04 17:53:54 -0700 | [diff] [blame^] | 217 | // Set M_DECAY_TIME so that our allocations aren't immediately purged on free. |
| 218 | mallopt(M_DECAY_TIME, 1); |
| 219 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 220 | 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 Hughes | e32a826 | 2017-08-15 07:58:20 -0700 | [diff] [blame] | 241 | printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR, |
| 242 | ADB_VERSION_MINOR, ADB_SERVER_VERSION); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 243 | 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 Gao | ab5449d | 2016-06-15 18:33:11 -0700 | [diff] [blame] | 252 | debuggerd_init(nullptr); |
Dan Albert | 08d552b | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 253 | adb_trace_init(argv); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 254 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 255 | D("Handling main()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 256 | return adbd_main(DEFAULT_ADB_PORT); |
| 257 | } |