C 程序运行为Apache CGI程序无法通过SHMGET访问共享存储器

C++ program running as Apache CGI program cannot access shared memory via shmget

本文关键字:程序 SHMGET 访问 共享存储器 运行 Apache CGI      更新时间:2023-10-16

apache CGI流程显然无法创建共享内存?shmget()将返回eacces,权限拒绝错误。有人知道我怎么能克服这个问题吗?我正在Linux(Fedora 17 W/3.9.10-100内核)和Apache 2.2.23上运行。这是在关闭的系统上,所以我真的不在乎这可能导致的安全孔。

这是一个最小CGI程序:

#include <iostream>
#include <string.h>
#include <sys/shm.h>
#include <errno.h>
using namespace std;
int main()
{
  cout << "Content-Type: text/plain" << endl << endl;
  if(shmget(0x1234, 1000, IPC_CREAT | 0666) < 0) {
    cout << "Error: " << strerror(errno) << endl;
  } else {
    cout << "Success!" << endl;
  }
  return 0;
}

这是最小的HTML:

<html>
<body>
  <form action="/cgi-bin/sscce" method="post">
    <input type="submit" value="Go" id="submit"/>
  </form>
</body>
</html>

这是命令行上的结果:

$ ./run
Content-Type: text/plain
Success!
$ ipcs -m
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x00001234 29917185   root       666        1000       0
$ ipcrm -m 29917185
$

这是通过apache运行的结果:

Content-Type: text/plain
Error: Permission denied

它正在返回eacces。其他CGI的东西效果很好。所以,我尝试的事情:

  • 将Apache更改为与命令行一样运行的用户(无帮助)
  • " Chmod s"在可执行文件上以使用根本权限(无帮助)
  • " setCap cap_ipc_owner iep"在可执行文件上(以及httpd operutable上),因为shmget()的manpage()表示它需要cap_ipc_owner功能,或者将返回eacces(无帮助)

我有点努力。不知何故,Apache正在剥夺CGI脚本(甚至作为根部运行)创建共享内存的一部分的能力吗?谷歌搜索出来的其他几个遇到了这个问题但没有解决方案的人。另外,如果我预先创建共享内存... shmget()返回eacces时,也会发生同样的事情。预先感谢。

好的,终于自己发现了答案。问题是Selinux。在/var/log/消息:

SELinux is preventing <executable> from using the sys_resource capability.

通过编辑/etc/sysconfig/selinux关闭SELINUX允许CGI进程使用SHMGET()成功。但是,我不知道关闭Selinux的全部后果。在我们的封闭系统上,我认为这没什么大不了的,但是对于其他情况,可能有一个较小的锤子解决方案。但这是头的一般方向。希望它有时会帮助某人。