selinux-添加

Aispeech Audio Hal porting到android系统中需要添加如下selinux权限(android8.1): diff --git a/system/sepolicy/public/domain.te b/system/sepolicy/public/domain.te --- a/system/sepolicy/public/domain.te +++ b/system/sepolicy/public/domain.te @@ -623,6 +623,7 @@ full_treble_only(`     -coredomain     -netdomain     -socket_between_core_and_vendor_violators +    -hal_audio   }, netd);

 

   # Vendor domains are not permitted to initiate create/open sockets owned by core domains diff --git a/system/sepolicy/public/hal_audio.te b/system/sepolicy/public/hal_audio.te index 33330bf..e69a19b7 100644 --- a/system/sepolicy/public/hal_audio.te +++ b/system/sepolicy/public/hal_audio.te @@ -32,7 +32,7 @@ neverallow hal_audio { file_type fs_type }:file execute_no_trans;

 

 # Should never need network access. # Disallow network sockets. -neverallow hal_audio domain:{ tcp_socket udp_socket rawip_socket } *; +#neverallow hal_audio domain:{ tcp_socket udp_socket rawip_socket } *;

 

 # Only audio HAL may directly access the audio hardware neverallow { halserverdomain -hal_audio_server } audio_device:chr_file *; diff --git a/system/sepolicy/public/hal_neverallows.te b/system/sepolicy/public/hal_neverallows.te index 036e1d2..13f78aa 100644 --- a/system/sepolicy/public/hal_neverallows.te +++ b/system/sepolicy/public/hal_neverallows.te @@ -16,6 +16,7 @@ neverallow {   -hal_wifi_server   -hal_wifi_supplicant_server   -rild +  -hal_audio } domain:{ tcp_socket udp_socket rawip_socket } *;

 

 ###

diff --git a/device/rockchip/common/sepolicy/hal_audio_default.te b/device/rockchip/common/sepolicy/hal_audio_default.te index ebc2023..f96c83d 100644 --- a/device/rockchip/common/sepolicy/hal_audio_default.te +++ b/device/rockchip/common/sepolicy/hal_audio_default.te @@ -7,3 +7,18 @@ allow hal_audio_default hidraw_device:chr_file { open read write };

 

 set_prop(hal_audio_default,media_prop) allow hal_audio_default sysfs:file rw_file_perms; + +#for aispeech +allow hal_audio_default hal_audio_default:udp_socket { create bind write read getattr }; +allow hal_audio_default hal_audio_default:tcp_socket { create connect getopt setopt read write

name_connect }; +allow hal_audio_default audioserver_data_file:dir { add_name remove_name write search }; +allow hal_audio_default port:tcp_socket name_connect; +allow hal_audio_default audioserver_data_file:file { write open getattr create }; +allow hal_audio_default node:udp_socket node_bind; +allow hal_audio_default netd:unix_stream_socket connectto; +allow hal_audio_default fwmarkd_socket:sock_file write; +allow hal_audio_default audioserver_data_file:file unlink; + +#fixed common +#allow hal_audio_default vendor_shell_exec:file execute_no_trans; +#allow hal_audio_default su_exec:file { execute_no_trans read open getattr}; diff --git a/device/rockchip/common/sepolicy/netd.te b/device/rockchip/common/sepolicy/netd.te index 3acb9a6..34f814f 100644 --- a/device/rockchip/common/sepolicy/netd.te +++ b/device/rockchip/common/sepolicy/netd.te @@ -9,3 +9,7 @@ allow netd netd:netlink_scsitransport_socket { create setopt bind write read get allow netd netd:netlink_rdma_socket { create setopt bind write read getopt getattr }; allow netd netd:netlink_crypto_socket { create setopt bind write read getopt getattr };

 

+#for aispeech +allow netd hal_audio_default:tcp_socket { setopt getopt read write}; +allow netd hal_audio_default:udp_socket { setopt getopt read write}; +allow netd hal_audio_default:fd { use }; 说明:以上修改可以过selinux验证,但是有修改系统默认selinux权限,无法过CTS某些测试。 见下面: armeabi-v7a CtsSecurityHostTestCases Test Result Details

android.cts.security.SELinuxNeverallowRulesTest# testNeverallowRules56 fail

junit.framework.AssertionFailedError: The following errors were encountered when validating the SELinuxneverallow rule:

 

使用脚本添加selinux

2.2.2Selinux 添加方法
Selinux 在 Android5.1 之后被真正的引入到 android 系统之中。目前在 Android8.0 及以上 系统常见 selinux 处于打开状态。详细的 Selinux 规则此处不再描述。Selinux 规则可以根据 log 中的提示信息进行添加。以 AispeechAudioHal 为例,开机之后可以通过 grep logcat 获取 selinux 的警告信息。命令如下: logcat|grepavc 或者 logcat |grepdenied 从 log 中再筛选出 audioserver 相关的 log,部分如下所示: 7 04-15 16:11:41.440 316 316 I Auth : type=1400 audit(0.0:9): avc: denied { read } for name="address" dev="sysfs" ino=14731 scontext=u:r:audioserver:s0 tcontext=u:object_r:sysfs:s0tclass=file permissive=1 8 04-15 16:11:41.440 316 316 I Auth : type=1400 audit(0.0:10): avc: denied { open } for path="/sys/devices/platform/fe300000.ethernet/net/eth0/address" dev="sysfs" ino=14731 scontext=u:r:audioserver:s0 tcontext=u:object_r: sysfs:s0 tclass=file permissive=1 9 04-15 16:11:41.443 316 316 I Auth : type=1400 audit(0.0:11): avc: denied { getattr } for path="/sys/devices/platform/fe300000.ethernet/net/eth0/address" dev="sysfs" ino=14731 scontext=u:r:audioserver:s0 tcontext=u:object _r:sysfs:s0 tclass=file permissive=1 10 04-15 16:11:41.460 316 316 I Auth : type=1400 audit(0.0:12): avc: denied { create } for scontext=u:r:audioserver:s0 tcontext=u:r:audioserver:s0 tclass=udp_socket permissive=1
11 04-15 16:11:41.460 316 316 I Auth : type=1400 audit(0.0:13): avc: denied { bind } for scontext=u:r:audioserver:s0 tcontext=u:r:audioserver:s0 tclass=udp_socket permissive=1 12 04-15 16:11:41.460 316 316 I Auth : type=1400 audit(0.0:14): avc: denied { node_bind } for saddr=127.0.0.1 scontext=u:r:audioserver:s0 tcontext=u:object_r:node:s0 tclass=udp_socketpermissive=1 13 04-15 16:11:41.460 316 316 I Auth : type=1400 audit(0.0:15): avc: denied { getattr } for laddr=127.0.0.1 lport=42108 scontext=u:r:audioserver:s0 tcontext=u:r:audioserver:s0tclass=udp_socketpermissive=1 14 04-15 16:11:41.460 316 316 I Auth : type=1400 audit(0.0:16): avc: denied { write } for name="fwmarkd" dev="tmpfs" ino=16490 scontext=u:r:audioserver:s0 tcontext=u:object_r:fwmarkd_socket:s0tclass=sock_file permissive=1 15 04-15 16:11:41.460 316 316 I Auth : type=1400 audit(0.0:17): avc: denied { connect } for scontext=u:r:audioserver:s0 tcontext=u:r:audioserver:s0 tclass=udp_socket permissive=1 16 04-15 16:11:41.466 316 316 I Auth : type=1400 audit(0.0:18): avc: denied { write } for scontext=u:r:audioserver:s0 tcontext=u:r:audioserver:s0 tclass=udp_socket permissive=1 17 04-15 16:11:51.483 316 316 I Auth : type=1400 audit(0.0:19): avc: denied { create } for scontext=u:r:audioserver:s0 tcontext=u:r:audioserver:s0 tclass=tcp_socket permissive=1 18 04-15 16:11:51.483 316 316 I Auth : type=1400 audit(0.0:20): avc: denied { write } for name="fwmarkd" dev="tmpfs" ino=16490 scontext=u:r:audioserver:s0 tcontext=u:object_r:fwmarkd_socket:s0tclass=sock_file permissive=1 19 04-15 16:11:51.483 316 316 I Auth : type=1400 audit(0.0:21): avc: denied { connectto } for path="/dev/socket/fwmarkd" scontext=u:r:audioserver:s0 tcontext=u:r:netd:s0tclass=unix_stream_socket permissive=1 把所有相关 log 全部筛选出来,存放在一个log 文件中, 例如存放在名为audiohal_avc.log 的文件中。

通过如下命令可以自动生成 avc 规则: external/selinux/prebuilts/bin$ ./audit2allow -i audiohal_avc.log

生成的规则如下:

#============= audioserver==============

allow  audioserver:fileopen;

allow  audioserverfwmarkd_socket:sock_filewrite;

allow  audioservernetd:unix_stream_socket connectto;

allow  audioservernode:udp_socketnode_bind; allowaudioserverself:tcp_socket create;

allow audioserverself:udp_socket{writebindcreategetattrconnect};

将上述规则添加到相应的系统 te 文件中即可。

系统 xxx.te 文件存放位置一般在如下两个地方:

1./external/sepolicy(注 7.0 及以上在/system/sepolicy)下面的 xxx.te

2.device/xxxxxxxx/common/sepolicy 下面的 xxxx.te。 添加好规则之后重新编译固件进行刷机测试,重复上述步骤,直到 log 中没有再出现audiohal avc 相关提示警告信息。

添加规则编译的时候有可能会出现 neverallow 规则冲突,导致编译出错。此时需要到 系统限定 neverallow 规则的 te 文件中去掉相关限制即可编译通过。

更加详细的方法可以 自行查看相关资料。修改 neverallow 规则会导致 cts 测试无法通过,请知悉。如果系统无 需过 cts 认证,可以按需求修改系统 neverallow 规则。

 

你可能感兴趣的:(selinux-添加)