LogRotate 和 Apache 的 SELinux 权限

Cra*_*cis 4 security linux selinux logrotate

使用目录结构:

/www/live/website1/app/
/www/live/website1/files/
/www/live/website1/logs/
Run Code Online (Sandbox Code Playgroud)

Apache 至少需要以下访问权限:

app: read-only access, but read-write is fine (files already chmod 0644)
files: read-write access
logs: read-write access
Run Code Online (Sandbox Code Playgroud)

通过以下方式设置了以下两个规则:

/usr/sbin/semanage fcontext -a -t httpd_sys_content_t "/www/live/.*/.*";
/usr/sbin/semanage fcontext -a -t httpd_log_t "/www/live/.*/logs(/.*)?";

/sbin/restorecon -vr "/www";
Run Code Online (Sandbox Code Playgroud)

应用了哪些,并且似乎工作正常……但是 LogRotate 并不满意。

LogRotate 配置当前为:

/www/live/*/logs/*access_log /www/live/*/logs/*access_log_443 {
    weekly
    rotate 52
    missingok
    notifempty
    nodateext
    sharedscripts
    postrotate
        /usr/sbin/apachectl graceful > /dev/null
    endscript
}
Run Code Online (Sandbox Code Playgroud)

然而,这似乎被 SELinux 阻止,当它试图点击与/www/live文件夹相关的 inode (在下面的示例中为 262146)时,条目出现在 audit.log 中......因为它可能试图列出 /www 中的文件夹/居住/。

type=AVC msg=audit(1396579563.324:316060): avc:  denied  { read } for  pid=12336 comm="logrotate" name="live" dev=dm-0 ino=262146 scontext=system_u:system_r:logrotate_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:default_t:s0 tclass=dir
type=SYSCALL msg=audit(1396579563.324:316060): arch=c000003e syscall=2 success=no exit=-13 a0=7fff2cef68b0 a1=90800 a2=7fff2cef6b5a a3=8 items=0 ppid=12334 pid=12336 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=35531 comm="logrotate" exe="/usr/sbin/logrotate" subj=system_u:system_r:logrotate_t:s0-s0:c0.c1023 key=(null)
Run Code Online (Sandbox Code Playgroud)

那么我应该设置这个父目录的上下文是什么?

/usr/sbin/semanage fcontext -a -t default_t "/www(/.*)";
Run Code Online (Sandbox Code Playgroud)

我知道的地方default_t不起作用,也不起作用var_t......作为参考,我并不真正关心可以看到这些文件夹的内容,因为它们已经是 chmod 0755。


对于奖励积分...是否有一种简单的方法可以查看程序拥有的完整权限列表?我知道 LogRotate 必须能够访问httpd_log_tvar_log_t.

令人讨厌的是,手动运行 LogRotate 似乎绕过了这些限制,因为我认为它继承了用户权限(与通过 cron 运行时不同)。

Cra*_*cis 11

尚未确认这是否是正确答案...

/usr/sbin/semanage fcontext -a -t sysfs_t "/www(/.*)";
/usr/sbin/semanage fcontext -a -t httpd_sys_content_t "/www/live/.*/.*";
/usr/sbin/semanage fcontext -a -t httpd_log_t "/www/live/.*/logs(/.*)?";
/sbin/restorecon -vr "/www";
Run Code Online (Sandbox Code Playgroud)

sysfs_t是重要的一点。

我可以找到 LogRotate 可以使用的域:

sesearch -s logrotate_t -SA
Run Code Online (Sandbox Code Playgroud)

快速搜索“dir”的“读取”(不仅仅是“打开”)权限:

sesearch -s logrotate_t -SA -c dir -p read | sort
Run Code Online (Sandbox Code Playgroud)

然后扫描列表,我会说这sysfs_t是最合适的。


我确实发现的一个问题是,如果我/usr/sbin/logrotate自己运行,它会继承 root 帐户上下文:

id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Run Code Online (Sandbox Code Playgroud)

所以它会自动获得“无限制”访问(即完全访问)......所以为了测试,我发现使用以下虽然不完美,但做了一些工作:

sandbox /usr/sbin/logrotate -d /etc/logrotate.conf
Run Code Online (Sandbox Code Playgroud)

我还发现了newroleruncon,两者都需要通过以下方式单独安装在 RedHat/CentOS 系统上:

yum install policycoreutils-newrole

newrole -r system_r -t logrotate_t
runcon -r system_r -t logrotate_t /usr/sbin/logrotate -d /etc/logrotate.conf
Run Code Online (Sandbox Code Playgroud)

但是这两个都给了我拒绝错误的许可(可能是由于不允许转换):

http://wiki.gentoo.org/wiki/SELinux/Tutorials/How_does_a_process_get_into_a_certain_context


我发现其他有用的东西:

yum install setools-console

seinfo -usystem_u -x
seinfo -rsystem_r -x
seinfo -tlogrotate_t -x

seinfo -tsysfs_t -x
Run Code Online (Sandbox Code Playgroud)

并检查我在系统上创建的规则列表:

cat /etc/selinux/targeted/contexts/files/file_contexts.local
Run Code Online (Sandbox Code Playgroud)

有关 SELinux 的更多信息,我发现这 17 个教程非常有帮助:

http://wiki.gentoo.org/wiki/SELinux/Tutorials


就我个人而言,我发现所有这些程序都非常不一致,并且可以理解为什么大多数人在默认情况下只是禁用 SELinux……例如

  • seinfo 选项 -u/r/t 后不能有空格
  • 您需要安装额外的软件包才能获取seinfonewrole
  • 您不能轻松地在给定上下文下手动运行程序(用于测试目的)。
  • audit.log文件使用时间戳,因此请尝试使用ausearch -m avc --start today.
  • 使用的许多程序没有命名约定(例如matchpathcon)。
  • 我不会说的输出(或操作)audit2allow是显而易见的。

这是一种耻辱,因为它总体上似乎是一个非常强大的系统。