/***********************************
* Author:劉江明
* Environment:MTK Android 6.0
* Date:2016年11月05日
***********************************/
一. 基本語法
很多te文件集中在\external\sepolicy文件夾下,MTK也有很多自定義的在\device\mediatek\common\sepolicy。它的最基本樣式是
allow factory powerctl_prop:property_service set;
allow factory ttyGS_device:chr_file { read write open ioctl};
allow factory ttyMT_device:chr_file { read write open ioctl};
allow factory irtx_device:chr_file { read write ioctl open };
allow factory devpts:chr_file { read write getattr ioctl };
這些allow語句就是最基本的te語句了,相似的te語句的會被歸類在一個的te文件下面。如上面的語句是作用於factory,則會在
factory
.te文件里。
\external\sepolicy集中了很多系統定義的te文件
文件名 | 歸類 |
mac_permissions.xml | App進程 |
seapp_contexts | App數據文件 |
file_contexts | 系統文件 |
property_contexts | 系統屬性 |
最基本的語法為:
/*
rule_name:規則名,分別有allow,dontaudit,neverallow等
source_type:主要作用是用來填寫一個域(domain)
target_type:類型
class:類別,主要有File,Dir,Socket,SEAndroid還有Binder等
perm_set:動作集
*/
rule_name source_type target_type:class perm_set
我們
從上面拿一個實例下來分析一下:
/*
用中文來表述是:允許factory域里的進程或服務
對類型為ttyMT_device的類別為文件(file)
執行open,read,write,ioctl權限
*/
allow factory ttyMT_device:chr_file { read write open ioctl};
file_contexts里面顯式定義了哪些文件屬於ttyMT_device類型的
/dev/ttyMT.* u:object_r:ttyMT_device:s0
te表達式基本上就是這樣:
rule_name:規則名稱,除了有allow還有dontaudit,auditallow和neverallow
source_type:源類型,對應一個很重要的概念--------域(domain)
tartget_type:目標的類型,SELinux一個重要的判斷對象
class:類別,目標(客體)是哪種類別,主要有File,Dir,Socket,SEAndroid還有Binder等,在這些基礎上又細分出設備字符類型(chr_file),鏈接文件(lnk_file)等。可以通過ls -l查看文件類型
perm_set:動作集
從上到下按順序介紹一下:
rule_name:

