Root权限

一,root 运行原理

  • 一个 root 守护进程,提供 setuid 的服务。
  • 一个使用者进程,通过 socket 与 root 守护进程通讯,使用其 setuid,把自己的 uid 设为0( root )。
  1. adbd 切换为 root 状态,挂载 system 分区为可读写,push 编译出来的 su 覆盖系统自带的,并给予06755权限,然后传入参数启动su。
    在这里插入图片描述
  2. 重启一下板子,让adbd 恢复到 shell 状态,并查看 sh 的 uid,此时没有执行 su,uid 为 shell(2000)。接着执行su,再次查看 sh 的 uid,发现已经重新 fork 了一个新的 sh ,并将 uid 改成了 root(0)。
    在这里插入图片描述
    至此,将其他进程的uid变成0(root),已经成功。

二,在编译阶段整合root

  1. 保证out目录下 /system/xbin/su 是由自己的源码编译出来的,而不是系统的;
源码覆盖 /system/xbin/su 
devices.mk 文件中加上:
PRODUCT_PACKAGES += su
  1. 给予 su 权限(system/core下)
    注:Android 版本不同,文件名和路径可能不一样。
diff --git a/system/core/libcutils/fs_config.c b/system/core/libcutils/fs_config.c
index 9854098293..5a9095c0c0 100644
--- a/system/core/libcutils/fs_config.c
+++ b/system/core/libcutils/fs_config.c
@@ -136,7 +136,7 @@ static const struct fs_path_config android_files[] = {
 
     /* the following two files are INTENTIONALLY set-uid, but they
      * are NOT included on user builds. */
-    { 04750, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },
+    { 06755, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },
     { 06755, AID_ROOT,      AID_ROOT,      0, "system/xbin/procmem" },
 
     /* the following files have enhanced capabilities and ARE included in user builds. */
  1. 启动 root 守护进程
    在 init.rc 添加一个服务,用于传入参数启动守护进程。
diff --git a/device/rockchip/rk3288/init.rk3288.rc b/device/rockchip/rk3288/init.rk3288.rc
index 3c8db59ee0..6127e6a851 100755
--- a/device/rockchip/rk3288/init.rk3288.rc
+++ b/device/rockchip/rk3288/init.rk3288.rc
@@ -42,6 +42,12 @@ on init
     # Load persistent dm-verity state
     verity_load_state
 
+service daemonsu /system/xbin/su --daemon
+       class main
+       user root
+       group root
+       oneshot
+
 service btattach /system/bin/btattach
     class main
     user root
  1. 关闭selinux 或者 根据客户需求微调 te 规则
    在这里插入图片描述

三,规避第三方app对su的检测

1. 重命名 su

  1. service 同步修改
service rootservice /vendor/bin/rootservice
	class main
 	user root
 	group root
 	disabled
 	oneshot

on property:sys.boot_completed=1
 	start rootservice
  1. 如果开启了 selinux ,需要重新配置 te。
diff --git a/sepolicy/vendor/file_contexts b/sepolicy/vendor/file_contexts
index b08c920..61e369e 100644
--- a/sepolicy/vendor/file_contexts
+++ b/sepolicy/vendor/file_contexts
@@ -84,6 +84,9 @@
#/data/tee(/.*)? u:object_r:tee_data_file:s0
/vendor/bin/rk_store_keybox
u:object_r:rk_store_keybox_exec:s0
+# for rootservice
+/vendor/bin/rootservice u:object_r:rootservice_exec:s0
+
/dev/block/mmcblk[0-9]
u:object_r:uboot_block_device:s0
/dev/block/mmcblk[0-9]rpmb u:object_r:rpmb_block_device:s0
diff --git a/sepolicy/vendor/rootservice.te b/sepolicy/vendor/rootservice.te
new file mode 100644
index 0000000..754bada
--- /dev/null
+++ b/sepolicy/vendor/rootservice.te
@@ -0,0 +1,5 @@
+type rootservice, domain, mlstrustedsubject;
+type rootservice_exec, exec_type, vendor_file_type, file_type;
+
+init_daemon_domain(rootservice)
+allow rootservice shell_exec:file { getattr read open };

2. 轻量级 root 通道

启动名为 tcpdumpService 的 root 进程,并创建一个 socket ,将通过 socket 为其他非 root 进程提供服务。

service tcpdumpService /system/bin/tcpdumpService
socket tcpdumpService stream 777 system system
class main
group root
user root
seclabel u:r:su:s0

附:

  1. su源码链接:https://download.youkuaiyun.com/download/weixin_45639314/89990982
  2. 使用方法:解压以上文件到到源码目录中单编(mm),可以在out/target/product/rk3288/system/xbin/su下生成新的su。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值