Skip to content

pkg/portutil/procnet: support big-endian hosts in ParseAddress - #5100

Merged
AkihiroSuda merged 1 commit into
containerd:mainfrom
larrasket:fix-4465-procnet-bigendian
Jul 26, 2026
Merged

AkihiroSuda merged 1 commit into
containerd:mainfrom
larrasket:fix-4465-procnet-bigendian

Conversation

@larrasket

Copy link
Copy Markdown
Contributor

/proc/net/{tcp,tcp6} address bytes are written in the host's byte order. ParseAddress reversed each 4-byte group, which mangles the address on big-endian hosts such as s390x (e.g. 7F000001 parsed to 1.0.0.127 instead of 127.0.0.1).

Add ParseAddressWithEndian and select the byte order from golang.org/x/sys/cpu.IsBigEndian, keeping the existing behavior on little-endian hosts.

Fixes #4465


ip, _, err = ParseAddressWithEndian("0100007F:0050", false)
assert.NilError(t, err)
assert.Check(t, net.ParseIP("127.0.0.1").Equal(ip))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should test IPv6 in little endian too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to binary.ByteOrder, ParseAddressWithByteOrder takes the order and ParseAddress passes binary.NativeEndian.

Comment thread pkg/portutil/procnet/procnet.go Outdated
// explicitly. The kernel writes each 4-byte group of the address in the host's
// native byte order, the bytes are reversed on little-endian hosts and kept
// as-is on big-endian hosts (for example, s390x).
func ParseAddressWithEndian(s string, isBE bool) (net.IP, uint16, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this take https://pkg.go.dev/encoding/binary#ByteOrder as an argument

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added little-endian IPv6 case, PTAL

The /proc/net/{tcp,tcp6} address bytes are written in the host's native
byte order. ParseAddress unconditionally reversed each 4-byte group, which
is correct on little-endian hosts but mangles the address on big-endian
hosts such as s390x (e.g. 7F000001 parsed to 1.0.0.127 instead of
127.0.0.1).

Add ParseAddressWithByteOrder, reading each group with the given
binary.ByteOrder and writing it back in network order; ParseAddress passes
binary.NativeEndian. Little-endian behavior is unchanged.

Fixes containerd#4465

Signed-off-by: Saleh <root@lr0.org>
@larrasket
larrasket force-pushed the fix-4465-procnet-bigendian branch from 9c43648 to ec50c5e Compare July 25, 2026 16:19
@larrasket
larrasket requested a review from AkihiroSuda July 26, 2026 11:45

@AkihiroSuda AkihiroSuda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda added this to the v2.4.0 milestone Jul 26, 2026
@AkihiroSuda
AkihiroSuda merged commit d79ad64 into containerd:main Jul 26, 2026
62 of 68 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkg/portutil/procnetlacks support for big endian (i.e., s390x)

2 participants