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