Httpd SELinux Context

  1. 查看網頁伺服器根目錄下有 file1, file2, file3 三個檔案,權限是任何人都可讀取。
    [root@kvm8 ~]# ll /var/www/html/
    total 12
    -rw-r--r--. 1 root root 10 Sep 27 17:10 file1
    -rw-r--r--. 1 root root 10 Sep 27 17:10 file2
    -rw-r--r--. 1 root root 10 Sep 27 17:10 file3
    
  2. 使用 curl 訪問網頁 file1,回應沒有權限存取,而 file2 及 file3 成功存取。
    [root@kvm8 ~]# curl http://localhost:82/file1
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>403 Forbidden</title>
    </head><body>
    <h1>Forbidden</h1>
    <p>You don't have permission to access this resource.</p>
    </body></html>
    [root@kvm8 ~]# curl http://localhost:82/file2
    web test2
    [root@kvm8 ~]# curl http://localhost:82/file3
    web test3
    
  3. 使用 ls 加 -Z 選項查看 file1,file2 及 file3 的 selinux fcontext type,file1 是 admin_home_t,而 file2 及 file3 是 httpd_sys_content_t,所以 SELinux 策略不允許 httpd 存取 file1。
    [root@kvm8 ~]# ll -Z /var/www/html/
    total 12
    -rw-r--r--. 1 root root system_u:object_r:admin_home_t:s0        10 Sep 27 17:10 file1
    -rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 10 Sep 27 17:10 file2
    -rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 10 Sep 27 17:10 file3
    
  4. 使用 restorecon 還原 file1 的 selinux fcontext type。
    [root@kvm8 ~]# restorecon -v /var/www/html/file1
    
  5. 查看 file1 的 selinux fcontext type 沒有變化。
    [root@kvm8 ~]# ll -Z /var/www/html/
    total 12
    -rw-r--r--. 1 root root system_u:object_r:admin_home_t:s0        10 Sep 27 17:10 file1
    -rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 10 Sep 27 17:10 file2
    -rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 10 Sep 27 17:10 file3
    
  6. 使用 semanage 列出 selinux 對檔案 file1 的 fcontext 設定為 admin_home_t,所以還原時不會變動。
    [root@kvm8 ~]# semanage fcontext --list | grep file1
    /var/www/html/file1        all files       system_u:object_r:admin_home_t:s0
    
  7. 使用 semanage 刪除 selinux 對檔案 file1 的 fcontext 設定。
    [root@kvm8 ~]# semanage fcontext --delete --type admin_home_t /var/www/html/file1
    
  8. 再使用 semanage 列出 selinux 對檔案 file1 的 fcontext 設定,已不存在。
    [root@kvm8 ~]# semanage fcontext --list | grep file1
    
  9. 將 /var/www/html 目錄下所有檔案,恢復預設的 selinux contexts。
    [root@kvm8 ~]# restorecon -v /var/www/html/file1
    Relabeled /var/www/html/file1 from system_u:object_r:admin_home_t:s0 to 
    system_u:object_r:httpd_sys_content_t:s0
    [root@kvm8 ~]# curl http://localhost:82/file1
    web test1
    
  10. 查看 SELinux Contexts,file1,file2 及 file3 的 selinux fcontext type 都是 httpd_sys_content_t。
    [root@kvm8 ~]# ll -Z /var/www/html/
    total 12
    -rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 10 Sep 27 17:10 file1
    -rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 10 Sep 27 17:10 file2
    -rw-r--r--. 1 root root system_u:object_r:httpd_sys_content_t:s0 10 Sep 27 17:10 file3
    
  11. 使用 curl 訪問網頁 file1,成功回應檔案內容。
    [root@kvm8 ~]# curl http://localhost:82/file1
    web test1