From 061a9ab065f21a9605860cf570f90cfb8abb4c1d Mon Sep 17 00:00:00 2001 From: "rick.chan" Date: Wed, 2 Sep 2020 21:17:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=AF=AD=E6=B3=95,=E9=80=9A?= =?UTF-8?q?=E9=A1=BA=E8=AF=AD=E5=8F=A5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rick.chan --- .../Android/SELinux/Android_SELinux_详解.md | 136 ++++++++++-------- 1 file changed, 76 insertions(+), 60 deletions(-) diff --git a/Software/Development/OperatingSystem/Android/SELinux/Android_SELinux_详解.md b/Software/Development/OperatingSystem/Android/SELinux/Android_SELinux_详解.md index d74881d..176599a 100644 --- a/Software/Development/OperatingSystem/Android/SELinux/Android_SELinux_详解.md +++ b/Software/Development/OperatingSystem/Android/SELinux/Android_SELinux_详解.md @@ -32,14 +32,14 @@ androidboot.selinux=disabled 在 Android 系统中,可在系统源码中,通过 TE 文件设置程序/文件的 SELinux 属性。 -SELinux 分为文件属性和进程属性(运行属性),文件属性被 file_contexts 使用: +SELinux 分为文件属性和进程属性(运行属性),普通文件属性定义在 file_contexts 中: ```bash # file_contexts /(vendor|system/vendor)/bin/demo u:object_r:demo_exec:s0 ``` -可通过 ls -Z 查看谋文件的 SELinux 文件属性,通过 ps -Z 查看谋运行中程序的进程属性,属性非为 4 组,如: +可通过 ls -Z 查看谋文件的 SELinux 文件属性,通过 ps -Z 查看谋运行中程序的进程属性,属性分为 4 组,如: ```bash u:r:init:s0 @@ -47,6 +47,22 @@ u:r:init:s0 上述的 u 为 Android 的唯一 user(有待确认);第二个参数 r 表示进程,object_r 表示文件;第三个参数是这个进程的 type,在 Android 里面,定义了 100 多个 type;第四个参数 s0 是一个安全等级。 +以下是一个 TE 文件示例: + +```bash +type demo, domain; +type demo_exec, exec_type, vendor_file_type, file_type; +init_daemon_domain(demo) + +allow demo self:capability { sys_admin }; +allow demo sysfs:file { create open read write }; +allow demo device:dir { create open read write }; +``` + +下面将对该文件的各部分进行讲解。 + +### 3.1.基本语法 + 在 TE 文件中,我们一般遇到的语法是这样的: ```bash @@ -67,19 +83,19 @@ allow factory ttyMT_device:chr_file { read write open ioctl}; TE 表达式基本上就是这样,下文将按顺序介绍 rule_name、source_type、target_type、class 和 perm_set。 -### 3.1.rule_name +### 3.2.rule_name allow:允许某个进程执行某个动作。 -auditallow:audit含义就是记录某项操作。默认SELinux只记录那些权限检查失败的操作。 auditallow则使得权限检查成功的操作也被记录。注意,allowaudit只是允许记录,它和赋予权限没关系。赋予权限必须且只能使用allow语句。 +auditallow:audit 含义就是记录某项操作。默认 SELinux 只记录那些权限检查失败的操作。 auditallow 则使得权限检查成功的操作也被记录。注意,allowaudit 只是允许记录,它和赋予权限没关系。赋予权限必须且只能使用 allow 语句。 dontaudit:对那些权限检查失败的操作不做记录。 -neverallow:没有被allow到的动作默认就不允许执行的。neverallow只是显式地写出某个动作不被允许,如果添加了该动作的allow,则会编译错误。 +neverallow:没有被 allow 到的动作默认就不允许执行的。neverallow 只是显式地写出某个动作不被允许,如果添加了该动作的 allow,则会编译错误。 -### 3.2.source_type +### 3.3.source_type -指定一个“域”(domain),一般用于描述进程,该域内的的进程,受该条TE语句的限制。用type关键字,把一个自定义的域与原有的域相关联 +指定一个“域”(domain),一般用于描述进程,该域内的的进程,受该条 TE 语句的限制。用 type 关键字,把一个自定义的域与原有的域相关联。 最简单地定义一个新域的方式为: @@ -87,9 +103,9 @@ neverallow:没有被allow到的动作默认就不允许执行的。neverallow type shell, domain ``` -上面这句话的意思是,赋予 shell 给 domain 属性,同时,shell 与属于 domain 这个集合里。如果有一个 allow domain xxxxx 的语句,同样地也给了 shell xxxxx 的属性。 +上面这句话的意思是,将 domain 的全部属性赋予 shell。如果 domain 集合里有一个 allow domain xxxxx,那么 shell 将会继承它。 -### 3.3.target_type +### 3.4.target_type 指定进程需要操作的客体(文件,文件夹等)类型(安全上下文),同样是用 type 与一些已有的类型,属性相关联 @@ -98,17 +114,17 @@ type shell, domain ```bash # 定义一个类型,属于 dev_type 属性 type ttyMT_device, dev_type; -# 属性 dev_type 在 external/sepolicyattributes 的定义如下 -attribute dev_type; +# 属性 dev_type 在 external/sepolicyattributes 的定义如下: attribute dev_type; +# 以上 attribute 关键字用于定义一个属性 ``` -attribute 关键字定义一个属性,type 可以与一个或多个属性关联,如 +type 可以与一个或多个属性关联,如 ```bash type usb_device, dev_type, mlstrustedobject; ``` -另外,还有一个关键字 typeattribute,type 有两个作用,定义(声明)并关联某个属性。可以把这两个作用分开,type 定义,typeattribute 进行关联 +另外,还有一个关键字 typeattribute,type 有两个作用,定义(声明)并关联某个属性。可以把这两个作用分开:用 type 进行定义;用 typeattribute 进行关联。示例如下: ```bash # 定义 httpd_user_content_t,并关联两个属性 @@ -120,7 +136,7 @@ type httpd_user_content_t; typeattribute httpd_user_content_t file_type, httpdcontent; ``` -这些类型(安全上下文)会显示地与一个“文件”想关联,如: +这些类型(安全上下文)会显示地与一个“文件”相关联,如: file_contexts 里面显式定义了哪些文件属于 ttyMT_device 类型,即用 ls -Z 显示出来文件的安全上下文 @@ -135,13 +151,13 @@ file_contexts 里面显式定义了哪些文件属于 ttyMT_device 类型,即 genfscon fs_type pathprefix [-file_type] context ``` -把 /proc/mtk_demo/demo_file 文件的安全上下文设置成 demo_context: +例如,把 /proc/mtk_demo/demo_file 文件的安全上下文设置成 demo_context: ```bash genfscon proc /mtk_demo/demo_file u:object_r:demo_context:s0 ``` -#### 3.3.1.网络对象上下文 +#### 3.4.1.网络对象上下文 ```bash # 例1:定义端口的上下文 @@ -186,9 +202,9 @@ attribute netdomain; attribute binderservicedomain; ``` -### 3.4.class +### 3.5.class -客体的具体类别。用 class 来定义一个客体类别,具体定义方式 如下 +客体的具体类别。用 class 来定义一个客体类别,具体定义方式如下: ```bash # [external/sepolicy/security_classes示例] @@ -209,7 +225,7 @@ class binder # Android 平台特有的 binder class zygote # Android 平台特有的 zygote ``` -### 3.5.perm_set +### 3.6.perm_set 具体的操作,系统的定义在 external/sepolicy/access_vectors。有两种定义方法。 @@ -222,7 +238,7 @@ common common_name { permission_name ... } # 如: common file { ioctl read write create getattr setattr lock relabelfrom relabelto - append unlink link rename execute swapon quotaon mounton + append unlink link rename execute swapon quotaon mounton } ``` 用 class 命令定义: @@ -236,22 +252,22 @@ class class_name [ inherits common_name ] { permission_name ... } class dir inherits file { - add_name - remove_name - reparent - search - rmdir - open - audit_access - execmod + add_name + remove_name + reparent + search + rmdir + open + audit_access + execmod } # 不继承任何 common,如 class binder { - impersonate - call - set_context_mgr - transfer + impersonate + call + set_context_mgr + transfer } ``` @@ -260,9 +276,9 @@ class binder ```bash # 所有定义的 attributes 都在这个文件 external/sepolicy/attributes -# 对应了每一个class可以被允许执行的命令 +# 对应了每一个 class 可以被允许执行的命令 external/sepolicy/access_vectors -# Android 中只定义了一个 role,名字就是r,将 r 和 attribute domain 关联起来 +# Android 中只定义了一个 role,名字就是 r,将 r 和 attribute domain 关联起来 external/sepolicy/roles # 其实是将 user 与 roles 进行了关联,设置了 user 的安全级别,s0 为最低级是默认的级别,mls_systemHigh 是最高的级别 external/sepolicy/users @@ -274,7 +290,7 @@ external/sepolicy/te_macros external/sepolicy/***.te ``` -### 3.6.TE 的正则表达式和集合 +### 3.7.TE 的正则表达式和集合 TE 文件支持正则表达式,从下面可以看到,通配符是常用的通配符,可以度娘 @@ -319,7 +335,7 @@ allow user_t bin_t : { file dir } ~{ read getattr }; allow domain { exec_type -sbin_t } : file execute; ``` -### 3.7.TE 的类型转换规则 +### 3.8.TE 的类型转换规则 为什么要转换类型? @@ -330,7 +346,7 @@ init 进程拥有系统的最高权限,如果由 Init 进程 fork,exec 出 1. 主体的域的转换 2. 客体的转换 -#### 3.7.1.域的转换 +#### 3.8.1.域的转换 type_transition 的完整格式为: @@ -349,15 +365,15 @@ init_t 进程执行 type 为 apache_exec_t 的可执行文件时,新的进程 但是上面只是告诉了转换的过程,却没有说明,有转换的权限,如果要上面的转换成功,还需要下面的语句: ```bash -#首先,你得让init_t域中的进程能够执行type为apache_exec_t的文件 +# 首先,你得让 init_t 域中的进程能够执行 type 为 apache_exec_t 的文件 allow init_t apache_exec_t : file execute; -#然后,你还得告诉SELiux,允许init_t做DT切换以进入apache_t域 +# 然后,你还得告诉 SELiux,允许 init_t 做 DT 切换以进入 apache_t 域 allow init_t apache_t : process transition; -#最后,你还得告诉SELinux,切换入口(对应为entrypoint权限)为执行pache_exec_t类型的文件 +# 最后,你还得告诉 SELinux,切换入口(对应为 entrypoint 权限)为执行 apache_exec_t 类型的文件 allow apache_t apache_exec_t : file entrypoint; ``` -#### 3.7.2.客体的转换 +#### 3.8.2.客体的转换 例子: @@ -374,21 +390,21 @@ passwd_t 在 tmp_t 目录下创建文件时,该文件的类型转化为 passwd 如果每个转换之前都需要这样繁锁地权限声音实在很麻烦。TE里允许把这些相同的,重复使用的语句定义成一个宏,类似于函数一样。 -### 3.8.TE的宏 +### 3.9.TE 宏 -如果把上面domain转换的例子定义成一个宏,应该定义如下: +如果把上面 domain 转换的例子定义成一个宏,应该定义如下: ```bash -#定义domain_auto_trans宏,$1,$2等等代表宏的第一个,第二个....参数 -define(`domain_auto_trans', ` - # 先allow相关权限,domain_trans宏定义在后面 +# 定义 domain_auto_trans 宏,$1,$2 等等代表宏的第一个,第二个....参数 +define('domain_auto_trans', ' + # 先 allow 相关权限, domain_trans 宏定义在后面 domain_trans($1,$2,$3) - # 然后设置type_transition + # 然后设置 type_transition type_transition $1 $2:process $3; ') -#定义domain_trans宏。 -define(`domain_trans', ` - # SEAndroid在上述三个最小权限上,还添加了自己的一些权限 +# 定义 domain_trans 宏 +define('domain_trans', ' + # SEAndroid 在上述三个最小权限上,还添加了自己的一些权限 allow $1 $2:file { getattr open read execute }; allow $1 $3:process transition; allow $3 $2:file { entrypoint read execute }; @@ -406,7 +422,7 @@ define(`domain_trans', ` # Automatically label new files with file_type when # they are created by domain in directories labeled dir_type. # -define(`file_type_auto_trans', ` +define('file_type_auto_trans', ' # Allow the necessary permissions. file_type_trans($1, $2, $3) # Make the transition occur by default. @@ -414,7 +430,7 @@ type_transition $1 $2:dir $3; type_transition $1 $2:notdevfile_class_set $3; ') -define(`file_type_trans', ` +define('file_type_trans', ' # Allow the domain to add entries to the directory. allow $1 $2:dir ra_dir_perms; # Allow the domain to create the file. @@ -426,14 +442,14 @@ allow $1 $3:dir create_dir_perms; TE 的集合也可以定义成一个宏代替,如读写文件操作集的宏: ```bash -define(`x_file_perms', `{ getattr execute execute_no_trans }') -define(`r_file_perms', `{ getattr open read ioctl lock }') -define(`w_file_perms', `{ open append write }') -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 }') +define('x_file_perms', '{ getattr execute execute_no_trans }') +define('r_file_perms', '{ getattr open read ioctl lock }') +define('w_file_perms', '{ open append write }') +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 }') ``` 使用方式是: