在 Centos 8 服务器上,我将一个更大的磁盘安装到 /mnt/data。服务器运行 rsyslogd,它有一个将外部系统的日志写入 /mnt/data/logs/$HOSTNAME 的策略。我现在遇到的问题是,当 /mnt/data/logs 的 selinux fcontext 设置为“syslogd_var_lib_t”时,logrotate 不会旋转日志,或者当 fcontext 设置为“logrotate_var_lib_t”时,rsyslod 不会将数据写入文件。
我找到了 samba 和 httpd 的几种解决方案,其中 bools 设置为“*anon_write 1”,但对于 syslog 和 logrotate,我没有看到 bools。
有没有办法让selinux在/mnt/data/logs中同时允许logrotate和rsyslogd?
fcontext 为 /mnt/data/logs 的 sealert 输出设置为“logrotate_var_lib_t”:
SELinux is preventing /usr/sbin/rsyslogd from append access on the file 10.9.7.15.log.
***** Plugin catchall (100. confidence) suggests **************************
If you believe that rsyslogd should be allowed append access on the 10.9.7.15.log file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'rs:main Q:Reg' --raw | audit2allow -M my-rsmainQReg
# semodule -X 300 -i my-rsmainQReg.pp
Additional Information:
Source Context system_u:system_r:syslogd_t:s0
Target Context system_u:object_r:logrotate_var_lib_t:s0
Target Objects 10.9.7.15.log [ file ]
Source rs:main Q:Reg
Source Path /usr/sbin/rsyslogd
Port <Unknown>
Host HAMS010125.global.bdfgroup.net
Source RPM Packages rsyslog-8.1911.0-6.el8.x86_64
Target RPM Packages
SELinux Policy RPM selinux-policy-targeted-3.14.3-54.el8.noarch
Local Policy RPM selinux-policy-targeted-3.14.3-54.el8.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name HAMS010125.global.bdfgroup.net
Platform Linux HAMS010125.global.bdfgroup.net
4.18.0-240.1.1.el8_3.x86_64 #1 SMP Thu Nov 19
17:20:08 UTC 2020 x86_64 x86_64
Alert Count 68679
First Seen 2021-03-30 07:47:58 CEST
Last Seen 2021-03-30 10:47:03 CEST
Local ID 5ee9e182-0188-4ac8-b28e-5c8c9ab18933
Raw Audit Messages
type=AVC msg=audit(1617094023.794:76568): avc: denied { append } for pid=1452 comm=72733A6D61696E20513A526567 n
ame="10.9.7.15.log" dev="dm-3" ino=23330820 scontext=system_u:system_r:syslogd_t:s0 tcontext=system_u:object_r:log
rotate_var_lib_t:s0 tclass=file permissive=0
type=SYSCALL msg=audit(1617094023.794:76568): arch=x86_64 syscall=openat success=no exit=EACCES a0=ffffff9c a1=7f8
2b40a4fa0 a2=80541 a3=1a4 items=0 ppid=1 pid=1452 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0
fsgid=0 tty=(none) ses=4294967295 comm=rs:main Q:Reg exe=/usr/sbin/rsyslogd subj=system_u:system_r:syslogd_t:s0 ke
y=(null)
Hash: rs:main Q:Reg,syslogd_t,logrotate_var_lib_t,file,append
sealert 输出,/mnt/data/logs 的 fcontext 设置为“syslogd_var_lib_t”:
SELinux is preventing /usr/sbin/logrotate from read access on the file 10.9.7.16.log.
***** Plugin catchall (100. confidence) suggests **************************
If you believe that logrotate should be allowed read access on the 10.9.7.16.log file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'logrotate' --raw | audit2allow -M my-logrotate
# semodule -X 300 -i my-logrotate.pp
Additional Information:
Source Context system_u:system_r:logrotate_t:s0-s0:c0.c1023
Target Context system_u:object_r:syslogd_var_lib_t:s0
Target Objects 10.9.7.16.log [ file ]
Source logrotate
Source Path /usr/sbin/logrotate
Port <Unknown>
Host HAMS010125.global.bdfgroup.net
Source RPM Packages
Target RPM Packages
SELinux Policy RPM selinux-policy-targeted-3.14.3-54.el8.noarch
Local Policy RPM selinux-policy-targeted-3.14.3-54.el8.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name HAMS010125.global.bdfgroup.net
Platform Linux HAMS010125.global.bdfgroup.net
4.18.0-240.1.1.el8_3.x86_64 #1 SMP Thu Nov 19
17:20:08 UTC 2020 x86_64 x86_64
Alert Count 8
First Seen 2021-03-26 02:51:01 CET
Last Seen 2021-03-29 02:51:01 CEST
Local ID 9ee8b323-1833-484b-b7c7-4b746c30d60b
Raw Audit Messages
type=AVC msg=audit(1616979061.951:6206): avc: denied { read } for pid=632546 comm="logrotate" name="10.9.7.16.$
og" dev="dm-3" ino=23330825 scontext=system_u:system_r:logrotate_t:s0-s0:c0.c1023 tcontext=system_u:object_r:sysl$
gd_var_lib_t:s0 tclass=file permissive=0
Hash: logrotate,logrotate_t,syslogd_var_lib_t,file,read
任何提示表示赞赏,谢谢。
答案1
您可以使用与具有相同的标签来标记您的日志目录/var/log
。然后调用restorecon就可以生效了。
semanage fcontext -a -t var_log_t /mnt/data/logs
restorecon /mnt/data/logs
只要下面的实际文件/mnt/data/logs
是由 rsyslog 创建并使用 logrotate 轮换的,它就可以工作。您可能需要标记您的$HOSTNAME
目录;取决于您的设置。
更正确的方法是创建您自己的 selinux 策略,以允许您需要的操作。这可以通过相关audit.log
线路和工具自动完成audit2allow
。
另一种可能性是将 selinux 设置为允许 logrotate semanage permissive -a logrotate_t
。 rsyslog 也是如此。但如果 logrotate 和 rsyslog 其中之一发疯,这会有效地消除系统范围的保护,所以我不推荐它。