Android P 中selinux

说在前面,文章大部分参考https://source.android.com/security/selinux/

1.一些基本概念

SELinux 运行的原则是:所有没有明确指定为allow的操作都会被拒绝。

两种模式:

Permissive mode, 会有denialslog,但是不会强制执行。在前期porting的时候可以使用这种mode。

Enforcing mode, 会有denialslog,也会强制执行。在基本跑通之后还是尽快切到这个mode,尽早发现更多问题。

 

policy rule:

Sockets, files, and processes 都有label:  user:role:type:mls_level 。

RULE_VARIANT SOURCE_TYPES TARGET_TYPES : CLASSES PERMISSIONS

例如:allow domains types:classes permissions

  • Domain – 进程的标签。也叫一个domain type,因为就是进程的type.
  • Type – objectfile,socket等)的标签
  • Class – 被访问的object的类型(例如,文件,socket等)
  • Permission – 执行的操作(读,写等)

这些rules会在.te文件中,并且type 和attribute的声明也是在te文件中。

 

关于attribute: 

我们可以指定一组domain或者type为一个attribute. 如果一个rule是针对一个attribute,那么也就对关联到这个attribute的domain和type都适用。

Domain attribute和所有的进程的domain相关联;

File_type attribute和所有的file type相关联。

 

context file:

Context 文件是用来给object打标签的文件。

  • file_contexts 是给file分派标签的文件。如果需要添加一个file_contexts,可以通过重新build image或者对需要修改标签的文件运行restorecon 
  • genfs_contexts filesystems分配标签,比如proc或者vfat不支持attributes.
  • property_contexts Android 系统的property分派标签来控制可以设置这些property的进程。这个设置在开机时init会解析。
  • service_contexts Android binder service 分派标签来控制哪些进程可以给这个service添加(注册)和查找(检查)binder reference。在启动时servicemanager 会读取这个设置。
  • seapp_contexts app进程和/data/data目录分派标签. 这个设置是在开机由installd读取或者在每个app launch 的之后被zygote读取,
  • mac_permissions.xml  依照appsignature或者package name app分配一个seinfo标签。这个设置会在启动的时候由system_server读取。

2. source code中的sepolicy

Android 8.0和更高的版本中,添加policyBOARD_SEPOLICY_DIRS 只会添加到vendor image

  • system/sepolicy/public.包含了导出给vendor-specific policy使用的policyPublic policy 的目的是在不同版本之间保持一致,所以可以在定制策略中包含任何/public的内容。因此,可以放到/publicpolicytype会更加严格。可以把这当成平台导出的policy API:任何处理/system/vendor之间接口的东西都属于这里。
  • system/sepolicy/private. 包含了system image 运行所必需的策略,但是vendor image不应该知道这些策略。
  • system/sepolicy/vendor./vendor的组件提供的policy,但是存在于核心平台tree(不是 device-specific目录) 这是build系统区分设备和全局组件的一个工件 从概念上来说这个其实是下面描述的device-specific policy 的一部分。
  • device/manufacturer/device-name/sepolicy. 包含了device-specific policy 。还包含设备对policy的定制化,也就是在Android 8.0和更高的版本中对应于vendor image中的组件的policy。这部分是需要在mk中添加到BOARD_SEPOLICY_DIRS 中的。也是ODM需要添加的部分。

3. 在机台中的sepolicy相关文件

Vendor/etc/selinux

1).两个cil 文件

  1.1 plat_pub_versioned.cil -->public platform policy

  1.2 vendor_sepolicy.cil-->vendor and BOARD_SEPOLICY_DIRS policy

2). 一些contexts 文件

vendor_file_contexts vendor_hwservice_contexts vendor_property_contexts vendor_seapp_contexts vndservice_contexts -->主要是BOARD_SEPOLICY_DIRS下面的contexts几个文件拼起来的

3.)其他几个文件

plat_sepolicy_vers.txt

precompiled_sepolicy

precompiled_sepolicy.plat_and_mapping.sha256

vendor_mac_permissions.xml

System/etc/selinux

├── mapping

 │   ├── 26.0.cil-->system/sepolicy/private/compat/26.0/26.0.cil

    ├── 27.0.cil-->system/sepolicy/private/compat/27.0/27.0.cil

 │    └── 28.0.cil-->system/sepolicy/public中的type 映射的cil. 格式为:

                (typeattributeset proc_pid_max_28_0 (proc_pid_max))

                (expandtypeattribute (proc_pid_max_28_0) true)

                (typeattribute proc_pid_max_28_0)

├── plat_and_mapping_sepolicy.cil.sha256 -->校验值

├── plat_file_contexts -->system/sepolicy/private/+vendor/app-prebuilt/data/sepolicy/                                                      

                                                 +device/qcom/sepolicy/private/

