• Android 11编译第三弹 ADB开启ROOT权限


    一、为什么需要adb root权限

    问题:Relese版本,默认adb访问会降级到shell权限,一些敏感操作不能进行,远程调试比较麻烦。且Release版本没有su模块,不能切换Root用户。

    开启adb调试以后,默认进入adb是system权限,不能切换到root(因为Release没有集成su).

    有两种方式切换Root:

    1) Release也集成su模块

    2)默认Release版本adb 开启Root权限

    二、开启adb ROOT权限

    开启Root权限

    ro.secure表示root权限,要开启Root权限,系统配置ro.secure=0 开启ROOT权限

    2.1 编译时默认开启ROOT权限

    build/make/core/main.mk

    1. ifneq (,$(user_variant))
    2. # ==== modify begin ====
    3. # fix: zhouronghua default as root
    4. # Target is secure in user builds.
    5. ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0
    6. # ==== modify end ====
    7. ADDITIONAL_DEFAULT_PROPERTIES += security.perf_harden=1
    8. ifeq ($(user_variant),user)
    9. # ==== modify begin ==== fix: default as root
    10. ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=0
    11. # ==== modify end ====
    12. endif

    user版本就是Releae版本,userdebug版本就是debug版本。

    2.2 Zygote关闭权限降级

    frameworks/base/core/jni/com_android_internal_os_Zygote.cpp

    1. static void DropCapabilitiesBoundingSet(fail_fn_t fail_fn) {
    2. // ==== modify begin ==== zhouronghua
    3. #if 0
    4. for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {;
    5. if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) == -1) {
    6. if (errno == EINVAL) {
    7. ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
    8. "your kernel is compiled with file capabilities support");
    9. } else {
    10. fail_fn(CREATE_ERROR("prctl(PR_CAPBSET_DROP, %d) failed: %s", i, strerror(errno)));
    11. }
    12. }
    13. }
    14. #endif
    15. // ==== modify end ====
    16. }

    2.3 Android.bp允许暴力修改selinux权限

    system/core/init/Android.bp

    -DALLOW_PERMISSIVE_SELINUX=0  修改为 -DALLOW_PERMISSIVE_SELINUX=1

    1. cc_defaults {
    2. name: "init_defaults",
    3. cpp_std: "experimental",
    4. sanitize: {
    5. misc_undefined: ["signed-integer-overflow"],
    6. },
    7. cflags: [
    8. "-DLOG_UEVENTS=0",
    9. "-Wall",
    10. "-Wextra",
    11. "-Wno-unused-parameter",
    12. "-Werror",
    13. "-Wthread-safety",
    14. "-DALLOW_FIRST_STAGE_CONSOLE=0",
    15. "-DALLOW_LOCAL_PROP_OVERRIDE=0",
    16. "-DALLOW_PERMISSIVE_SELINUX=1",
    17. "-DREBOOT_BOOTLOADER_ON_PANIC=0",
    18. "-DWORLD_WRITABLE_KMSG=0",
    19. "-DDUMP_ON_UMOUNT_FAILURE=0",

    2.4 init程序允许暴力修改selinux权限

    system/core/init/Android.mk

    1. ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
    2. init_options += \
    3. -DALLOW_FIRST_STAGE_CONSOLE=1 \
    4. -DALLOW_LOCAL_PROP_OVERRIDE=1 \
    5. -DALLOW_PERMISSIVE_SELINUX=1 \
    6. -DREBOOT_BOOTLOADER_ON_PANIC=1 \
    7. -DWORLD_WRITABLE_KMSG=1 \
    8. -DDUMP_ON_UMOUNT_FAILURE=1
    9. else
    10. # ==== modify begin ==== zhouronghua allow permissive
    11. init_options += \
    12. -DALLOW_FIRST_STAGE_CONSOLE=0 \
    13. -DALLOW_LOCAL_PROP_OVERRIDE=0 \
    14. -DALLOW_PERMISSIVE_SELINUX=1 \
    15. -DREBOOT_BOOTLOADER_ON_PANIC=0 \
    16. -DWORLD_WRITABLE_KMSG=0 \
    17. -DDUMP_ON_UMOUNT_FAILURE=0
    18. # ==== modify end ====
    19. endif

    2.5 su程序权限提级

    system/core/libcutils/fs_config.cpp

    1. // the following two files are INTENTIONALLY set-uid, but they
    2. // are NOT included on user builds.
    3. { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
    4. // ==== modify begin ==== zhouronghua su right improve
    5. { 06755, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },

    2.6 修改su程序权限

    system/core/rootdir/init.rc

    1. chown system system /sys/devices/system/cpu/cpufreq/interactive/io_is_busy
    2. chmod 0660 /sys/devices/system/cpu/cpufreq/interactive/io_is_busy
    3. # ==== modify begin ==== zhouronghua su right
    4. chmod 6755 /system/xbin/su
    5. # ==== modify end ====

    2.7 su程序构建

    system/extras/su/Android.mk

    1. LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
    2. # ==== modify begin ==== zhouronghua su as common module
    3. LOCAL_MODULE_TAGS := optional
    4. # ==== modify end ====

    2.8 su程序去掉Root用户检测

    system/extras/su/su.cpp

    1. int main(int argc, char** argv) {
    2. // ==== modify begin ==== zhouronghua delete root shell check
    3. #if 0
    4. uid_t current_uid = getuid();
    5. if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");
    6. #endif
    7. // ==== modify end ====

    2.9 关闭selinux.cpp强制安全检测

    system/core/init/selinux.cpp

    1. bool IsEnforcing() {
    2. // ==== modify start ==== zhouronghua 不需要强制安全检测
    3. return false;
    4. // ==== modify end
    5. if (ALLOW_PERMISSIVE_SELINUX) {
    6. return StatusFromCmdline() == SELINUX_ENFORCING;
    7. }
    8. return true;
    9. }

    2.10 adb不降级采用ROOT访问

    adbd启动时检查属性,决定是否进行权限降级到AID_SHELL

    system/core/adb/daemon/main.cpp

    1. static bool should_drop_privileges() {
    2. // ==== modify begin ====
    3. // fix: zhouronghua "adb root" not allowed, always drop privileges.
    4. if (!ALLOW_ADBD_ROOT && !is_device_unlocked()) return false;
    5. // ==== modifu end ====

    adb Root权限访问不需要降级。 

    2.11 安卓内核默认开启selLinux

    kernel/configs/o-mr1/android-3.18/android-base.config

    kernel/configs/o-mr1/android-4.4/android-base.config

    kernel/configs/o-mr1/android-4.9/android-base.config

    kernel/configs/o/android-3.18/android-base.config

    kernel/configs/o/android-3.18/android-base.config

    kernel/configs/o/android-4.4/android-base.config

    kernel/configs/o/android-4.9/android-base.config

    kernel/configs/p/android-4.14/android-base.config

    kernel/configs/p/android-4.4/android-base.config

    kernel/configs/p/android-4.9/android-base.config

    kernel/configs/q/android-4.14/android-base.config

    kernel/configs/q/android-4.19/android-base.config

    kernel/configs/q/android-4.9/android-base.config

    kernel/configs/r/android-4.14/android-base.config

    kernel/configs/r/android-4.19/android-base.config

    kernel/configs/r/android-5.4/android-base.config

    1. CONFIG_XFRM_USER=y
    2. # ==== modify begin ==== zhouronghua selinux
    3. CONFIG_SECURITY_SELINUX_DEVELOP=y
    4. # # ==== modify end ====

  • 相关阅读:
    13000 行代码、19 大技术,这位 16 岁高中生用 C++ 从头到尾构建了一个机器学习库
    Kafka一个节点挂掉,导致服务不可消费
    使用人工智能聊天机器人时要注意这些!(配提问技巧)
    基于Python实现的决策树
    如何将数据显示到UI上?
    vue 中 asstes 和 static 有什么联系与区别
    全新自适应导航网模板 导航网系统源码 网址导航系统源码 网址目录网系统源码
    过拟合与过拟合的经典例子
    干货丨产品的可行性分析要从哪几个方面入手?
    IDEA翻译插件Translation报错 -> 更新 TKK 失败,请检查网络连接问题,已解决
  • 原文地址:https://blog.csdn.net/joedan0104/article/details/132650597