加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

SELinux / PostgresQL“拒绝{open} for [..] comm =”pg_ctl“pa

发布时间:2020-12-14 02:37:53 所属栏目:Linux 来源:网络整理
导读:我已经在支持SE Linux的Centos 7盒子上安装了PostgresQL,并将其默认数据目录更改为/ srv / postgres,这是一个单独的LUKS加密的LVM卷组/逻辑卷,出于移动性的原因,以防我必须移动服务器如果数据媒体在移动时被盗或暴露,则保密.我认为所涉及的LUKS / LVM功能不
我已经在支持SE Linux的Centos 7盒子上安装了PostgresQL,并将其默认数据目录更改为/ srv / postgres,这是一个单独的LUKS加密的LVM卷组/逻辑卷,出于移动性的原因,以防我必须移动服务器如果数据媒体在移动时被盗或暴露,则保密.我认为所涉及的LUKS / LVM功能不应该影响我的问题,但出于完整性的原因提及它.

现在,当我启动postgresql服务时:

root@fafner:~ # systemctl start postgresql

…我在/var/log/audit/audit.log中得到这个:

root@fafner:~ # tail -f /var/log/audit/audit.log | grep "postgresql" 
[..]
type=AVC msg=audit(1476614020.689:522): avc: denied  { open } for  pid=2900 comm="pg_ctl" path="/srv/postgres/data/postgresql.conf" dev="dm-4" ino=136 scontext=system_u:system_r:postgresql_t:s0 tcontext=unconfined_u:object_r:var_t:s0 tclass=file type=SYSCALL msg=audit(1476614020.689:522): arch=c000003e syscall=2 success=no exit=-13 a0=7ffc681cc430 a1=0 a2=1b6 a3=24 items=1 ppid=1 pid=2900 auid=4294967295 uid=989 gid=986 euid=989 suid=989 fsuid=989 egid=986 sgid=986 fsgid=986 tty=(none) ses=4294967295 comm="pg_ctl" exe="/usr/bin/pg_ctl" subj=system_u:system_r:postgresql_t:s0 key=(null)
type=PATH msg=audit(1476614020.689:522): item=0 name="/srv/postgres/data/postgresql.conf" inode=136 dev=fd:04 mode=0100600 ouid=989 ogid=986 rdev=00:00 obj=unconfined_u:object_r:var_t:s0 objtype=NORMAL
type=AVC msg=audit(1476614020.725:523): avc: denied  { open } for  pid=2904 comm="postgres" path="/srv/postgres/data/postgresql.conf" dev="dm-4" ino=136 scontext=system_u:system_r:postgresql_t:s0 tcontext=unconfined_u:object_r:var_t:s0 tclass=file type=SYSCALL msg=audit(1476614020.725:523): arch=c000003e syscall=2 success=no exit=-13 a0=befc30 a1=0 a2=1b6 a3=24 items=1 ppid=2900 pid=2904 auid=4294967295 uid=989 gid=986 euid=989 suid=989 fsuid=989 egid=986 sgid=986 fsgid=986 tty=(none) ses=4294967295 comm="postgres" exe="/usr/bin/postgres" subj=system_u:system_r:postgresql_t:s0 key=(null)
type=PATH msg=audit(1476614020.725:523): item=0 name="/srv/postgres/data/postgresql.conf" inode=136 dev=fd:04 mode=0100600 ouid=989 ogid=986 rdev=00:00 obj=unconfined_u:object_r:var_t:s0 objtype=NORMAL
type=SERVICE_START msg=audit(1476614021.712:524): pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg='unit=postgresql comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=failed'

我尝试使用audit2allow来解决问题:

root@fafner:~ # grep "postgresql" /var/log/audit/audit.log | audit2allow -M postgresql_tskjoedt
root@fafner:~ # semodule -i postgresql_tskjoedt.pp

…在/var/log/audit/audit.log中呈现一些我已经[从帖子中删除,因为它被证明无关紧要]的输出.但是,它没有解决问题,错误仍以完全相同的形式存在.

我也尝试过:

root@fafner:~ # restorecon -Rv /usr/bin/pg_ctl
root@fafner:~ # restorecon -Rv /usr/bin/postgres
root@fafner:~ # restorecon -Rv /srv/postgres/data

…甚至在root和postgres文件系统上触摸’.autorelabel’文件,然后重新启动,重新标记所涉及的所有内容.但我仍然在audit.log中得到“在postgresql.conf上拒绝打开pg_ctl”错误.

假设变化不是累积的,我已经做了几次这些事情.

从this answer和它引用的一些链接我收集到这些SELinux上下文/标签不能正确排列:

root@fafner:~ # ls -Z /usr/bin/pg_ctl
-rwxr-xr-x. root root system_u:object_r:postgresql_exec_t:s0 /usr/bin/pg_ctl
root@fafner:~ # ls -Z /usr/bin/postgres
-rwxr-xr-x. root root system_u:object_r:postgresql_exec_t:s0 /usr/bin/postgres
root@fafner:~ # ls -Z /srv/postgres/data/postgresql.conf
-rw-------. postgres postgres unconfined_u:object_r:var_t:s0   /srv/postgres/data/postgresql.conf

我可以随意洗牌,直到某些东西开始工作,但我不想随意设置或破坏SELinux标签只是为了让事情顺利进行;然后我不妨关掉SELinux.

另外,我不明白为什么’audit2allow’无法解决具体问题;是不是这个命令应该做到这一点,实质上,加宽SELinux上下文只是为了允许特定的操作?

整个问题当然源于我对SELinux的理解不足,而且audit.log的大部分内容对我来说都比较模糊.有人可以找出我应该采取的线索吗?

亲切的问候,

LANerd

解决方法

为什么要更改数据目录?这只会让你的生活变得复杂.您可以将文件系统挂载到默认数据目录的位置,一切都会正常工作.理解和维护也更容易.
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/volgroup-pgsql  1.1T  128K  1.1T   1% /var/lib/pgsql

如果您真的想保留非默认数据目录,那么您需要告诉SELinux要应用于该目录及其内容的上下文.这是通过semanage fcontext完成的.在这种情况下,我们将使用–equal选项使您的非默认目录与默认目录/ var / lib / pgsql具有相同的上下文.

semanage fcontext --add --equal /var/lib/pgsql /srv/postgres

从手册页:

-e EQUAL,--equal EQUAL
              Substitute  target  path with sourcepath when generating default
              label. This is used with fcontext. Requires  source  and  target
              path  arguments.  The context labeling for the target subtree is
              made equivalent to that defined for the source.

这是持久的,但它不会更改现有标签.要完成,您需要运行restorecon以重置所有标签.

restorecon -rv /srv/postgres

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读