SELinux 随笔

参考文章:

https://www.cnblogs.com/zhongguiyao/p/13955398.html

https://wizzie.top/android/android-selinux/

 

常用的定义,方便使用 一组权限或一类对象

源码位置:/system/sepolicy/public/global_macros

比如下面这些:

#####################################
# Common groupings of permissions.
#
define(`x_file_perms', `{ getattr execute execute_no_trans map }')
define(`r_file_perms', `{ getattr open read ioctl lock map watch watch_reads }')
define(`w_file_perms', `{ open append write lock map }')
define(`rx_file_perms', `{ r_file_perms x_file_perms }')
define(`ra_file_perms', `{ r_file_perms append }')
define(`rw_file_perms', `{ r_file_perms w_file_perms }')
define(`rwx_file_perms', `{ rw_file_perms x_file_perms }')
define(`create_file_perms', `{ create rename setattr unlink rw_file_perms }')

 

 

原生的一些SElinux sepolicy

源码位置:/system/sepolicy/private/  or /system/sepolicy/public/ 

比如

init.te
vendor_init.te
vendor_shell.te

 

一些neverallow的规则常常定义在domain.te

源码位置:/system/sepolicy/public/domain.te

 

文件的安全上下文

原生的定义

/system/sepolicy/public/file.te

 

vendor厂商自己的定义

比如可能是

/device/generic/goldfish/sepolicy/common/file.te

或者其它 te档案中,如

 /system/sepolicy/public/surfaceflinger.te

 

通常会在file_contexts文件中指定进程或档案的selinux安全上下文

比如:

[ /system/sepolicy/private/file_contexts]

// 文件
/data/vendor(/.*)?              u:object_r:vendor_data_file:s0
/data/vendor_ce(/.*)?           u:object_r:vendor_data_file:s0
/data/vendor_de(/.*)?           u:object_r:vendor_data_file:s0

// 进程
/system/bin/surfaceflinger	u:object_r:surfaceflinger_exec:s0

 

可以通过 ls -Z 或 ps -Z 命令查看(是不是和上面定义的一致

// 查看进程
console:/ # ps -AZ | grep SurfaceFLinger
1|console:/ # ps -AZ | grep surfaceflinger
u:r:surfaceflinger:s0          system          188      1  134632  34488 SyS_epoll_wait      0 S surfaceflinger

// 查看档案
console:/ # ls -Z /data | grep vendor
u:object_r:vendor_data_file:s0                    vendor
u:object_r:vendor_data_file:s0                    vendor_ce
u:object_r:vendor_data_file:s0                    vendor_de

 

查看赋予某一个对象的权限sepolicy

cat /vendor/etc/selinux/vendor_sepolicy.cil | grep "allow xxxx_service"

 

selinux dac_override/dac_read_search问题处理思路

https://blog.csdn.net/u013377887/article/details/111568465

 

 

在seapp_contexts中为特定APK新建SELinux权限域

https://blog.csdn.net/suixin______/article/details/125422576

一些规则:/system/sepolicy/private/seapp_contexts 

运行时的匹配:/external/selinux/libselinux/src/android/android_platform.c

 

isPrivApp=true时,匹配的目录

/system/priv-app/app-debug.apk

/product/priv-app/app-debug.apk

/system_ext/priv-app/app-debug.apk

 

注:使用seapp_contexts的自定义规则可以实现“只允许特定的app访问特定的目录” or  “只允许特定的app访问特定的service process”

 

Android源码中有一些例子:比如 /device/google/sunfish-sepolicy/private/

seapp_contexts中指定app的domain为 wfc_activation_app

wfc_activation_app# Domain for WfcActivation app
user=_app seinfo=wfcactivation name=com.google.android.wfcactivation domain=wfc_activation_app levelFrom=all

自定义wfc_activation_app.te

这样如果某些特定目录或Process只允许特定的app访问,就可以使用它自己的domain独立授权,而避免使用 platform_app or untrusted_app等对某类型的所有app授权。

 

留意seinfo, 和app signature 有关,  对于platform app 有 seinfo=platform, 对于untrusted app好像默认是 seinfo=default,   isPrivApp=true时,可以不指定seinfo

 

 

These device policy files can be configured through the use of
the BOARD_VENDOR_SEPOLICY_DIRS variable. This variable should be set
in the BoardConfig.mk file in the device or vendor directories.

BOARD_VENDOR_SEPOLICY_DIRS contains a list of directories to search
for additional policy files.

 

# vendor sepolicy
BOARD_VENDOR_SEPOLICY_DIRS += device/xxxx/xxxx/xxxx/sepolicy

 

posted on 2022-11-20 10:35  二的次方  阅读(615)  评论(0编辑  收藏  举报