allow:允許某個進程執行某個動作
auditallow:audit含義就是記錄某項操作。默認SELinux只記錄那些權限檢查失敗的操作。 auditallow則使得權限檢查成功的操作也被記錄。注意,allowaudit只是允許記錄,它和賦予權限沒關系。賦予權限必須且只能使用allow語句。
dontaudit:對那些權限檢查失敗的操作不做記錄。
neverallow:沒有被allow到的動作默認就不允許執行的。neverallow只是顯式地寫出某個動作不被允許,如果添加了該動作的allow,則會編譯錯誤
source_type:
指定一個“域”(domain),一般用於描述進程,該域內的的進程,受該條TE語句的限制。用type關鍵字,把一個自定義的域與原有的域相關聯
最簡單地定義一個新域的方式為:
type shell, domain
上面這句話的意思是,賦予shell給domain屬性,同時,shell與屬於domain這個集合里。如果有一個allow domain xxxxx 的語句,同樣地也給了shell xxxxx的屬性
target_type:
指定進程需要操作的客體(文件,文件夾等)類型,同樣是用type與一些已有的類型,屬性相關聯
以上面的
ttyMT_device為例:
//定義一個類型,屬於dev_type屬性
type ttyMT_device, dev_type;
//屬性dev_type在external/sepolicyattributes的定義如下
attribute dev_type;
attribute關鍵字定義一個屬性,type可以與一個或多個屬性關聯,如
type usb_device, dev_type, mlstrustedobject;
另外,還有一個關鍵字typeattribute,type有兩個作用,定義(聲明)並關聯某個屬性。可以把這兩個作用分開,type定義,typeattribute進行關聯
#定義httpd_user_content_t,並關聯兩個屬性
type httpd_user_content_t, file_type, httpdcontent;
分成兩條語句進行表述:
#定義httpd_user_content_t
type httpd_user_content_t;
#關聯屬性
typeattribute httpd_user_content_t file_type, httpdcontent;
然后你會有一個疑問,這么多屬性,這些屬性有什么作用,這些屬性會有一個地方顯式地說明這個屬性擁有什么權限,在external/sepolicy/domain里就有非常詳細的描述。另個在external/sepolicy/attributes里定義了很多屬性,下面截取了一些常見的定義
# All types used for devices.
attribute dev_type;
# All types used for processes.
attribute domain;
# All types used for filesystems.
attribute fs_type;
# All types used for files that can exist on a labeled fs.
# Do not use for pseudo file types.
attribute file_type;
# All types used for domain entry points.
attribute exec_type;
# All types used for property service
attribute property_type;
# All service_manager types created by system_server
attribute system_server_service;
# All domains that can override MLS restrictions.
# i.e. processes that can read up and write down.
attribute mlstrustedsubject;
# All types that can override MLS restrictions.
# i.e. files that can be read by lower and written by higher
attribute mlstrustedobject;
# All domains used for apps.
attribute appdomain;
# All domains used for apps with network access.
attribute netdomain;
# All domains used for binder service domains.
attribute binderservicedomain;
class:
客體的具體類別。用class來定義一個客體類別,具體定義方式 如下
[external/sepolicy/security_classes示例]
# file-related classes
class filesystem
class file #代表普通文件
class dir #代表目錄
class fd #代表文件描述符
class lnk_file #代表鏈接文件
class chr_file #代表字符設備文件
......
# network-related classes
class socket #socket
class tcp_socket
class udp_socket
......
class binder #Android平台特有的binder
class zygote #Android平台特有的zygote
perm_set:
具體的操作,系統的定義在
external/sepolicy/access_vectors。
有兩種定義方法。
用common命令定義:
格式為:common common_name { permission_name ... }
common定義的perm set能被另外一種perm set命令class所繼承
如:
common file {
ioctl read write create getattr setattr lock relabelfrom relabelto
append unlink link rename execute swapon quotaon mounton
用class命令定義:
class class_name [ inherits common_name ] { permission_name ... }
inherits表示繼承了某個common定義的權限
注意,class命令它不能被其他class繼承
繼承一個common,如繼承了file common
class dir
inherits file
{
add_name
remove_name
reparent
search
rmdir
open
audit_access
execmod
}
不繼承任何common,如
class binder
{
impersonate
call
set_context_mgr
transfer
}
然后是一些特殊的配置文件:
a. external/sepolicy/attributes -> 所有定義的attributes都在這個文件
b. external/sepolicy/access_vectors -> 對應了每一個class可以被允許執行的命令
c. external/sepolicy/roles -> Android中只定義了一個role,名字就是r,將r和attribute domain關聯起來
d. external/sepolicy/users -> 其實是將user與roles進行了關聯,設置了user的安全級別,s0為最低級是默認的級別,mls_systemHigh是最高的級別
e. external/sepolicy/security_classes -> 指的是上文命令中的class,個人認為這個class的內容是指在android運行過程中,程序或者系統可能用到的操作的模塊
f. external/sepolicy/te_macros -> 系統定義的宏全在te_macros文件
g. external/sepolicy/***.te -> 一些配置的文件,包含了各種運行的規則
----------------引用於《
android中SELINUX規則分析和語法簡介
》
二. TE的正則表達式和集合
TE文件支持正則表達式,從下面可以看到,通配符是常用的通配符,可以度娘
/sys/devices/system/cpu(/.*)? u:object_r:sysfs_devices_system_cpu:s0
/sys/power/wake_lock -- u:object_r:sysfs_wake_lock:s0
/sys/power/wake_unlock -- u:object_r:sysfs_wake_lock:s0
/sys/kernel/uevent_helper -- u:object_r:usermodehelper:s0
/sys/module/lowmemorykiller(/.*)? -- u:object_r:sysfs_lowmemorykiller:s0
#############################
# asec containers
/mnt/asec(/.*)? u:object_r:asec_apk_file:s0
/mnt/asec/[^/]+/[^/]+\.zip u:object_r:asec_public_file:s0
需要注意的是上面的"--",這里的“--”表示二進制文件,類似的還有
#‘-b’ - Block Device ‘-c’ - Character Device
#‘-d’ - Directory ‘-p’ - Named Pipe
#‘-l’ - Symbolic Link ‘-s’ - Socket
#‘--’ - Ordinary file
TE表達式里可以用“{}”來表示一個集合,如:
/*
允許user_t對bin_t類型的文件和文件夾執行read,getattr操作
*/
allow user_t bin_t : { file dir } { read getattr };
/*
允許domain對exec_type,sbin_t類型的文件執行execute的動作
*/
allow domain { exec_type sbin_t } : file execute;
可以在集合里使用“*”,“-” 和 “~” 三個通配符
/*
允許user_t對bin_t類型的文件和文件夾執行所有操作
*/
allow user_t bin_t : { file dir } *;
/*
允許user_t對bin_t類型的文件和文件夾執行除了read,getattr以外的所有操作
*/
allow user_t bin_t : { file dir } ~{ read getattr };
/*
允許domain對exec_type類型的文件執行execute的動作,除了sbin_t以外
*/
allow domain { exec_type -sbin_t } : file execute;
三. TE的類型轉換規則
為什么要轉換類型
init進程擁有系統的最高權限,如果由Init進程fork,execu出來的進程默認是與init相同的權限,這肯定是不安全的。另一個場景是,由init生成的文件,默認也是init的讀寫權限,不方便其他低權限的文件進行訪問。
類型轉換有兩種類型轉換:
1. 主體的域的轉換
2. 客體的轉換
域的轉換
type_transition的完整格式為:
type_transition source_type target_type : class default_type;
舉個例子
type_transition init_t apache_exec_t : process apache_t;
type_transition init_t apache_exec_t : process apache_t;
init_t 進程執行type為apache_exec_t的可執行文件時,新的進程轉換到apache_t域
但是上面只是告訴了轉換的過程,卻沒有說明,有轉換的權限,如果要上面的轉換成功,還需要下面的語句:
#首先,你得讓init_t域中的進程能夠執行type為apache_exec_t的文件
allow init_t apache_exec_t : file execute;
#然后,你還得告訴SELiux,允許init_t做DT切換以進入apache_t域
allow init_t apache_t : process transition;
#最后,你還得告訴SELinux,切換入口(對應為entrypoint權限)為執行pache_exec_t類型的文件
allow apache_t apache_exec_t : file entrypoint;
客體的轉換
例子:
type_transition passwd_t tmp_t : file passwd_tmp_t;
passwd_t在tmp_t目錄下創建文件時,該文件的類型轉化為passwd_tmp_t。這里默認隱含了一個tmp_t類型dir,因為file的容器只能是個dir。同樣的,如果要上面的語句運行成功,與需要有相應的權限說明:
對應的必須有兩個前提條件:
* The source domain needs permission to add file entries into the directory
這個process 必須有在這個目錄下添加文件的權限.
* The source domain needs permission to create file entries
這個process 必須有在這個目錄下創建以這個Security Context 為Label 的文件權限.
如果每個轉換之前都需要這樣繁鎖地權限聲音實在很麻煩。TE里允許把這些相同的,重復使用的語句定義成一個宏,類似於函數一樣。
四. TE的宏
如果把上面domain轉換的例子定義成一個宏,應該定義如下:
#定義domain_auto_trans宏,$1,$2等等代表宏的第一個,第二個....參數
define(`domain_auto_trans', `
# 先allow相關權限,domain_trans宏定義在后面
domain_trans($1,$2,$3)
# 然后設置type_transition
type_transition $1 $2:process $3;
')
#定義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 };
allow $3 $1:process sigchld;
dontaudit $1 $3:process noatsecure;
allow $1 $3:process { siginh rlimitinh };
')
上面的宏定義在external/sepolicy/te_macros里。客體的轉換定義如下:
#####################################
# file_type_auto_trans(domain, dir_type, file_type)
# Automatically label new files with file_type when
# they are created by domain in directories labeled dir_type.
#
define(`file_type_auto_trans', `
# Allow the necessary permissions.
file_type_trans($1, $2, $3)
# Make the transition occur by default.
type_transition $1 $2:dir $3;
type_transition $1 $2:notdevfile_class_set $3;
')
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.
allow $1 $3:notdevfile_class_set create_file_perms;
allow $1 $3:dir create_dir_perms;
')
TE的集合也可以定義成一個宏代替,如讀寫文件操作集的宏:
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 }')
使用方式是:
allow demo demo_device:chr_file rw_file_perms;
參考文章: