From bd9206bc7399b22d3917c737bb75f8534fa77e35 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Thu, 19 Jan 2012 10:18:59 -0800 Subject: [PATCH] Move permission checking code to it's own function. Change-Id: Ibe46ca3043791f3b3815080f9e23ac307b1f5900 --- adb/adb.c | 54 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/adb/adb.c b/adb/adb.c index 6dbd562bc..ff9e92d0e 100644 --- a/adb/adb.c +++ b/adb/adb.c @@ -842,10 +842,39 @@ void build_local_name(char* target_str, size_t target_size, int server_port) snprintf(target_str, target_size, "tcp:%d", server_port); } +#if !ADB_HOST +static int should_drop_privileges() { + int secure = 0; + char value[PROPERTY_VALUE_MAX]; + + /* run adbd in secure mode if ro.secure is set and + ** we are not in the emulator + */ + property_get("ro.kernel.qemu", value, ""); + if (strcmp(value, "1") != 0) { + property_get("ro.secure", value, "1"); + if (strcmp(value, "1") == 0) { + // don't run as root if ro.secure is set... + secure = 1; + + // ... except we allow running as root in userdebug builds if the + // service.adb.root property has been set by the "adb root" command + property_get("ro.debuggable", value, ""); + if (strcmp(value, "1") == 0) { + property_get("service.adb.root", value, ""); + if (strcmp(value, "1") == 0) { + secure = 0; + } + } + } + } + return secure; +} +#endif /* !ADB_HOST */ + int adb_main(int is_daemon, int server_port) { #if !ADB_HOST - int secure = 0; int port; char value[PROPERTY_VALUE_MAX]; #endif @@ -873,31 +902,10 @@ int adb_main(int is_daemon, int server_port) exit(1); } #else - /* run adbd in secure mode if ro.secure is set and - ** we are not in the emulator - */ - property_get("ro.kernel.qemu", value, ""); - if (strcmp(value, "1") != 0) { - property_get("ro.secure", value, "1"); - if (strcmp(value, "1") == 0) { - // don't run as root if ro.secure is set... - secure = 1; - - // ... except we allow running as root in userdebug builds if the - // service.adb.root property has been set by the "adb root" command - property_get("ro.debuggable", value, ""); - if (strcmp(value, "1") == 0) { - property_get("service.adb.root", value, ""); - if (strcmp(value, "1") == 0) { - secure = 0; - } - } - } - } /* don't listen on a port (default 5037) if running in secure mode */ /* don't run as root if we are running in secure mode */ - if (secure) { + if (should_drop_privileges()) { struct __user_cap_header_struct header; struct __user_cap_data_struct cap;