2007年10月11日 星期四

Apache under SELinux - 讓Apache跑得順起來!

Apache under SELinux - 讓Apache跑得順起來!
對於剛使用Redhat Enterprise Linux4 或Fedora Core 2以上/CentOS 4的用戶,一定會為Apache經常無法正常運轉,報以"Permission denied"等錯誤而大為不解,甚至大為惱火。

其實這是因為這些系統裡激活了SELinux,而用戶的apache配置與SELinux的配置策略有牴觸產生的,只有通過適當調整,使apache的配置和訪問符合策略才能正常使用。

現在下面來分析一下SELinux中有關httpd(apache)的context定義(略有刪節)

/home/[^/]+/((www)|(web)|(public_html))(/.+)? system_u:object_r:httpd_user_content_t
/var/www(/.*)? system_u:object_r:httpd_sys_content_t
/var/www/cgi-bin(/.*)? system_u:object_r:httpd_sys_script_exec_t
/usr/lib/cgi-bin(/.*)? system_u:object_r:httpd_sys_script_exec_t
/var/www/perl(/.*)? system_u:object_r:httpd_sys_script_exec_t
/var/www/icons(/.*)? system_u:object_r:httpd_sys_content_t
/var/cache/httpd(/.*)? system_u:object_r:httpd_cache_t
/etc/vhosts -- system_u:object_r:httpd_config_t
/usr/sbin/httpd -- system_u:object_r:httpd_exec_t
/usr/sbin/apache(2)? -- system_u:object_r:httpd_exec_t
/usr/sbin/suexec -- system_u:object_r:httpd_suexec_exec_t
/var/log/httpd(/.*)? system_u:object_r:httpd_log_t
/var/log/apache(2)?(/.*)? system_u:object_r:httpd_log_t
/var/log/cgiwrap.log.* -- system_u:object_r:httpd_log_t
/var/cache/ssl.*.sem -- system_u:object_r:httpd_cache_t
/var/cache/mod_ssl(/.*)? system_u:object_r:httpd_cache_t
/var/run/apache(2)?.pid.* -- system_u:object_r:httpd_var_run_t
/var/lib/httpd(/.*)? system_u:object_r:httpd_var_lib_t
/var/lib/php/session(/.*)? system_u:object_r:httpd_var_run_t
/etc/apache-ssl(2)?(/.*)? system_u:object_r:httpd_config_t
/usr/lib/apache-ssl(/.*)? -- system_u:object_r:httpd_exec_t
/usr/sbin/apache-ssl(2)? -- system_u:object_r:httpd_exec_t
/var/log/apache-ssl(2)?(/.*)? system_u:object_r:httpd_log_t
/var/run/apache-ssl(2)?.pid.* -- system_u:object_r:httpd_var_run_t
/var/run/gcache_port -s system_u:object_r:httpd_var_run_t
/var/lib/squirrelmail/prefs(/.*)? system_u:object_r:httpd_squirrelmail_t
/usr/bin/htsslpass -- system_u:object_r:httpd_helper_exec_t
/usr/share/htdig(/.*)? system_u:object_r:httpd_sys_content_t
/var/lib/htdig(/.*)? system_u:object_r:httpd_sys_content_t

針對上述的內容,可以對如下的幾個常見問題進行簡單處理:

1.phpmyadmin在非默認/var/www/html目錄下無法運轉
通常類似的情況都是在配置了虛擬主機時,訪問/phpmyadmin等提示403訪問拒絕,日誌裡也提示Permission denied,這是因為phpmyadmin防止的目錄及文件本身屬性不符合context要求。

假設phpmyadmin放在/web目錄下,那麼執行:

chcon -R -t httpd_user_content_t /web

則會令/web及其下所有子目錄/文件,包括phpmyadmin文件都獲得了httpd_user_content_t的屬性,如果其傳統的Unix屬性對httpd來說是可讀的話,再重新訪問一下就應該可以了。


2./home目錄下的虛擬主機無法運轉
與問題1也是類似的,不過根據上文中context的定義,/home目錄下必須是用戶的$HOME/www或public_html或web目錄才是httpd_user_content_t類型,因此建議將要作為web頁面的內容放置在用戶的$HOME/www或web或public_html裡,並確保其屬性是httpd_user_content_t,使用如下命令查看:

