blob: b0a0162b0a02696ea6c17dcbe9b26fb913f18f25 [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"
Josh Gao809607a2016-06-15 18:33:11 -070035#include "debuggerd/client.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070036#include "private/android_filesystem_config.h"
Tom Cherry38cd57a2015-12-11 14:28:09 -080037#include "selinux/android.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070038
39#include "adb.h"
40#include "adb_auth.h"
41#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080042#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070043#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070044
45static const char* root_seclabel = nullptr;
46
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080047static void drop_capabilities_bounding_set_if_needed(struct minijail *j) {
48#if defined(ALLOW_ADBD_ROOT)
Dan Albertc89e0cc2015-05-08 16:13:53 -070049 char value[PROPERTY_VALUE_MAX];
50 property_get("ro.debuggable", value, "");
51 if (strcmp(value, "1") == 0) {
52 return;
53 }
54#endif
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080055 minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
Dan Albertc89e0cc2015-05-08 16:13:53 -070056}
57
58static bool should_drop_privileges() {
59#if defined(ALLOW_ADBD_ROOT)
60 char value[PROPERTY_VALUE_MAX];
61
Dan Albertc89e0cc2015-05-08 16:13:53 -070062 // The properties that affect `adb root` and `adb unroot` are ro.secure and
63 // ro.debuggable. In this context the names don't make the expected behavior
64 // particularly obvious.
65 //
66 // ro.debuggable:
67 // Allowed to become root, but not necessarily the default. Set to 1 on
68 // eng and userdebug builds.
69 //
70 // ro.secure:
71 // Drop privileges by default. Set to 1 on userdebug and user builds.
72 property_get("ro.secure", value, "1");
73 bool ro_secure = (strcmp(value, "1") == 0);
74
75 property_get("ro.debuggable", value, "");
76 bool ro_debuggable = (strcmp(value, "1") == 0);
77
78 // Drop privileges if ro.secure is set...
79 bool drop = ro_secure;
80
81 property_get("service.adb.root", value, "");
82 bool adb_root = (strcmp(value, "1") == 0);
83 bool adb_unroot = (strcmp(value, "0") == 0);
84
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080085 // ... except "adb root" lets you keep privileges in a debuggable build.
Dan Albertc89e0cc2015-05-08 16:13:53 -070086 if (ro_debuggable && adb_root) {
87 drop = false;
88 }
89
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080090 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -070091 if (adb_unroot) {
92 drop = true;
93 }
94
95 return drop;
96#else
97 return true; // "adb root" not allowed, always drop privileges.
98#endif // ALLOW_ADBD_ROOT
99}
100
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500101static void drop_privileges(int server_port) {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800102 std::unique_ptr<minijail, void (*)(minijail*)> jail(minijail_new(),
103 &minijail_destroy);
104
Dan Albertc89e0cc2015-05-08 16:13:53 -0700105 // Add extra groups:
106 // AID_ADB to access the USB driver
107 // AID_LOG to read system logs (adb logcat)
108 // AID_INPUT to diagnose input issues (getevent)
109 // AID_INET to diagnose network issues (ping)
110 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
111 // AID_SDCARD_R to allow reading from the SD card
112 // AID_SDCARD_RW to allow writing to the SD card
113 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800114 // AID_READPROC for reading /proc entries across UID boundaries
Dan Albertc89e0cc2015-05-08 16:13:53 -0700115 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT,
116 AID_INET, AID_NET_BT, AID_NET_BT_ADMIN,
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800117 AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS,
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800118 AID_READPROC};
Jorge Lucangeli Obesfd79a5d2016-01-08 10:55:09 -0800119 minijail_set_supplementary_gids(jail.get(),
120 sizeof(groups) / sizeof(groups[0]),
121 groups);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700122
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800123 // Don't listen on a port (default 5037) if running in secure mode.
124 // Don't run as root if running in secure mode.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700125 if (should_drop_privileges()) {
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -0800126 drop_capabilities_bounding_set_if_needed(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700127
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800128 minijail_change_gid(jail.get(), AID_SHELL);
129 minijail_change_uid(jail.get(), AID_SHELL);
130 // minijail_enter() will abort if any priv-dropping step fails.
131 minijail_enter(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700132
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700133 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700134 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800135 // minijail_enter() will abort if any priv-dropping step fails.
136 minijail_enter(jail.get());
137
Nick Kralevich4d870952015-06-12 22:03:50 -0700138 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800139 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800140 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700141 }
142 }
Spencer Low5200c662015-07-30 23:07:55 -0700143 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700144 std::string local_name =
145 android::base::StringPrintf("tcp:%d", server_port);
David Purselleaae97e2016-04-07 11:25:48 -0700146 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
147 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700148 }
149 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500150}
151
152int adbd_main(int server_port) {
153 umask(0);
154
155 signal(SIGPIPE, SIG_IGN);
156
157 init_transport_registration();
158
159 // We need to call this even if auth isn't enabled because the file
160 // descriptor will always be open.
161 adbd_cloexec_auth_socket();
162
163 if (ALLOW_ADBD_NO_AUTH && property_get_bool("ro.adb.secure", 0) == 0) {
164 auth_required = false;
165 }
166
167 adbd_auth_init();
168
169 // Our external storage path may be different than apps, since
170 // we aren't able to bind mount after dropping root.
171 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
172 if (adb_external_storage != nullptr) {
173 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
174 } else {
175 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
176 " unchanged.\n");
177 }
178
179 drop_privileges(server_port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700180
181 bool is_usb = false;
182 if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {
183 // Listen on USB.
184 usb_init();
185 is_usb = true;
186 }
187
188 // If one of these properties is set, also listen on that port.
189 // If one of the properties isn't set and we couldn't listen on usb, listen
190 // on the default port.
191 char prop_port[PROPERTY_VALUE_MAX];
192 property_get("service.adb.tcp.port", prop_port, "");
193 if (prop_port[0] == '\0') {
194 property_get("persist.adb.tcp.port", prop_port, "");
195 }
196
197 int port;
198 if (sscanf(prop_port, "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700199 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700200 // Listen on TCP port specified by service.adb.tcp.port property.
201 local_init(port);
202 } else if (!is_usb) {
203 // Listen on default port.
204 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
205 }
206
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700207 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700208 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700209 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700210
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700211 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700212 fdevent_loop();
213
214 return 0;
215}
216
Dan Albertc89e0cc2015-05-08 16:13:53 -0700217int main(int argc, char** argv) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700218 while (true) {
219 static struct option opts[] = {
220 {"root_seclabel", required_argument, nullptr, 's'},
221 {"device_banner", required_argument, nullptr, 'b'},
222 {"version", no_argument, nullptr, 'v'},
223 };
224
225 int option_index = 0;
226 int c = getopt_long(argc, argv, "", opts, &option_index);
227 if (c == -1) {
228 break;
229 }
230
231 switch (c) {
232 case 's':
233 root_seclabel = optarg;
234 break;
235 case 'b':
236 adb_device_banner = optarg;
237 break;
238 case 'v':
239 printf("Android Debug Bridge Daemon version %d.%d.%d %s\n",
240 ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
241 ADB_REVISION);
242 return 0;
243 default:
244 // getopt already prints "adbd: invalid option -- %c" for us.
245 return 1;
246 }
247 }
248
249 close_stdin();
250
Josh Gao809607a2016-06-15 18:33:11 -0700251 debuggerd_init(nullptr);
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}