- /***********************************
- * 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};
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;
這些類型(安全上下文)會顯示地與一個“檔案”想關聯,如: file_contexts裡面顯式定義了哪些檔案屬于ttyMT_device類型,即用ls -Z顯示出來檔案的安全上下文
- /dev/ttyMT.* u:object_r:ttyMT_device:s0
虛拟檔案系統的辨別方式與普通的檔案系統檔案辨別方式不一樣,用genfscon來配置。
- genfscon的文法是:
- genfscon fs_type pathprefix [-file_type] context
把/proc/mtk_demo/demo_file檔案的安全上下文設定成demo_context
- genfscon proc /mtk_demo/demo_file u:object_r:demo_context:s0
網絡對象上下文
- 例1:定義端口的上下文
- portcon tcp 80 system_u:object_r:http_port_t
- portcon tcp 8080 system_u:object_r:http_port_t
-
- 例2:定義網絡接口的上下文:
- netifcon eth0 system_u:object_r:netif_eth0_t system_u:object_r:netmsg_eth0_t
-
- 例3:定義節點的上下文
- nodecon 10.33.10.66 255.255.255.255 system_u:object_r:node_zeus_t;
- nodecon 10.33.10.0 255.255.255.0 system_u:object_r:node_any_t
然後你會有一個疑問,這麼多屬性,這些屬性有什麼作用,這些屬性會有一個地方顯式地說明這個屬性擁有什麼權限,在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;
參考文章: http://blog.csdn.net/Innost/article/details/19299937?locationNum=2 系列文章 http://www.2cto.com/kf/201504/390742.html http://blog.csdn.net/luoshengyang/article/details/35392905 系列文章 http://blog.csdn.net/myarrow/article/details/10105961
- /***********************************
- * 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};
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;
這些類型(安全上下文)會顯示地與一個“檔案”想關聯,如: file_contexts裡面顯式定義了哪些檔案屬于ttyMT_device類型,即用ls -Z顯示出來檔案的安全上下文
- /dev/ttyMT.* u:object_r:ttyMT_device:s0
虛拟檔案系統的辨別方式與普通的檔案系統檔案辨別方式不一樣,用genfscon來配置。
- genfscon的文法是:
- genfscon fs_type pathprefix [-file_type] context
把/proc/mtk_demo/demo_file檔案的安全上下文設定成demo_context
- genfscon proc /mtk_demo/demo_file u:object_r:demo_context:s0
網絡對象上下文
- 例1:定義端口的上下文
- portcon tcp 80 system_u:object_r:http_port_t
- portcon tcp 8080 system_u:object_r:http_port_t
-
- 例2:定義網絡接口的上下文:
- netifcon eth0 system_u:object_r:netif_eth0_t system_u:object_r:netmsg_eth0_t
-
- 例3:定義節點的上下文
- nodecon 10.33.10.66 255.255.255.255 system_u:object_r:node_zeus_t;
- nodecon 10.33.10.0 255.255.255.0 system_u:object_r:node_any_t
然後你會有一個疑問,這麼多屬性,這些屬性有什麼作用,這些屬性會有一個地方顯式地說明這個屬性擁有什麼權限,在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;
參考文章: http://blog.csdn.net/Innost/article/details/19299937?locationNum=2 系列文章 http://www.2cto.com/kf/201504/390742.html http://blog.csdn.net/luoshengyang/article/details/35392905 系列文章 http://blog.csdn.net/myarrow/article/details/10105961