SELinux:為什麼httpd可以讀取/var/www/html中的檔案?

SELinux:為什麼httpd可以讀取/var/www/html中的檔案?

我只是想更好地理解目標 SELinux。雖然我了解用戶、進程、連接埠等都以四元組標記,以及如何<user>,role>,<type>,<sensitivity>使用等設定標籤。semanage, restorecon

例如,查看httpdREL8 上的我可以在進程和預設DocumentRoot目錄上看到以下標籤/var/www/html

[root@mylinux targeted]# ls -alZ /var/www/html
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0  6 Aug 17 16:53 .
drwxr-xr-x. 4 root root system_u:object_r:httpd_sys_content_t:s0 33 Aug 17 16:53 ..

[root@mylinux targeted]# ps -efZ | grep httpd
system_u:system_r:httpd_t:s0    root       35291       1  0 13:23 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     35292   35291  0 13:23 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     35293   35291  0 13:23 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     35294   35291  0 13:23 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache     35295   35291  0 13:23 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 35838 27088  0 13:30 pts/0 00:00:00 grep --color=auto httpd

但我正在尋找的是以下問題的答案:

  1. 為什麼允許httpd進程存取/var/www/html中的文件?此處檢查哪些標籤,哪些順序以及哪些標籤必須相符?
  2. role允許此存取的定義是什麼樣的?如何查看角色定義
  3. 如何確定進程如何存取特定標籤的資源
  4. 身為管理員,我可以修改角色,以便也可以存取其他文件上下文嗎

預先非常感謝您的任何澄清。

答案1

從你的輸出:

[root@mylinux targeted]# ps -efZ | grep httpd
system_u:system_r:httpd_t:s0    root       35291       1  0 13:23 ?        

httpd作為標籤運行httpd_t。這樣的標籤透過特定的selinux域轉換規則附加到進程上。

然後,我們可以看到:

[root@mylinux targeted]# ls -alZ /var/www/html
total 0
drwxr-xr-x. 2 root root system_u:object_r:httpd_sys_content_t:s0  6 Aug 17 16:53 .
drwxr-xr-x. 4 root root system_u:object_r:httpd_sys_content_t:s0 33 Aug 17 16:53 ..

/var/www/html具有允許的網域標籤httpd_sys_content_t。因此,運行 的進程httpd_d可以讀取標有相同標籤的檔案系統內容httpd_sys_content_t

所有這些都是透過非常複雜(且冗長)的 selinux 規則來控制的。系統管理員不需要理解整個規則集,所以不要驚慌。重要的是要理解 a) selinux 日誌、b)semanage和 c)audit2allow

欲了解更多信息,您可以閱讀紅帽 Selinux 文檔

回答您的問題:

  1. 為什麼允許httpd進程存取/var/www/html中的檔案?因為規則允許有httpd_t標籤的程序讀取有httpd_sys_content_t標籤的檔案(即:ALLOW apache_process apache_log:FILE READ;

  2. 定義允許此存取的角色是什麼樣的?角色將 MAC 應用於使用者類型(即:員工),但在預設策略中不使用;相反,它們用於(非預設)MLS 策略。

  3. 如何確定進程如何存取特定標籤的資源?您可以轉儲整個策略集,但這不會提供簡單的資訊。相反,看看semange fcontext -l

  4. 作為管理員,我是否可以修改角色,以便也可以存取其他文件上下文?當然。您可以從頭開始編寫策略(不建議)或使用audit2allow(更好)。但是,請盡量避免這種情況,因為任何自訂策略變更都將難以追蹤/管理;相反,如果可能,請使用setseboolsemanage自訂您的基本策略。

相關內容