我已经在一个启用了SELinux的Centos 7盒子上安装了PostgresQL,并且为了移动性的原因, 把它的默认数据目录改成了/ srv / postgres,一个单独的LUKSencryption的LVM卷组/逻辑卷,以防万一我不得不移动服务器保密性,数据媒体在移动时是否被偷窃或暴露。 我认为所涉及的LUKS / LVMfunction不应该影响我的问题,但出于完整性的原因提及它。
现在,当我启动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中渲染一些输出,我已经[从post中删除,因为它被certificate是不相关的]。 它没有解决问题,但是,错误仍然以完全相同的forms存在。
我也试过:
root@fafner:~ # restorecon -Rv /usr/bin/pg_ctl root@fafner:~ # restorecon -Rv /usr/bin/postgres root@fafner:~ # restorecon -Rv /srv/postgres/data
…甚至触摸根和postgres文件系统上的“.autorelabel”文件,然后重新引导,重新标记涉及的所有内容。 但是我仍然在audit.log中得到了“在postgresql.conf中拒绝对pg_ctl打开”的错误。
我已经做了几次这些事情,假设这些变化不是累积的。
从这个答案以及它引用的一些链接中我可以看出,这些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