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> |
| 22 | #include <signal.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <getopt.h> |
| 26 | #include <sys/prctl.h> |
| 27 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 28 | #include <memory> |
| 29 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 30 | #include <android-base/logging.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 31 | #include <android-base/macros.h> |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 32 | #include <android-base/properties.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 34 | #include <libminijail.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 35 | #include <scoped_minijail.h> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 36 | |
Josh Gao | ab5449d | 2016-06-15 18:33:11 -0700 | [diff] [blame] | 37 | #include "debuggerd/client.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 38 | #include "private/android_filesystem_config.h" |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 39 | #include "selinux/android.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 40 | |
| 41 | #include "adb.h" |
| 42 | #include "adb_auth.h" |
| 43 | #include "adb_listeners.h" |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 44 | #include "adb_utils.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 45 | #include "transport.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 46 | |
| 47 | static const char* root_seclabel = nullptr; |
| 48 | |
Jorge Lucangeli Obes | 4906142 | 2016-02-19 15:23:28 -0800 | [diff] [blame] | 49 | static void drop_capabilities_bounding_set_if_needed(struct minijail *j) { |
| 50 | #if defined(ALLOW_ADBD_ROOT) |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 51 | if (android::base::GetBoolProperty("ro.debuggable", false)) { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | #endif |
Jorge Lucangeli Obes | 4906142 | 2016-02-19 15:23:28 -0800 | [diff] [blame] | 55 | 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] | 56 | } |
| 57 | |
| 58 | static bool should_drop_privileges() { |
| 59 | #if defined(ALLOW_ADBD_ROOT) |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 60 | // The properties that affect `adb root` and `adb unroot` are ro.secure and |
| 61 | // ro.debuggable. In this context the names don't make the expected behavior |
| 62 | // particularly obvious. |
| 63 | // |
| 64 | // ro.debuggable: |
| 65 | // Allowed to become root, but not necessarily the default. Set to 1 on |
| 66 | // eng and userdebug builds. |
| 67 | // |
| 68 | // ro.secure: |
| 69 | // Drop privileges by default. Set to 1 on userdebug and user builds. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 70 | bool ro_secure = android::base::GetBoolProperty("ro.secure", true); |
| 71 | bool ro_debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 72 | |
| 73 | // Drop privileges if ro.secure is set... |
| 74 | bool drop = ro_secure; |
| 75 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 76 | // ... except "adb root" lets you keep privileges in a debuggable build. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 77 | std::string prop = android::base::GetProperty("service.adb.root", ""); |
| 78 | bool adb_root = (prop == "1"); |
| 79 | bool adb_unroot = (prop == "0"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 80 | if (ro_debuggable && adb_root) { |
| 81 | drop = false; |
| 82 | } |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 83 | // ... and "adb unroot" lets you explicitly drop privileges. |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 84 | if (adb_unroot) { |
| 85 | drop = true; |
| 86 | } |
| 87 | |
| 88 | return drop; |
| 89 | #else |
| 90 | return true; // "adb root" not allowed, always drop privileges. |
| 91 | #endif // ALLOW_ADBD_ROOT |
| 92 | } |
| 93 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 94 | static void drop_privileges(int server_port) { |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 95 | ScopedMinijail jail(minijail_new()); |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 96 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 97 | // Add extra groups: |
| 98 | // AID_ADB to access the USB driver |
| 99 | // AID_LOG to read system logs (adb logcat) |
| 100 | // AID_INPUT to diagnose input issues (getevent) |
| 101 | // AID_INET to diagnose network issues (ping) |
| 102 | // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump) |
| 103 | // AID_SDCARD_R to allow reading from the SD card |
| 104 | // AID_SDCARD_RW to allow writing to the SD card |
| 105 | // AID_NET_BW_STATS to read out qtaguid statistics |
Nick Kralevich | 8a1b4b3 | 2015-11-07 16:52:17 -0800 | [diff] [blame] | 106 | // AID_READPROC for reading /proc entries across UID boundaries |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 107 | gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, |
| 108 | AID_INET, AID_NET_BT, AID_NET_BT_ADMIN, |
Nick Kralevich | 8a1b4b3 | 2015-11-07 16:52:17 -0800 | [diff] [blame] | 109 | AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS, |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 110 | AID_READPROC}; |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 111 | minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 112 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 113 | // Don't listen on a port (default 5037) if running in secure mode. |
| 114 | // Don't run as root if running in secure mode. |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 115 | if (should_drop_privileges()) { |
Jorge Lucangeli Obes | 4906142 | 2016-02-19 15:23:28 -0800 | [diff] [blame] | 116 | drop_capabilities_bounding_set_if_needed(jail.get()); |
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 | minijail_change_gid(jail.get(), AID_SHELL); |
| 119 | minijail_change_uid(jail.get(), AID_SHELL); |
| 120 | // minijail_enter() will abort if any priv-dropping step fails. |
| 121 | minijail_enter(jail.get()); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 122 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 123 | D("Local port disabled"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 124 | } else { |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 125 | // minijail_enter() will abort if any priv-dropping step fails. |
| 126 | minijail_enter(jail.get()); |
| 127 | |
Nick Kralevich | 11f73a1 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 128 | if (root_seclabel != nullptr) { |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 129 | if (selinux_android_setcon(root_seclabel) < 0) { |
Jorge Lucangeli Obes | 116b8b9 | 2015-11-11 11:33:19 -0800 | [diff] [blame] | 130 | LOG(FATAL) << "Could not set SELinux context"; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 133 | std::string error; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 134 | std::string local_name = |
| 135 | android::base::StringPrintf("tcp:%d", server_port); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 136 | if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) { |
| 137 | LOG(FATAL) << "Could not install *smartsocket* listener: " << error; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int adbd_main(int server_port) { |
| 143 | umask(0); |
| 144 | |
| 145 | signal(SIGPIPE, SIG_IGN); |
| 146 | |
| 147 | init_transport_registration(); |
| 148 | |
| 149 | // We need to call this even if auth isn't enabled because the file |
| 150 | // descriptor will always be open. |
| 151 | adbd_cloexec_auth_socket(); |
| 152 | |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 153 | if (ALLOW_ADBD_NO_AUTH && !android::base::GetBoolProperty("ro.adb.secure", false)) { |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 154 | auth_required = false; |
| 155 | } |
| 156 | |
| 157 | adbd_auth_init(); |
| 158 | |
| 159 | // Our external storage path may be different than apps, since |
| 160 | // we aren't able to bind mount after dropping root. |
| 161 | const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE"); |
| 162 | if (adb_external_storage != nullptr) { |
| 163 | setenv("EXTERNAL_STORAGE", adb_external_storage, 1); |
| 164 | } else { |
| 165 | D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE" |
| 166 | " unchanged.\n"); |
| 167 | } |
| 168 | |
| 169 | drop_privileges(server_port); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 170 | |
| 171 | bool is_usb = false; |
| 172 | if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) { |
| 173 | // Listen on USB. |
| 174 | usb_init(); |
| 175 | is_usb = true; |
| 176 | } |
| 177 | |
| 178 | // If one of these properties is set, also listen on that port. |
| 179 | // If one of the properties isn't set and we couldn't listen on usb, listen |
| 180 | // on the default port. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 181 | std::string prop_port = android::base::GetProperty("service.adb.tcp.port", ""); |
| 182 | if (prop_port.empty()) { |
| 183 | prop_port = android::base::GetProperty("persist.adb.tcp.port", ""); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | int port; |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame^] | 187 | if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 188 | D("using port=%d", port); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 189 | // Listen on TCP port specified by service.adb.tcp.port property. |
| 190 | local_init(port); |
| 191 | } else if (!is_usb) { |
| 192 | // Listen on default port. |
| 193 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 194 | } |
| 195 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 196 | D("adbd_main(): pre init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 197 | init_jdwp(); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 198 | D("adbd_main(): post init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 199 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 200 | D("Event loop starting"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 201 | fdevent_loop(); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 206 | int main(int argc, char** argv) { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 207 | while (true) { |
| 208 | static struct option opts[] = { |
| 209 | {"root_seclabel", required_argument, nullptr, 's'}, |
| 210 | {"device_banner", required_argument, nullptr, 'b'}, |
| 211 | {"version", no_argument, nullptr, 'v'}, |
| 212 | }; |
| 213 | |
| 214 | int option_index = 0; |
| 215 | int c = getopt_long(argc, argv, "", opts, &option_index); |
| 216 | if (c == -1) { |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | switch (c) { |
| 221 | case 's': |
| 222 | root_seclabel = optarg; |
| 223 | break; |
| 224 | case 'b': |
| 225 | adb_device_banner = optarg; |
| 226 | break; |
| 227 | case 'v': |
| 228 | printf("Android Debug Bridge Daemon version %d.%d.%d %s\n", |
| 229 | ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION, |
| 230 | ADB_REVISION); |
| 231 | return 0; |
| 232 | default: |
| 233 | // getopt already prints "adbd: invalid option -- %c" for us. |
| 234 | return 1; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | close_stdin(); |
| 239 | |
Josh Gao | ab5449d | 2016-06-15 18:33:11 -0700 | [diff] [blame] | 240 | debuggerd_init(nullptr); |
Dan Albert | 08d552b | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 241 | adb_trace_init(argv); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 242 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 243 | D("Handling main()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 244 | return adbd_main(DEFAULT_ADB_PORT); |
| 245 | } |