使用 Podman 连接到 VPN

Peq*_*que 3 dockerfile podman

有这个Dockerfile:

FROM fedora:30

ENV LANG C.UTF-8

RUN dnf upgrade -y \
    && dnf install -y \
        openssh-clients \
        openvpn \
        slirp4netns \
    && dnf clean all

CMD ["openvpn", "--config", "/vpn/ovpn.config", "--auth-user-pass", "/vpn/ovpn.auth"]
Run Code Online (Sandbox Code Playgroud)

使用以下方法构建图像:

podman build -t peque/vpn .
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用(注意$(pwd),存储 VPN 配置和凭据的位置)运行它:

podman run -v $(pwd):/vpn:Z --cap-add=NET_ADMIN --device=/dev/net/tun -it peque/vpn
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

ERROR: Cannot open TUN/TAP dev /dev/net/tun: Permission denied (errno=13)
Run Code Online (Sandbox Code Playgroud)

关于如何解决这个问题的任何想法?如果有帮助,我不介意更改基本图像(即:更改为 Alpine 或其他任何内容,只要它允许我openvpn用于连接)。

系统信息

使用1.4.4带有内核的Podman (无根)和 Fedora 30 发行版5.1.19。

/dev/net/tun 权限

运行容器:

podman run -v $(pwd):/vpn:Z --cap-add=NET_ADMIN --device=/dev/net/tun -it peque/vpn
Run Code Online (Sandbox Code Playgroud)

然后,从容器中,我可以:

# ls -l /dev/ | grep net
drwxr-xr-x. 2 root   root       60 Jul 23 07:31 net
Run Code Online (Sandbox Code Playgroud)

我也可以列出/dev/net,但会得到“权限被拒绝错误”:

# ls -l /dev/net
ls: cannot access '/dev/net/tun': Permission denied
total 0
-????????? ? ? ? ?            ? tun
Run Code Online (Sandbox Code Playgroud)

试 --privileged

如果我尝试--privileged:

podman run -v $(pwd):/vpn:Z --privileged --cap-add=NET_ADMIN --device=/dev/net/tun -it peque/vpn
Run Code Online (Sandbox Code Playgroud)

然后,而不是权限被拒绝错误 (errno=13),我得到一个 no-such-file-or-directory 错误 (errno=2):

ERROR: Cannot open TUN/TAP dev /dev/net/tun: No such file or directory (errno=2)
Run Code Online (Sandbox Code Playgroud)

使用时我可以有效地验证没有/dev/net/目录--privileged,即使我传递了--cap-add=NET_ADMIN --device=/dev/net/tun参数。

详细日志

这是我在配置客户端时得到的日志verb 3:

OpenVPN 2.4.7 x86_64-redhat-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Feb 20 2019
library versions: OpenSSL 1.1.1c FIPS  28 May 2019, LZO 2.08
Outgoing Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication
Incoming Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication
TCP/UDP: Preserving recently used remote address: [AF_INET]xx.xx.xx.xx:1194
Socket Buffers: R=[212992->212992] S=[212992->212992]
UDP link local (bound): [AF_INET][undef]:0
UDP link remote: [AF_INET]xx.xx.xx.xx:1194
TLS: Initial packet from [AF_INET]xx.xx.xx.xx:1194, sid=3ebc16fc 8cb6d6b1
WARNING: this configuration may cache passwords in memory -- use the auth-nocache option to prevent this
VERIFY OK: depth=1, C=ES, ST=XXX, L=XXX, O=XXXXX, emailAddress=email@domain.com, CN=internal-ca
VERIFY KU OK
Validating certificate extended key usage
++ Certificate has EKU (str) TLS Web Server Authentication, expects TLS Web Server Authentication
VERIFY EKU OK
VERIFY OK: depth=0, C=ES, ST=XXX, L=XXX, O=XXXXX, emailAddress=email@domain.com, CN=ovpn.server.address
Control Channel: TLSv1.2, cipher TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384, 2048 bit RSA
[ovpn.server.address] Peer Connection Initiated with [AF_INET]xx.xx.xx.xx:1194
SENT CONTROL [ovpn.server.address]: 'PUSH_REQUEST' (status=1)
PUSH: Received control message: 'PUSH_REPLY,route xx.xx.xx.xx 255.255.255.0,route xx.xx.xx.0 255.255.255.0,dhcp-option DOMAIN server.net,dhcp-option DNS xx.xx.xx.254,dhcp-option DNS xx.xx.xx.1,dhcp-option DNS xx.xx.xx.1,route-gateway xx.xx.xx.1,topology subnet,ping 10,ping-restart 60,ifconfig xx.xx.xx.24 255.255.255.0,peer-id 1'
OPTIONS IMPORT: timers and/or timeouts modified
OPTIONS IMPORT: --ifconfig/up options modified
OPTIONS IMPORT: route options modified
OPTIONS IMPORT: route-related options modified
OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified
OPTIONS IMPORT: peer-id set
OPTIONS IMPORT: adjusting link_mtu to 1624
Outgoing Data Channel: Cipher 'AES-128-CBC' initialized with 128 bit key
Outgoing Data Channel: Using 160 bit message hash 'SHA1' for HMAC authentication
Incoming Data Channel: Cipher 'AES-128-CBC' initialized with 128 bit key
Incoming Data Channel: Using 160 bit message hash 'SHA1' for HMAC authentication
ROUTE_GATEWAY xx.xx.xx.xx/255.255.255.0 IFACE=tap0 HWADDR=0a:38:ba:e6:4b:5f
ERROR: Cannot open TUN/TAP dev /dev/net/tun: No such file or directory (errno=2)
Exiting due to fatal error
Run Code Online (Sandbox Code Playgroud)

