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 | |
Josh Gao | 41da100 | 2018-07-25 18:13:44 -0700 | [diff] [blame^] | 21 | #include <android/fdsan.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 22 | #include <errno.h> |
Josh Gao | 358aca2 | 2018-04-04 17:53:54 -0700 | [diff] [blame] | 23 | #include <getopt.h> |
| 24 | #include <malloc.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 25 | #include <signal.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 28 | #include <sys/capability.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 29 | #include <sys/prctl.h> |
| 30 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 31 | #include <memory> |
| 32 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 34 | #include <android-base/macros.h> |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 35 | #include <android-base/properties.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include <android-base/stringprintf.h> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 37 | #include <libminijail.h> |
Steven Moreland | b087d30 | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 38 | #include <log/log_properties.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 39 | #include <scoped_minijail.h> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 40 | |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 41 | #include <private/android_filesystem_config.h> |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 42 | #include "selinux/android.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 43 | |
| 44 | #include "adb.h" |
| 45 | #include "adb_auth.h" |
| 46 | #include "adb_listeners.h" |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 47 | #include "adb_utils.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 48 | #include "transport.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 49 | |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 50 | #include "mdns.h" |
| 51 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 52 | static const char* root_seclabel = nullptr; |
| 53 | |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 54 | static bool should_drop_capabilities_bounding_set() { |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 55 | #if defined(ALLOW_ADBD_ROOT) |
| 56 | if (__android_log_is_debuggable()) { |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 57 | return false; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 58 | } |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 59 | #endif |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 60 | return true; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static bool should_drop_privileges() { |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 64 | #if defined(ALLOW_ADBD_ROOT) |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 65 | // 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 Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 75 | bool ro_secure = android::base::GetBoolProperty("ro.secure", true); |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 76 | bool ro_debuggable = __android_log_is_debuggable(); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 77 | |
| 78 | // Drop privileges if ro.secure is set... |
| 79 | bool drop = ro_secure; |
| 80 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 81 | // ... except "adb root" lets you keep privileges in a debuggable build. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 82 | std::string prop = android::base::GetProperty("service.adb.root", ""); |
| 83 | bool adb_root = (prop == "1"); |
| 84 | bool adb_unroot = (prop == "0"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 85 | if (ro_debuggable && adb_root) { |
| 86 | drop = false; |
| 87 | } |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 88 | // ... and "adb unroot" lets you explicitly drop privileges. |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 89 | if (adb_unroot) { |
| 90 | drop = true; |
| 91 | } |
| 92 | |
| 93 | return drop; |
Bowgo Tsai | 71e154f | 2017-08-31 06:26:47 +0000 | [diff] [blame] | 94 | #else |
| 95 | return true; // "adb root" not allowed, always drop privileges. |
| 96 | #endif // ALLOW_ADBD_ROOT |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 99 | static void drop_privileges(int server_port) { |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 100 | ScopedMinijail jail(minijail_new()); |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 101 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 102 | // 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 Kralevich | 8a1b4b3 | 2015-11-07 16:52:17 -0800 | [diff] [blame] | 111 | // AID_READPROC for reading /proc entries across UID boundaries |
Siarhei Vishniakou | 209c27b | 2017-05-08 15:50:55 -0700 | [diff] [blame] | 112 | // 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 Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 116 | minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 117 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 118 | // 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 120 | if (should_drop_privileges()) { |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 121 | 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 126 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 127 | 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 131 | |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 132 | // 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 Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 152 | D("Local port disabled"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 153 | } else { |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 154 | // minijail_enter() will abort if any priv-dropping step fails. |
| 155 | minijail_enter(jail.get()); |
| 156 | |
Nick Kralevich | 11f73a1 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 157 | if (root_seclabel != nullptr) { |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 158 | if (selinux_android_setcon(root_seclabel) < 0) { |
Jorge Lucangeli Obes | 116b8b9 | 2015-11-11 11:33:19 -0800 | [diff] [blame] | 159 | LOG(FATAL) << "Could not set SELinux context"; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 162 | std::string error; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 163 | std::string local_name = |
| 164 | android::base::StringPrintf("tcp:%d", server_port); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 165 | if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) { |
| 166 | LOG(FATAL) << "Could not install *smartsocket* listener: " << error; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 171 | static void setup_port(int port) { |
| 172 | local_init(port); |
| 173 | setup_mdns(port); |
| 174 | } |
| 175 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 176 | int adbd_main(int server_port) { |
| 177 | umask(0); |
| 178 | |
| 179 | signal(SIGPIPE, SIG_IGN); |
| 180 | |
Josh Gao | 41da100 | 2018-07-25 18:13:44 -0700 | [diff] [blame^] | 181 | 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 Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 186 | 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 Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 192 | #if defined(ALLOW_ADBD_NO_AUTH) |
Josh Gao | 7dbe8b7 | 2018-03-06 13:37:45 -0800 | [diff] [blame] | 193 | // If ro.adb.secure is unset, default to no authentication required. |
| 194 | auth_required = android::base::GetBoolProperty("ro.adb.secure", false); |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 195 | #endif |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 196 | |
| 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 210 | |
| 211 | bool is_usb = false; |
Josh Gao | 0dde9c8 | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 212 | if (access(USB_FFS_ADB_EP0, F_OK) == 0) { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 213 | // 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 Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 221 | 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 Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | int port; |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 227 | if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 228 | D("using port=%d", port); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 229 | // Listen on TCP port specified by service.adb.tcp.port property. |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 230 | setup_port(port); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 231 | } else if (!is_usb) { |
| 232 | // Listen on default port. |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 233 | setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 236 | D("adbd_main(): pre init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 237 | init_jdwp(); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 238 | D("adbd_main(): post init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 239 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 240 | D("Event loop starting"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 241 | fdevent_loop(); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 246 | int main(int argc, char** argv) { |
Josh Gao | 358aca2 | 2018-04-04 17:53:54 -0700 | [diff] [blame] | 247 | // Set M_DECAY_TIME so that our allocations aren't immediately purged on free. |
| 248 | mallopt(M_DECAY_TIME, 1); |
| 249 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 250 | 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 Hughes | e32a826 | 2017-08-15 07:58:20 -0700 | [diff] [blame] | 271 | printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR, |
| 272 | ADB_VERSION_MINOR, ADB_SERVER_VERSION); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 273 | 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 Albert | 08d552b | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 282 | adb_trace_init(argv); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 283 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 284 | D("Handling main()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 285 | return adbd_main(DEFAULT_ADB_PORT); |
| 286 | } |