ping 在 Fedora 上的无根 Ubuntu podman 容器上不起作用

epo*_*khe 3 networking linux fedora ping podman

在 Fedora 主机上使用 podman 运行无根 Ubuntu 映像时,ping失败并出现“不允许操作”错误。

root@337e8ebdc287:/# ping google.com
bash: /usr/bin/ping: Operation not permitted
Run Code Online (Sandbox Code Playgroud)

在 alpine/fedora 图像上, ping 有效

$ podman run alpine ping -c1 google.com
PING google.com (172.217.169.110): 56 data bytes
64 bytes from 172.217.169.110: seq=0 ttl=42 time=119.706 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 119.706/119.706/119.706 ms
Run Code Online (Sandbox Code Playgroud)

当我使用 运行图像时,它也适用于 Ubuntu --privileged

在主机上,ping_group_range设置为包括所有 uid。

$ sudo sysctl --all | grep ping
net.ipv4.ping_group_range = 0   2147483647
Run Code Online (Sandbox Code Playgroud)

将 Fedora 36 与 Podman 4.2.1 结合使用。

如何ping在无根 Ubuntu 映像上工作?

除了这个具体问题的答案之外,我还有兴趣学习自己调试此类权限问题。我是否必须事先知道导致“不允许操作”错误的每种可能性?此错误是由操作系统还是正在运行的程序引发的?如果它是由操作系统抛出的,它是否也会在某处记录为什么它不允许该操作?

Sas*_* S. 6

正如您所知,容器利用某些内核功能。最突出的是,这包括内核命名空间和资源组。

为了与网络交互,通常通过 syscals 与内核交互。当您尝试执行系统调用时,内核会检查您是否有权执行这些系统调用。此检查考虑当前使用的用户(调用用户)、执行程序的用户(执行用户)以及在执行上下文中授予了哪些功能。

正如您在此 GitHub 问题 ( https://github.com/mviereck/dockerfile-x11docker-deepin/issues/19 ) 中所看到的,ping 需要该NET_RAW功能。检查https://security.stackexchange.com/a/128988了解这意味着什么。您可能还想阅读https://superuser.com/a/1702188/1737591上类似的问题。这也有助于解释为什么它在 alpine 上按预期工作,但在 ubuntu 图像上却不行。

有了标志,您基本上就有了将其包含在许多其他权限中的--privileged简写。CAP_NET_RAW相反,您应该能够使用该--cap-add=NET_RAW标志执行容器,以便将功能限制在最低限度。

我不知道这些错误可能会记录在哪里,但我会dmesg首先检查系统的系统日志。

编辑:

当使用官方的 ubuntu 镜像并随后在其上安装 ping 时,您还需要setcap cap_net_raw+p /usr/bin/ping 在所述容器内运行。否则,容器本身内的权限检查(定义执行上下文)将不允许您执行 ping,因为所需的功能将不属于有效功能的一部分。

$ podman run --rm -it ubuntu:22.04
Resolved "ubuntu" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull docker.io/library/ubuntu:22.04...
Getting image source signatures
Copying blob cf92e523b49e skipped: already exists  
Copying config 216c552ea5 done  
Writing manifest to image destination
Storing signatures
root@af1a4b5052e9:/# apt-get update && apt-get install -y iputils-ping && apt-get clean autoclean && apt-get -y autoremove
### Skipped apt output ###
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@af1a4b5052e9:/# ping -c 3 stackoverflow.com
bash: /usr/bin/ping: Operation not permitted
root@30d9c3c936fc:/# sysctl 'net.ipv4.ping_group_range'             
net.ipv4.ping_group_range = 0   0
root@af1a4b5052e9:/# setcap cap_net_raw+p /usr/bin/ping
root@af1a4b5052e9:/# ping -c 3 stackoverflow.com
PING stackoverflow.com (151.101.193.69) 56(84) bytes of data.
64 bytes from 151.101.193.69 (151.101.193.69): icmp_seq=1 ttl=255 time=8.61 ms
64 bytes from 151.101.193.69 (151.101.193.69): icmp_seq=2 ttl=255 time=8.67 ms
64 bytes from 151.101.193.69 (151.101.193.69): icmp_seq=3 ttl=255 time=8.86 ms

--- stackoverflow.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 8.609/8.713/8.857/0.105 ms
root@af1a4b5052e9:/#
Run Code Online (Sandbox Code Playgroud)

您尝试的其他图像很可能已经包含正确的图像net.ipv4.ping_group_range,或者已经执行了类似于setcap cap_net_raw+p /usr/bin/ping发布图像之前的操作。

请随意使用getcap $(which ping)列出其他图像可能已在 ping 上设置的功能。

cap_net_raw+ep表示:
对于能力net_raw添加 ( +) 有效 ( e) 和允许 ( p)。

如果您渴望了解更多信息,我建议您查看https://book.hacktricks.xyz/linux-hardening/privilege-escalation/linux-capability来了解有关功能的入门知识。