1. 从logcat中过滤avc信息
avc: denied { read write } for comm="vendor.demo" name="ttyHW5" dev="tmpfs" ino=610 scontext=u:r:hal_gnss_default:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1
avc: denied { ioctl } for comm="vendor.demo" path="/dev/ttyHW5" dev="tmpfs" ino=610 ioctlcmd=0x5401 scontext=u:r:hal_gnss_default:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1
avc: denied { ioctl } for comm="vendor.demo" path="/dev/ttyHW5" dev="tmpfs" ino=610 ioctlcmd=0x5401 scontext=u:r:hal_gnss_default:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=1
2. 从avc中配置相关规则
allow hal_gnss_default device:chr_file rw_file_perms;
但上面的规则无法生效。如上规则,放在系统里编译,会出现如下错误:
libsepol.report_failure: neverallow on line 681 of sepolicy/private/domain.te violated by allow hal_gnss_default

3. 解决拒绝提供核心服务的问题
3.1 权限过大问题
要解决这类问题,可以给文件一个更具体的标签,在本例中就是: gnss_device。不需要其他权限,因为hal_gnss_default已经在核心策略中拥有访问 gnss_device 的必要权限。
allow hal_gnss_default gnss_device:chr_file rw_file_perms;
3.2 从file_contents中定义新的label
avc: denied { map } for comm="vendor.demo" path="/dev/__properties__/u:object_r:default_prop:s0" dev="tmpfs" ino=212 scontext=u:r:hal_gnss_default:s0 tcontext=u:object_r:default_prop:s0 tclass=file permissive=1
从avc信息看,需要配置:
allow hal_gnss_default default_prop:file { map };
但如上规则,放在系统里依然无法编译。原因是Google定义了相关规则,不允许按上述方法配置;


处理方案是:
①. 在file_contexts文件中新增label,如下图所示;

②. 在*.te文件中,通过set_prop/get_prop方法来配置;

编辑/添加策略和file_contexts后,更新 /device/manufacturer/device-name/BoardConfig.mk 以引用 sepolicy 子目录和每个新策略文件。
有关 BOARD_SEPOLICY 变量的更多信息,请参见:system/sepolicy/README文件。
BOARD_SEPOLICY_DIRS += \<root>/device/manufacturer/device-name/sepolicyBOARD_SEPOLICY_UNION += \genfs_contexts \file_contexts \sepolicy.te