blob: 7f40b96b2b438251a4619fdc891581b70992f847 [file] [log] [blame]
Dan Albertc89e0cc2015-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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albertc89e0cc2015-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 Obes683dc482015-12-14 13:18:57 -080028#include <memory>
29
Elliott Hughes4f713192015-12-04 22:00:26 -080030#include <android-base/logging.h>
31#include <android-base/stringprintf.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080032#include <libminijail.h>
33
Dan Albertc89e0cc2015-05-08 16:13:53 -070034#include "cutils/properties.h"
35#include "private/android_filesystem_config.h"
Tom Cherry38cd57a2015-12-11 14:28:09 -080036#include "selinux/android.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070037
38#include "adb.h"
39#include "adb_auth.h"
40#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080041#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070042#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070043
44static const char* root_seclabel = nullptr;
45
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080046static void drop_capabilities_bounding_set_if_needed(struct minijail *j) {
47#if defined(ALLOW_ADBD_ROOT)
Dan Albertc89e0cc2015-05-08 16:13:53 -070048 char value[PROPERTY_VALUE_MAX];
49 property_get("ro.debuggable", value, "");
50 if (strcmp(value, "1") == 0) {
51 return;
52 }
53#endif
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080054 minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
Dan Albertc89e0cc2015-05-08 16:13:53 -070055}
56
57static bool should_drop_privileges() {
58#if defined(ALLOW_ADBD_ROOT)
59 char value[PROPERTY_VALUE_MAX];
60
Dan Albertc89e0cc2015-05-08 16:13:53 -070061 // The properties that affect `adb root` and `adb unroot` are ro.secure and
62 // ro.debuggable. In this context the names don't make the expected behavior
63 // particularly obvious.
64 //
65 // ro.debuggable:
66 // Allowed to become root, but not necessarily the default. Set to 1 on
67 // eng and userdebug builds.
68 //
69 // ro.secure:
70 // Drop privileges by default. Set to 1 on userdebug and user builds.
71 property_get("ro.secure", value, "1");
72 bool ro_secure = (strcmp(value, "1") == 0);
73
74 property_get("ro.debuggable", value, "");
75 bool ro_debuggable = (strcmp(value, "1") == 0);
76
77 // Drop privileges if ro.secure is set...
78 bool drop = ro_secure;
79
80 property_get("service.adb.root", value, "");
81 bool adb_root = (strcmp(value, "1") == 0);
82 bool adb_unroot = (strcmp(value, "0") == 0);
83
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080084 // ... except "adb root" lets you keep privileges in a debuggable build.
Dan Albertc89e0cc2015-05-08 16:13:53 -070085 if (ro_debuggable && adb_root) {
86 drop = false;
87 }
88
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080089 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -070090 if (adb_unroot) {
91 drop = true;
92 }
93
94 return drop;
95#else
96 return true; // "adb root" not allowed, always drop privileges.
97#endif // ALLOW_ADBD_ROOT
98}
99
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500100static void drop_privileges(int server_port) {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800101 std::unique_ptr<minijail, void (*)(minijail*)> jail(minijail_new(),
102 &minijail_destroy);
103
Dan Albertc89e0cc2015-05-08 16:13:53 -0700104 // Add extra groups:
105 // AID_ADB to access the USB driver
106 // AID_LOG to read system logs (adb logcat)
107 // AID_INPUT to diagnose input issues (getevent)
108 // AID_INET to diagnose network issues (ping)
109 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
110 // AID_SDCARD_R to allow reading from the SD card
111 // AID_SDCARD_RW to allow writing to the SD card
112 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800113 // AID_READPROC for reading /proc entries across UID boundaries
Dan Albertc89e0cc2015-05-08 16:13:53 -0700114 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT,
115 AID_INET, AID_NET_BT, AID_NET_BT_ADMIN,
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800116 AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS,
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800117 AID_READPROC};
Jorge Lucangeli Obesfd79a5d2016-01-08 10:55:09 -0800118 minijail_set_supplementary_gids(jail.get(),
119 sizeof(groups) / sizeof(groups[0]),
120 groups);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700121
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800122 // Don't listen on a port (default 5037) if running in secure mode.
123 // Don't run as root if running in secure mode.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700124 if (should_drop_privileges()) {
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -0800125 drop_capabilities_bounding_set_if_needed(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700126
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800127 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 Albertc89e0cc2015-05-08 16:13:53 -0700131
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700132 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700133 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800134 // minijail_enter() will abort if any priv-dropping step fails.
135 minijail_enter(jail.get());
136
Nick Kralevich4d870952015-06-12 22:03:50 -0700137 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800138 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800139 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700140 }
141 }
Spencer Low5200c662015-07-30 23:07:55 -0700142 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700143 std::string local_name =
144 android::base::StringPrintf("tcp:%d", server_port);
Spencer Low5200c662015-07-30 23:07:55 -0700145 if (install_listener(local_name, "*smartsocket*", nullptr, 0,
146 &error)) {
147 LOG(FATAL) << "Could not install *smartsocket* listener: "
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800148 << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700149 }
150 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500151}
152
153int adbd_main(int server_port) {
154 umask(0);
155
156 signal(SIGPIPE, SIG_IGN);
157
158 init_transport_registration();
159
160 // We need to call this even if auth isn't enabled because the file
161 // descriptor will always be open.
162 adbd_cloexec_auth_socket();
163
164 if (ALLOW_ADBD_NO_AUTH && property_get_bool("ro.adb.secure", 0) == 0) {
165 auth_required = false;
166 }
167
168 adbd_auth_init();
169
170 // Our external storage path may be different than apps, since
171 // we aren't able to bind mount after dropping root.
172 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
173 if (adb_external_storage != nullptr) {
174 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
175 } else {
176 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
177 " unchanged.\n");
178 }
179
180 drop_privileges(server_port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700181
182 bool is_usb = false;
183 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
184 // Listen on USB.
185 usb_init();
186 is_usb = true;
187 }
188
189 // If one of these properties is set, also listen on that port.
190 // If one of the properties isn't set and we couldn't listen on usb, listen
191 // on the default port.
192 char prop_port[PROPERTY_VALUE_MAX];
193 property_get("service.adb.tcp.port", prop_port, "");
194 if (prop_port[0] == '\0') {
195 property_get("persist.adb.tcp.port", prop_port, "");
196 }
197
198 int port;
199 if (sscanf(prop_port, "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700200 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700201 // Listen on TCP port specified by service.adb.tcp.port property.
202 local_init(port);
203 } else if (!is_usb) {
204 // Listen on default port.
205 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
206 }
207
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700208 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700209 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700210 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700211
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700212 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700213 fdevent_loop();
214
215 return 0;
216}
217
Dan Albertc89e0cc2015-05-08 16:13:53 -0700218int main(int argc, char** argv) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700219 while (true) {
220 static struct option opts[] = {
221 {"root_seclabel", required_argument, nullptr, 's'},
222 {"device_banner", required_argument, nullptr, 'b'},
223 {"version", no_argument, nullptr, 'v'},
224 };
225
226 int option_index = 0;
227 int c = getopt_long(argc, argv, "", opts, &option_index);
228 if (c == -1) {
229 break;
230 }
231
232 switch (c) {
233 case 's':
234 root_seclabel = optarg;
235 break;
236 case 'b':
237 adb_device_banner = optarg;
238 break;
239 case 'v':
240 printf("Android Debug Bridge Daemon version %d.%d.%d %s\n",
241 ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
242 ADB_REVISION);
243 return 0;
244 default:
245 // getopt already prints "adbd: invalid option -- %c" for us.
246 return 1;
247 }
248 }
249
250 close_stdin();
251
Dan Albert9313c0d2015-05-21 13:58:50 -0700252 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700253
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700254 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700255 return adbd_main(DEFAULT_ADB_PORT);
256}