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