Setup SELinux When Installing nginx on Centos 7 - goddes4/python-study-wiki GitHub Wiki
Centos 7 ์ Nginx + uWSGI ์ค์น์ SELinux ์ค์
Centos 7 ์์ Nginx + uWSGU + Flask ๋ฅผ ์ฌ์ฉํ ๋ Nginx ์ uWSGI ๊ฐ ๋ด๋ถ ์์ผ ํต์ ์์ ๊ถํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ๋ฌธ์ ์ ์์ธ์ SELinux ์ธ ๊ฒฝ์ฐ๊ฐ ๋ค์์ด๋ค.
SELinux๊ฐ ํ์ฑํ ๋๋ฉด ํน์ ํ๋ก๊ทธ๋จ์ด ํน์ ํ์ผ์ ์ ๊ทผ ๊ฐ๋ฅํ์ง๋ฅผ ํ๋จํ๊ณ ๊ทธ์ ๋ฐ๋ผ ํ์ฉ ๋๋ ์ฐจ๋จํ๋ค.
์๋ฅผ ๋ค๋ฉด nginx๊ฐ uWSGI ์ ํต์ ํ๊ธฐ ์ํด ์์ฑํ unix sock ํ์ผ์ ์ ์ฐ๊ธฐ ๊ถํ์ด ์๋์ง ํ์ธํ๊ณ ์์ผ๋ฉด ์ฐจ๋จํ๋ค.
๋ค์์ ์ ์์์ ๋ํ ์ฐจ๋จ ๋ก๊ทธ์ด๋ค. ๋ก๊ทธ ํ์ผ์ /var/log/audit/audit.log ์ด๋ค.
type=AVC msg=audit(1475712790.359:4968): avc: denied { write } for pid=17389 comm="nginx"
name="uwsgi.sock" dev="tmpfs" ino=113259 scontext=system_u:system_r:httpd_t:s0
tcontext=system_u:object_r:var_run_t:s0 tclass=sock_file
SELinux ๋
- Linux์ ๋ณด์์ ๊ฐํํด ์ฃผ๋ ๋ณด์ ๊ฐํ ์ปค๋
- zero-day ๊ณต๊ฒฉ ๋ฐ buffer overflow ๋ฑ ์ดํ๋ฆฌ์ผ์ด์ ์ทจ์ฝ์ ์ผ๋ก ์ธํ ํดํน์ ๋ฐฉ์งํด์ค
SELinux ๋ชจ๋ ํ์ธ
# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
ํด๊ฒฐ๋ฐฉ๋ฒ
๋ค์์ ์ค๋ช ๋ 4๊ฐ์ง ๋ฐฉ๋ฒ์ค ๋ณ๋์ ๋ฏฟ์๋งํ ๋ณด์์๋ฃจ์ ์ ๊ฐ์ง๊ณ ์์ง ์๋ค๋ฉด 4๋ฒ์งธ ๋ฐฉ๋ฒ์ ๊ฐ์ฅ ์ถ์ฒํ๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ #1 (SELinux ํด์ )
- SELINUX ๊ฐ์ enforcing ์์ disabled ๋ก ๋ณ๊ฒฝ ํ ์ ์ฅ ๋ฐ ์ฌ๋ถํ ํ๋ค.
- ์ธํฐ๋ท์ ์คํ๋์ง ์๋ ๊ฒฝ์ฐ๋ ์๋ฒํ๋จ์ ๋ณ๋์ ๋ณด์์ฅ๋น(๋ฐฉํ๋ฒฝ, IPS)๋ฑ์ด ๊ตฌ์ฑ๋์ด ์๋ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
# vi /etc/sysconfig/selinux
ํด๊ฒฐ ๋ฐฉ๋ฒ #2 (๋์๋ชจ๋ : Permissive ๋ณ๊ฒฝ)
- ์ด ๋ฐฉ๋ฒ์ SELinux์ ๋์ ๋ชจ๋๋ฅผ Enforcing ์์ Permissive ๋ก ๋ณ๊ฒฝํ์ฌ, rule์ ์ด๊ธ๋๋๋ผ๋ audit log๋ฅผ ๋จ๊ธฐ๊ณ ๋์์ ํ์ฉ๋๋ค.
- ๊ทผ๋ณธ์ ํด๊ฒฐ์ ์๋๊ณ ์ค๋ฅ์ ์์ธ์ด SELinux ์์ ํ๋จํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋๋ค.
# setenforce 0
# getenforce
Permissive
ํด๊ฒฐ ๋ฐฉ๋ฒ #3 (httpd_can_network_connect ํ์ฑํ)
- httpd ๋คํธ์ํฌ ์ ์์ ํ์ฉํ์ฌ Nginx ์ uWSGI์ ํต์ ์ ๊ฐ๋ฅํ๊ฒ ํด์ค๋ค.
# getsebool -a | grep http
# setsebool httpd_can_network_connect on -P
ํด๊ฒฐ ๋ฐฉ๋ฒ #4 (์๋ก์ด ์ ์ฑ ์ ์ฉ)
- audit log ๋ฅผ ์ด์ฉํ์ฌ ๊ถํ ์ ์ฑ ํ์ผ์ ๋ง๋ ๋ค.
- ์คํ ๊ฒฐ๊ณผ๋ก .pp ํ์ผ๊ณผ .te ํ์ผ์ด ๋ง๋ค์ด ์ง๋ค.
- te ํ์ผ์ ์ถ๊ฐ๋๋ ์ ์ฑ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๊ณ , pp ํ์ผ์ ์๋ก์ด ๊ถํ ์ ์ฑ ์ ์ ์ฉํ๋๋ฐ ์ฌ์ฉ๋๋ค.
audit2allow ์ฌ์ฉ์ ์ํ ํจํค์ง ์ค์น
audit2allow ํ์ผ์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ policycoreutils-python๋ฅผ ์ค์นํ๋ค.
# yum install policycoreutils-python
์ ๊ทผ์ด ํ์ฉ๋์ง ์๋ ํญ๋ชฉ์๋ํด ๊ถํ์ ์ฃผ๋ ์ ์ฑ ์์ฑ ๋ฐ ์ ์ฉ
# cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M nginx
# semodule -i nginx.pp
์ ์ฑ ์ทจ์
ํฅํ์ ์ ์ฑ ์ ์ทจ์ํ ๊ฒฝ์ฐ๋ฅผ ๋๋นํ์ฌ pp ํ์ผ์ ๋ณด์กดํด์ฃผ๋ ๊ฒ์ด ์ข๋ค.
# semodule -r httpd.pp
write ๊ถํ ์ ์ฑ ๊ฒฐ๊ณผ๋ฌผ ์์
Audit Denied ๋ก๊ทธ
type=AVC msg=audit(1475712790.359:4968): avc: denied { write } for pid=17389 comm="nginx"
name="uwsgi.sock" dev="tmpfs" ino=113259 scontext=system_u:system_r:httpd_t:s0
tcontext=system_u:object_r:var_run_t:s0 tclass=sock_file
์ ์ฑ ๊ฒฐ๊ณผ๋ฌผ
module nginx 1.0;
require {
type var_run_t;
type httpd_t;
class sock_file write;
}
#============= httpd_t ==============
allow httpd_t var_run_t:sock_file write;
name_connect ๊ถํ ์ ์ฑ ๊ฒฐ๊ณผ๋ฌผ ์์
Audit Denied ๋ก๊ทธ
type=AVC msg=audit(1475713241.083:4976): avc: denied { name_connect } for pid=17896 comm="nginx"
dest=8787 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:jboss_debug_port_t:s0
tclass=tcp_socket
์ ์ฑ ๊ฒฐ๊ณผ๋ฌผ
module nginx 1.0;
require {
type httpd_t;
type jboss_debug_port_t;
class tcp_socket name_connect;
}
#============= httpd_t ==============
#!!!! This avc is allowed in the current policy
allow httpd_t jboss_debug_port_t:tcp_socket name_connect;