2015-02-19 03:22:45 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG ADB
|
2015-03-19 23:21:08 +01:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2018-07-26 03:13:44 +02:00
|
|
|
#include <android/fdsan.h>
|
2015-02-19 03:22:45 +01:00
|
|
|
#include <errno.h>
|
2018-04-05 02:53:54 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <malloc.h>
|
2015-02-19 03:22:45 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-04-05 00:59:59 +02:00
|
|
|
#include <sys/capability.h>
|
2015-02-19 20:36:53 +01:00
|
|
|
#include <sys/prctl.h>
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2015-12-14 22:18:57 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/logging.h>
|
2016-07-18 19:46:42 +02:00
|
|
|
#include <android-base/macros.h>
|
2016-09-24 00:40:03 +02:00
|
|
|
#include <android-base/properties.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-12-14 22:18:57 +01:00
|
|
|
#include <libminijail.h>
|
2017-04-14 08:48:57 +02:00
|
|
|
#include <log/log_properties.h>
|
2016-07-18 19:46:42 +02:00
|
|
|
#include <scoped_minijail.h>
|
2015-12-14 22:18:57 +01:00
|
|
|
|
2016-03-29 00:52:13 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2015-12-11 23:28:09 +01:00
|
|
|
#include "selinux/android.h"
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
#include "adb.h"
|
|
|
|
#include "adb_auth.h"
|
|
|
|
#include "adb_listeners.h"
|
2015-11-21 00:37:31 +01:00
|
|
|
#include "adb_utils.h"
|
2015-05-09 01:13:53 +02:00
|
|
|
#include "transport.h"
|
2015-05-09 01:08:57 +02:00
|
|
|
|
2016-05-07 01:19:13 +02:00
|
|
|
#include "mdns.h"
|
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
static const char* root_seclabel = nullptr;
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2018-04-05 00:59:59 +02:00
|
|
|
static bool should_drop_capabilities_bounding_set() {
|
2017-08-31 08:26:47 +02:00
|
|
|
#if defined(ALLOW_ADBD_ROOT)
|
|
|
|
if (__android_log_is_debuggable()) {
|
2018-04-05 00:59:59 +02:00
|
|
|
return false;
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
2017-08-31 08:26:47 +02:00
|
|
|
#endif
|
2018-04-05 00:59:59 +02:00
|
|
|
return true;
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool should_drop_privileges() {
|
2017-08-31 08:26:47 +02:00
|
|
|
#if defined(ALLOW_ADBD_ROOT)
|
2015-02-19 20:03:26 +01:00
|
|
|
// The properties that affect `adb root` and `adb unroot` are ro.secure and
|
|
|
|
// ro.debuggable. In this context the names don't make the expected behavior
|
|
|
|
// particularly obvious.
|
|
|
|
//
|
|
|
|
// ro.debuggable:
|
|
|
|
// Allowed to become root, but not necessarily the default. Set to 1 on
|
|
|
|
// eng and userdebug builds.
|
|
|
|
//
|
|
|
|
// ro.secure:
|
|
|
|
// Drop privileges by default. Set to 1 on userdebug and user builds.
|
2016-09-24 00:40:03 +02:00
|
|
|
bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
|
2016-03-29 00:52:13 +02:00
|
|
|
bool ro_debuggable = __android_log_is_debuggable();
|
2015-02-19 20:03:26 +01:00
|
|
|
|
2015-02-19 03:22:45 +01:00
|
|
|
// Drop privileges if ro.secure is set...
|
|
|
|
bool drop = ro_secure;
|
|
|
|
|
2015-12-14 22:18:57 +01:00
|
|
|
// ... except "adb root" lets you keep privileges in a debuggable build.
|
2016-09-24 00:40:03 +02:00
|
|
|
std::string prop = android::base::GetProperty("service.adb.root", "");
|
|
|
|
bool adb_root = (prop == "1");
|
|
|
|
bool adb_unroot = (prop == "0");
|
2015-02-19 03:22:45 +01:00
|
|
|
if (ro_debuggable && adb_root) {
|
|
|
|
drop = false;
|
|
|
|
}
|
2015-12-14 22:18:57 +01:00
|
|
|
// ... and "adb unroot" lets you explicitly drop privileges.
|
2015-02-19 03:22:45 +01:00
|
|
|
if (adb_unroot) {
|
|
|
|
drop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return drop;
|
2017-08-31 08:26:47 +02:00
|
|
|
#else
|
|
|
|
return true; // "adb root" not allowed, always drop privileges.
|
|
|
|
#endif // ALLOW_ADBD_ROOT
|
2015-05-09 01:08:57 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 00:57:47 +01:00
|
|
|
static void drop_privileges(int server_port) {
|
2016-07-18 19:46:42 +02:00
|
|
|
ScopedMinijail jail(minijail_new());
|
2015-12-14 22:18:57 +01:00
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
// Add extra groups:
|
|
|
|
// AID_ADB to access the USB driver
|
|
|
|
// AID_LOG to read system logs (adb logcat)
|
|
|
|
// AID_INPUT to diagnose input issues (getevent)
|
|
|
|
// AID_INET to diagnose network issues (ping)
|
|
|
|
// AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
|
|
|
|
// AID_SDCARD_R to allow reading from the SD card
|
|
|
|
// AID_SDCARD_RW to allow writing to the SD card
|
|
|
|
// AID_NET_BW_STATS to read out qtaguid statistics
|
2015-11-08 01:52:17 +01:00
|
|
|
// AID_READPROC for reading /proc entries across UID boundaries
|
2017-05-09 00:50:55 +02:00
|
|
|
// AID_UHID for using 'hid' command to read/write to /dev/uhid
|
|
|
|
gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET,
|
|
|
|
AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
|
|
|
|
AID_NET_BW_STATS, AID_READPROC, AID_UHID};
|
2016-07-18 19:46:42 +02:00
|
|
|
minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups);
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2015-12-14 22:18:57 +01:00
|
|
|
// Don't listen on a port (default 5037) if running in secure mode.
|
|
|
|
// Don't run as root if running in secure mode.
|
2015-02-19 03:22:45 +01:00
|
|
|
if (should_drop_privileges()) {
|
2018-04-05 00:59:59 +02:00
|
|
|
const bool should_drop_caps = should_drop_capabilities_bounding_set();
|
|
|
|
|
|
|
|
if (should_drop_caps) {
|
|
|
|
minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
|
|
|
|
}
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2015-12-14 22:18:57 +01:00
|
|
|
minijail_change_gid(jail.get(), AID_SHELL);
|
|
|
|
minijail_change_uid(jail.get(), AID_SHELL);
|
|
|
|
// minijail_enter() will abort if any priv-dropping step fails.
|
|
|
|
minijail_enter(jail.get());
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2018-04-05 00:59:59 +02:00
|
|
|
// Whenever ambient capabilities are being used, minijail cannot
|
|
|
|
// simultaneously drop the bounding capability set to just
|
|
|
|
// CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
|
|
|
|
// and permitted sets. So we need to do that in two steps.
|
|
|
|
using ScopedCaps =
|
|
|
|
std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
|
|
|
|
ScopedCaps caps(cap_get_proc(), &cap_free);
|
|
|
|
if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
|
|
|
|
PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
|
|
|
|
}
|
|
|
|
if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
|
|
|
|
PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
|
|
|
|
}
|
|
|
|
if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
|
|
|
|
PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
|
|
|
|
}
|
|
|
|
if (cap_set_proc(caps.get()) != 0) {
|
|
|
|
PLOG(FATAL) << "cap_set_proc() failed";
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Local port disabled");
|
2015-02-19 03:22:45 +01:00
|
|
|
} else {
|
2015-12-14 22:18:57 +01:00
|
|
|
// minijail_enter() will abort if any priv-dropping step fails.
|
|
|
|
minijail_enter(jail.get());
|
|
|
|
|
2015-06-13 07:03:50 +02:00
|
|
|
if (root_seclabel != nullptr) {
|
2015-12-11 23:28:09 +01:00
|
|
|
if (selinux_android_setcon(root_seclabel) < 0) {
|
2015-11-11 20:33:19 +01:00
|
|
|
LOG(FATAL) << "Could not set SELinux context";
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 08:07:55 +02:00
|
|
|
std::string error;
|
2015-05-09 01:13:53 +02:00
|
|
|
std::string local_name =
|
|
|
|
android::base::StringPrintf("tcp:%d", server_port);
|
2016-04-07 20:25:48 +02:00
|
|
|
if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
|
|
|
|
LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-09 00:57:47 +01:00
|
|
|
}
|
|
|
|
|
2016-05-07 01:19:13 +02:00
|
|
|
static void setup_port(int port) {
|
|
|
|
local_init(port);
|
|
|
|
setup_mdns(port);
|
|
|
|
}
|
|
|
|
|
2015-12-09 00:57:47 +01:00
|
|
|
int adbd_main(int server_port) {
|
|
|
|
umask(0);
|
|
|
|
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2018-07-26 03:13:44 +02:00
|
|
|
auto fdsan_level = android_fdsan_get_error_level();
|
|
|
|
if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
|
|
|
|
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
|
|
|
|
}
|
|
|
|
|
2015-12-09 00:57:47 +01:00
|
|
|
init_transport_registration();
|
|
|
|
|
|
|
|
// We need to call this even if auth isn't enabled because the file
|
|
|
|
// descriptor will always be open.
|
|
|
|
adbd_cloexec_auth_socket();
|
|
|
|
|
2018-01-02 21:01:43 +01:00
|
|
|
#if defined(ALLOW_ADBD_NO_AUTH)
|
2018-03-06 22:37:45 +01:00
|
|
|
// If ro.adb.secure is unset, default to no authentication required.
|
|
|
|
auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
|
2018-01-02 21:01:43 +01:00
|
|
|
#endif
|
2015-12-09 00:57:47 +01:00
|
|
|
|
|
|
|
adbd_auth_init();
|
|
|
|
|
|
|
|
// Our external storage path may be different than apps, since
|
|
|
|
// we aren't able to bind mount after dropping root.
|
|
|
|
const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE");
|
|
|
|
if (adb_external_storage != nullptr) {
|
|
|
|
setenv("EXTERNAL_STORAGE", adb_external_storage, 1);
|
|
|
|
} else {
|
|
|
|
D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE"
|
|
|
|
" unchanged.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
drop_privileges(server_port);
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
bool is_usb = false;
|
2017-01-11 23:39:19 +01:00
|
|
|
if (access(USB_FFS_ADB_EP0, F_OK) == 0) {
|
2015-05-09 01:13:53 +02:00
|
|
|
// Listen on USB.
|
2015-02-19 03:22:45 +01:00
|
|
|
usb_init();
|
2015-05-09 01:13:53 +02:00
|
|
|
is_usb = true;
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
// If one of these properties is set, also listen on that port.
|
|
|
|
// If one of the properties isn't set and we couldn't listen on usb, listen
|
|
|
|
// on the default port.
|
2016-09-24 00:40:03 +02:00
|
|
|
std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
|
|
|
|
if (prop_port.empty()) {
|
|
|
|
prop_port = android::base::GetProperty("persist.adb.tcp.port", "");
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
2015-05-09 01:13:53 +02:00
|
|
|
|
|
|
|
int port;
|
2016-09-24 00:40:03 +02:00
|
|
|
if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("using port=%d", port);
|
2015-05-09 01:13:53 +02:00
|
|
|
// Listen on TCP port specified by service.adb.tcp.port property.
|
2016-05-07 01:19:13 +02:00
|
|
|
setup_port(port);
|
2015-05-09 01:13:53 +02:00
|
|
|
} else if (!is_usb) {
|
|
|
|
// Listen on default port.
|
2016-05-07 01:19:13 +02:00
|
|
|
setup_port(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("adbd_main(): pre init_jdwp()");
|
2015-02-19 03:22:45 +01:00
|
|
|
init_jdwp();
|
2015-09-03 02:44:28 +02:00
|
|
|
D("adbd_main(): post init_jdwp()");
|
2015-02-19 03:22:45 +01:00
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Event loop starting");
|
2015-02-19 03:22:45 +01:00
|
|
|
fdevent_loop();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-09 01:13:53 +02:00
|
|
|
int main(int argc, char** argv) {
|
2018-04-05 02:53:54 +02:00
|
|
|
// Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
|
|
|
|
mallopt(M_DECAY_TIME, 1);
|
|
|
|
|
2015-04-18 02:03:59 +02:00
|
|
|
while (true) {
|
2015-02-19 03:22:45 +01:00
|
|
|
static struct option opts[] = {
|
2015-05-06 02:46:50 +02:00
|
|
|
{"root_seclabel", required_argument, nullptr, 's'},
|
|
|
|
{"device_banner", required_argument, nullptr, 'b'},
|
|
|
|
{"version", no_argument, nullptr, 'v'},
|
2015-02-19 03:22:45 +01:00
|
|
|
};
|
2015-05-06 02:46:50 +02:00
|
|
|
|
|
|
|
int option_index = 0;
|
|
|
|
int c = getopt_long(argc, argv, "", opts, &option_index);
|
2015-05-09 01:13:53 +02:00
|
|
|
if (c == -1) {
|
2015-05-09 01:08:57 +02:00
|
|
|
break;
|
2015-05-09 01:13:53 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 03:22:45 +01:00
|
|
|
switch (c) {
|
|
|
|
case 's':
|
|
|
|
root_seclabel = optarg;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
adb_device_banner = optarg;
|
|
|
|
break;
|
2015-05-06 02:46:50 +02:00
|
|
|
case 'v':
|
2017-08-15 16:58:20 +02:00
|
|
|
printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR,
|
|
|
|
ADB_VERSION_MINOR, ADB_SERVER_VERSION);
|
2015-05-06 02:46:50 +02:00
|
|
|
return 0;
|
2015-02-19 03:22:45 +01:00
|
|
|
default:
|
2015-05-09 01:13:53 +02:00
|
|
|
// getopt already prints "adbd: invalid option -- %c" for us.
|
|
|
|
return 1;
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 02:46:50 +02:00
|
|
|
close_stdin();
|
|
|
|
|
2015-05-21 22:58:50 +02:00
|
|
|
adb_trace_init(argv);
|
2015-05-06 02:46:50 +02:00
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Handling main()");
|
2015-05-09 01:13:53 +02:00
|
|
|
return adbd_main(DEFAULT_ADB_PORT);
|
2015-02-19 03:22:45 +01:00
|
|
|
}
|