blob: f4e054e3c7c2ccfa6661ad34ef59b57f5c76021d [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
28#include "base/logging.h"
29#include "base/stringprintf.h"
30#include "cutils/properties.h"
31#include "private/android_filesystem_config.h"
32#include "selinux/selinux.h"
33
34#include "adb.h"
35#include "adb_auth.h"
36#include "adb_listeners.h"
37#include "transport.h"
Dan Albert53a37442015-05-08 16:13:53 -070038
39static const char* root_seclabel = nullptr;
40
41static void drop_capabilities_bounding_set_if_needed() {
42#ifdef ALLOW_ADBD_ROOT
43 char value[PROPERTY_VALUE_MAX];
44 property_get("ro.debuggable", value, "");
45 if (strcmp(value, "1") == 0) {
46 return;
47 }
48#endif
49 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
50 if (i == CAP_SETUID || i == CAP_SETGID) {
51 // CAP_SETUID CAP_SETGID needed by /system/bin/run-as
52 continue;
53 }
54
55 int err = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
56
57 // Some kernels don't have file capabilities compiled in, and
58 // prctl(PR_CAPBSET_DROP) returns EINVAL. Don't automatically
59 // die when we see such misconfigured kernels.
60 if ((err < 0) && (errno != EINVAL)) {
61 PLOG(FATAL) << "Could not drop capabilities";
62 }
63 }
64}
65
66static bool should_drop_privileges() {
67#if defined(ALLOW_ADBD_ROOT)
68 char value[PROPERTY_VALUE_MAX];
69
Dan Albert53a37442015-05-08 16:13:53 -070070 // 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
93 // ...except "adb root" lets you keep privileges in a debuggable build.
94 if (ro_debuggable && adb_root) {
95 drop = false;
96 }
97
98 // ...and "adb unroot" lets you explicitly drop privileges.
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
109int adbd_main(int server_port) {
110 umask(0);
111
112 signal(SIGPIPE, SIG_IGN);
113
114 init_transport_registration();
115
116 // We need to call this even if auth isn't enabled because the file
117 // descriptor will always be open.
118 adbd_cloexec_auth_socket();
119
Elliott Hughesd7eb8542015-06-17 15:23:42 -0700120 if (ALLOW_ADBD_NO_AUTH && property_get_bool("ro.adb.secure", 0) == 0) {
121 auth_required = false;
Dan Albert53a37442015-05-08 16:13:53 -0700122 }
123
Elliott Hughesd7eb8542015-06-17 15:23:42 -0700124 adbd_auth_init();
125
Dan Albert53a37442015-05-08 16:13:53 -0700126 // Our external storage path may be different than apps, since
127 // we aren't able to bind mount after dropping root.
128 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
129 if (adb_external_storage != nullptr) {
130 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
131 } else {
132 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
133 " unchanged.\n");
134 }
135
136 // Add extra groups:
137 // AID_ADB to access the USB driver
138 // AID_LOG to read system logs (adb logcat)
139 // AID_INPUT to diagnose input issues (getevent)
140 // AID_INET to diagnose network issues (ping)
141 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
142 // AID_SDCARD_R to allow reading from the SD card
143 // AID_SDCARD_RW to allow writing to the SD card
144 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevich8a1b4b32015-11-07 16:52:17 -0800145 // AID_READPROC for reading /proc entries across UID boundaries
Dan Albert53a37442015-05-08 16:13:53 -0700146 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT,
147 AID_INET, AID_NET_BT, AID_NET_BT_ADMIN,
Nick Kralevich8a1b4b32015-11-07 16:52:17 -0800148 AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS,
149 AID_READPROC };
Dan Albert53a37442015-05-08 16:13:53 -0700150 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0) {
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800151 PLOG(FATAL) << "Could not set supplemental groups";
Dan Albert53a37442015-05-08 16:13:53 -0700152 }
153
154 /* don't listen on a port (default 5037) if running in secure mode */
155 /* don't run as root if we are running in secure mode */
156 if (should_drop_privileges()) {
157 drop_capabilities_bounding_set_if_needed();
158
159 /* then switch user and group to "shell" */
160 if (setgid(AID_SHELL) != 0) {
161 PLOG(FATAL) << "Could not setgid";
162 }
163 if (setuid(AID_SHELL) != 0) {
164 PLOG(FATAL) << "Could not setuid";
165 }
166
Yabin Cui815ad882015-09-02 17:44:28 -0700167 D("Local port disabled");
Dan Albert53a37442015-05-08 16:13:53 -0700168 } else {
Nick Kralevich11f73a12015-06-12 22:03:50 -0700169 if (root_seclabel != nullptr) {
Dan Albert53a37442015-05-08 16:13:53 -0700170 if (setcon(root_seclabel) < 0) {
Jorge Lucangeli Obes116b8b92015-11-11 11:33:19 -0800171 LOG(FATAL) << "Could not set SELinux context";
Dan Albert53a37442015-05-08 16:13:53 -0700172 }
173 }
Spencer Low753d4852015-07-30 23:07:55 -0700174 std::string error;
Dan Albert53a37442015-05-08 16:13:53 -0700175 std::string local_name =
176 android::base::StringPrintf("tcp:%d", server_port);
Spencer Low753d4852015-07-30 23:07:55 -0700177 if (install_listener(local_name, "*smartsocket*", nullptr, 0,
178 &error)) {
179 LOG(FATAL) << "Could not install *smartsocket* listener: "
180 << error;
Dan Albert53a37442015-05-08 16:13:53 -0700181 }
182 }
183
184 bool is_usb = false;
185 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
186 // Listen on USB.
187 usb_init();
188 is_usb = true;
189 }
190
191 // If one of these properties is set, also listen on that port.
192 // If one of the properties isn't set and we couldn't listen on usb, listen
193 // on the default port.
194 char prop_port[PROPERTY_VALUE_MAX];
195 property_get("service.adb.tcp.port", prop_port, "");
196 if (prop_port[0] == '\0') {
197 property_get("persist.adb.tcp.port", prop_port, "");
198 }
199
200 int port;
201 if (sscanf(prop_port, "%d", &port) == 1 && port > 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700202 D("using port=%d", port);
Dan Albert53a37442015-05-08 16:13:53 -0700203 // Listen on TCP port specified by service.adb.tcp.port property.
204 local_init(port);
205 } else if (!is_usb) {
206 // Listen on default port.
207 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
208 }
209
Yabin Cui815ad882015-09-02 17:44:28 -0700210 D("adbd_main(): pre init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700211 init_jdwp();
Yabin Cui815ad882015-09-02 17:44:28 -0700212 D("adbd_main(): post init_jdwp()");
Dan Albert53a37442015-05-08 16:13:53 -0700213
Yabin Cui815ad882015-09-02 17:44:28 -0700214 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700215 fdevent_loop();
216
217 return 0;
218}
219
220static void close_stdin() {
221 int fd = unix_open("/dev/null", O_RDONLY);
222 if (fd == -1) {
223 perror("failed to open /dev/null, stdin will remain open");
224 return;
225 }
226 dup2(fd, STDIN_FILENO);
Spencer Low3a2421b2015-05-22 20:09:06 -0700227 unix_close(fd);
Dan Albert53a37442015-05-08 16:13:53 -0700228}
229
230int main(int argc, char** argv) {
Dan Albert53a37442015-05-08 16:13:53 -0700231 while (true) {
232 static struct option opts[] = {
233 {"root_seclabel", required_argument, nullptr, 's'},
234 {"device_banner", required_argument, nullptr, 'b'},
235 {"version", no_argument, nullptr, 'v'},
236 };
237
238 int option_index = 0;
239 int c = getopt_long(argc, argv, "", opts, &option_index);
240 if (c == -1) {
241 break;
242 }
243
244 switch (c) {
245 case 's':
246 root_seclabel = optarg;
247 break;
248 case 'b':
249 adb_device_banner = optarg;
250 break;
251 case 'v':
252 printf("Android Debug Bridge Daemon version %d.%d.%d %s\n",
253 ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
254 ADB_REVISION);
255 return 0;
256 default:
257 // getopt already prints "adbd: invalid option -- %c" for us.
258 return 1;
259 }
260 }
261
262 close_stdin();
263
Dan Albert08d552b2015-05-21 13:58:50 -0700264 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700265
Yabin Cui815ad882015-09-02 17:44:28 -0700266 D("Handling main()");
Dan Albert53a37442015-05-08 16:13:53 -0700267 return adbd_main(DEFAULT_ADB_PORT);
268}