SELinux(Security-Enhanced Linux) 是[美國(guó)國(guó)家安全局](NSA)對(duì)于[強(qiáng)制訪問(wèn)控制]的實(shí)現(xiàn),是 Linux歷史上最杰出的新安全子系統(tǒng)。

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??
SELinux

SELinux說(shuō)明
SELinux(Security-Enhanced Linux) 是[美國(guó)國(guó)家安全局](NSA)對(duì)于[強(qiáng)制訪問(wèn)控制]的實(shí)現(xiàn),是 Linux歷史上最杰出的新安全子系統(tǒng)。NSA是在Linux社區(qū)的幫助下開(kāi)發(fā)了一種訪問(wèn)控制體系,在這種訪問(wèn)控制體系的限制下,進(jìn)程只能訪問(wèn)那些在他的任務(wù)中所需要文件。SELinux 主要作用就是最大限度地減小系統(tǒng)中服務(wù)進(jìn)程可訪問(wèn)的資源(最小權(quán)限原則)。
SELinux基本概念
- 主體Subjects:可以完全等同于進(jìn)程。
- 目標(biāo)Objects:被主體訪問(wèn)的資源??梢允俏募?、目錄、端口、設(shè)備等。
- 策略Policy:哪些進(jìn)程需要管制、要怎么管制是由政策決定。規(guī)則是模塊化、可擴(kuò)展的。在安裝新的應(yīng)用程序時(shí),應(yīng)用程序可通過(guò)添加新的模塊來(lái)添加規(guī)則。用戶也可以手動(dòng)地增減規(guī)則。
- 模式Mode:默認(rèn)有三種模式:
- Enforcing 強(qiáng)制— SELinux 策略強(qiáng)制執(zhí)行,基于 SELinux 策略規(guī)則授予或拒絕主體對(duì)目標(biāo)的訪問(wèn)。
- Permissive 寬容— SELinux 策略不強(qiáng)制執(zhí)行,不實(shí)際拒絕訪問(wèn),但會(huì)有拒絕信息寫(xiě)入日志。
- Disabled 禁用— 完全禁用SELinux。
OpenHarmony SELinux使用介紹

OpenHarmony里的selinux部件負(fù)責(zé)對(duì)文件,屬性,服務(wù)等系統(tǒng)資源提供強(qiáng)制訪問(wèn)控制保護(hù)。提供neverallow規(guī)則限制系統(tǒng)中的高危操作,減少系統(tǒng)安全風(fēng)險(xiǎn)。
文件結(jié)構(gòu)(distributedatamgr):
├── BUILD.gn
├── bundle.json
├── config # 板側(cè) 三方庫(kù)配置文件
│ ├── config.enforce
│ └── config.permissive
├── docs # 文檔
├── interfaces
│ ├── policycoreutils # 板側(cè) libload_policy.so, librestorecon.so
│ │ ├── include
│ │ └── src
│ └── tools # 板側(cè) load_policy, restorecon
│ ├── hap_restorecon
│ ├── load_policy
│ ├── param_check
│ ├── restorecon
│ └── service_check
├── LICENSE
├── OAT.xml
├── README-en.md
├── README.md
├── scripts # 編譯側(cè) 策略編譯腳本
├── selinux.gni
├── sepolicy # 編譯側(cè) 策略文件
│ ├── 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
運(yùn)行驗(yàn)證:
- 將鏡像燒錄到開(kāi)發(fā)板上,開(kāi)機(jī),hdc_std shell登錄板子 ,在其中執(zhí)行:
ls -lZ / # 查看文件標(biāo)簽
ls -lLZ / # 查看link源文件標(biāo)簽
ps -eZ # 查看進(jìn)程標(biāo)簽
setenforce 1 # 使能selinux強(qiáng)制模式
setenforce 0 # 是能selinux寬容模式,當(dāng)前默認(rèn)寬容模式
getenforce # 獲取selinux工作模式
- 策略文件:/etc/selinux/targeted/policy/policy.31
- 文件標(biāo)簽規(guī)則 /etc/selinux/targeted/policy/file_contexts
- selinux模式開(kāi)關(guān) /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
//關(guān)鍵字:avc: denied
//日志解讀
open #操作為open
pid=1658 #訪問(wèn)主體進(jìn)程號(hào)為1658
comm="setenforce" #訪問(wèn)主體進(jìn)程名為setenforce
path="/sys/fs/selinux/enforce" #被訪問(wèn)客體為/sys/fs/selinux/enforce
dev="selinuxfs" #被訪問(wèn)文件屬于selinuxfs這一文件系統(tǒng)
ino=4 #文件節(jié)點(diǎn)編號(hào)為4
scontext=u:r:hdcd:s0 #訪問(wèn)主體selinux標(biāo)簽為u:r:hdcd:s0
tcontext=u:object_r:selinuxfs:s0 #被訪問(wèn)客體selinux標(biāo)簽為u:object_r:selinuxfs:s0
tclass=file #當(dāng)前告警屬于file類型的操作
permissive=1 #當(dāng)前selinux處于寬容模式,只告警不做訪問(wèn)攔截。強(qiáng)制模式時(shí),做攔截, permissive=0
//分析:
缺少啥權(quán)限:denied { open }
誰(shuí)缺少權(quán)限:scontext=u:r:hdcd:s0
對(duì)哪個(gè)文件缺少:tcontext=u:object_r:selinuxfs:s0
什么類型:tclass=file
通用公式為:all {scontext} {tcontext}:{tclass} {denied權(quán)限}
策略編寫(xiě):
根據(jù)avc告警,獲取訪問(wèn)信息
如:
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
對(duì)應(yīng)規(guī)則為
allow hdcd selinuxfs:file open;
提交倉(cāng):
- 倉(cāng)名:OpenHarmony? / security_selinux。
- 位置:按照部件分類,比如wifi為sepolicy/ohos_policy/communication/wifi/system/xxx.te。
- 加具體規(guī)則進(jìn)對(duì)應(yīng)te文件。
小結(jié)
SELinux是3.2增加的安全保護(hù)措施,所以如果我們開(kāi)發(fā)了新服務(wù),新框架,很有可能會(huì)碰到程序不能運(yùn)行的問(wèn)題,簡(jiǎn)單的判斷方法有三種:
編譯大法:
找到這個(gè)文件:vendor/hihope/rk3568/config.json (不同的開(kāi)發(fā)板文件不同哦)
改build_selinux為false
這樣鏡像就不支持selinux了,然后應(yīng)該就能運(yùn)行了,但是不推薦,破壞安全環(huán)境
臨時(shí)命令:
setenforce 0
使用此命令后,會(huì)關(guān)閉selinux判斷,但是下次重啟會(huì)恢復(fù),此方法用于臨時(shí)驗(yàn)證問(wèn)題
增加te規(guī)則(如上解釋)。
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??