背景:
我有两台CentOS7服务器。 Test
& Prod
.我的网站中
有一个脚本myfilename.sh
,由 php 在 php 中执行shell_exec
。
两台服务器中的脚本和所有父文件夹都具有相同的 selinux 上下文。httpd_sys_rw_content_t
问题陈述:
脚本在服务器上运行良好Test
,但在Prod
.
审核日志Prod
显示SELinux is preventing /usr/bin/bash from execute access on the file myfilename.sh.
,但框中没有显示任何内容Test
。
知道可能出了什么问题以及为什么脚本在Test
盒子上运行。我需要在Test
服务器上重现该问题,然后再在产品上修复它。
这是审核日志:
SELinux is preventing /usr/bin/bash from execute access on the file myfilename.sh.
***** Plugin catchall (100. confidence) suggests **************************
If you believe that bash should be allowed execute access on the myfilename.sh 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 'sh' --raw | audit2allow -M my-sh
# semodule -i my-sh.pp
Additional Information:
Source Context system_u:system_r:httpd_sys_script_t:s0
Target Context unconfined_u:object_r:httpd_sys_rw_content_t:s0
Target Objects myfilename.sh [ file ]
Source sh
Source Path /usr/bin/bash
Port <Unknown>
Host <Unknown>
Source RPM Packages bash-4.2.46-21.el7_3.x86_64
Target RPM Packages
Policy RPM selinux-policy-3.13.1-102.el7_3.16.noarch
Selinux Enabled True
Policy Type targeted
Enforcing Mode Enforcing
Host Name ip-X-X-X-X
Platform Linux ip-X-X-X-X 3.10.0-327.10.1.el7.x86_64
#1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64
Alert Count 23
First Seen 2017-06-17 06:33:36 CDT
Last Seen 2017-06-18 02:18:01 CDT
Local ID 340158ca-61f6-4472-aadb-18a3bbc34a22
Raw Audit Messages
type=AVC msg=audit(1497770281.465:12508): avc: denied { execute } for pid=10042 comm="sh" name="myfilename.sh" dev="xvdc" ino=3416830 scontext=system_u:system_r:httpd_sys_script_t:s0 tcontext=unconfined_u:object_r:httpd_sys_rw_content_t:s0 tclass=file
type=SYSCALL msg=audit(1497770281.465:12508): arch=x86_64 syscall=execve success=no exit=EACCES a0=20d5650 a1=20d5730 a2=20d3c20 a3=7ffe9f4d7de0 items=0 ppid=10041 pid=10042 auid=4294967295 uid=995 gid=992 euid=995 suid=995 fsuid=995 egid=992 sgid=992 fsgid=992 tty=(none) ses=4294967295 comm=sh exe=/usr/bin/bash subj=system_u:system_r:httpd_sys_script_t:s0 key=(null)
Hash: sh,httpd_sys_script_t,httpd_sys_rw_content_t,file,execute
答案1
httpd_sys_rw_content_t上下文不适用于可执行文件。我们需要设置httpd_sys_script_exec_t背景
sudo semanage fcontext -a -t httpd_sys_script_exec_t "/PATH_TO_FILE/myfilename.sh"
然后不要忘记运行restorecon。
sudo restorecon -Rv /PATH_TO_FILE/