blob: 094988a99f4ca8b616d174d48782fb716c3864ab [file] [log] [blame]
Dan Albert53a37442015-05-08 16:13:53 -07001/*
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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert53a37442015-05-08 16:13:53 -070018
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 Obes83e28512015-12-14 13:18:57 -080028#include <memory>
29
Elliott Hughesf55ead92015-12-04 22:00:26 -080030#include <android-base/logging.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040031#include <android-base/macros.h>
Elliott Hughes8b249d22016-09-23 15:40:03 -070032#include <android-base/properties.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080033#include <android-base/stringprintf.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080034#include <libminijail.h>
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040035#include <scoped_minijail.h>
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080036
Josh Gaoab5449d2016-06-15 18:33:11 -070037#include "debuggerd/client.h"
Dan Albert53a37442015-05-08 16:13:53 -070038#include "private/android_filesystem_config.h"
Tom Cherry46845222015-12-11 14:28:09 -080039#include "selinux/android.h"
Dan Albert53a37442015-05-08 16:13:53 -070040
41#include "adb.h"
42#include "adb_auth.h"
43#include "adb_listeners.h"
Josh Gao1eef4782015-11-20 15:37:31 -080044#include "adb_utils.h"
Dan Albert53a37442015-05-08 16:13:53 -070045#include "transport.h"
Dan Albert53a37442015-05-08 16:13:53 -070046
47static const char* root_seclabel = nullptr;
48
Jorge Lucangeli Obes49061422016-02-19 15:23:28 -080049static void drop_capabilities_bounding_set_if_needed(struct minijail *j) {
50#if defined(ALLOW_ADBD_ROOT)
Elliott Hughes8b249d22016-09-23 15:40:03 -070051 if (android::base::GetBoolProperty("ro.debuggable", false)) {
Dan Albert53a37442015-05-08 16:13:53 -070052 return;
53 }
54#endif
Jorge Lucangeli Obes49061422016-02-19 15:23:28 -080055 minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
Dan Albert53a37442015-05-08 16:13:53 -070056}
57
58static bool should_drop_privileges() {
59#if defined(ALLOW_ADBD_ROOT)
Dan Albert53a37442015-05-08 16:13:53 -070060 // 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 Hughes8b249d22016-09-23 15:40:03 -070070 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
71 bool ro_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
Dan Albert53a37442015-05-08 16:13:53 -070072
73 // Drop privileges if ro.secure is set...
74 bool drop = ro_secure;
75
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080076 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughes8b249d22016-09-23 15:40:03 -070077 std::string prop = android::base::GetProperty("service.adb.root", "");
78 bool adb_root = (prop == "1");
79 bool adb_unroot = (prop == "0");
Dan Albert53a37442015-05-08 16:13:53 -070080 if (ro_debuggable && adb_root) {
81 drop = false;
82 }
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080083 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albert53a37442015-05-08 16:13:53 -070084 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 Frysinger2e114072015-12-08 18:57:47 -050094static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -040095 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -080096
Dan Albert53a37442015-05-08 16:13:53 -070097 // 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 Kralevich8a1b4b32015-11-07 16:52:17 -0800106 // AID_READPROC for reading /proc entries across UID boundaries
Dan Albert53a37442015-05-08 16:13:53 -0700107 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT,
108 AID_INET, AID_NET_BT, AID_NET_BT_ADMIN,
Nick Kralevich8a1b4b32015-11-07 16:52:17 -0800109 AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS,
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800110 AID_READPROC};
Jorge Lucangeli Obesbd75c672016-07-18 13:46:42 -0400111 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albert53a37442015-05-08 16:13:53 -0700112
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800113 // 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 Albert53a37442015-05-08 16:13:53 -0700115 if (should_drop_privileges()) {
Jorge Lucangeli Obes49061422016-02-19 15:23:28 -0800116 drop_capabilities_bounding_set_if_needed(jail.get());
Dan Albert53a37442015-05-08 16:13:53 -0700117
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800118 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 Albert53a37442015-05-08 16:13:53 -0700122
Yabin Cui815ad882015-09-02 17:44:28 -0700123 D("Local port disabled");
Dan Albert53a37442015-05-08 16:13:53 -0700124 } else {
Jorge Lucangeli Obes83e28512015-12-14 13:18:57 -0800125 // minijail_enter() will abort if any priv-dropping step fails.
126 minijail_enter(jail.get());
127
Nick Kralevich11f73a12015-06-12 22:03:50 -0700128 if (root_seclabel != nullptr) {
Tom Cherry46845222015-12-11 14:28:09 -0800129 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800130 LOG(FATAL) << "Could not set SELinux context";
Dan Albert53a37442015-05-08 16:13:53 -0700131 }
132 }
Spencer Low753d4852015-07-30 23:07:55 -0700133 std::string error;
Dan Albert53a37442015-05-08 16:13:53 -0700134 std::string local_name =
135 android::base::StringPrintf("tcp:%d", server_port);
David Pursell19d0c232016-04-07 11:25:48 -0700136 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
137 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albert53a37442015-05-08 16:13:53 -0700138 }
139 }
Mike Frysinger2e114072015-12-08 18:57:47 -0500140}
141
142int 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 Hughes8b249d22016-09-23 15:40:03 -0700153 if (ALLOW_ADBD_NO_AUTH && !android::base::GetBoolProperty("ro.adb.secure", false)) {
Mike Frysinger2e114072015-12-08 18:57:47 -0500154 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 Albert53a37442015-05-08 16:13:53 -0700170
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 Hughes8b249d22016-09-23 15:40:03 -0700181 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 Albert53a37442015-05-08 16:13:53 -0700184 }
185
186 int port;
Elliott Hughes8b249d22016-09-23 15:40:03 -0700187 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700188 D("using port=%d", port);
Dan Albert53a37442015-05-08 16:13:53 -0700189 // 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 Cui815ad882015-09-02 17:44:28 -0700196 D("adbd_main(): pre init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700197 init_jdwp();
Yabin Cui815ad882015-09-02 17:44:28 -0700198 D("adbd_main(): post init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700199
Yabin Cui815ad882015-09-02 17:44:28 -0700200 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700201 fdevent_loop();
202
203 return 0;
204}
205
Dan Albert53a37442015-05-08 16:13:53 -0700206int main(int argc, char** argv) {
Dan Albert53a37442015-05-08 16:13:53 -0700207 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 Gaoab5449d2016-06-15 18:33:11 -0700240 debuggerd_init(nullptr);
Dan Albert08d552b2015-05-21 13:58:50 -0700241 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700242
Yabin Cui815ad882015-09-02 17:44:28 -0700243 D("Handling main()");
Dan Albert53a37442015-05-08 16:13:53 -0700244 return adbd_main(DEFAULT_ADB_PORT);
245}