I. SELinux là gì?
Theo wiki thì:
Security-Enhanced Linux (SELinux) is a Linux kernel security module that provides a mechanism for supporting access control security policies, including United States Department of Defense–style mandatory access controls (MAC).
Nghĩa là nó sẽ được tăng cường thêm 1 module security nữa để hỗ trợ việc quản lý quyền truy cập, bao gồm cái MAC của tụi mẽo như ở trên.
Thế nên mới nảy sinh thêm vấn đề là: File thuộc về owner là tôi, permission 755, mà tôi vẫn chẳng thể write content vào file này được.
II. Hoàn cảnh
Khi thuê 1 con AWS EC2, thì ở bước 1, ta phải chọn Amazon Machine Image (AMI) cho instance sắp chào đời. Ở đây tôi chọn Centos7 tại AWS Marketplace cho instance của mình. Trong phần mô tả về AMI này có đoạn: All official CentOS Linux images are built with SELINUX set to enforcing mode. However, we test the images with both Selinux enabled as well as permissive - nghĩa là AMI này tích hợp sẵn SELINUX rồi, và đang được bật.
Với 1 stack kinh điển cho web app là LAMP, thì sau khi cài đặt xong đúng như tutorial, nhưng app vẫn ko thể chạy được vì có thể gặp phải 1 số vấn đề sau đây:
Application must reside outside of the default Apache directory (/var/www/html).
SELinux blocks Apache from loading content outside of default directories.
Some files require for the application read and write access. By default, they cannot.
Some directories require write access for uploading content. By default, they cannot.
Hay gặp nhất chắc là trường hợp 2 và 3, khi mà app ko thể write content vào log file hoặc ko thể save file vào folder uploads.
Lý do: SELINUX nó có phần set quyền cho httpd service được làm gì và ko được làm gì.
Cụ thể, check với lệnh:
$sestatus -b | grep httpd
[centos@ip-xxx-yyy-zzz-ttt ~]$ sestatus -b | grep httpd
httpd_anon_write off
httpd_builtin_scripting on
httpd_can_check_spam off
httpd_can_connect_ftp off
httpd_can_connect_ldap off
httpd_can_connect_mythtv off
httpd_can_connect_zabbix off
httpd_can_network_connect on
httpd_can_network_connect_cobbler off
httpd_can_network_connect_db off
httpd_can_network_memcache off
httpd_can_network_relay off
httpd_can_sendmail off
httpd_dbus_avahi off
httpd_dbus_sssd off
httpd_dontaudit_search_dirs off
httpd_enable_cgi on
httpd_enable_ftp_server off
httpd_enable_homedirs off
httpd_execmem off
httpd_graceful_shutdown on
httpd_manage_ipa off
httpd_mod_auth_ntlm_winbind off
httpd_mod_auth_pam off
httpd_read_user_content off
httpd_run_ipa off
httpd_run_preupgrade off
httpd_run_stickshift off
httpd_serve_cobbler_files off
httpd_setrlimit off
httpd_ssi_exec off
httpd_sys_script_anon_write off
httpd_tmp_exec off
httpd_tty_comm off
httpd_unified off
httpd_use_cifs off
httpd_use_fusefs off
httpd_use_gpg off
httpd_use_nfs off
httpd_use_openstack off
httpd_use_sasl off
httpd_verify_dns off
Nhìn thấy mấy cái flag liên quan tới write mà đang là off thì đúng bệnh rồi.
III. Cách giải quyết
Có thể giải quyết vấn đề bằng một trong các cách sau:
Đách dùng httpd(apache) nữa, chuyển sang nginx với php-fpm, nghĩa là thay stack LAMP thành stack LEMP. Mình cũng ưa LEMP hơn, do cấu hình "có vẻ" đơn giản và dễ hiểu hơn LAMP.
Disable SELinux mode đi, cho OS về dạng ko có SELinux, lúc này thì cứ đúng permission là nhai được thôi. Tuy nhiên cách này ko được khuyến khích.
Thêm nữa, nếu làm thế này thì chọn luôn OS ko có SELinux module cho rồi :))
Tự tạo các SELinux Policies cho các folders/files cần thiết, và enable các policies này lên, thì sẽ tránh được 2 mà app vẫn chạy ngon. Cách này thì hơi loằng ngoằng tí, vui lòng xem link tham khảo 1.
Cài tool vào, chạy theo tool hướng dẫn, và làm theo lời tool mách bảo :)))
Cái này cũng vui lòng xem link tham khảo.
Theo:kipalog
Nhận xét
Đăng nhận xét