blob: 6382b67895ad92799d2cfa0523f939053b9e0081 [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>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040031#include <android-base/macros.h>
Elliott Hughesffdec182016-09-23 15:40:03 -070032#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080033#include <android-base/stringprintf.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080034#include <libminijail.h>
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040035#include <scoped_minijail.h>
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080036
Mark Salyzyn97787a02016-03-28 15:52:13 -070037#include <private/android_filesystem_config.h>
38#include <private/android_logger.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070039#include "debuggerd/handler.h"
Tom Cherry38cd57a2015-12-11 14:28:09 -080040#include "selinux/android.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070041
42#include "adb.h"
43#include "adb_auth.h"
44#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080045#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070046#include "transport.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070047
48static const char* root_seclabel = nullptr;
49
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080050static void drop_capabilities_bounding_set_if_needed(struct minijail *j) {
51#if defined(ALLOW_ADBD_ROOT)
Mark Salyzyn97787a02016-03-28 15:52:13 -070052 if (__android_log_is_debuggable()) {
Dan Albertc89e0cc2015-05-08 16:13:53 -070053 return;
54 }
55#endif
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -080056 minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
Dan Albertc89e0cc2015-05-08 16:13:53 -070057}
58
59static bool should_drop_privileges() {
60#if defined(ALLOW_ADBD_ROOT)
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.
Elliott Hughesffdec182016-09-23 15:40:03 -070071 bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
Mark Salyzyn97787a02016-03-28 15:52:13 -070072 bool ro_debuggable = __android_log_is_debuggable();
Dan Albertc89e0cc2015-05-08 16:13:53 -070073
74 // Drop privileges if ro.secure is set...
75 bool drop = ro_secure;
76
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080077 // ... except "adb root" lets you keep privileges in a debuggable build.
Elliott Hughesffdec182016-09-23 15:40:03 -070078 std::string prop = android::base::GetProperty("service.adb.root", "");
79 bool adb_root = (prop == "1");
80 bool adb_unroot = (prop == "0");
Dan Albertc89e0cc2015-05-08 16:13:53 -070081 if (ro_debuggable && adb_root) {
82 drop = false;
83 }
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080084 // ... and "adb unroot" lets you explicitly drop privileges.
Dan Albertc89e0cc2015-05-08 16:13:53 -070085 if (adb_unroot) {
86 drop = true;
87 }
88
89 return drop;
90#else
91 return true; // "adb root" not allowed, always drop privileges.
92#endif // ALLOW_ADBD_ROOT
93}
94
Mike Frysinger4120ebc2015-12-08 18:57:47 -050095static void drop_privileges(int server_port) {
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -040096 ScopedMinijail jail(minijail_new());
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -080097
Dan Albertc89e0cc2015-05-08 16:13:53 -070098 // Add extra groups:
99 // AID_ADB to access the USB driver
100 // AID_LOG to read system logs (adb logcat)
101 // AID_INPUT to diagnose input issues (getevent)
102 // AID_INET to diagnose network issues (ping)
103 // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
104 // AID_SDCARD_R to allow reading from the SD card
105 // AID_SDCARD_RW to allow writing to the SD card
106 // AID_NET_BW_STATS to read out qtaguid statistics
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800107 // AID_READPROC for reading /proc entries across UID boundaries
Dan Albertc89e0cc2015-05-08 16:13:53 -0700108 gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT,
109 AID_INET, AID_NET_BT, AID_NET_BT_ADMIN,
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800110 AID_SDCARD_R, AID_SDCARD_RW, AID_NET_BW_STATS,
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800111 AID_READPROC};
Jorge Lucangeli Obesbae15b42016-07-18 13:46:42 -0400112 minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700113
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800114 // Don't listen on a port (default 5037) if running in secure mode.
115 // Don't run as root if running in secure mode.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700116 if (should_drop_privileges()) {
Jorge Lucangeli Obes4d186ad2016-02-19 15:23:28 -0800117 drop_capabilities_bounding_set_if_needed(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700118
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800119 minijail_change_gid(jail.get(), AID_SHELL);
120 minijail_change_uid(jail.get(), AID_SHELL);
121 // minijail_enter() will abort if any priv-dropping step fails.
122 minijail_enter(jail.get());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700123
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700124 D("Local port disabled");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700125 } else {
Jorge Lucangeli Obes683dc482015-12-14 13:18:57 -0800126 // minijail_enter() will abort if any priv-dropping step fails.
127 minijail_enter(jail.get());
128
Nick Kralevich4d870952015-06-12 22:03:50 -0700129 if (root_seclabel != nullptr) {
Tom Cherry38cd57a2015-12-11 14:28:09 -0800130 if (selinux_android_setcon(root_seclabel) < 0) {
Jorge Lucangeli Obesf39c5642015-11-11 11:33:19 -0800131 LOG(FATAL) << "Could not set SELinux context";
Dan Albertc89e0cc2015-05-08 16:13:53 -0700132 }
133 }
Spencer Low5200c662015-07-30 23:07:55 -0700134 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700135 std::string local_name =
136 android::base::StringPrintf("tcp:%d", server_port);
David Purselleaae97e2016-04-07 11:25:48 -0700137 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
138 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700139 }
140 }
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500141}
142
143int adbd_main(int server_port) {
144 umask(0);
145
146 signal(SIGPIPE, SIG_IGN);
147
148 init_transport_registration();
149
150 // We need to call this even if auth isn't enabled because the file
151 // descriptor will always be open.
152 adbd_cloexec_auth_socket();
153
Elliott Hughesffdec182016-09-23 15:40:03 -0700154 if (ALLOW_ADBD_NO_AUTH && !android::base::GetBoolProperty("ro.adb.secure", false)) {
Mike Frysinger4120ebc2015-12-08 18:57:47 -0500155 auth_required = false;
156 }
157
158 adbd_auth_init();
159
160 // Our external storage path may be different than apps, since
161 // we aren't able to bind mount after dropping root.
162 const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
163 if (adb_external_storage != nullptr) {
164 setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
165 } else {
166 D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
167 " unchanged.\n");
168 }
169
170 drop_privileges(server_port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700171
172 bool is_usb = false;
Josh Gao183b73e2017-01-11 14:39:19 -0800173 if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700174 // Listen on USB.
175 usb_init();
176 is_usb = true;
177 }
178
179 // If one of these properties is set, also listen on that port.
180 // If one of the properties isn't set and we couldn't listen on usb, listen
181 // on the default port.
Elliott Hughesffdec182016-09-23 15:40:03 -0700182 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
183 if (prop_port.empty()) {
184 prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700185 }
186
187 int port;
Elliott Hughesffdec182016-09-23 15:40:03 -0700188 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700189 D("using port=%d", port);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700190 // Listen on TCP port specified by service.adb.tcp.port property.
191 local_init(port);
192 } else if (!is_usb) {
193 // Listen on default port.
194 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
195 }
196
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700197 D("adbd_main(): pre init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700198 init_jdwp();
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700199 D("adbd_main(): post init_jdwp()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700200
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700201 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700202 fdevent_loop();
203
204 return 0;
205}
206
Dan Albertc89e0cc2015-05-08 16:13:53 -0700207int main(int argc, char** argv) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700208 while (true) {
209 static struct option opts[] = {
210 {"root_seclabel", required_argument, nullptr, 's'},
211 {"device_banner", required_argument, nullptr, 'b'},
212 {"version", no_argument, nullptr, 'v'},
213 };
214
215 int option_index = 0;
216 int c = getopt_long(argc, argv, "", opts, &option_index);
217 if (c == -1) {
218 break;
219 }
220
221 switch (c) {
222 case 's':
223 root_seclabel = optarg;
224 break;
225 case 'b':
226 adb_device_banner = optarg;
227 break;
228 case 'v':
229 printf("Android Debug Bridge Daemon version %d.%d.%d %s\n",
230 ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION,
231 ADB_REVISION);
232 return 0;
233 default:
234 // getopt already prints "adbd: invalid option -- %c" for us.
235 return 1;
236 }
237 }
238
239 close_stdin();
240
Josh Gao809607a2016-06-15 18:33:11 -0700241 debuggerd_init(nullptr);
Dan Albert9313c0d2015-05-21 13:58:50 -0700242 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700243
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700244 D("Handling main()");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700245 return adbd_main(DEFAULT_ADB_PORT);
246}