ls -Z /home/abc/
drwxr-xr-x abc abc user_u:object_r:user_home_dir_t tmp
drwxrwxr-x abc abc user_u:object_r:httpd_user_content www

如不是,則可通過chcon來逐級目錄及文件更改,直至最後能訪問:

chcon -R -t httpd_user_content_t /home/abc/web
chcon -t user_home_dir_t /home/abc

3.CGI程序無法運行
如果cgi程序放在/var/www/cgi-bin/裡也無法執行,遇到403或500錯誤的話,可以檢查cgi程序的屬性,按SELinux contexts文件裡定義的,/var/www/cgi-bin/裡必須是httpd_sys_script_exec_t 屬性。通過ls -Z查看,如果不是則通過如下命令更改:

chcon -t httpd_sys_script_exec_t /var/www/cgi-bin/*.cgi

如果是虛擬主機裡的cgi,則參考問題2使之能正常使用普通的功能後,再通過chcon設置cgi文件的context為httpd_sys_script_exec_t即可。

4.Setuid/gid 程序無法運行
例如早期的SqWebMail及qmailadmin等,需要setuid/gid的支持,但在SELinux下這將受到嚴格限制。第一種方法是比較徹底的辦法,能保留系統的安全性,通過:

audit2allow -l -i /var/log/messages

將SELinux拒絕的信息轉換為相應的policy allow指令,將這些指令添加到SELinux policy 的src裡相應的配置文件,重新生成policy並加載。但這樣做相對比較麻煩。

另一個方法最簡單,但將使apache得不到保護。首先確定SELinux 類型是targeted的:

cat /etc/selinux/config|grep SELINUXTYPE

然後,使apache脫離SELinux保護:

setsebool -P httpd_disable_trans 1

然後重啟動apache:

/etc/init.d/httpd restart

這樣所有apache強制的檢查都失效,需要setuid/gid的程序可以正常使用。但這樣帶來了增加漏洞的危險,對於迫切需要運行而又很急的情況,本方法是一個最大限度減少系統安全缺失的最後辦法。對於取消SELinux 未必是一個好方法。



Q: 要怎麼把SELinux的功能關閉?

有兩種方法, 一種是在開機設定檔 /etc/grub.conf 中, 把selinux=0加到傳遞給kernel的命令列.
ex:
...
title Fedora Core (2.6.x-xxx)
root (hd0,7)
kernel /boot/vmlinuz-2.6.x-xxx ro selinux=0 root=LABEL=/
initrd /boot/initrd-2.6.x-xxx.img
...

或者直接修改SELinux設定檔 /etc/sysconfig/selinux, 把SELINUX這個參數設成disabled, 再重新開機
就可以了.
ex:
# 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=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted


====================================================================

SELinux 對 httpd 預設的一些 Types:

httpd_sys_content_t
httpd_sys_script_exec_t
httpd_sys_script_ro_t
httpd_sys_script_rw_t
httpd_sys_script_ra_t
httpd_unconfined_script_exec_t
httpd_user_script_exec_t
跟 httpd 相關的一些 booleans 的設定:

允許執行 cgi:
setsebool -P httpd_enable_cgi 1

允許存取使用者的 $HOME 目錄:
setsebool -P httpd_enable_homedirs 1
chcon -R -t httpd_sys_content_t ~user/public_html

允許存取 the controling terminal
setsebool -P httpd_tty_comm 1

不區分 file controls based on context, 設成 0 時 one httpd service can not interfere with another
setsebool -P httpd_unified 0

關掉 internel scripting (PHP),這樣 php 就無法運作了
setsebool -P httpd_builtin_scripting 0

允許 httpd scripts 連到外面的網路
setsebool -P httpd_can_network_connect 1

停用 suexec transition
setsebool -P httpd_suexec_disable_trans 1

停用 SELinux 對 httpd daemon 的保護
setsebool -P httpd_disable_trans 1

1 則留言:

蒼龍之風 提到...

感謝你,留這份學習紀錄。

原因:想說已經改了檔案權限了怎麼還是權限不足,看了messages後上網查才知道是SELinux這部分出問題。