SEAndroid的各种策略文件

selinux的配置规则:
首先要了解sepolicy的结构:
a. App进程 -> mac_permissions.xml
b. App数据文件 -> seapp_contexts
c. 系统文件 -> file_contexts
d. 系统属性 -> property_contexts

class命令的格式为:
class class_name [ inherits common_name ] { permission_name … }
inherits表示继承了common定义的权限,然后自己额外实现了permission_name的权限

type_transition system wifi_data_file:sock_file system_wpa_socket;
当一个类型为system的类别去进行wifi_data_file类型的sock_file访问时,类型默认切换到system_wpa_socket

如果下面这条语句想要执行成功
type_transition init_t apache_exec_t:process apache_t;
至少首先声明下面的三条规则:
allow init_t apache_exec_t:file execute;
allow init_t apache_t:process transition;
allow apache_t apache_exec_t:file entrypoint;
type_transition和type_change的语法规则是一样的, type_change规则的影响不会在内核中生效,而是依赖于用户空间应用程序,如login或sshd

File 类型:

– 类型定义: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te 
– 绑定类型: external/sepolicy/file_contexts;device/mediatek/common/sepolicy/file_contexts

虚拟File 类型: 
– 类型定义: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te 
– 绑定类型: external/sepolicy/genfs_contexts;device/mediatek/common/sepolicy/genfs_contexts

Service 类型: 
– 类型定义: external/sepolicy/service.te; device/mediatek/common/sepolicy/service.te 
– 绑定类型:external/sepolicyservice_contexts;device/mediatek/common/sepolicy/service_contexts

Property 类型: 
– 类型定义: external/sepolicy/property.te;device/mediatek/common/sepolicy/property.te 
– 绑定类型: external/sepolicy/property_contexts;device/mediatek/common/sepolicy/property_contexts;

http://blog.sina.com.cn/s/blog_dae890d10101f0i2.html

在external/sepolicy目录存放了很多SELinux的策略定义文件。通过这些文件我们能了解到SEAndroid更多的细节,下面我们一起看看这些文件的内容。
1. 角色定义文件roles

角色定义文件用来定义SELinux系统的角色。文件roles的内容如下:
role r;
role r types domain;
从这里可以看到,SEAndroid实际上只定义了一种角色r。
2. 用户定义文件users

用户定义文件用来定义用户,前面我们介绍了,SELinux中的用户可以有三种,但是SEAndroid中只定义了一种u,下面是文件user的内容:
user u roles { r } level s0 range s0 - mls_systemhigh;
无论是user,还是role,在SEAndroid目前的定义中都只有一种,所以暂时也就没有大的用处。
3. 属性定义文件attributes

属性定义文件attributes中定义了所有type定义中需要用到的属性值,如下所示:

attribute dev_type;                # 表示设备文件
attribute domain;              # 表示域
attribute fs_type;                 # 表示文件系统
attribute file_type;               # 表示所有的普通文件
attribute exec_type;               # 表示所有可执行文件
attribute data_file_type;          # 表示所有数据文件
attribute sysfs_type;          # 表示sysfs类型的文件
attribute sdcard_type;         # 表示sdcard中的文件
attribute node_type;               # 表示网络中的host节点
attribute netif_type;          # 表示网络接口
attribute port_type;               # 表示网络的端口
attribute property_type;           # 表示所有Android的属性
attribute mlstrustedsubject;       # 表示系统中所有受信任的主体
attribute mlstrustedobject;        # 表示系统中所有受信任的客体
attribute unconfineddomain;        # 表示系统中哪些没有任何限制的域
attribute appdomain;               # 表示系统中所有的app的域
attribute netdomain;               # 表示系统中所有能访问网络的app的域
attribute bluetoothdomain;     # 表示系统中所有能访问bluetooth的app的域
attribute binderservicedomain; # 表示系统中所有的binder server的域
attribute platformappdomain;       # 表示系统中所有平台提供的app的域
attribute relabeltodomain;     # 表示系统中所有能够重新设置安全上下文的域
  1. Class定义文件security_classes

在规则定义语句中会用到客体限制类别(Object Class),文件security_classes定义了所有系统中用到的class,如下所示:

