ADB ROOT开启流程

server/2024/9/23 16:50:19/

开启adb root 选项后,执行如下代码:

packages/apps/Settings/src/com/android/settings/development/AdbRootPreferenceController.java

mADBRootService = new ADBRootService();   @Override
public boolean onPreferenceChange(Preference preference, Object newValue) {final boolean rootEnabled = (Boolean) newValue;mADBRootService.setEnabled(rootEnabled);return true;
}
ADBRootService类位于 frameworks/base/core/java/android/adb/ADBRootService.java
public void setEnabled(boolean enable) {try {final IADBRootService svc = getService();if (svc != null) {svc.setEnabled(enable);}} catch (RemoteException e) {throw e.rethrowFromSystemServer();}
}

实际调用 IADBRootService 服务的 setEnabled 

adb_root服务的入口函数位于 system/core/adb/root/main.cpp 

void ADBRootService::Register() {auto service = ndk::SharedRefBase::make<ADBRootService>();binder_status_t status = AServiceManager_addService(service->asBinder().get(), getServiceName());if (status != STATUS_OK) {LOG(FATAL) << "Could not register adbroot service: " << status;}
}ndk::ScopedAStatus ADBRootService::setEnabled(bool enabled) {uid_t uid = AIBinder_getCallingUid();if (uid != AID_SYSTEM) {return SecurityException("Caller must be system");}AutoMutex _l(lock_);if (enabled_ != enabled) {enabled_ = enabled;WriteStringToFile(std::to_string(enabled), kStoragePath + kEnabled);// Turning off adb root, restart adbd.if (!enabled) {SetProperty("service.adb.root", "0");SetProperty("ctl.restart", "adbd");}}return ndk::ScopedAStatus::ok();
}

mian函数执行时,调用 Register 将native层的 ADBRootService 注册到名为 adbroot_service 的系统服务

执行 adb root命令时, 调用  restart_root_service

void restart_root_service(unique_fd fd) {if (getuid() == 0) {WriteFdExactly(fd.get(), "adbd is already running as root\n");return;}#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_getService("adbroot_service"));std::shared_ptr<aidl::android::adbroot::IADBRootService> service =aidl::android::adbroot::IADBRootService::fromBinder(binder);if (!service) {LOG(ERROR) << "Failed to get adbroot_service interface";return;}
#endif#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)bool enabled = false;if (auto status = service->getEnabled(&enabled); !status.isOk()) {
#endifif (!__android_log_is_debuggable()) {WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n");return;}
#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)}if (!enabled) {WriteFdExactly(fd, "ADB Root access is disabled by system setting - ""enable in Settings -> System -> Developer options\n");return;}
#endifLOG(INFO) << "adbd restarting as root";android::base::SetProperty("service.adb.root", "1");WriteFdExactly(fd.get(), "restarting adbd as root\n");
}

主要作用 设置属性  service.adb.root = 1

ADB ROOT必要条件分析

开发者选项: development_settings_enabled

adb调试:adb_enabled

上述字段通过 getString 获取

1、开发者选项中adb root是否显示取决于 ro.debuggable属性:

当 ro.debuggable = 0 时, 开发者选项界面不显示开启adb root 选项

2、adb始终以root权限运行

adb 服务被 init进程拉起时,是以root权限运行,拉起后会进行降权操作。

system/core/adb/daemon/main.cpp

static bool should_drop_privileges() {// 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.bool ro_secure = android::base::GetBoolProperty("ro.secure", true);bool ro_debuggable = __android_log_is_debuggable();// Drop privileges if ro.secure is set...bool drop = ro_secure;// ... except "adb root" lets you keep privileges in a debuggable build.std::string prop = android::base::GetProperty("service.adb.root", "");bool adb_root = (prop == "1");bool adb_unroot = (prop == "0");if (ro_debuggable && adb_root) {drop = false;}// ... and "adb unroot" lets you explicitly drop privileges.if (adb_unroot) {drop = true;}return false; //不降权,始终以root权限运行
}

 当  service.adb.root 属性为 1 且 ro.debuggable = 1 时,会继续保持root权限运行


http://www.ppmy.cn/server/120879.html

相关文章

Git提交类型

说明&#xff1a;Git提交类型指的是代码commit时&#xff0c;写在comment前面的标志&#xff0c;表示此次commit的提交类型&#xff0c;如下&#xff1a; Git提交类型 常见的Git提交类型有&#xff1a; feat&#xff1a;新特性、新功能或优化&#xff1b; fix&#xff1a;修复…

react + antDesignPro 企业微信扫码登录

效果 实现步骤 1、项目中document.ejs文件引入企微js链接 注意&#xff1a;技术栈是使用的react antDesignPro&#xff0c;不同的技术栈有不同的入口文件&#xff08;如vue在html文件引入&#xff09; <script src"https://wwcdn.weixin.qq.com/node/wework/wwopen/j…

代码随想录算法day38 | 动态规划算法part11 | 1143.最长公共子序列,1035.不相交的线,53. 最大子序和,392.判断子序列

1143.最长公共子序列 体会一下本题和 718. 最长重复子数组 的区别 力扣题目链接(opens new window) 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长公共子序列的长度。 一个字符串的 子序列 是指这样一个新的字符串&#xff1a;它是由原字符串在不改变字符的…

【Webpack--006】处理字体图标资源

&#x1f913;&#x1f60d;Sam9029的CSDN博客主页:Sam9029的博客_CSDN博客-前端领域博主 &#x1f431;‍&#x1f409;若此文你认为写的不错&#xff0c;不要吝啬你的赞扬&#xff0c;求收藏&#xff0c;求评论&#xff0c;求一个大大的赞&#xff01;&#x1f44d;* &#x…

外包干了4年,技术退步太明显了。。。。。

先说一下自己的情况&#xff0c;本科生生&#xff0c;20年通过校招进入武汉某软件公司&#xff0c;干了差不多4年的功能测试&#xff0c;今年国庆&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了四年的功能…

搜维尔科技:工程师已经解决OptiTrack捕捉过程中肘部不自然的弯曲

工程师已经解决OptiTrack捕捉过程中肘部不自然的弯曲 搜维尔科技&#xff1a;工程师已经解决OptiTrack捕捉过程中肘部不自然的弯曲

【WebGIS实例】(17)下载瓦片底图并实现离线加载——以天地图为例

前言 在有些项目中&#xff0c;会有部署到无法访问互联网的内网环境中&#xff0c;这时候就会有离线部署应用和地图服务等需求了。 本博客是本着交流学习的目的&#xff0c;分享一个离线瓦片地图的获取方案&#xff0c;以天地图为案例&#xff0c;实现步骤&#xff1a; 安装…

【AI创作组】Matlab简介

1. MATLAB 简介 1.1 科学计算语言 MATLAB是一种高性能的科学计算语言,广泛应用于工程计算、控制设计、信号处理和图形显示等领域。 语言特性:MATLAB语言简洁明了,接近自然数学语言,使得用户能够轻松编写数学表达式和算法,进行高效的数值计算和符号运算。计算能力:MATLA…