2010年8月25日 星期三

how to disable SeLinux for httpd

http://copilotco.com/mail-archives/fedora.2008/msg03076.html

Solution: chcon -t bin_t /usr/sbin/httpd

2010年8月23日 星期一

Disable Apache SeLinux protection

http://www.cyberciti.biz/faq/howto-disable-httpd-selinux-security-protection/
http://nixcraft.com/networking-firewalls-security/13433-explain-selinux-setsebool-getsebool-commands.html

The setsebool is used to set SELinux boolean value i.e. various configurtions can be enabled or disabled using this tool. In other words, the setsebool command switches on and off the protection of Selinux. Type getsebool -a to see all such options which can be enabled or disabled at run time:

Code:
getsebool -aFor example, if httpd_disable_trans set to 1, it will disable SELinux protection for Apache web server.

To disable it, enter Code:
setsebool httpd_disable_trans 1

To enable it, enter Code:
setsebool httpd_disable_trans 0

2010年8月18日 星期三

Permission denied on SELinux?

Original: http://blog.xuite.net/huenlil/note/4992916

在UNIX或Linux作業系統中,系統管理員「root」擁有至高無上的權限,可以完全掌控系統,任意修改設定,管理系統十分容易。但是,當這個帳號被盜用或是駭客藉由網路服務的漏洞入侵取得帳號權限時,對系統本身將是一大危害,後果難以想像。

假設,駭客現在藉由Apache網頁伺服器成功入侵主機,如果是發生在未使用SELinux的系統,駭客將因為這個漏洞而獲得系統管理員「root」的全部權限,最後整個系統因此淪落他手,完全癱瘓。但是,如果同樣的狀況發生在啟用SELinux的主機上,結果將會不同,SELinux會將對方限制在Apache所能管轄的範圍內,因此系統的其他部份不會造成危害,安全性提高不少。



經由以上的例子,讀者就可以清楚瞭解SELinux的重要性。以下內容先概略說明SELinux的發起源由,接著介紹相關的重要設定檔案,以及如何在Fedora Core 3內如何設定SELinux。



近年來,由於病毒、駭客入侵的網路災情頻傳,所以網路安全成了大眾最關注的議題。目前在作業系統的設計方面,對資料安全的關注更勝於操作便利的考量。而Linux為了因應這方面的問題,也推出了安全性增強版的Linux—SELinux。



Fedora Core 3是目前內建SELinux最完整的Linux發行版本,無論是網路服務或相關的設定,Fedora Core 3都可以利用圖形管理界面來進行,所以想要瞭解SELinux,Fedora Core 3肯定是最好的選擇。

前面介紹的事Fedora Core 3所提供的SELinux政策設定工具,直接在圖形界面中進行安全政策的制定,使用者不需要自行修改組態檔案,對於初次接觸SELinux的系統管理人員提供相當大的便利。



--------------------------------------------------------------------------------


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