天天看點

Android系統10 RK3399 init程序啟動(二十七) Selinux Type和Attribute

說明

系統:Android10.0

裝置: FireFly RK3399 (ROC-RK3399-PC-PLUS)

前言

本章節重點介紹selinux 政策檔案te中經常用到的type和attribute。

一, Type

Selinux實作的是強制類型通路模型, 可以簡單了解一切都是類型, 不管是主體還是客體,都必須類型化, 如客體和客體和主體都需要設定安全上下文, 客體格式為u:r:xxx:s0, 主體為u:object_r:yyy:s0,  其中xxx和yyy是可以自定義的, 但是需要通過type進行類型化聲明, 你可以了解type為一個動詞,為xxx取名,客體一般聲明type, 主體一般聲明為domain。type的完整格式:

 type type_id [, attribute_id,][attribute_id,] …;

方括号表示可選, attribute_id表示和type_id進行關聯的屬性,可以是多個,之間用逗号隔開

例子如下:

type device, dev_type, fs_type;

此處表示聲明(或了解建立)一個device類型, 該類型關聯檔案系統屬性,以及裝置屬性。在比如以下例子:

type graphics_device, dev_type;

此處表示聲明(或了解建立)一個graphics_device類型, 該類型關聯另外一個裝置屬性

定義好之後, 在設定安全上下文的時候就可以做如下操作:

system/sepolicy/private/file_contexts:74:

/dev/adf[0-9]*               u:object_r:graphics_device:s0

程序的allow的語句就可以出現:

system/sepolicy/public/charger.te:32:allow charger graphics_device:dir r_dir_perms;

通俗一點可以這麼了解:

type  張三,  男人, 律師;     

意思是:取了一個新名字叫張三, 屬于男人, 也屬于律師

type一般會在以下幾個檔案中出現:

system/sepolicy/public/file.te

系統中各種檔案類型的聲明, 如type sockfs, fs_type;

# Default type for anything under /system.

type system_file, system_file_type, file_type;

system/sepolicy/public/device.te

系統中各種裝置類型的聲明, 如type camera_device, dev_type;

很多時候我們在開發驅動的時候,會建立裝置節點如一個序列槽裝置,這個裝置節點的type一般都習慣放在device.te中聲明

system/sepolicy/public/service.te

聲明系統中各種背景服務,

如type audioserver_service,       service_manager_type;

各個程序的te檔案 如zygote.te,init.te等檔案, 主要是用于聲明domain, 如type vold, domain;

二, Attribute

attribute直接翻譯過來叫屬性, 主要用于在聲明type的時候進行關聯。 屬性類似一個組,type關聯屬性之後, 新增的type就可以加入到這個屬性組中,這個屬性(組)擁有的權限, 新增的type也就自然擁有了這個權限),舉例:

type system_server, coredomain, domain;

type performanced, domain, coredomain;

{ system_server performanced }都關聯(加入)了domain屬性組

檔案public/domain.te +66

# Device accesses.

allow domain device:dir search;

就可以等同于(僞代碼):

allow { system_server performanced } device:dir search;

屬性文法:

attribute  attribute_id;

聲明屬性的檔案system/sepolicypublic/attributes:

attribute dev_type;

attribute domain;

其實attribute也可以了解成一種type,意味着屬性也是可以用在上下文和政策中的, 如:

u:object_r:某屬性:s0

allow  某屬性  device:dir search;

allow  adbd    某屬性:dir search;

不同屬性所擁有的權限會在不同的te的檔案中聲明, 比如屬性domain會在以下檔案中定義

system/sepolicy/public/domain.te:

allow domain self:fd use;

allow domain proc:dir r_dir_perms;

allow domain proc_net_type:dir search;

r_dir_file(domain, self)

allow domain self:{ fifo_file file } rw_file_perms;

allow domain self:unix_dgram_socket { create_socket_perms sendto };

