Fedora 25 で systemd を使用すると、SELinux はコマンドの実行をブロックします。

Fedora 25 で systemd を使用すると、SELinux はコマンドの実行をブロックします。

Fedora 25では、systemdを介してネットワーク名前空間でOpenVPNを実行しようとしています。mynsSELinuxが動作を妨げているようです。

それを見ると、journalctl -xeこれが関連部分だと思います。

-- Subject: Unit myvpn.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit myvpn.service has begun starting up.
Jul 11 16:26:19 castiel audit[32456]: AVC avc:  denied  { mounton } for  pid=32456 comm="ip" path="/etc/resolv.conf" dev="sda1" ino=1572869 scontext=system_u:system_r:ifconfig_t:s0 tcontext=system_u:o
bject_r:net_conf_t:s0 tclass=file permissive=0
Jul 11 16:26:19 castiel audit[32456]: AVC avc:  denied  { execute } for  pid=32456 comm="ip" name="openvpn" dev="sda1" ino=1705174 scontext=system_u:system_r:ifconfig_t:s0 tcontext=system_u:object_r:o
penvpn_exec_t:s0 tclass=file permissive=0
Jul 11 16:26:19 castiel ip[32456]: Bind /etc/netns/myns/resolv.conf -> /etc/resolv.conf failed: Permission denied
Jul 11 16:26:19 castiel ip[32456]: exec of "/sbin/openvpn" failed: Permission denied
Jul 11 16:26:19 castiel systemd[1]: myvpn.service: Control process exited, code=exited status=1
Jul 11 16:26:19 castiel audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg='unit=nordvpn comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? a
ddr=? terminal=? res=failed'
Jul 11 16:26:19 castiel systemd[1]: Failed to start OpenVPN connection to myvpn.
-- Subject: Unit myvpn.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit myvpn.service has failed.

/var/log/audit/audit.log:

type=AVC msg=audit(1499799030.259:16264): avc:  denied  { mounton }
    for  pid=1419 comm="ip" path="/etc/resolv.conf" dev="sda1" ino=1572869
    scontext=system_u:system_r:ifconfig_t:s0
    tcontext=system_u:object_r:net_conf_t:s0
    tclass=file permissive=0
type=AVC msg=audit(1499799030.259:16265): avc:  denied  { execute }
    for  pid=1419 comm="ip" name="openvpn" dev="sda1" ino=1705174
    scontext=system_u:system_r:ifconfig_t:s0
    tcontext=system_u:object_r:openvpn_exec_t:s0
    tclass=file permissive=0
type=SERVICE_START msg=audit(1499799030.268:16266):
    pid=1 uid=0 auid=4294967295 ses=4294967295
    subj=system_u:system_r:init_t:s0 msg='unit=myvpn
    comm="systemd" exe="/usr/lib/systemd/systemd"
    hostname=? addr=? terminal=? res=failed'
type=SERVICE_START msg=audit(1499800402.528:16267):
    pid=1 uid=0 auid=4294967295 ses=4294967295 
    subj=system_u:system_r:init_t:s0 msg='unit=dnf-makecache comm="systemd" 
    exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
type=SERVICE_STOP msg=audit(1499800402.528:16268): 
    pid=1 uid=0 auid=4294967295 ses=4294967295 
    subj=system_u:system_r:init_t:s0 msg='unit=dnf-makecache comm="systemd" 
    exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'

# ll -Z /etc/resolv.confこれを見せてください:
-rw-r--r--. 1 root root system_u:object_r:net_conf_t:s0 61 Jul 9 17:26 /etc/resolv.conf


サービスファイルは次のとおりです。

[Unit]
[Service]
Type=forking
ExecStart=/sbin/ip netns exec myns /sbin/openvpn --config /etc/openvpn/profiles/myprofile.ovpn --auth-user-pass /etc/openvpn/credentials --daemon [email protected] --writepid /run/[email protected]
PIDFile=/run/[email protected]

[Install]
WantedBy=multi-user.target

selinuxをpermissive()に設定するとsetenforce 0正常に動作します。


コマンドを直接実行すると、selinuxを強制的にうまく機能します。

/sbin/ip netns exec myns /sbin/openvpn --config /etc/openvpn/profiles/myprofile.ovpn --auth-user-pass /etc/openvpn/credentials --daemon [email protected] --writepid /run/[email protected]

curl ifconfig.io私のパブリックIPを返し、
ip netns exec myns curl ifconfig.ioVPNサーバーのIPを返します。


SELinuxを完全に無効にせずにsystemdスクリプトが機能するようにSELinuxを取得するにはどうすればよいですか?

ベストアンサー1

実行時にsetenforce 0以下を使用して、次の許可規則を取得できましたaudit2allow

#============= ifconfig_t ==============
allow ifconfig_t net_conf_t:file mounton;
allow ifconfig_t openvpn_etc_t:file { getattr open read };
allow ifconfig_t openvpn_exec_t:file { execute execute_no_trans open read };
allow ifconfig_t openvpn_tmp_t:dir { read write };

私も同じログからこの規則を得ました。

#============= ifconfig_t ==============

#!!!! WARNING: 'tmp_t' is a base type.
allow ifconfig_t tmp_t:dir { read write };

これを許可しないように、ラベル付きopenvpn_tmp_tのサブフォルダを作成し、そのディレクトリを指す/tmp引数を使用して実行しました。openvpn--tmp-dir


この後もまだサービスを開始できません。を見て、journalctl -xe設定ファイルの読み込みに問題があることを確認しました。

-- Unit myvpn.service has begun starting up.
Jul 17 06:19:56 castiel ip[22825]: Options error: In [CMD-LINE]:1: Error opening configuration file: /etc/openvpn/profiles/myprofile.ovpn
Jul 17 06:19:56 castiel ip[22825]: Use --help for more information.
Jul 17 06:19:56 castiel systemd[1]: myvpn.service: Control process exited, code=exited status=1
Jul 17 06:19:56 castiel systemd[1]: Failed to start OpenVPN connection to myprofile.ovpn.

もう一度試しましたがsetenforce 0起動しました。驚くほどあります何もないこれは監査ログに記録されています。

検索して一つ見つけました。Fedora selinuxメーリングリストに関する古い質問dontaudit特定のメッセージが記録されないようにする規則があるようです。この機能はsemodule -DB(再有効化を使用)を使用してオフにできますsemodule -B

残りのエラーを記録した後、さらに数行を取得し、それをルールに変換しましたaudit2allow

#============= ifconfig_t ==============
allow ifconfig_t openvpn_etc_t:dir search;
allow ifconfig_t openvpn_tmp_t:dir search;

#============= init_t ==============
allow init_t ifconfig_t:process noatsecure;
allow init_t kernel_t:unix_stream_socket { read write };

これらの戦略を組み合わせることで、systemdを介してネットワーク名前空間内でopenvpnを実行することができました。

このルールがどれほど安全であるか、この問題を解決するより良い方法があるかどうかはまだわかりません。

おすすめ記事