Android Framework 常见解决方案(26)user版本可root方案

112 篇文章 81 订阅

1 原理说明

User版本默认是没有root权限和remount功能的,一般该方法用于调试性能相关问题。如果使用debug版本对照,差异过大,因此就有了这样的需求。

修改的核心原理就是调整adbd及相关属性中的一些判定,即user和debug版本的区别点入手。另外就是user版本中本身是没有remount的,需要单独添加才行。同时也需要修改should_drop_privileges的返回值以防止降低adbd进程的权限等限制。

2 修改方案(Android S)

2.1 在packages/modules下的修改

在packages/modules/adb/Android.bp文件中修改:

//...
cc_binary {
    name: "adbd",
    defaults: ["adbd_defaults", "host_adbd_supported", "libadbd_binary_dependencies"],
    recovery_available: true,
    apex_available: ["com.android.adbd"],

    srcs: [
        "daemon/main.cpp",
    ],

    cflags: [
        "-D_GNU_SOURCE",
        "-Wno-deprecated-declarations",
    ],

    strip: {
        keep_symbols: true,
    },

    static_libs: [
        "libadbd",
        "libadbd_services",
        "libasyncio",
        "libcap",
        "liblz4",
        "libminijail",
        "libssl",
    ],

    shared_libs: [
        "libadb_protos",
        "libadbd_auth",
    ],

    target: {
        recovery: {
            exclude_shared_libs: [
                "libadb_pairing_auth",
                "libadb_pairing_connection",
            ],
        }
    },
+    required: [
+        "libadbd_auth",
+        "libadbd_fs",
+        "remount",
+    ],
}
//...

在packages/modules/adb/daemon/main.cpp文件中修改:

//...
//should_drop_privileges直接返回false,目的是防止因此降低adbd进程的权限
static bool should_drop_privileges() {
+    return false;
    //...
}
//...
int adbd_main(int server_port) {
    umask(0);

    signal(SIGPIPE, SIG_IGN);

#if defined(__BIONIC__)
    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);
    }
#endif

    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 defined(__ANDROID__)
    // If we're on userdebug/eng or the device is unlocked, permit no-authentication.
-    bool device_unlocked = "orange" == android::base::GetProperty("ro.boot.verifiedbootstate", "");
+      bool device_unlocked = true;
    if (__android_log_is_debuggable() || device_unlocked) {
-      auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
+      auth_required = false;
    }
#endif
//...

至此,package下的修改就结束了。接下来是system部分的修改。

2.2 system下的修改

在system/core/fs_mgr/Android.bp文件中修改:

//...
cc_defaults {
    name: "libfs_mgr_defaults",
    defaults: ["fs_mgr_defaults"],
    export_include_dirs: ["include"],
    include_dirs: ["system/vold"],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
    ],
    srcs: [
        "blockdev.cpp",
        "file_wait.cpp",
        "fs_mgr.cpp",
        "fs_mgr_format.cpp",
        "fs_mgr_verity.cpp",
        "fs_mgr_dm_linear.cpp",
        "fs_mgr_overlayfs.cpp",
        "fs_mgr_roots.cpp",
        "fs_mgr_vendor_overlay.cpp",
        ":libfiemap_srcs",
    ],
    shared_libs: [
        "libbase",
        "libcrypto",
        "libcrypto_utils",
        "libcutils",
        "libext4_utils",
        "libfec",
        "liblog",
        "liblp",
        "libselinux",
    ],
    static_libs: [
        "libavb",
        "libfs_avb",
        "libfstab",
        "libdm",
        "libgsi",
    ],
    export_static_lib_headers: [
        "libfs_avb",
        "libfstab",
        "libdm",
    ],
    export_shared_lib_headers: [
      "liblp",
    ],
    whole_static_libs: [
        "liblogwrap",
        "libdm",
        "libext2_uuid",
        "libfscrypt",
        "libfstab",
    ],
    cppflags: [
-        "-DALLOW_ADBD_DISABLE_VERITY=0",
+        "-UALLOW_ADBD_DISABLE_VERITY",
+        "-DALLOW_ADBD_DISABLE_VERITY=1",
    ],
