SELinux 阻止 Apache 写入文件

Clo*_*ldo 14 redhat selinux centos apache-2.4 centos7

SELinux 阻止apache用户写入它拥有的日志文件。当我这样做setenforce 0时。否则显示此错误

IOError: [Errno 13] Permission denied: '/var/www/webapp/k/site/k.log'
Run Code Online (Sandbox Code Playgroud)

文件的安全上下文:

$ ll -Z k.log 
-rw-r--r--. apache apache system_u:object_r:httpd_sys_content_t:s0 k.log
Run Code Online (Sandbox Code Playgroud)

该文件是在 SELinux 模式设置为 permissive 时创建的。

如何设置安全上下文以便apache用户可以在该目录中写入?我确实使用了该目录安全上下文,chcon但我找不到合适的文件类型。

来自audit.log

type=AVC msg=audit(1409945481.163:1561): avc:  denied  { append } for  pid=16862 comm="httpd" name="k.log" dev="dm-1" ino=201614333 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=file
type=SYSCALL msg=audit(1409945481.163:1561): arch=c000003e syscall=2 success=no exit=-13 a0=7fa8080847a0 a1=441 a2=1b6 a3=3 items=0 ppid=15256 pid=16862 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="httpd" exe="/usr/sbin/httpd" subj=system_u:system_r:httpd_t:s0 key=(null)
Run Code Online (Sandbox Code Playgroud)

sho*_*hok 20

如前所述,您应该指示 SELINUX 允许写入该文件。正确的做法是标记/var/www/webapp/k/site/为类型httpd_sys_rw_content_t

要将该目录永久标记为httpd_sys_rw_content_t,您可以使用该命令semanage fcontext -a -t httpd_sys_rw_content_t /var/www/webapp/k/site(/.*)?; restorecon -RF /var/www/webapp/k/site/ 这将在 SELINUX 二进制策略更新和文件系统重新标记后继续存在。

  • 这应该被标记为正确答案。并且建议永久标记目录(如本答案所述)。 (3认同)

Mic*_*ton 10

对于您希望 Apache 能够写入的文件,类型必须设置为httpd_sys_rw_content_t.


小智 7

这将更改权限:

chcon --type httpd_sys_rw_content_t /var/www/webapp/k/site/k.log
Run Code Online (Sandbox Code Playgroud)