⑴ 如何設置SElinux 策略規則
[SELinux Policy]如何設置SELinux策略規則?
[Description]
android KK 4.4 版本後,Google 默認啟用了SELinux, 並會把SELinux 審查異常列印在kernel log
或者 android log(L 版本)中,對應的關鍵字是: "avc: denied" 或者"avc: denied"
如一行LOG:
<5>[ 17.285600].(0)[503:idmap]type=1400 audit(1356999072.320:202): avc: denied { create
} for pid=503 comm="idmap" name="overlays.list" scontext=u:r:zygote:s0
tcontext=ubject_r:resource_cache_data_file:s0 tclass=file
即表明idmap 這個process, 使用zygote 的source context, 訪問/data/resource_cache 目錄,並
創建文件時,被SELinux 拒絕訪問。
[Keyword]
android, SELinux, avc: denied, audit
[Solution]
KK 版本, Google 只有限制的啟用SELinux, 即只有針對netd, installd, zygote, vold 以及它們
直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.
L 版本, Google 全面開啟SELinux, 幾乎所有的process 都使enforcing mode, 影響面非常廣.
目前所有的SELinux check 失敗,在kernel log 或者android log(L版本後)中都有對應的"avc:
denied" 或者 "avc: denied"的LOG 與之對應。反過來,有此LOG,並非就會直接失敗,還需要確認
當時SELinux 的模式, 是enforcing mode 還是 permissve mode.
首先, 務必確認對應進程訪問系統資源是否正常, 是否有必要 ?如果本身是異常非法訪問,那麼
就要自行消除訪問。
其次, 如果確認訪問是必要,並且正常的,那麼就要對對應的process/domain 增加新的policy.
1). 簡化方法
1.1 提取所有的avc LOG. 如 adb shell "cat /proc/kmsg | grep avc" > avc_log.txt
1.2 使用 audit2allow tool 直接生成policy. audit2allow -i avc_log.txt 即可自動輸出生成的
policy
1.3 將對應的policy 添加到selinux policy 規則中,對應MTK Solution, 您可以將它們添加在KK:
mediatek/custom/common/sepolicy, L: device/mediatek/common/sepolicy 下面,如
allow zygote resource_cache_data_file:dir rw_dir_perms;
allow zygote resource_cache_data_file:file create_file_perms;
===> mediatek/custom/common/sepolicy/zygote.te (KK)
===> device/mediatek/common/sepolicy/zygote.te (L)
注意audit2allow 它自動機械的幫您將LOG 轉換成policy, 而無法知道你操作的真實意圖,有可能
出現許可權放大問題,經常出現policy 無法編譯通過的情況。
2). 按需確認方法
此方法需要工程人員對SELinux 基本原理,以及SELinux Policy Language 有了解.
2.1 確認是哪個進程訪問哪個資源,具體需要哪些訪問許可權,read ? write ? exec ? create ?
search ?
2.2 當前進程是否已經創建了policy 文件? 通常是process 的執行檔.te,如果沒有,並且它的父
進程即source context 無須訪問對應的資源,則創建新的te 文件.
在L 版本上, Google 要求維護關鍵 security context 的唯一性, 比如嚴禁zygote, netd,
installd, vold, ueventd 等關鍵process 與其它process 共享同一個security context.
2.3 創建文件後,關聯它的執行檔,在file_contexts 中, 關聯相關的執行檔.
比如 /system/bin/idmap 則是 /system/bin/idmap ubject_r:idmap_exec:s0
2.4 填寫policy 到相關的te 文件中
如果沿用原來父進程的te 文件,則直接添加.
如果是新的文件,那麼首先:
#==============================================
# Type Declaration
#==============================================
type idmap, domain;
type idmap_exec, exec_type,file_type;
#==============================================
# Android Policy Rule
#==============================================
#permissive idmap;
domain_auto_trans(zygote, idmap_exec, idmap);
然後添加新的policy
# new policy
allow idmap resource_cache_data_file:dir rw_dir_perms;
allow idmap resource_cache_data_file:file create_file_perms;
3). 許可權放大情況處理
如果直接按照avc: denied 的LOG 轉換出SELinux Policy, 往往會產生許可權放大問題. 比如因為要
訪問某個device, 在這個device 沒有細化SELinux Label 的情況下, 可能出現:
<7>[11281.586780] avc: denied { read write } for pid=1217 comm="mediaserver"
name="tfa9897" dev="tmpfs" ino=4385 scontext=u:r:mediaserver:s0
tcontext=ubject_r:device:s0 tclass=chr_file permissive=0
如果直接按照此LOG 轉換出SELinux Policy: allow mediaserver device:chr_file {read write};
那麼就會放開mediaserver 讀寫所有device 的許可權. 而Google 為了防止這樣的情況, 使用了
neverallow 語句來約束, 這樣你編譯sepolicy 時就無法編譯通過.
為了規避這種許可權放大情況, 我們需要細化訪問目標(Object) 的SELinux Label, 做到按需申請.
通常會由三步構成
3.1 定義相關的SELinux type.
比如上述案例, 在 device/mediatek/common/sepolicy/device.te 添加
type tfa9897_device, dev_type;
3.2 綁定文件與SELinux type.
比如上述案例, 在 device/mediatek/common/sepolicy/file_contexts 添加
/dev/tfa9897(/.*)? ubject_r:tfa9897_device:s0
3.3 添加對應process/domain 的訪問許可權.
比如上述案例, 在 device/mediatek/common/sepolicy/mediaserver.te 添加
allow mediaserver tfa9897_device:chr_file { open read write };
那麼哪些訪問對象通常會遇到此類呢?(以L 版本為例)
* device
-- 類型定義: external/sepolicy/device.te;device/mediatek/common/sepolicy/device.te
-- 類型綁定:
external/sepolicy/file_contexts;device/mediatek/common/sepolicy/file_contexts
* File 類型:
-- 類型定義: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te
-- 綁定類型:
external/sepolicy/file_contexts;device/mediatek/common/sepolicy/file_contexts
* 虛擬File 類型:
-- 類型定義: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te
-- 綁定類型:
external/sepolicy/genfs_contexts;device/mediatek/common/sepolicy/genfs_contexts
* Service 類型:
-- 類型定義: external/sepolicy/service.te; device/mediatek/common/sepolicy/service.te
-- 綁定類型
:external/sepolicyservice_contexts;device/mediatek/common/sepolicy/service_contexts
* Property 類型:
-- 類型定義: external/sepolicy/property.te;device/mediatek/common/sepolicy/property.te
-- 綁定類型:
external/sepolicy/property_contexts;device/mediatek/common/sepolicy/property_contexts;
通常我們強烈反對更新google default 的policy, 大家可以更新mediatek 下面的相關的policy.
⑵ 如何設置SELinux 策略規則
[SELinux Policy]如何設置SELinux策略規則?
[Description]
android KK 4.4 版本後,Google 默認啟用了SELinux, 並會把SELinux 審查異常列印在kernel log
或者 android log(L 版本)中,對應的關鍵字是: "avc: denied" 或者"avc: denied"
如一行LOG:
<5>[ 17.285600].(0)[503:idmap]type=1400 audit(1356999072.320:202): avc: denied { create
} for pid=503 comm="idmap" name="overlays.list" scontext=u:r:zygote:s0
tcontext=ubject_r:resource_cache_data_file:s0 tclass=file
即表明idmap 這個process, 使用zygote 的source context, 訪問/data/resource_cache 目錄,並
創建文件時,被SELinux 拒絕訪問。
[Keyword]
android, SELinux, avc: denied, audit
[Solution]
KK 版本, Google 只有限制的啟用SELinux, 即只有針對netd, installd, zygote, vold 以及它們
直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.
L 版本, Google 全面開啟SELinux, 幾乎所有的process 都使enforcing mode, 影響面非常廣.
目前所有的SELinux check 失敗,在kernel log 或者android log(L版本後)中都有對應的"avc:
denied" 或者 "avc: denied"的LOG 與之對應。反過來,有此LOG,並非就會直接失敗,還需要確認
當時SELinux 的模式, 是enforcing mode 還是 permissve mode.
首先, 務必確認對應進程訪問系統資源是否正常, 是否有必要 看如果本身是異常非法訪問,那麼
就要自行消除訪問。
其次, 如果確認訪問是必要,並且正常的,那麼就要對對應的process/domain 增加新的policy.
1). 簡化方法
1.1 提取所有的avc LOG. 如 adb shell "cat /proc/kmsg | grep avc" > avc_log.txt
1.2 使用 audit2allow tool 直接生成policy. audit2allow -i avc_log.txt 即可自動輸出生成的
policy
1.3 將對應的policy 添加到selinux policy 規則中,對應MTK Solution, 您可以將它們添加在KK:
mediatek/custom/common/sepolicy, L: device/mediatek/common/sepolicy 下面,如
allow zygote resource_cache_data_file:dir rw_dir_perms;
allow zygote resource_cache_data_file:file create_file_perms;
===> mediatek/custom/common/sepolicy/zygote.te (KK)
===> device/mediatek/common/sepolicy/zygote.te (L)
注意audit2allow 它自動機械的幫您將LOG 轉換成policy, 而無法知道你操作的真實意圖,有可能
出現許可權放大問題,經常出現policy 無法編譯通過的情況。
2). 按需確認方法
此方法需要工程人員對SELinux 基本原理,以及SELinux Policy Language 有了解.
2.1 確認是哪個進程訪問哪個資源,具體需要哪些訪問許可權,read ? write ? exec ? create ?
search ?
2.2 當前進程是否已經創建了policy 文件看 通常是process 的執行檔.te,如果沒有,並且它的父
進程即source context 無須訪問對應的資源,則創建新的te 文件.
在L 版本上, Google 要求維護關鍵 security context 的唯一性, 比如嚴禁zygote, netd,
installd, vold, ueventd 等關鍵process 與其它process 共享同一個security context.
2.3 創建文件後,關聯它的執行檔,在file_contexts 中, 關聯相關的執行檔.
比如 /system/bin/idmap 則是 /system/bin/idmap ubject_r:idmap_exec:s0
2.4 填寫policy 到相關的te 文件中
如果沿用原來父進程的te 文件,則直接添加.
如果是新的文件,那麼首先:
#==============================================
# Type Declaration
#==============================================
type idmap, domain;
type idmap_exec, exec_type,file_type;
#==============================================
# Android Policy Rule
#==============================================
#permissive idmap;
domain_auto_trans(zygote, idmap_exec, idmap);
然後添加新的policy
# new policy
allow idmap resource_cache_data_file:dir rw_dir_perms;
allow idmap resource_cache_data_file:file create_file_perms;
3). 許可權放大情況處理
如果直接按照avc: denied 的LOG 轉換出SELinux Policy, 往往會產生許可權放大問題. 比如因為要
訪問某個device, 在這個device 沒有細化SELinux Label 的情況下, 可能出現:
<7>[11281.586780] avc: denied { read write } for pid=1217 comm="mediaserver"
name="tfa9897" dev="tmpfs" ino=4385 scontext=u:r:mediaserver:s0
tcontext=ubject_r:device:s0 tclass=chr_file permissive=0
如果直接按照此LOG 轉換出SELinux Policy: allow mediaserver device:chr_file {read write};
那麼就會放開mediaserver 讀寫所有device 的許可權. 而Google 為了防止這樣的情況, 使用了
neverallow 語句來約束, 這樣你編譯sepolicy 時就無法編譯通過.
為了規避這種許可權放大情況, 我們需要細化訪問目標(Object) 的SELinux Label, 做到按需申請.
通常會由三步構成
3.1 定義相關的SELinux type.
比如上述案例, 在 device/mediatek/common/sepolicy/device.te 添加
type tfa9897_device, dev_type;
3.2 綁定文件與SELinux type.
比如上述案例, 在 device/mediatek/common/sepolicy/file_contexts 添加
/dev/tfa9897(/.*)? ubject_r:tfa9897_device:s0
3.3 添加對應process/domain 的訪問許可權.
比如上述案例, 在 device/mediatek/common/sepolicy/mediaserver.te 添加
allow mediaserver tfa9897_device:chr_file { open read write };
那麼哪些訪問對象通常會遇到此類呢看(以L 版本為例)
* device
-- 類型定義: external/sepolicy/device.te;device/mediatek/common/sepolicy/device.te
-- 類型綁定:
external/sepolicy/file_contexts;device/mediatek/common/sepolicy/file_contexts
* File 類型:
-- 類型定義: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te
-- 綁定類型:
external/sepolicy/file_contexts;device/mediatek/common/sepolicy/file_contexts
* 虛擬File 類型:
-- 類型定義: external/sepolicy/file.te;device/mediatek/common/sepolicy/file.te
-- 綁定類型:
external/sepolicy/genfs_contexts;device/mediatek/common/sepolicy/genfs_contexts
* Service 類型:
-- 類型定義: external/sepolicy/service.te; device/mediatek/common/sepolicy/service.te
-- 綁定類型
:external/sepolicyservice_contexts;device/mediatek/common/sepolicy/service_contexts
* Property 類型:
-- 類型定義: external/sepolicy/property.te;device/mediatek/common/sepolicy/property.te
-- 綁定類型:
external/sepolicy/property_contexts;device/mediatek/common/sepolicy/property_contexts;
通常我們強烈反對更新google default 的policy, 大家可以更新mediatek 下面的相關的policy.
⑶ Android 中怎樣查找SELinux導致的許可權受限問題
android KK 4.4 版本後,如果發現進程無法訪問某些文件,無法連接socket 等問題,並且發現errno 是EPERM(Operation not permitted) 或者 EACCES (Permission denied), 如何確認此類問題是因為SELinux 約束引起?
[Keyword]
android, SELinux, Permission denied, 訪問限制, 許可權問題
[Solution]
在Android KK 4.4 版本後,Google 有正式有限制的啟用SELinux, 來增強android 的安全保護。
SELinux 分成enforcing mode 和 permissive mode, enforcing mode 會強制性限制訪問; 而permissve mode 只審查許可權, 但不限制, 即不會產生實質性影響.
KK 版本, Google 只有限制的啟用SELinux, 即只有針對netd, installd, zygote, vold 以及它們直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.
L 版本, Google 全面開啟SELinux, 幾乎所有的process 都使enforcing mode, 影響面非常廣.
另外為了限制user 版本root 許可權,針對su 有做特別的處理,可以參考FAQ android KK 4.4 版本後,user 版本su 許可權嚴重被限制問題說明
目前所有的SELinux check 失敗,在kernel log 或者android log(L版本後)中都有對應的"avc: denied" 或者 "avc: denied"的LOG 與之對應。反過來,有此LOG,並非就會直接失敗,還需要確認當時SELinux 的模式, 是enforcing mode 還是 permissve mode.
如果問題容易復現,我們可以先將SELinux 模式調整到Permissive mode,然後再測試確認是否與SELinux 約束相關.
在ENG 版本中:
adb shell setenforce 0
⑷ Android 中怎樣查找SELinux導致的許可權受限有關問題
android KK 4.4 版本後,如果發現進程無法訪問某些文件,無法連接socket 等問題,並且發現errno 是EPERM(Operation not permitted) 或者 EACCES (Permission denied), 如何確認此類問題是因為SELinux 約束引起?
[Keyword]
android, SELinux, Permission denied, 訪問限制, 許可權問題
[Solution]
在Android KK 4.4 版本後,Google 有正式有限制的啟用SELinux, 來增強android 的安全保護。
SELinux 分成enforcing mode 和 permissive mode, enforcing mode 會強制性限制訪問; 而permissve mode 只審查許可權, 但不限制, 即不會產生實質性影響.
KK 版本, Google 只有限制的啟用SELinux, 即只有針對netd, installd, zygote, vold 以及它們直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.
L 版本, Google 全面開啟SELinux, 幾乎所有的process 都使enforcing mode, 影響面非常廣.
另外為了限制user 版本root 許可權,針對su 有做特別的處理,可以參考FAQ android KK 4.4 版本後,user 版本su 許可權嚴重被限制問題說明
目前所有的SELinux check 失敗,在kernel log 或者android log(L版本後)中都有對應的"avc: denied" 或者 "avc: denied"的LOG 與之對應。反過來,有此LOG,並非就會直接失敗,還需要確認當時SELinux 的模式, 是enforcing mode 還是 permissve mode.
如果問題容易復現,我們可以先將SELinux 模式調整到Permissive mode,然後再測試確認是否與SELinux 約束相關.
在ENG 版本中:
adb shell setenforce 0
⑸ android kk 版本 哪個好
android KK,KK是版本號的意思
android KK就是之android 4.4
拓展:android L指android5.0
⑹ Android 中怎樣查找SELinux導致的許可權受限有關問題
在Android KK 4.4 版本後,Google 有正式有限制的啟用SELinux, 來增強android 的安全保護。
SELinux 分成enforcing mode 和 permissive mode, enforcing mode 會強制性限制訪問; 而permissve mode 只審查許可權, 但不限制, 即不會產生實質性影響.
KK 版本, Google 只有限制的啟用SELinux, 即只有針對netd, installd, zygote, vold 以及它們直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.
L 版本, Google 全面開啟SELinux, 幾乎所有的process 都使enforcing mode, 影響面非常廣.
另外為了限制user 版本root 許可權,針對su 有做特別的處理,可以參考FAQ android KK 4.4 版本後,user 版本su 許可權嚴重被限制問題說明
目前所有的SELinux check 失敗,在kernel log 或者android log(L版本後)中都有對應的"avc: denied" 或者 "avc: denied"的LOG 與之對應。反過來,有此LOG,並非就會直接失敗,還需要確認當時SELinux 的模式, 是enforcing mode 還是 permissve mode.
如果問題容易復現,我們可以先將SELinux 模式調整到Permissive mode,然後再測試確認是否與SELinux 約束相關.
⑺ Android 中怎樣查找SELinux導致的許可權受限有關問題
在Android KK 4.4 版本後,Google 有正式有限制的啟用SELinux, 來增強android 的安全保護。
SELinux 分成enforcing mode 和 permissive mode, enforcing mode 會強制性限制訪問; 而permissve mode 只審查許可權, 但不限制, 即不會產生實質性影響.
KK 版本, Google 只有限制的啟用SELinux, 即只有針對netd, installd, zygote, vold 以及它們直接fork 出的child process 使用enforcing mode, 但不包括zygote fork的普通app.
L 版本, Google 全面開啟SELinux, 幾乎所有的process 都使enforcing mode, 影響面非常廣.
另外為了限制user 版本root 許可權,針對su 有做特別的處理,可以參考FAQ android KK 4.4 版本後,user 版本su 許可權嚴重被限制問題說明
目前所有的SELinux check 失敗,在kernel log 或者android log(L版本後)中都有對應的"avc: denied" 或者 "avc: denied"的LOG 與之對應。反過來,有此LOG,並非就會直接失敗,還需要確認當時SELinux 的模式, 是enforcing mode 還是 permissve mode.
如果問題容易復現,可以先將SELinux 模式調整到Permissive mode,然後再測試確認是否與SELinux 約束相關.