错误号可能会根据我是否运行命令而改变--privileged。

Dan*_*ver 5

结果是你被 SELinux 屏蔽了:在运行客户端容器并尝试访问/dev/net/tun其中后,你将在审计日志中得到以下 AVC 拒绝:

type=AVC msg=audit(1563869264.270:833): avc:  denied  { getattr } for  pid=11429 comm="ls" path="/dev/net/tun" dev="devtmpfs" ino=15236 scontext=system_u:system_r:container_t:s0:c502,c803 tcontext=system_u:object_r:tun_tap_device_t:s0 tclass=chr_file permissive=0
Run Code Online (Sandbox Code Playgroud)

为了让你的容器配置隧道,同时保持不完全特权并强制执行 SELinux,你需要稍微自定义 SELinux 策略。但是,我没有找到一种简单的方法来正确地做到这一点。

幸运的是,有一个名为udica的工具,它可以从容器配置中生成 SELinux 策略。它本身不提供所需的策略,需要一些手动干预,所以我将描述我如何让openvpn容器逐步工作。

首先,安装所需的工具:

type=AVC msg=audit(1563869264.270:833): avc:  denied  { getattr } for  pid=11429 comm="ls" path="/dev/net/tun" dev="devtmpfs" ino=15236 scontext=system_u:system_r:container_t:s0:c502,c803 tcontext=system_u:object_r:tun_tap_device_t:s0 tclass=chr_file permissive=0
Run Code Online (Sandbox Code Playgroud)

创建具有所需权限的容器,然后为此容器生成策略:

$ sudo dnf install policycoreutils-python-utils policycoreutils udica
Run Code Online (Sandbox Code Playgroud)

这是由udica以下生成的策略:

$ cat ovpn_container.cil 
(block ovpn_container
    (blockinherit container)
    (allow process process ( capability ( chown dac_override fsetid fowner mknod net_raw setgid setuid setfcap setpcap net_bind_service sys_chroot kill audit_write net_admin ))) 

    (allow process default_t ( dir ( open read getattr lock search ioctl add_name remove_name write ))) 
    (allow process default_t ( file ( getattr read write append ioctl lock map open create  ))) 
    (allow process default_t ( sock_file ( getattr read write append open  ))) 
)
Run Code Online (Sandbox Code Playgroud)

让我们试试这个策略(注意这个--security-opt选项,它告诉podman在新创建的域中运行容器):

$ podman run -it --cap-add NET_ADMIN --device /dev/net/tun -v $PWD:/vpn:Z --name ovpn peque/vpn
$ podman inspect ovpn | sudo udica -j - ovpn_container

Policy ovpn_container created!

Please load these modules using: 
# semodule -i ovpn_container.cil /usr/share/udica/templates/base_container.cil

Restart the container with: "--security-opt label=type:ovpn_container.process" parameter
Run Code Online (Sandbox Code Playgroud)

啊。问题是:由 生成的策略udica仍然不知道我们容器的具体要求,因为它们没有反映在其配置中(好吧,可能,可以推断出您希望tun_tap_device_t基于以下事实允许操作你要求--device /dev/net/tun,但是......)。因此,我们需要通过使用更多语句对其进行扩展来自定义策略。

