无法访问 PHP/C++通过网络共享内存

发布于 2024-10-03 02:58:30 字数 454 浏览 0 评论 0原文

我在 C++ 和 PHP 之间共享一些内存

在 PHP 端我有:

   $inputshm_id = shmop_open($shid, "w", 0777, 1024);

其中 shid 是我用 ftok 创建的标识符。

当我在服务器上以 root 身份运行这个 PHP 脚本时,这一切都工作正常,但是当我尝试通过网络远程运行它时,我得到:

警告:shmop_open()[function.shmop-open]:无法附加或创建共享/var/www/html/prof/phpsm.php 第 6 行中的内存段

...其中第 6 行是我上面显示的行。

由于当我以 root 身份从服务器运行它时,一切都运行良好,因此我假设某个地方阻止了 Web 用户请求连接到共享内存。

有谁知道这可能是什么原因造成的?

谢谢

I am sharing some memory between C++ and PHP

At the PHP end I have:

   $inputshm_id = shmop_open($shid, "w", 0777, 1024);

Where shid is an identifier I created with ftok.

This all works fine when I run this PHP script logged in as root on the server but when I try to run it remotely over the web I get:

Warning: shmop_open() [function.shmop-open]: unable to attach or create shared memory segment in /var/www/html/prof/phpsm.php on line 6

...where line 6 is the line I've shown above.

Since it all runs fine when I run it from the server as root I'm assuming something somewhere is preventing web user requests from connecting to the shared memory.

Does anyone know what could be causing this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽 2024-10-10 02:58:30

问题是 SELinux 阻止了 shm 访问(您可以通过运行 setenforce 0 进行验证,测试,然后运行 ​​setenforce 1),但我不知道一个好的方法除了修改策略或切换到 mmap 之外,还有其他解决方法。

The issue is that SELinux is blocking the shm access (you can verify by running setenforce 0, testing, and running setenforce 1 after), but I don't know a good way of solving it other than modifying the policy or switching to mmap.

醉殇 2024-10-10 02:58:30

只是为了添加已接受的答案,我需要将 SELinux 保持在强制模式,因此我最终执行了以下操作以允许访问 PHP 中的共享内存操作:

  1. 将 selinux 置于许可模式
  2. 将 selinux 置于“不阻止”模式: semodule -DB (这很重要,因为默认情况下不记录 shmop 操作)
  3. 清除 /var/log/audit/audit.log
  4. 使用共享内存操作执行有问题的脚本
  5. ,生成一个 selinux 模块: audit2allow -a -Maudit.log
  6. 安装的模块: semodule -iaudit.log.pp

我最终确实经历了几次迭代才得到正确的结果,但是我对 CentOS 6 的最终政策是:

module audit.log 1.0;
require {
        type unconfined_t;
        type httpd_t;
        type audisp_t;
        type auditd_t;
        type user_tmpfs_t;
        class process { siginh noatsecure rlimitinh };
        class shm { associate unix_read getattr read };
        class file { read };
}
allow auditd_t audisp_t:process { siginh rlimitinh noatsecure };
allow httpd_t unconfined_t:shm { associate unix_read getattr read };
allow httpd_t user_tmpfs_t:file read;

Just to add to the accepted answer, I needed to keep SELinux in enforcing mode, so I ended up doing the following to allow access to shared memory operations in PHP:

  1. put selinux in permissive mode
  2. put selinux in "don't block" mode: semodule -DB (this was important, because the shmop operations were not by default logged)
  3. cleared out /var/log/audit/audit.log
  4. executed the offending script with shared memory operations
  5. generated an selinux module: audit2allow -a -M audit.log
  6. installed module: semodule -i audit.log.pp

I did end up going through a couple iterations of this to get it right, but my final policy on CentOS 6 was:

module audit.log 1.0;
require {
        type unconfined_t;
        type httpd_t;
        type audisp_t;
        type auditd_t;
        type user_tmpfs_t;
        class process { siginh noatsecure rlimitinh };
        class shm { associate unix_read getattr read };
        class file { read };
}
allow auditd_t audisp_t:process { siginh rlimitinh noatsecure };
allow httpd_t unconfined_t:shm { associate unix_read getattr read };
allow httpd_t user_tmpfs_t:file read;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文