天天看點

selinux學習筆記二(selinux基礎關鍵字介紹)

Android作業系統中內建selinux子產品,是一種對系統安全的增強。以前基于DAC的安全防護模式有個緻命漏洞,即:程序自身可以被動态的修改,如第三方app通過hack的方式把自身變成system程序,則該app就具有了system權限。為了防止此類漏洞被觸發,在DAC的基礎上增加MAC的權限控制,而selinux是MAC安全子產品中比較完善的一種,是以把selinux功能引入Android。

在selinux的世界裡,把所有的東西分為subject(一般可以了解為動态運作的程序)和object(一般可以了解為靜态存在的檔案),其安全防護即是指管理所有subject對所有object的通路,而所謂的管理所指的是把各個object能接受的通路方式(如:增,删,改,查)細化成規則,并指定不同的subject對不同的object有那些通路規則。是以簡單的來講可以把selinux的安全防護分為三個部分:

1)對所有subject分類,分類的結果是每個subject都有與之對應的security context;

2)對所有object分類,分類的結果是每個object都有與之對應的security context;

3)描述各種不同subject對各種不同object的通路規則,并把描述形成檔案,即policy檔案。

當subject要對object通路時,首先要去查詢該通路方式是否在現有的policy檔案中被授權。其實DAC的安全防護也可以抽象成類似selinux中定義的subject,object和policy這種模式。而MAC不同于DAC的地方是,在MAC模式中subject的security context,object的security context和policy都是一經确立不可以修改,而DAC模式中這三者在初始化以後變動仍然可以被改變。

使用selinux實作安全防護時,對subject和object的security context定義相對來講比較簡單,最繁重的工作是policy檔案的撰寫。逐漸的分别介紹如下:

首先介紹selinux中的security context,通過指令行

ps -eZ

看到各個程序的security context,通過指令行

ls -Z

可以得到目前目錄下所有檔案的security context。例如得到的init程序security context描述為:

u:r:init:s0

得到的'/data'目錄的security context為:

u:object_r:system_data_file:s0

可見在selinux中,security context是用一個字元串表示的,字元串包含四個部分,對這四部分的描述

可以參考:https://selinuxproject.org/page/NB_SC 中所述:

SELinux requires a security context to be associated with every process (or subject) and object that are used by the security server to decide whether access is allowed or not as defined by the policy.

The security context is also known as a 'security label' or just label that can cause confusion as there are many types of label depending on the context (another context!!).

Within SELinux, a security context is represented as variable-length strings that define the SELinux user[1], their role, a type identifier and an optional MCS / MLS security range or level as follows:

user:role:type[:range]

selinux學習筆記二(selinux基礎關鍵字介紹)

其中前兩個字段user和role基本上是固定的,對于subject來講這兩者大多數情況下被分别寫死為“u”和“r”,對object來講這兩者大多數情況下被分别寫死為“u”和“object_r”;最後一個字段又是可選的,大多數情況下都被寫死為“s0”。是以對于subject和object來講,他們security context全部用type字段來表示,是以selinux又可以被叫做TYPE ENFORCED ACCESS CONTROL。

subject 的security context的“type”字段,一般被了解為“domain”;object的security context的“type”字段一般被了解為“type”。

介紹完security context以後,然後就可以開始介紹policy了,随便從找到一個.te檔案(包含了多條policy内容)中找到一條規則描述如下:

allow netd properties_device:dir relabelto;

allow語句的格式如下:

allow domains types:classes operations

a):domains - subject的一種類型;

b):types - object的一種類型;

c):classes - 這個字段沒有具體的含義,是後者operations的一種歸類;

d):operations - 要執行的操作(例如,讀,寫等)。

繼續閱讀