这是一个脚本来演示:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/stat.h> #include <unistd.h> int main(int argc, char **argv) { printf("Content-Type: text/plain\r\n\r\n"); printf("UID, EUID = %d, %d\n", getuid(), geteuid()); printf("GID, EGID = %d, %d\n", getgid(), getegid()); struct stat stbuf; if (stat("/dev/shm", &stbuf)) printf("stat() failed: %s\n", strerror(errno)); else printf("stat() succeeded\n"); return 0; }
当我在sudo su -s /bin/bash apache shell下运行这个脚本时,它产生:
Content-Type: text/plain UID, EUID = 48, 48 GID, EGID = 48, 48 stat() succeeded
但是当通过Apache / 2.2作为CGI脚本运行时,页面响应:
UID, EUID = 48, 48 GID, EGID = 48, 48 stat() failed: Permission denied
这怎么可能? 有问题的位置(/ dev / shm)在这台机器上的这些权限:
drwxrwxrwt. 2 root root 260 Mar 29 06:52 . drwxr-xr-x. 20 root root 4300 Mar 28 10:31 ..
这是由于selinux。
您的脚本不允许在tmpfs_t作为httpd_sys_script_ttypes进行统计。 这是devise。 取决于你想要stat()(不只是/ dev / shm)可以取决于你想采取什么行动。
如果你使程序运行在标签httpd_unconfined_script_exec_t这将避免SELinux的限制,代价是没有任何SELinux保护脚本在运行。