Android系统SELinux简单整理

SELinux分成了两部分,位于 /system/sepolicy 下的 platform 部分和位于 /device/vendorName/sepolicy 下的 vendor 部分。
对应开发板子上目录 /system/etc/selinux 下的 platform 部分和位于 /vendor/etc/selinux 下的 vendor 部分。

>>>>>> 哪些是 coredomain ???
Coredomain 是 attribute(属性),属于 domain (针对进程)或者 type(针对对象,如文件等)的集合。coremain 可以理解为包含 system 下可执行文件和 apps 所运行的 domain 或者说包含所有属于 Android 的 domain。总结为,Coredomain里包含一个或多个domain。

>>>> coredomain有两种定义方式????
    1. type XXX ..., coredomain ...;
    2. typeattribute XXX coredomain;
上面两种方式都是将 XXX 放入到 coredomain 集合中,即 XXX 是属于 coredomain,对 coredomain 配置的权限也会同步给 XXX,同样对 coredomain 的限制也会限制 XXX。

>>>>>>>工具查看???
Android的system/sepolicy/tools下sepolicy-analyze 工具,然后需要编译好的 sepolicy 文件,使用下面命令可以查看 coredomain 都包含那些 domain:
sepolicy-analyze 编译好的sepolicy  attribute coredomain

首先selinux是一种加强文件安全的一种策略。主要包含进程和文件对象。
在system\sepolicy\public\attributes文件中有:
(1)# All types used for processes.
attribute domain;      ##用于进程的所有类型
(2)# All core domains (as opposed to vendor/device-specific domains)
attribute coredomain;   ##和设备特性域相对的所有核心域

>>>>>>>>>>>>selinux有两种工作模式???
“permissive”:所有操作都被允许(即没有MAC),但是如果有违反权限的话,会记录日志
“enforcing”:所有操作都会进行权限检查

>>>>>>>>>>>>SEAndroid app分类
SELinux(或SEAndroid)将app划分为主要三种类型(根据user不同,也有其他的domain类型):
(1)untrusted_app 第三方app,没有Android平台签名,没有system权限
(2)platform_app 有android平台签名,没有system权限
(3)system_app 有android平台签名和system权限
从上面划分,权限等级,理论上:untrusted_app < platform_app < system_app

权限规则增加:
adb logcat | grep avc
查看对应的缺少的log 或者 adb shell进入提示后
dmesg | grep avc
标志性log 格式如下
avc: denied { 操作权限 } for pid=7201 comm=“进程名” scontext=u:r:源类型:s0
解析说明
Domain:u:r:logpersist:s0是一个“域”,它是一个进程或一组进程的标签。
Type:u:object_r:system_data_file:s0是一个“对象标签”,它是一个对象或一组对象的标签。
Class:dir 是客体的类型。(dir是文件夹,file是文件…)
Permission:write 是缺少的权限。

>>>>>>>查看SELinux模式命令:getenforce
更改SELinux模式命令:setenforce 1 设置为Enforcing
                     setenforce 0 设置为Permissive

>>>>>>> 安卓中快速编译sepolicy并验证(需要本地代码整编过一次,已经生成out目录)
$ mmm system/sepolicy/
$ adb push out/target/product/xxx/system/etc/selinux /system/etc/selinux
$ adb push out/target/product/xxx/vendor/etc/selinux /vendor/etc/selinux
也可单编systemimage,并刷机
$ make systemimage
$ adb reboot bootloader
$ fastboot flash system ./system.img
$ fastboot reboot

>>>>>>>>如何应对neverallow ??? 绕过CTS认证
在system/sepolicy/private/logpersist.te与system/sepolicy/prebuilts/api/29.0/private/logpersist.te中配置以下allow语句并编译,
会报neverallow的错。如下声明allow:
allow  logpersist  system_data_file:dir  write;
这表示谷歌不允许我们使用allow语句,解除限制的最暴力方法就是将报错处的neverallow语句删掉,这样确实可行,但是会过不了cts。
由于我们要访问的目录path为/data/syslog,将该目录定义成自己的Type,可以自定义Type,如下:
在file.te中自定义一个type为file_type,data_file_type,core_data_file_type:
(1)type log_data_file, file_type, data_file_type, core_data_file_type;
在file_contexts中定义安全上下文:
(2)/data/syslog(/.*)?            u:object_r:log_data_file:s0
在logpersist.te将allow语句改为:
(3)allow  logpersist  log_data_file write;
然后在logpersist.te中单独将自定义的log_data_file减去即可。(这里最好的是自定义一个service代替logpersist,那就要新建一个te文件了,比较麻烦)
neverallow logpersist {
  file_type
  userdebug_or_eng(`-misc_logd_file -coredump_file')
  with_native_coverage(`-method_trace_data_file')
  -log_data_file
}:file { create write append };


>>>>>>>>>>>如何新增domian域???(一般在平台相关目录下添加)
例子如下
在device/平台名/system/private/file_contexts 文件添加
# tcontext=u:object_r:sysfs:s0
/sys/kernel/display/abcd u:object_r:wxl_abcd:s0 #wxl_abcd替换sysfs,wxl_abcd随便取

在device/平台名/system/public/file.te 中添加
type wxl_cabc, fs_type,sysfs_type;

在 device/平台名/system/private/system_server.te 文件中添加
allow system_server wxl_abcd:file { r_file_perms w_file_perms rw_file_perms };
添加 write 或者 read 的权限要注意 open 的权限,最后使用 r_file_perms、w_file_perms、rw_file_perms。

>>>>>>>>>>> 添加设备文件节点权限(sysfs gpio管脚节点权限)???
/sys/class/leds/green/brightness //快捷方式
/sys/devices/soc.0/gpio-leds.66/leds/green/brightness //实际节点

操作LED灯的设备文件节点为APP层system app进程开放该节点访问权限(读或写),权限配置主要修改(一般在/device/平台/sepolicy/common)目录下的file.te、file_contexts和system_app.te三个文件
(1)file.te修改如下:
    # GPIO accessed by system app
    type sysfs_gpio, fs_type, sysfs_type;

(2)file_contexts修改如下:
    /sys/devices/soc/1010000.pinctrl/gpio/gpio62/value           u:object_r:sysfs_gpio:s0
    /sys/devices/soc/1010000.pinctrl/gpio/gpio63/value           u:object_r:sysfs_gpio:s0

(3)system_app.te修改如下:
    allow system_app sysfs_gpio:file rw_file_perms;

如果通过以上添加SELinux之后,仍没有权限读写sys或proc节点,需要到/system/core/rootdir/init.rc里面配置如下:
(1)修改设备节点用户所有者和所属用户组,以及它们所对应的权限
    chown system system 设备文件结点
    chmod 777 设备文件结点

>>>>>>>>>>>修改selinux没有生效???

将SELinux Policy 文件存放在下面目录
1). Google 原生目录 /system/sepolicy
2). 厂商配置目录 /device/厂商平台/sepolicy/
android将SELinux Policy 文件存放的te一般都是在device/平台/sepolicy 和 /system/sepolicy两个目录下,然后在这篇上面找到答案
https://www.jianshu.com/p/8b0e72776118
例如:RK平台android10.0中在device/rockchip/common/sepolicy下有private、public、vendor三个目录te文件,
      那么我们到底应该改private、public、vendor哪个路径呢?
      在device/rockchip/common/BoardConfig.mk定义了不同版本所要编译包含的目录。

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值