allow domain self:unix_stream_socket { create_stream_socket_perms connectto };

dev_type屬性的權限會在檔案system/sepolicy/public/file.te +486中定義

allow file_type tmpfs:filesystem associate;

allow file_type rootfs:filesystem associate;

allow dev_type tmpfs:filesystem associate;

除了使用type語句關聯類型和屬性外,還可以使用typeattribute語句,這個語句允許我們在聲明類型之後,再單獨關聯屬性.

如: system/sepolicy/public/mediaserver.te檔案中:

# mediaserver - multimedia daemon

type mediaserver, domain;  #此處并沒有關聯mlstrustedsubject屬性

type mediaserver_exec, system_file_type, exec_type, file_type;

type mediaserver_tmpfs, file_type;

typeattribute mediaserver mlstrustedsubject; #此處可以添加新的關聯屬性

三,常見宏

大部分宏都是在system/sepolicy/public/te_macros中定義:

type_transition  source_type  target_type : class  default_type

例子:

type_transition vold storage_file:dir storage_stub_file;

當主體程序域source_type對target_type類型的檔案進行class定義的操作時,程序會切換到default_type域中。此時指的是主體程序域的type切換(上下文切換), 注意:此時隻是描述了從哪裡切換到哪裡的問題。

domain_trans(olddomain, target_type, newdomain)

縮寫: DT

例子:

domain_trans(vold, shell_exec, blkid);

定義允許在olddomain操作target_type時,從olddomain到newdomain轉化規則。主要針對程序上下文的轉換,大部分程序都是從init程序調動fork和execv,此時子程序就需要從init的程序安全上下文切換到對應的子程序自己應有的程序安全上下文。此時就需要用該宏來切換。注意點, 該宏重點隻是申請允許切換。也可以了解為type_transition() 申請權限。

domain_auto_trans(olddomain, target_type, newdomain)

例子:

domain_auto_trans(init, charger_exec, charger)

當init程序在執行charger_exec可執行程式時,自動切換到charger程序上下文。

等同于type_transition和domain_trans兩個,既描述從哪裡切換到哪裡, 也描述切換所需要的允許規則。
init_daemon_domain(domain) 等同于domain_auto_trans, 在定義背景服務serivice的時候,我們經常用這個宏, 背景伺服器由init程序啟動, 并啟動後需要切換到對應的程序安全上下文上去。

file_type_trans(domain, dir_type, file_type)

縮寫: TT

在某個domain(程序)在安全上下文為dir_type類型的目錄下建立子檔案, 子檔案會自動繼承父目錄的檔案安全上下文,如果想子檔案的上下文建立時變為file_type, 可通過 file_type_trans申請權限, 注意,該宏重點是也隻是申請允許切換。

file_type_auto_trans(domain, dir_type, file_type)

例子:一般在實際例子用的少

等同于type_transition和file_type_trans兩個,既描述從哪裡切換到哪裡, 也描述切換所需要的允許規則。
r_dir_file(domain, type) 允許domain通路安全上下文為type的目錄下的檔案和符号連接配接檔案
unconfined_domain(domain) 使指定的doamin獲得更多的許可(這一宏隻是讓指定的程序安全上下文繼承mlstrustedsubject(在作為主體時, 能繞過MLS檢查相關)和unconfineddomain屬性(在作為客體時, 能繞過MLS檢查相關))
binder_use(domain) 允許domain使用binder通信
binder_call(clientdomain, serverdomain) 允許clientdomain通過binder調用serverdomain
binder_service(domain)   定義domain作為binder service的相關許可
app_domain(domain)   為domain定義所有android app所需的基本許可(實際上是繼承appoamin屬性)
net_domain(domain) 為doamin定義通路網絡所需的基本許可(實際上是繼承netdoamin屬性)
set_prop(domain, prop) 允許domain對prop屬性有設定權限
get_prop(domain, prop)

繼續閱讀