├── plat_hwservice_contexts-->system/sepolicy/private/hwservice_contexts

├── plat_mac_permissions.xml--> system/sepolicy/private/mac_permissions.xml

├── plat_property_contexts-->system/sepolicy/private/+device/qcom/sepolicy/private/

├── plat_seapp_contexts-->system/sepolicy/private/seapp_contexts

├── plat_service_contexts-->system/sepolicy/private/+vendor/app-prebuilt/data/sepolicy/                                                      

                                                      +device/qcom/sepolicy/private/

 

├── selinux_denial_metadata-->sytsem/sepolicy/private/bug_map 

└── plat_sepolicy.cil-->system/sepolicy/private/ +system/sepolicy/public中的policy

4.兼容性

在Android p中,有一些为了之后的兼容性而有一些做法。

4.1. public中的type映射为_vNattribute(其中vN是版本。)

     (typeattributeset foo_vN (foo)) -->vN.cil

4.2. Platform-public policy导出为allow source_foo target_bar:class perm; 会被包入到vendor policy中。在编译的时候这会转化为vendor image 中的policy(在转化后的CIL中):

 (allow source_foo_vN target_bar_vN (class (perm))) -->plat_pub_versioned.cilplat_sepolicy.cil

4.3 对于platform 升级,会有以下几种情况。

4.3.1.Same type

例如/dev/binder/dev/binder在所有的版本中的label都是binder_device。这体现在转换之后的policy中如下:

binder_device_v1 … binder_device_vN

v1->v2升级时,platform policy必须包含:

type binder_device; -> (type binder_device) (in CIL)

v1的映射文件中(CIL v1.cil:

(typeattributeset binder_device_v1 (binder_device))

v2的映射文件中(CILv2.cil:

 (typeattributeset binder_device_v2 (binder_device))

v1vendor policy(CILplat_plat_versioned.cil):

(typeattribute binder_device_v1)
(allow binder_device_v1 …)

v2vendor policy(CILplat_plat_versioned.cil)

(typeattribute binder_device_v2)
(allow binder_device_v2 …)

4.3.2.New types

例如新加的typesysfs_A v1->v2升级时,platform policyboth plat_pub_versioned.cilplat_sepolicy.cil必须包含:

type sysfs_A; -> (type sysfs_A) (in CIL)
type sysfs; (type sysfs) (in CIL)

v1的映射文件中(CILv1.cil:

(typeattributeset sysfs_v1 (sysfs sysfs_A))

v2的映射文件中(CILv2.cil:

(typeattributeset sysfs_v2 (sysfs))
(
typeattributeset sysfs_A_v2 (sysfs_A))

v1vendor policy(CILplat_pub_versioned.cil):

(typeattribute sysfs_v1)
(allow … sysfs_v1 …)

v2vendor policy(CILplat_pub_versioned.cil):

(typeattribute sysfs_A_v2)
(allow … sysfs_A_v2 …)
(
typeattribute sysfs_v2)
(allow … sysfs_v2 …)

4.3.3 Removed types

 (typeattribute sysfs_v1)
(allow … sysfs_v1 …)

4.3.3.1.Example Version 1: Collapsing types (removing sysfs_A)

v1->v2升级时,platform policy必须包含:

type sysfs; (type sysfs) (in CIL)

v1的映射文件中(CIL:

(typeattributeset sysfs_v1 (sysfs))
(type
sysfs_A) # in case vendors used the sysfs_A label on objects
(
typeattributeset sysfs_A_v1 (sysfs sysfs_A))

v2的映射文件中(CIL:

(typeattributeset sysfs_v2 (sysfs))

v1vendor policy(CIL):

(typeattribute sysfs_A_v1)
(allow … sysfs_A_v1 …)
(
typeattribute sysfs_v1)
(allow … sysfs_v1 …)

v2vendor policy(CIL):

(typeattribute sysfs_v2)
(allow … sysfs_v2 …)

4.3.3.2.Example Version 2: Removing completely (foo type)

v1->v2升级时,platform policy必须包含:

# nothing - we got rid of the type

v1的映射文件中(CIL:

(type foo) #needed in case vendors used the foo label on objects
(
typeattributeset foo_v1 (foo))

v2的映射文件中(CIL:

# nothing - get rid of it

v1vendor policy(CIL):

(typeattribute foo_v1)
(allow foo …)
(
typeattribute sysfs_v1)
(allow sysfs_v1 …)

v2vendor policy(CIL):

(typeattribute sysfs_v2)
(allow sysfs_v2 …)

 

PS:其实主要是参考官网上的内容做的一点整理。后面后更多理解再来编辑。

 

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值