本文結構:SELinux介紹、安全上下文-->模式、策略設置和使用-->bool值作用、查詢修改-->semanager工具安全上下文的使用
Security Enhanced Linux 安全強化linux
是一個嵌到內核中的模塊,用於針對進程對系統資源的訪問加強安全防護,以網絡服務來說,服務的主體是進程,SELinx便可以控制網絡服務是否能訪問系統資源
爲什麼需要SELinux
傳統的訪問文件系統的方式是自主訪問控制(Discretionary Access Control,DAC)通過所有者和文件資源的rwx權限決定可否訪問
缺點:進程所有者只要擁有rwx權限,就可以對文件資源進行隨意修改(例:若是管理者不小心將對外的服務目錄如/var/www/html 權限改爲777,則任何人都可以進行隨意訪問和修改)
SELinux,委託訪問控制(Mandatory Access Contral,MAC)控制主體變成進程而不是用戶,利用策略和安全上下文的匹配控制進程對文件資源的訪問
策略(policy)
targeted:針對網絡服務的限制較多,針對本機限制較少,是默認策略
strict:嚴格限制,完整的SELinux限制
安全上下文(security context)
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
Identify:role:type
身份標識:root、system_u、user_u
角色:object_r、system_r
類型(type):
主體和目標安全上下文type字段一致纔可訪問
安全上下文是放置到文件inode內的 和rwx一樣
進程和 SElinux type字段的關係:以httpd爲例
首先運行程序 usr/sbin/httpd,type類型爲 httpd_exec_t
該類型會讓文件觸發的主體進程(subject)具有httpd這個域(domain)
這個域已被制定了許多規則,包括域可以讀取的目標資源類型type
httpd domain被設置爲可以讀取 httpd_sys_content_t這個類型的Object
/var/www/html的type類型爲 httpd_sys_content_t
所以網頁放到/var/www/html下就能被httpd進程讀取了
最後再根據rwx的權限
SELinux使用
查看模式
getenforce
Enforcing // 強制模式
permissive:寬容模式(只提醒不禁止)disabled 關閉SELinux
改變模式
setenforce 0 / 1
查看策略
sestatus 列出當前策略和狀態 [-v 列出/etc/sestatus.conf下的安全上下文內容] [-b 以布爾值的方式列出策略規則]
配置文件:可修改模式和策略
/etc/selinux/config
[root@boxin ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted // 還有strict
啓動與關閉
查看內核有無關閉 SElinux
[root@boxin ~]# cat /boot/grub/menu.lst
default=0
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-431.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=7f7fc25f-c295-4b8a-8d2c-48ae97b01d41 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img若內核參數加上selinux=0則表示關閉功能
selinux是寫到內核中的所以:改變模式或改變策略都要重啓系統
修改安全上下文
chcon [-t 類型] [-R 遞歸] 目錄/文件
[root@boxin ~]# chcon -R -t httpd_sys_content_t /var/www/html
restorecon [-R 遞歸] [-v 顯示過程] 目錄/文件
// 通過默認安全上下文還原類型
[root@boxin ~]# restorecon -Rv /var/www/html
SElinux日誌記錄
setroubleshoot:錯誤信息寫入 /var/log/messages
auditd:詳細日誌信息包括錯誤信息寫入/var/log/audit/audit.log
策略規則
查詢狀態規則(setools-console 使用seinfo的rpm包)
seinfo [-b 列出所有的(booleans)規則] [-u user] [-r role] [-t type] 列出當前狀態下的統計狀態
[root@boxin ~]# seinfo // 當前策略下的統計狀態
Statistics for policy file: /etc/selinux/targeted/policy/policy.24
Policy Version & Type: v.24 (binary, mls)
Classes: 81 Permissions: 235
Sensitivities: 1 Categories: 1024
Types: 3637 Attributes: 280
Users: 9 Roles: 12
Booleans: 217 Cond. Expr.: 257 // 針對網絡的規則boolens217條
Allow: 291033 Neverallow: 0
Auditallow: 123 Dontaudit: 226650
Type_trans: 33142 Type_change: 38
Type_member: 48 Role allow: 19
Role_trans: 308 Range_trans: 4521
Constraints: 90 Validatetrans: 0
Initial SIDs: 27 Fs_use: 23
Genfscon: 83 Portcon: 446
Netifcon: 0 Nodecon: 0
Permissives: 75 Polcap: 2
[root@boxin ~]# seinfo -b | grep httpd // httpd的bool值
httpd_manage_ipa
httpd_run_stickshift
httpd_use_fusefs
httpd_use_openstack
allow_httpd_mod_auth_pam
httpd_setrlimit
httpd_enable_ftp_server
httpd_use_nfs
布爾值查詢修改(sesearch查看詳細信息)
[root@boxin ~]# getsebool -a //查詢
abrt_anon_write --> off
abrt_handle_event --> off
allow_console_login --> on
allow_cvs_read_shadow --> off
[root@boxin ~]# getsebool allow_console_login // 查詢某一個
allow_console_login --> on
[root@boxin ~]# setsebool -P allow_console_login=1 //(1表示on, 0表示off),-P寫入配置文件
文件默認安全上下文設置
[root@boxin ~]# semanage fcontext [-l 查詢] [-a 增加] [-m 修改] [-d 刪除] // fcontext用於安全上下文
例:
[root@boxin ~]# ls -Zd /srv/samba
drwxr-xr-x. root root unconfined_u:object_r:var_t:s0 /srv/samba
[root@boxin ~]# semanage fcontext -l | grep /srv/
/srv/([^/]*/)?ftp(/.*)? all files system_u:object_r:public_content_t:s0
/srv/([^/]*/)?rsync(/.*)? all files system_u:object_r:public_content_t:s0
/srv/([^/]*/)?www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
/srv/.* all files system_u:object_r:var_t:s0
/srv/gallery2(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
/srv/git(/.*)? all files system_u:object_r:git_sys_content_t:s0
[root@boxin ~]# semanage fcontext -a -t public_content_t "/srv/samba(/.*)?"
[root@boxin ~]# semanage fcontext -l | grep /srv/samba
/srv/samba(/.*)? all files system_u:object_r:public_content_t:s0
[root@boxin ~]# restorecon -Rv /srv/samba
restorecon reset /srv/samba context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:public_content_t:s0
[root@boxin ~]# ls -dZ /srv/samba/
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /srv/samba/