From 4120ebc635b41b17a7283b5c2d0ef1e4425a169f Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 8 Dec 2015 18:57:47 -0500 Subject: [PATCH] adbd: refactor priv dropping logic Makes the code a little cleaner. Change-Id: I961b6fc8730c45d8fd4f9fd2981a6d6ab8f51fb6 --- adb/daemon/main.cpp | 58 ++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/adb/daemon/main.cpp b/adb/daemon/main.cpp index 10c5296cd..debe0de56 100644 --- a/adb/daemon/main.cpp +++ b/adb/daemon/main.cpp @@ -106,33 +106,7 @@ static bool should_drop_privileges() { #endif // ALLOW_ADBD_ROOT } -int adbd_main(int server_port) { - umask(0); - - signal(SIGPIPE, SIG_IGN); - - 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(); - - if (ALLOW_ADBD_NO_AUTH && property_get_bool("ro.adb.secure", 0) == 0) { - auth_required = false; - } - - 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"); - } - +static void drop_privileges(int server_port) { // Add extra groups: // AID_ADB to access the USB driver // AID_LOG to read system logs (adb logcat) @@ -180,6 +154,36 @@ int adbd_main(int server_port) { << error; } } +} + +int adbd_main(int server_port) { + umask(0); + + signal(SIGPIPE, SIG_IGN); + + 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(); + + if (ALLOW_ADBD_NO_AUTH && property_get_bool("ro.adb.secure", 0) == 0) { + auth_required = false; + } + + 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); bool is_usb = false; if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) {