-    product_variables: {
-        debuggable: {
-            cppflags: [
-                "-UALLOW_ADBD_DISABLE_VERITY",
-                "-DALLOW_ADBD_DISABLE_VERITY=1",
-            ],
-        },
-    },
    header_libs: [
        "libfiemap_headers",
        "libstorage_literals_headers",
    ],
    export_header_lib_headers: [
        "libfiemap_headers",
    ],
    required: [
        "e2freefrag",
        "e2fsdroid",
    ],
//...
cc_binary {
    name: "remount",
    defaults: ["fs_mgr_defaults"],
    static_libs: [
        "libavb_user",
        "libgsid",
        "libutils",
        "libvold_binder",
    ],
    shared_libs: [
        "libbootloader_message",
        "libbase",
        "libbinder",
        "libcutils",
        "libcrypto",
        "libext4_utils",
        "libfec",
        "libfs_mgr_binder",
        "liblog",
        "liblp",
        "libselinux",
    ],
    header_libs: [
        "libcutils_headers",
    ],
    srcs: [
        "fs_mgr_remount.cpp",
    ],
    cppflags: [
-        "-DALLOW_ADBD_DISABLE_VERITY=0",
+       "-UALLOW_ADBD_DISABLE_VERITY",
+        "-DALLOW_ADBD_DISABLE_VERITY=1",
    ],
-    product_variables: {
-        debuggable: {
-            cppflags: [
-                "-UALLOW_ADBD_DISABLE_VERITY",
-                "-DALLOW_ADBD_DISABLE_VERITY=1",
-            ],
-        },
-    },
    required: [
        "clean_scratch_files",
    ],
}
//...

这里-DALLOW_ADBD_DISABLE_VERITY=1的含义是允许adbd进程关闭Verity检查。

在system/core/fs_mgr/fs_mgr_remount.cpp文件中修改:

//...
static int do_remount(int argc, char* argv[]) {
    RemountStatus retval = REMOUNT_SUCCESS;

    // If somehow this executable is delivered on a "user" build, it can
    // not function, so providing a clear message to the caller rather than
    // letting if fall through and provide a lot of confusing failure messages.
-    if (!ALLOW_ADBD_DISABLE_VERITY || (android::base::GetProperty("ro.debuggable", "0") != "1")) {
+    if (!ALLOW_ADBD_DISABLE_VERITY) {
        LOG(ERROR) << "only functions on userdebug or eng builds";
        return NOT_USERDEBUG;
    }

    const char* fstab_file = nullptr;
    auto can_reboot = false;
    //...
}
//...

在core/init/property_service.cpp文件中修改:

//...
 static void update_sys_usb_config() {
-    bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
+    bool is_debuggable = true;
     std::string config = android::base::GetProperty("persist.sys.usb.config", "");
     // b/150130503, add (config == "none") condition here to prevent appending
     // ",adb" if "none" is explicitly defined in default prop.
     //...
}

在system/core/set-verity-state/set-verity-state.cpp文件中修改:

static bool overlayfs_setup(bool enable) {
  auto change = false;
+#if 0
  errno = 0;
  if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change)
             : fs_mgr_overlayfs_setup(nullptr, nullptr, &change)) {
    if (change) {
      printf("%s overlayfs\n", enable ? "disabling" : "using");
    }
  } else if (errno) {
    printf("Overlayfs %s failed with error %s\n", enable ? "teardown" : "setup", strerror(errno));
    suggest_run_adb_root();
  }
+#endif
+  printf("overlayfs_setup(%d)",enable); //fix build error
  return change;
}

至此,system部分的修改也结束了。

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图王大胜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值