Skip to content

Commit fb2ab83

Browse files
committed
fix incorrect host.containers.internal entry for rootless bridge mode
We have to exclude the ips in the rootless netns as they are not the host. Now that fix only works if there are more than one ip one the host available, if there is only one we do not set the entry at all which I consider better as failing to resolve this name is a much better error for users than connecting to a wrong ip. It also matches what --network pasta already does. The test is bit more compilcated as I would like, however it must deal with both cases one ip, more than one so there is no way around it I think. Fixes containers#22653 Signed-off-by: Paul Holzinger <pholzing@redhat.com>
1 parent 54ce5c6 commit fb2ab83

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

libpod/container_internal_common.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,15 @@ func (c *Container) addHosts() error {
22992299
var exclude []net.IP
23002300
if c.pastaResult != nil {
23012301
exclude = c.pastaResult.IPAddresses
2302+
} else if c.config.NetMode.IsBridge() {
2303+
// When running rootless we have to check the rootless netns ip addresses
2304+
// to not assign a ip that is already used in the rootless netns as it would
2305+
// not be routed to the host.
2306+
// https://github.com/containers/podman/issues/22653
2307+
info, err := c.runtime.network.RootlessNetnsInfo()
2308+
if err == nil {
2309+
exclude = info.IPAddresses
2310+
}
23022311
}
23032312

23042313
return etchosts.New(&etchosts.Params{

test/system/505-networking-pasta.bats

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ EOF
778778
assert "$output" =~ "$mac2" "mac address from cli is set on custom interface"
779779
}
780780

781-
### Rootless unshare testins
781+
### Rootless unshare testing
782782

783783
@test "Podman unshare --rootless-netns with Pasta" {
784784
skip_if_remote "unshare is local-only"
@@ -794,3 +794,30 @@ EOF
794794
run_podman unshare --rootless-netns ip addr
795795
is "$output" ".*${pasta_iface}.*"
796796
}
797+
798+
# https://github.com/containers/podman/issues/22653
799+
@test "pasta/bridge and host.containers.internal" {
800+
skip_if_no_ipv4 "IPv4 not routable on the host"
801+
pasta_ip="$(default_addr 4)"
802+
803+
for network in "pasta" "bridge"; do
804+
# special exit code logic needed here, it is possible that there is no host.containers.internal
805+
# when there is only one ip one the host and that one is used by pasta.
806+
# As such we have to deal with both cases.
807+
run_podman '?' run --rm --network=$network $IMAGE grep host.containers.internal /etc/hosts
808+
if [ "$status" -eq 0 ]; then
809+
assert "$output" !~ "$pasta_ip" "pasta host ip must not be assigned ($network)"
810+
assert "$(hostname -I)" =~ "$(cut -f1 <<<$output)" "ip is one of the host ips ($network)"
811+
elif [ "$status" -eq 1 ]; then
812+
# if only pasta ip then we cannot have a host.containers.internal entry
813+
# make sure this fact is actually the case
814+
assert "$pasta_ip" == "$(hostname -I | tr -d '[:space:]')" "pasta ip must the only one one the host ($network)"
815+
else
816+
die "unexpected exit code '$status' from grep or podman ($network)"
817+
fi
818+
done
819+
820+
host_ip=$(hostname -I | cut -f 1 -d " ")
821+
run_podman run --rm --network=pasta:-a,169.254.0.2,-g,169.254.0.1,-n,24 $IMAGE grep host.containers.internal /etc/hosts
822+
assert "$output" =~ "^$host_ip" "uses host first ip"
823+
}

0 commit comments

Comments
 (0)