【全志R18】Android6.0第三方应用访问dev节点正确配置Selinux策略后,依旧报错 avc: denied

在此做下一个笔记,以前其实也遇到过,没想到这次还是踩坑。

由于客户使用第三方APP需要访问/dev/snd/下的声卡节点,因此打开APP后,发现并不能正常访问,使用
cat /proc/kmsg | grep avc,报错以下信息:

[   70.119120] type=1400 audit(1554076874.080:6):avc: denied { search } for pid=6383 comm="SpeechAudioReco" name="snd" dev="tmpfs" ino=4354 scontext=u:r:untrusted_app:s0:c512,c768 
 tcontext=u:object_r:audio_device:s0 tclass=dir permissive=0

同时进行确认是selinux的问题测试:
1、串口shell或者adb工具,使用
setenforce 0 (临时禁用掉 SELinux)
getenforce (得到结果为 Permissive)

2、给/dev/snd/下的节点0777权限

测试结果:Success

既然是这个问题,那么我们就来补selinux的策略,于是根据万能公式得到:
【全志R18】Android6.0第三方应用访问dev节点正确配置Selinux策略后,依旧报错 avc: denied以及借助overlay机制,直接找到device下的策略文件untrusted_app.te,加入以下权限:

allow untrusted_app audio_device:dir search;

编译烧录后,给节点0777权限,再次打开APP访问,发现又报错:

[  250.220688] type=1400 audit(1554077054.150:6): avc: denied { write } for pid=17838 comm="SpeechAudioReco" name="pcmC0D0c" dev="tmpfs" ino=312 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:audio_device:s0 tclass=chr_file permissive=0

于是在untrusted_app.te文件再加入以下权限:

allow untrusted_app audio_device:chr_file  rwx_file_perms;

编译报错externl/selinux/app.te中的策略与我在untrusted_app.te中加入的权限allow untrusted_app audio_device:chr_file rwx_file_perms;冲突

找到app.te,注释掉冲突项audio_device:

# Access to any of the following character devices.
neverallow appdomain {
#   audio_device
    camera_device
    dm_device
    radio_device
    gps_device
    rpmsg_device
}:chr_file { read write };

# Note: Try expanding list of app domains in the future.
#neverallow { untrusted_app isolated_app shell } graphics_device:chr_file { read write };

编译烧录后,再给节点0777权限,再次打开APP访问,还是报错如下,为什么??

[  250.220688] type=1400 audit(1554077054.150:6): avc: denied { write } for pid=17838 comm="SpeechAudioReco" name="pcmC0D0c" dev="tmpfs" ino=312 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:audio_device:s0 tclass=chr_file permissive=0

这就奇怪了,该给的权限都给了,可还是报没有write权限,记起来以前也是遇到过这个坑,第三方APP访问节点,要在定义type的时候加入mlstrustedobject才行。

那么就找到audio_device定义的地方external/device.te,修改如下:

diff --git a/device.te b/device.te
index b2f4f1d..c72d216 100644
--- a/device.te
+++ b/device.te
@@ -3,7 +3,7 @@ type device, dev_type, fs_type;
 type alarm_device, dev_type, mlstrustedobject;
 type adb_device, dev_type;
 type ashmem_device, dev_type, mlstrustedobject;
-type audio_device, dev_type;
+type audio_device, dev_type, mlstrustedobject;
 type binder_device, dev_type, mlstrustedobject;
 type block_device, dev_type;
 type camera_device, dev_type;

编译烧录后,成功了!!!

参考链接:https://blog.csdn.net/gnnulzy/article/details/52868696