让我们暂时禁用 SELinux 并运行容器以收集预期的拒绝:

$ cat ovpn_container.cil 
(block ovpn_container
    (blockinherit container)
    (allow process process ( capability ( chown dac_override fsetid fowner mknod net_raw setgid setuid setfcap setpcap net_bind_service sys_chroot kill audit_write net_admin ))) 

    (allow process default_t ( dir ( open read getattr lock search ioctl add_name remove_name write ))) 
    (allow process default_t ( file ( getattr read write append ioctl lock map open create  ))) 
    (allow process default_t ( sock_file ( getattr read write append open  ))) 
)
Run Code Online (Sandbox Code Playgroud)

这些是:

$ sudo semodule -i ovpn_container.cil /usr/share/udica/templates/base_container.cil
$ podman run -it --cap-add NET_ADMIN --device /dev/net/tun -v $PWD:/vpn:Z --security-opt label=type:ovpn_container.process peque/vpn
<...>
ERROR: Cannot open TUN/TAP dev /dev/net/tun: Permission denied (errno=13)
Run Code Online (Sandbox Code Playgroud)

或更易读:

$ sudo setenforce 0
$ podman run -it --cap-add NET_ADMIN --device /dev/net/tun -v $PWD:/vpn:Z --security-opt label=type:ovpn_container.process peque/vpn
Run Code Online (Sandbox Code Playgroud)

好的,让我们修改udica-生成的策略,向allow它添加s建议(注意,这里我手动将语法翻译为CIL):

(block ovpn_container
    (blockinherit container)
    (allow process process ( capability ( chown dac_override fsetid fowner mknod net_raw setgid setuid setfcap setpcap net_bind_service sys_chroot kill audit_write net_admin )))

    (allow process default_t ( dir ( open read getattr lock search ioctl add_name remove_name write )))
    (allow process default_t ( file ( getattr read write append ioctl lock map open create  )))
    (allow process default_t ( sock_file ( getattr read write append open  )))

    ; This is our new stuff.
    (allow process tun_tap_device_t ( chr_file ( ioctl open read write )))
    (allow process self ( netlink_route_socket ( nlmsg_write )))
)
Run Code Online (Sandbox Code Playgroud)

现在我们重新启用 SELinux,重新加载模块并在我们指定自定义域时检查容器是否正常工作:

$ sudo grep denied /var/log/audit/audit.log
type=AVC msg=audit(1563889218.937:839): avc:  denied  { read write } for  pid=3272 comm="openvpn" name="tun" dev="devtmpfs" ino=15178 scontext=system_u:system_r:ovpn_container.process:s0:c138,c149 tcontext=system_u:object_r:tun_tap_device_t:s0 tclass=chr_file permissive=1
type=AVC msg=audit(1563889218.937:840): avc:  denied  { open } for  pid=3272 comm="openvpn" path="/dev/net/tun" dev="devtmpfs" ino=15178 scontext=system_u:system_r:ovpn_container.process:s0:c138,c149 tcontext=system_u:object_r:tun_tap_device_t:s0 tclass=chr_file permissive=1
type=AVC msg=audit(1563889218.937:841): avc:  denied  { ioctl } for  pid=3272 comm="openvpn" path="/dev/net/tun" dev="devtmpfs" ino=15178 ioctlcmd=0x54ca scontext=system_u:system_r:ovpn_container.process:s0:c138,c149 tcontext=system_u:object_r:tun_tap_device_t:s0 tclass=chr_file permissive=1
type=AVC msg=audit(1563889218.947:842): avc:  denied  { nlmsg_write } for  pid=3273 comm="ip" scontext=system_u:system_r:ovpn_container.process:s0:c138,c149 tcontext=system_u:system_r:ovpn_container.process:s0:c138,c149 tclass=netlink_route_socket permissive=1
Run Code Online (Sandbox Code Playgroud)

最后,检查其他容器是否仍然没有这些权限:

$ sudo grep denied /var/log/audit/audit.log | audit2allow


#============= ovpn_container.process ==============
allow ovpn_container.process self:netlink_route_socket nlmsg_write;
allow ovpn_container.process tun_tap_device_t:chr_file { ioctl open read write };
Run Code Online (Sandbox Code Playgroud)

好极了!我们继续使用 SELinux,并且只允许隧道配置到我们的特定容器。