解决SELinux导致Apache无法访问文件

问题描述

RHEL8服务器安装有Apache做文件访问用,数据卷是/dev/sdb挂载在/nfs下,要从网站访问的内容存在于/nfs/perf-insight/路径下,在Apache默认路径下设有同名软连接即/var/www/html/perf-insight/

访问网站提示“Forbidden”,查询错误日志/var/log/httpd/error_log提示如下:

[Tue Jan 19 16:38:25.344744 2021] [autoindex:error] [pid 6095:tid 139653503821568] (13)Permission denied: [client 10.72.12.63:46114] AH01275: Can't open directory for index: /var/www/html/perf-insight/

检查文件权限无误,检查SELinux Context不匹配:

# ls -lZd /var/www/html/perf-insight
lrwxrwxrwx. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 18 Dec 29 14:48 /var/www/html/perf-insight -> /nfs/perf-insight/
# ls -lZd /nfs/perf-insight
drwxrwxrwx. 5 root root unconfined_u:object_r:unlabeled_t:s0 54 Jan 19 17:22 /nfs/perf-insight

临时关闭SELinux网站可以正常访问,故此确定问题所在。

# setenforce 0
......
# setenforce 1

深入研究

检查SELinux日志:

# tail -n 3 /var/log/audit/audit.log | tee /tmp/audit
type=AVC msg=audit(1611062693.562:138): avc:  denied  {
      read } for  pid=1179 comm="httpd" name="perf-insight" dev="sdb1" ino=544682841 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:unlabeled_t:s0 tclass=dir permissive=0
type=SYSCALL msg=audit(1611062693.562:138): arch=c000003e syscall=257 success=no exit=-13 a0=ffffff9c a1=7f4728003420 a2=90800 a3=0 items=0 ppid=1034 pid=1179 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)ARCH=x86_64 SYSCALL=openat AUID="unset" UID="apache" GID="apache" EUID="apache" SUID="apache" FSUID="apache" EGID="apache" SGID="apache" FSGID="apache"
type=PROCTITLE msg=audit(1611062693.562:138): proctitle=2F7573722F7362696E2F6874747064002D44464F524547524F554E44

audit2why查看具体原因:

# audit2why -i /tmp/audit 
type=AVC msg=audit(1611062693.562:138): avc:  denied  {
      read } for  pid=1179 comm="httpd" name="perf-insight" dev="sdb1" ino=544682841 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:unlabeled_t:s0 tclass=dir permissive=0

	Was caused by:
		Missing type enforcement (TE) allow rule.

		You can use audit2allow to generate a loadable module to allow this access.

解决问题

没有采用更改/nfs/perf-insight及其子目录的SELinux Context原因是这部分数据需要被包括NFS在内的其他服务访问。

使用audit2allow生成规则:

# grep httpd_t /var/log/audit/audit.log | audit2allow -M httpd
******************** IMPORTANT ***********************
To make this policy package active, execute:

semodule -i httpd.pp

使用audit2allow-M选项会生产下面两个文件:

  1. 扩展名为.te的module policy文件
  2. 扩展名为.pp的loadable module package文件

检查.te文件:

# cat httpd.te 

module httpd 1.0;

require {
     
	type container_file_t;
	type httpd_t;
	type unlabeled_t;
	class dir read;
	class file {
      getattr open read };
}

#============= httpd_t ==============
allow httpd_t container_file_t:dir read;
allow httpd_t container_file_t:file {
      open read };
allow httpd_t unlabeled_t:dir read;
allow httpd_t unlabeled_t:file {
      getattr open read };

若配置无误,使用semodule命令将配置导入内核:

# semodule -i httpd.pp

可以手动修改.te文件并重新编译.pp文件,详见man semodule中的示例。

如要从内核中检查并删除相应配置,可以执行:

# semodule -lfull | grep httpd
400 httpd             pp         
# semodule -r httpd

你可能感兴趣的:(经验积累,SELinux,Linux)