当前位置:   article > 正文

Android U user+root实现方案

Android U user+root实现方案

 背景

       由于项目(MTK平台)上要实现user+root的版本,供特殊用户使用。Android T上的方案无效,经历了各种搜索查看资料,和bsp大佬一起通宵奋战,整出了方案。梳理记录下,有需要的同学可以参考。

Root代码实现原理

      系统判断是否有root权限的地方在system/packages/modules/adb/daemon/main.cpp里面,

should_drop_privileges()函数返回false,表明可以root。

auth_required 是否需要adb鉴权(adb弹框确认),false时默认授权adb鉴权权限,不需要弹框确认。

userdebug 版本可以root是因为ro.secure = 0,代码路径build/core/main.mk 在should_drop_privileges()里面有判断

  1. else # !user_variant
  2. # Turn on checkjni for non-user builds.
  3. ADDITIONAL_SYSTEM_PROPERTIES += ro.kernel.android.checkjni=1
  4. # Set device insecure for non-user builds.
  5. ADDITIONAL_SYSTEM_PROPERTIES += ro.secure=0
  1. static bool should_drop_privileges() {
  2. // The properties that affect `adb root` and `adb unroot` are ro.secure and
  3. // ro.debuggable. In this context the names don't make the expected behavior
  4. // particularly obvious.
  5. //
  6. // ro.debuggable:
  7. // Allowed to become root, but not necessarily the default. Set to 1 on
  8. // eng and userdebug builds.
  9. //
  10. // ro.secure:
  11. // Drop privileges by default. Set to 1 on userdebug and user builds.
  12. bool ro_secure = android::base::GetBoolProperty("ro.secure", true);
  13. bool ro_debuggable = __android_log_is_debuggable();
  14. // Drop privileges if ro.secure is set...
  15. bool drop = ro_secure;
  16. // ... except "adb root" lets you keep privileges in a debuggable build.
  17. std::string prop = android::base::GetProperty("service.adb.root", "");
  18. bool adb_root = (prop == "1");
  19. bool adb_unroot = (prop == "0");
  20. if (ro_debuggable && adb_root) {
  21. drop = false;
  22. }
  23. // ... and "adb unroot" lets you explicitly drop privileges.
  24. if (adb_unroot) {
  25. drop = true;
  26. }
  27. return drop;
  28. }

实现中遇到的问题

      1,selinux问题(主要解决问题);

      2,缺少su 和remount执行的bin文件;

具体实现方案和步骤

       1),特殊版本标识,CUSTOM_ROOT_VERSION=yes,编译版本时需要export下该环境变量

       2),添加flag,路径build/core/soong_config.mk

  1. --- a/core/soong_config.mk
  2. +++ b/core/soong_config.mk
  3. @@ -58,6 +58,9 @@
  4. $(call add_json_bool, Debuggable, $(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
  5. $(call add_json_bool, Eng, $(filter eng,$(TARGET_BUILD_VARIANT)))
  6. +$(call add_json_bool, ROOTVersion, $(filter yes,$(CUSTOM_ROOT_VERSION)))
  7. $(call add_json_str, DeviceName, $(TARGET_DEVICE))

 3),添加root版本的数据声明,设置root相关的flag需要

  1. --- a/android/variable.go
  2. +++ b/android/variable.go
  3. @@ -151,6 +151,14 @@
  4. }
  5. }
  6. + ROOTVersion struct {
  7. + Cflags []string
  8. + Cppflags []string
  9. + Init_rc []string
  10. + }
  11. Pdk struct {
  12. Enabled *bool `android:"arch_variant"`
  13. } `android:"arch_variant"`
  14. @@ -315,6 +323,9 @@
  15. UseRBED8 *bool `json:",omitempty"`
  16. Debuggable *bool `json:",omitempty"`
  17. Eng *bool `json:",omitempty"`
  18. + ROOTVersion *bool `json:",omitempty"`
  19. Treble_linker_namespaces *bool `json:",omitempty"`

4),添加su 和 remount 模块,在产品的mk文件中添加

PRODUCT_PACKAGES +=remount

PRODUCT_PACKAGES +=su

