2022-07-20 Android 11 SELinux avc 修改sys目录下面某个节点的权限

一、我这里有/sys/devices/platform/thermal-camera-control/powerenable 这样一个节点,用命令ls -Z 查看该文件的域。

 

二、我现在在一个普通app上面读写这个节点,提示提无认证的app没有读写权限。

onsole:/ $ [  382.626960] type=1400 audit(1658283554.576:39): avc: denied { write } for comm="don.powerenable" name="powerenable" dev="sysfs" ino=15716 scontext=u:r:untrusted_app_29:s0:c122,c256,c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 app=com.topdon.powerenable

console:/ $ [  384.749073] type=1400 audit(1658283556.703:40): avc: denied { read } for comm="don.powerenable" name="powerenable" dev="sysfs" ino=15716 scontext=u:r:untrusted_app_29:s0:c122,c256,c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 app=com.topdon.powerenable

三、可以修改策略让所有的无信任app都可以读写sys下面的节点,但这样不安全,我这里就单独开放这个节点的权限,步骤如下。

1、在file.te中,加入一个域的别名,如在该例中,我们原本需要访问的是sysfs域中的一个文件,所以我们在file.te中定义一个新的域,名字可以随便取,定义一个叫做sysfs_powerenable的域(domain),该域具有fs_type和sysfs_type属性(attribute)如

type sysfs_powerenable, fs_type, sysfs_type, mlstrustedobject;

 2、在同目录中的file_contexts中加入对需要操作权限的文件路径与我们定义的域进行映射,如:

#/sys/devices/platform/thermal-camera-control/status   u:object_r:sysfs_powerenable:s0

3、在我们新建的powerenable.te策略文件中,把之前的sysfs替换为我们新定义的域,即

allow untrusted_app_29  sysfs_powerenable:file  { read open write getattr};

4、adb shell中通过ls -Z 取查看该文件的域,如果映射成功了,会变为sysfs_powerenable

5、编译的时候可能会有提示不允许untrusted apps 写sysfs下面的节点,按下面的修改策略。 

 6、测试结果如下,第三方app 可以读写这个节点了

四、/sys/devices/platform/thermal-camera-control/status 本来系统app是可以读写的,但是我在\device\rockchip\common\sepolicy\vendor\file_contexts 文件里面添加

/sys/devices/platform/thermal-camera-control/status   u:object_r:sysfs_powerenable:s0

这个时候第三方app是可以读写这个status节点,但是系统app反而读写不了这个节点,提示

rk3566_rgo:/ # cd /sys/devices/platform/thermal-camera-control/
rk3566_rgo:/sys/devices/platform/thermal-camera-control # ls -Z status
u:object_r:sysfs_powerenable:s0 status
rk3566_rgo:/sys/devices/platform/thermal-camera-control #

解决方法是在powerenable.te 文件里面添加系统app读写这个节点的权限

allow system_app        sysfs_powerenable:file  { read open write getattr};

 这样修改后第三方app也可以修改这个status节点了,我这里写这个status节点后usb adb会物理断开。 

六、整个过程需要修改的内容

1、添加文件device/rockchip/common/sepolicy/vendor/powerenable.te

allow untrusted_app_29  sysfs_powerenable:file  { read open write getattr};
allow system_app        sysfs_powerenable:file  { read open write getattr};

2、其他修改的内容


project device/rockchip/common/
diff --git a/sepolicy/vendor/file.te b/sepolicy/vendor/file.te
index 464180c..45feac3 100755
--- a/sepolicy/vendor/file.te
+++ b/sepolicy/vendor/file.te
@@ -39,6 +39,7 @@ type sysfs_timestamp_switch, sysfs_type, fs_type;
 type sysfs_video, sysfs_type, fs_type;
 type sysfs_fb, fs_type, sysfs_type;
 type sysfs_hdmi, fs_type, sysfs_type, mlstrustedobject;
+type sysfs_powerenable, fs_type, sysfs_type, mlstrustedobject;
 type sysfs_dev, fs_type, sysfs_type, mlstrustedobject;
 type sysfs_lut, fs_type, sysfs_type, mlstrustedobject;
 type sysfs_lcdc, fs_type, sysfs_type, mlstrustedobject;
diff --git a/sepolicy/vendor/file_contexts b/sepolicy/vendor/file_contexts
old mode 100644
new mode 100755
index e0a46b2..1e5e005
--- a/sepolicy/vendor/file_contexts
+++ b/sepolicy/vendor/file_contexts
@@ -220,4 +220,6 @@
 
 #flash_img
 /vendor/bin/flash_img.sh u:object_r:vendor_install_recovery_exec:s0
+/sys/devices/platform/thermal-camera-control/powerenable   u:object_r:sysfs_powerenable:s0
+/sys/devices/platform/thermal-camera-control/status   u:object_r:sysfs_powerenable:s0
 

project system/sepolicy/
diff --git a/prebuilts/api/30.0/private/app_neverallows.te b/prebuilts/api/30.0/private/app_neverallows.te
old mode 100644
new mode 100755
index 1157187..f01eb91
--- a/prebuilts/api/30.0/private/app_neverallows.te
+++ b/prebuilts/api/30.0/private/app_neverallows.te
@@ -90,7 +90,7 @@ neverallow all_untrusted_apps file_type:file link;
 neverallow all_untrusted_apps sysfs_net:file no_rw_file_perms;
 
 # Do not allow any write access to files in /sys
-neverallow all_untrusted_apps sysfs_type:file { no_w_file_perms no_x_file_perms };
+#neverallow all_untrusted_apps sysfs_type:file { no_w_file_perms no_x_file_perms };
 
 # Apps may never access the default sysfs label.
 neverallow all_untrusted_apps sysfs:file no_rw_file_perms;
diff --git a/private/app_neverallows.te b/private/app_neverallows.te
old mode 100644
new mode 100755
index 1157187..f01eb91
--- a/private/app_neverallows.te
+++ b/private/app_neverallows.te
@@ -90,7 +90,7 @@ neverallow all_untrusted_apps file_type:file link;
 neverallow all_untrusted_apps sysfs_net:file no_rw_file_perms;
 
 # Do not allow any write access to files in /sys
-neverallow all_untrusted_apps sysfs_type:file { no_w_file_perms no_x_file_perms };
+#neverallow all_untrusted_apps sysfs_type:file { no_w_file_perms no_x_file_perms };
 
 # Apps may never access the default sysfs label.
 neverallow all_untrusted_apps sysfs:file no_rw_file_perms;

七、有价值的参考文章:

SeLinux问题解决方法

Android/SELinux 添加 AVC 权限 - 码农教程

[Android Framework]Android 11 SELinux avc权限解决方法_usmaI的博客-CSDN博客_android avc 权限

android selinux报avc denied权限和编译报neverallow解决方案_楼中望月的博客-CSDN博客_android avc: denied

猜你喜欢

转载自blog.csdn.net/qq_37858386/article/details/125886941