class security
class process
class system
class capability

## file-related classes  ##
class filesystem
class file
class dir
class fd
class lnk_file
class chr_file
class blk_file
class sock_file
class fifo_file
....

security_classes文件比较长,这里只列举了其中的一部分,class的定义比较容易理解,基本上是对系统资源一个细化的类别定义。定义客体的类型(type)时,附加属性相当于指定了客体能代表的一个比较粗的范围,在allow规则中加上客体限制类别后,相当于把客体限制到了一个更小的范围。
5. 操作定义文件access_vectors

allow语句的最后一项为允许的操作,所有的操作都在文件access_vectors中定义,文件的部分内容如下:

common file
{
ioctl
read
write
create
getattr
......
}
......
class filesystem
{
mount
remount
unmount
getattr
relabelfrom
relabelto
transition
associate
quotamod
quotaget
}

class dir
inherits file
{
add_name
remove_name
reparent
search
rmdir
open
audit_access
execmod
}
......

access_vectors文件通过两种方式定义操作,一种方式是通过common语句,这种方式定义的操作是一种公共的操作,没有限定哪种类别的客体可以使用,还可以被继承。另一种定义的方式是通过class语句,但是calss语句后面的名称必须是某种客体限制类别,这也意味着通过class语句定义的操作只能使用在相应的客体限制类别中。class语句可以继承common语句中定义的操作。
6. 类型强制规则文件

目录中凡是以te结尾的文件都属于类型强制规则文件(Type Enforcement)。它主要有类型定义和规则定义两部分组成。我们看看su.te文件的内容:

type su, domain;
permissive su;
type su_exec, file_type;
domain_auto_trans(shell, su_exec, su)
su is unconfined.
unconfined_domain(su)

su.te中定义了两种类型:su和su_exec。su用在进程的安全上下文中,su_exec则用在文件的安全上下文中。su.te中还调用了两个宏,domain_auto_trans宏我们前面已经分析过了,用来规定在shell执行su文件时将进程转移到su域。unconfined_domain宏则用来把su域定义成一个不受限制的域。
7. TE的宏定义文件te_macros

te_macros 文件中定义了在TE规则文件中用到的宏。前面我们已经介绍了domain_auto_trans宏。下面我们再看看unconfined_domain宏是如何定义的:
define(unconfined_domain',
typeattribute 1mlstrustedsubject;typeattribute 1 unconfineddomain;
‘)
unconfined_domain中使用了typeattribute语句。 typeattribute语句的作用是指定类型(type)的属性,我们知道定义type时可以在后面用逗号分割后指定属性,typeattribute语句可以给定义好的类型增加属性。因此unconfined_domain(su)的结果是给域su增加了mlstrustedsubject和unconfineddomain两种属性。这两种属性分别代表了系统中所有可信任的客体和不受限制的主体,因此su域将拥有系统中类似以前系统中超级用户的权限。这里虽然通过规则给了su域相当大的权限,但是也能通过修改规则来限制su的权限。这就是SELinux的强大之处,它能灵活的通过配置文件来修改任何的访问权限。当然这也对系统管理员的能力提出了更高的要求。所有通常我们不需要去修改Android中的这些配置文件,但是我们需要能理解它们的含义。
8. file_contexts文件

file_contexts文件保存的是系统中所有文件的安全上下文定义,文件部分内容如下:
下面我们看看文件file_contexts的内容。

#line 1 "external/sepolicy/file_contexts"
###########################################
# Root
/             u:object_r:rootfs:s0

## Data files  ##
/adb_keys     u:object_r:rootfs:s0
/default.prop     u:object_r:rootfs:s0
/fstab\..*        u:object_r:rootfs:s0
/init\..*     u:object_r:rootfs:s0
/res(/.*)?        u:object_r:rootfs:s0
/ueventd\..*      u:object_r:rootfs:s0

# Executables
/charger      u:object_r:rootfs:s0
/init             u:object_r:rootfs:s0
/sbin(/.*)?       u:object_r:rootfs:s0
......

