zoukankan      html  css  js  c++  java
  • 在开启selinux时_增加规则_允许httpd_php-fpm执行iptables命令

    在开启selinux时_增加规则_允许httpd_php-fpm执行iptables命令

    转载注明来源: 本文链接 来自osnosn的博客,写于 2020-01-04.

    起因

    • 写了个php的页面,经过一番验证之后,要调用iptables 添加或修改一条记录。
    • 我的环境是centos7,nginx+php7-fpm,开启了selinux。
    • nginx 和 php-fpm 都是以apache用户身份运行。且运行在 httpd_t 标签下。
    • 因为 iptables的命令比较固定。所以写了个简单的 C 程序,在 C 程序内用execl()执行iptables语句(为了安全,所有参数写死在程序中)。
      然后把这个 C 程序编译为执行文件myrule,设置myrule文件的ower为root,设置myrule可执行,并设SUID chmod u+s myrule
    • 在php程序中用 exec()调用这个myrule程序。

    临时关闭selinux

    • setenforce 0
    • 测试,一切OK,php成功调用myrule程序,用 iptables -nL 见到了加入的记录。

    恢复selinux

    • setenforce 1
    • php执行失败,没看到有新记录加入到iptables表中。
    • /var/log/message 中出现 audit 错误。
    • 上网搜索,比较容易解决。通过 tail /var/log/message | audit2allow -M myrule-se 生成需要的规则。
      然后 semodule -i myrule-se.pp 导入这个规则即可。
    • 设置 chcon -t bin_t myrule 可以让message中少点错误,audit2allow就能少生成点规则。
    • 根据 tail /var/log/message | audit2why 提示,设置 setsebool -P httpd_execmem on
    • 生成规则,导入规则 的过程可能要搞好几次。
      semodule -i myrule-se.pp后,又会发现有新的错误。
      只好 semodule -r myrule-se 卸载这个规则。 然后重新从message中(新旧信息一起)生成一个更完整的规则。再导入试试。
    • 折腾的几次,终于把se的规则搞完整了。并且导入了这个规则。在我的机子里,这部分的规则是:
    module myrule-se 1.0;
    
    require {
            type httpd_t;
            type iptables_var_run_t;
            class capability net_raw;
            class file { lock open read };
            class rawip_socket { create getopt setopt };
    }
    
    #============= httpd_t ==============
    allow httpd_t iptables_var_run_t:file { lock open read };
    allow httpd_t self:capability net_raw;
    allow httpd_t self:rawip_socket { create getopt setopt };
    

    发现新问题

    • /var/log/message 不再有错误信息,php-fpm的log中也无错误信息。
    • 从php程序中看,myrule程序调用成功,返回码也正常。
    • iptables -nL 中,就是看不到新增的记录

    解决问题

    • 上网搜索了好几个小时,包括度娘,bing,甚至去google搜英文。也没找到什么有用的。但多多少少总有一点提示。
    • 解决过程不写了,试了好多办法。
    • 下面贴出最终结果,给大家参考。
      创建文件 myrule-se2.te
    module myrule-se2 1.0;
    
    require {
            type httpd_t;
            type iptables_exec_t;
            type iptables_t;
            class process transition;
            class process iptables_t;
    }
    
    #============= httpd_t ==============
    allow httpd_t iptables_t: process transition;
    type_transition httpd_t iptables_exec_t : process iptables_t;
    

    执行以下命令:
    checkmodule -m -M -o myrule-se2.mod myrule-se2.te
    semodule_package -o myrule-se2.pp -m myrule-se2.mod
    semodule -i myrule-se2.pp
    再测试,成功。
    ---end---


    转载注明来源: 本文链接 来自osnosn的博客.

  • 相关阅读:
    PAT Advanced 1067 Sort with Swap(0, i) (25分)
    PAT Advanced 1048 Find Coins (25分)
    PAT Advanced 1060 Are They Equal (25分)
    PAT Advanced 1088 Rational Arithmetic (20分)
    PAT Advanced 1032 Sharing (25分)
    Linux的at命令
    Sublime Text3使用指南
    IntelliJ IDEA创建第一个Groovy工程
    Sublime Text3 安装ftp插件
    Sublime Text3配置Groovy运行环境
  • 原文地址:https://www.cnblogs.com/osnosn/p/12150119.html
Copyright © 2011-2022 走看看