SELinux(Security-Enhanced Linux) 是[美國國家安全局](NSA)對于[強制訪問控制]的實現,是 Linux歷史上最杰出的新安全子系統。

??想了解更多關于開源的內容,請訪問:??
??51CTO 開源基礎軟件社區??
??https://ost.51cto.com??
SELinux

SELinux說明
SELinux(Security-Enhanced Linux) 是[美國國家安全局](NSA)對于[強制訪問控制]的實現,是 Linux歷史上最杰出的新安全子系統。NSA是在Linux社區的幫助下開發了一種訪問控制體系,在這種訪問控制體系的限制下,進程只能訪問那些在他的任務中所需要文件。SELinux 主要作用就是最大限度地減小系統中服務進程可訪問的資源(最小權限原則)。
SELinux基本概念
- 主體Subjects:可以完全等同于進程。
- 目標Objects:被主體訪問的資源。可以是文件、目錄、端口、設備等。
- 策略Policy:哪些進程需要管制、要怎么管制是由政策決定。規則是模塊化、可擴展的。在安裝新的應用程序時,應用程序可通過添加新的模塊來添加規則。用戶也可以手動地增減規則。
- 模式Mode:默認有三種模式:
- Enforcing 強制— SELinux 策略強制執行,基于 SELinux 策略規則授予或拒絕主體對目標的訪問。
- Permissive 寬容— SELinux 策略不強制執行,不實際拒絕訪問,但會有拒絕信息寫入日志。
- Disabled 禁用— 完全禁用SELinux。
OpenHarmony SELinux使用介紹

OpenHarmony里的selinux部件負責對文件,屬性,服務等系統資源提供強制訪問控制保護。提供neverallow規則限制系統中的高危操作,減少系統安全風險。
文件結構(distributedatamgr):
├── BUILD.gn
├── bundle.json
├── config # 板側 三方庫配置文件
│ ├── config.enforce
│ └── config.permissive
├── docs # 文檔
├── interfaces
│ ├── policycoreutils # 板側 libload_policy.so, librestorecon.so
│ │ ├── include
│ │ └── src
│ └── tools # 板側 load_policy, restorecon
│ ├── hap_restorecon
│ ├── load_policy
│ ├── param_check
│ ├── restorecon
│ └── service_check
├── LICENSE
├── OAT.xml
├── README-en.md
├── README.md
├── scripts # 編譯側 策略編譯腳本
├── selinux.gni
├── sepolicy # 編譯側 策略文件
│ ├── base
│ │ ├── public
│ │ ├── system
│ │ └── te
│ └── ohos_policy
│ ├── ability
│ ├── ark
│ ├── arkXtest
│ ├── barrierfree
│ ├── bundlemanager
│ ├── cloud
│ ├── communication
│ ├── customization
│ ├── developtools
│ ├── deviceprofile
│ ├── distributeddatamgr
│ ├── distributedhardware
│ ├── distributedschedule
│ ├── drivers
│ ├── dsoftbus
│ ├── filemanagement
│ ├── global
│ ├── graphic
│ ├── hiviewdfx
│ ├── iam
│ ├── kernel
│ ├── location
│ ├── misc
│ ├── miscservices
│ ├── msdp
│ ├── multimedia
│ ├── multimodalinput
│ ├── notification
│ ├── os_account
│ ├── powermgr
│ ├── print
│ ├── resourceschedule
│ ├── security
│ ├── sensors
│ ├── startup
│ ├── telephony
│ ├── test_framework
│ ├── update
│ ├── usb
│ ├── useriam
│ ├── web
│ ├── window
│ └── xts
└── test
運行驗證:
- 將鏡像燒錄到開發板上,開機,hdc_std shell登錄板子 ,在其中執行:
ls -lZ / # 查看文件標簽
ls -lLZ / # 查看link源文件標簽
ps -eZ # 查看進程標簽
setenforce 1 # 使能selinux強制模式
setenforce 0 # 是能selinux寬容模式,當前默認寬容模式
getenforce # 獲取selinux工作模式
- 策略文件:/etc/selinux/targeted/policy/policy.31
- 文件標簽規則 /etc/selinux/targeted/policy/file_contexts
- selinux模式開關 /etc/selinux/config
日志解讀:
//使用dmsg查看日志
audit: type=1400 audit(1502458430.566:4): avc: denied { open } for pid=1658 comm="setenforce" path="/sys/fs/selinux/enforce" dev="selinuxfs" ino=4 scontext=u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 tclass=file permissive=1
//關鍵字:avc: denied
//日志解讀
open #操作為open
pid=1658 #訪問主體進程號為1658
comm="setenforce" #訪問主體進程名為setenforce
path="/sys/fs/selinux/enforce" #被訪問客體為/sys/fs/selinux/enforce
dev="selinuxfs" #被訪問文件屬于selinuxfs這一文件系統
ino=4 #文件節點編號為4
scontext=u:r:hdcd:s0 #訪問主體selinux標簽為u:r:hdcd:s0
tcontext=u:object_r:selinuxfs:s0 #被訪問客體selinux標簽為u:object_r:selinuxfs:s0
tclass=file #當前告警屬于file類型的操作
permissive=1 #當前selinux處于寬容模式,只告警不做訪問攔截。強制模式時,做攔截, permissive=0
//分析:
缺少啥權限:denied { open }
誰缺少權限:scontext=u:r:hdcd:s0
對哪個文件缺少:tcontext=u:object_r:selinuxfs:s0
什么類型:tclass=file
通用公式為:all {scontext} {tcontext}:{tclass} {denied權限}
策略編寫:
根據avc告警,獲取訪問信息
如:
audit: type=1400 audit(1502458430.566:4): avc: denied { open } for pid=1658 comm="setenforce" path="/sys/fs/selinux/enforce" dev="selinuxfs" ino=4 scontext=u:r:hdcd:s0 tcontext=u:object_r:selinuxfs:s0 tclass=file permissive=1
對應規則為
allow hdcd selinuxfs:file open;
提交倉:
- 倉名:OpenHarmony? / security_selinux。
- 位置:按照部件分類,比如wifi為sepolicy/ohos_policy/communication/wifi/system/xxx.te。
- 加具體規則進對應te文件。
小結
SELinux是3.2增加的安全保護措施,所以如果我們開發了新服務,新框架,很有可能會碰到程序不能運行的問題,簡單的判斷方法有三種:
編譯大法:
找到這個文件:vendor/hihope/rk3568/config.json (不同的開發板文件不同哦)
改build_selinux為false
這樣鏡像就不支持selinux了,然后應該就能運行了,但是不推薦,破壞安全環境
臨時命令:
setenforce 0
使用此命令后,會關閉selinux判斷,但是下次重啟會恢復,此方法用于臨時驗證問題
增加te規則(如上解釋)。
??想了解更多關于開源的內容,請訪問:??
??51CTO 開源基礎軟件社區??
??https://ost.51cto.com??