file_contexts文件的格式比较简单,每行的前半部分是文件的路径,后面是它的安全上下文的定义。从文件可以看到,这里的路径定义也支持通配符。

  1. property_contexts文件

property_contexts文件中保存的是系统中所有Android属性的安全上下文定义,内容如下:

#line 1 "external/sepolicy/property_contexts"
##########################
# property service keys
#
#
net.rmnet0              u:object_r:radio_prop:s0
net.gprs                u:object_r:radio_prop:s0
net.ppp                 u:object_r:radio_prop:s0
net.qmi                 u:object_r:radio_prop:s0
net.lte                 u:object_r:radio_prop:s0
net.cdma                u:object_r:radio_prop:s0
gsm.                    u:object_r:radio_prop:s0
persist.radio           u:object_r:radio_prop:s0
net.dns                 u:object_r:radio_prop:s0
sys.usb.config          u:object_r:radio_prop:s0
......

property_contexts文件的格式也相当简单,每行前面是属性,后面是它对应的安全上下文。

SELinux内核策略文件包含file_contexts配置文件、genfs_contexts配置文件、property_contexts配置文件、seapp_contexts配置文件和mac_permissions.xml配置文件,SE Android项目的开发人员仍然在对Android系统安全进行深入的研究,因此这些策略配置也处在随时变化中。其中genfs_contexts配置文件、property_contexts配置文件和seapp_contexts配置文件是专门为SE Android系统创建的,因此不属于传统SELinux策略
这些策略文件是在Android系统编译过程中产生并且被添加到ramdisk镜像当中,因此可以保证这些策略在系统启动的初期,映射系统分区之前,最先被加载。一旦数据分区被加载后,可以将策略文件放置在/data/system目录下并且将selinux.reload_policy属性置为1(使用setprop selinux.reload_policy 1 命令),之后重新加载/data/system目录下的策略文件,这种设置将会触发系统的init进程重新加载策略,同时重新启动ueventd和installd进程以保证它们可以重新加载与这两个服务相关的策略。需要注意的是,对于内核策略,需要在主机的编译环境中,重新编译sepolicy(make policy),并且更新到/data/system中,如果希望每次设备启动时都会自动加载/data/system下的策略文件,需要将setprop命令添加到init.rc(system/core/rootdir/init.rc)中。

2.1 seapp_contexts配置文件

Android L(5.1) seapp_contexts文件内容
seapp_contexts文件用来标记应用程序的进程和应用程序包所在的目录。该文件的源位置在external/sepolicy目录下,下面是可以再其中设置的选项(输入)
1).isSystemServer :布尔值,匹配系统级服务程序,在文件中只能被定义为真(true)一次,默认值为假(false)
2).user:字符串,匹配应用程序的用户,若为空或没有定义为任意用户,以*结尾的字符串将进行前缀匹配。user=_app将匹配任何一般应用UID,user=_isolated将匹配任意被隔离的服务UID
3).seinfo:字符串,匹配SELinux控制类型
4).name:字符串,匹配应用名称,如com.android.deskclock
5).sebool:字符串,匹配布尔值,该字符串定义的布尔值为真时匹配

SELinux会通过该定义文件为匹配的应用程序找到对应的结果(输出)
1).domain:字符串,程序所属于域
2).type:字符串,程序所属类型
3).levelFromUid:布尔值,是否根据UID设置程序级别,当前只针对应用程序的UID。
4).level:字符串,应用程序的级别

根据这个结果,SELinux会为应用程序进程以及目录分配相应的权限。

2.2 property_contexts配置文件

Android L(5.1)property_contexts文件内容
property_contexts配置文件为权限检查定义了Android系统各属性间的安全关联。该文件为系统中的每一种服务类型定义了不同的属性,包括用户(user)、角色(role)、属性(property)和级别(level)。一种应用程序在调用某一服务资源时,系统将会根据这些属性检查是否有权限使用这些资源。
以下是目前各项属性可用的值。
1) 用户:u,系统默认,唯一值
2) 角色:object_r ,系统默认,唯一值
3) 属性:默认属性是default_prop,其他属性分别是:见上,如net_radio_prop、system_radio_prop、shell_prop …
4) 级别:s0 ,系统默认,唯一值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值