5),在文件系统模块fs_mgr中添加ROOTVersion相应的flag,property_service中设置ro.secure和ro.debuggable的值

  1. --- a/fs_mgr/Android.bp
  2. +++ b/fs_mgr/Android.bp
  3. @@ -118,6 +118,14 @@
  4. "-DALLOW_ADBD_DISABLE_VERITY=1",
  5. ],
  6. },
  7. + ROOTVersion: {
  8. + cppflags: [
  9. + "-UALLOW_ADBD_DISABLE_VERITY",
  10. + "-DALLOW_ADBD_DISABLE_VERITY=1",
  11. + ],
  12. + },
  13. },
  14. header_libs: [
  15. "libfiemap_headers",
  16. @@ -248,6 +256,17 @@
  17. "clean_scratch_files.rc",
  18. ],
  19. },
  20. + ROOTVersion: {
  21. + cppflags: [
  22. + "-UALLOW_ADBD_DISABLE_VERITY",
  23. + "-DALLOW_ADBD_DISABLE_VERITY=1",
  24. + ],
  25. + init_rc: [
  26. + "clean_scratch_files.rc",
  27. + ],
  28. + },
  29. },
  30. symlinks: [
  31. "clean_scratch_files",
  32. --- a/init/Android.bp
  33. +++ b/init/Android.bp
  34. @@ -149,6 +149,13 @@
  35. "-DSHUTDOWN_ZERO_TIMEOUT=1",
  36. ],
  37. },
  38. + ROOTVersion: {
  39. + cppflags: [
  40. + "-DROOT_VERSION",
  41. + ],
  42. + },
  43. uml: {
  44. cppflags: ["-DUSER_MODE_LINUX"],
  45. },
  46. --- a/init/property_service.cpp
  47. +++ b/init/property_service.cpp
  48. @@ -1328,6 +1328,19 @@
  49. }
  50. }
  51. + bool adbAuthorized = false;
  52. +#ifdef ROOT_VERSION
  53. + adbAuthorized = true;
  54. +#endif
  55. + if (adbAuthorized) {
  56. + InitPropertySet("ro.adb.secure", "0");
  57. + InitPropertySet("ro.debuggable", "1");
  58. + }
  59. +
  60. for (const auto& [name, value] : properties) {

6)adb模块添加权限的判断,和5)中设置属性的值有重复,此处是为了确保生效。有时间的同学可以验证下去掉这一步看是否生效。

should_drop_privileges()函数最后添加

  1. #ifdef CUSTON_ROOT_VERSION
  2. return false;
  3. #endif

drop_privileges()函数最后添加

  1. #ifdef CUSTON_ROOT_VERSION
  2. auth_required=false;
  3. #endif

7),最重要的一步,更换sepolicy文件为debug版本的

(1)添加sepolicy,src文件是debug版本的,修改路径system/sepolicy/android.bp
  1. --- a/Android.bp
  2. +++ b/Android.bp
  3. },
  4. }
  5. +se_policy_cil {
  6. + name: "userdebug_plat_sepolicy_root.cil",
  7. + src: ":userdebug_plat_sepolicy.conf",
  8. + additional_cil_files: [":sepolicy_technical_debt{.plat_private}"],
  9. + dist: {
  10. + targets: ["droidcore"],
  11. + },
  12. +}
  13. +
  14. // A copy of the userdebug_plat_policy in GSI.
(2),同路径下mk文件加入编译
  1. --- a/Android.mk
  2. +++ b/Android.mk
  3. LOCAL_REQUIRED_MODULES += \
  4. userdebug_plat_sepolicy.cil \
  5. +ifeq ($(strip $(CUSTOM_ROOT_VERSION)),yes)
  6. +LOCAL_REQUIRED_MODULES += \
  7. + userdebug_plat_sepolicy_root.cil
  8. +endif
(3)代码中加载sepolicy地方GetUserdebugPlatformPolicyFile()也使用上面生成的sepolicy文件,代码路径system/core/init/selinux.cpp
  1. std::optional<const char*> GetUserdebugPlatformPolicyFile() {
  2. +#ifdef DF_VERSION
  3. + return "/system/etc/selinux/userdebug_plat_sepolicy_root.cil";
  4. +#endif

至此,Android U版本user+root+remount方案修改完成。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/381238
推荐阅读
相关标签
  

闽ICP备14008679号