Httpd SELinux Context

  1. 查看網頁伺服器根目錄下有 file1, file2 兩個檔案,權限是任何人都可讀取。
    [root@kvm8 ~]# ll /var/www/html/
    total 8
    -rw-r--r--. 1 root root 10 Nov  6 20:55 file1
    -rw-r--r--. 1 root root 10 Nov  6 21:18 file2
    
  2. 使用 curl 訪問網頁 file1,回應沒有權限存取。
    [root@kvm8 ~]# curl http://127.0.0.1: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 /file1
    on this server.<br />
    </p>
    </body></html>
    
  3. 使用 curl 訪問網頁 file2,成功回應檔案內容 "web test2"。
    
    
  4. 查看 SELinux Contexts,file1 是 admin_home_t,file2 是 httpd_sys_contect
    [root@kvm8 ~]# ls -Z /var/www/html/
           system_u:object_r:admin_home_t:s0 file1
    system_u:object_r:httpd_sys_content_t:s0 file2
    
  5. 將 /var/www/html 目錄下所有檔案,恢復預設的 selinux contexts。
    [root@kvm8 ~]# restorecon -Rv /var/www/html/
    
  6. 再查看 SELinux Contexts,file1 還是 admin_home_t
    [root@kvm8 ~]# ls -Z /var/www/html/
           system_u:object_r:admin_home_t:s0 file1
    system_u:object_r:httpd_sys_content_t:s0 file2
    
  7. 查看用戶新增的 SELinux Contexts type,/var/www/html/file1 被設成 admin_home_t
    [root@kvm8 ~]# cat /etc/selinux/targeted/contexts/files/file_contexts.local
    # This file is auto-generated by libsemanage
    # Do not edit directly.
    
    /var/www/html/file1    system_u:object_r:admin_home_t:s0
    
  8. semanage -d 刪除 /var/www/html/file1 自訂的 admin_home_t
    [root@kvm8 ~]# semanage fcontext -d -t admin_home_t /var/www/html/file1
    
  9. 再查看,已無用戶新增的 SELinux Contexts type。
    [root@kvm8 ~]# cat /etc/selinux/targeted/contexts/files/file_contexts.local
    # This file is auto-generated by libsemanage
    # Do not edit directly.
    
  10. 將 /var/www/html 目錄下所有檔案,恢復預設的 selinux contexts。
    [root@kvm8 ~]# restorecon -Rv /var/www/html/
    Relabeled /var/www/html/file1 from system_u:object_r:admin_home_t:s0 to system_u:object_r:httpd_sys_content_t:s0
    
  11. 查看 SELinux Contexts,file1 及 file2 都是 httpd_sys_contect
    [root@kvm8 ~]# ls -Z /var/www/html/
    system_u:object_r:httpd_sys_content_t:s0 file1
    system_u:object_r:httpd_sys_content_t:s0 file2
    
  12. 使用 curl 訪問網頁 file1 及 file2,都成功回應檔案內容。
    [root@kvm8 ~]# curl http://127.0.0.1:82/file1
    web test1
    [root@kvm8 ~]# curl http://127.0.0.1:82/file2
    web test2