From 1e638101df598a2021589b119c659547ca236d51 Mon Sep 17 00:00:00 2001 From: F Y Date: Tue, 17 Oct 2023 10:01:27 +0100 Subject: [PATCH] unix: stop counting trailing NUL for abstract addresses starting with NUL Changes trailing-NUL-counting behavior for abstract addresses starting with the NUL character to be the same as abstract addresses starting with the @ character. For golang/go#63579. Change-Id: I2f26de4bcf614c4635ad188b1afa3d14ebd9a95f Reviewed-on: https://go-review.googlesource.com/c/sys/+/535955 Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Reviewed-by: Benny Siegert LUCI-TryBot-Result: Go LUCI --- unix/syscall_aix.go | 3 ++- unix/syscall_internal_linux_test.go | 12 +++++++++++- unix/syscall_linux.go | 3 ++- unix/syscall_solaris.go | 3 ++- windows/syscall_windows.go | 3 ++- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/unix/syscall_aix.go b/unix/syscall_aix.go index 6d3009d21..67ce6cef2 100644 --- a/unix/syscall_aix.go +++ b/unix/syscall_aix.go @@ -106,7 +106,8 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { if n > 0 { sl += _Socklen(n) + 1 } - if sa.raw.Path[0] == '@' { + if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) { + // Check sl > 3 so we don't change unnamed socket behavior. sa.raw.Path[0] = 0 // Don't count trailing NUL for abstract address. sl-- diff --git a/unix/syscall_internal_linux_test.go b/unix/syscall_internal_linux_test.go index 7d34c4f1e..e8b749170 100644 --- a/unix/syscall_internal_linux_test.go +++ b/unix/syscall_internal_linux_test.go @@ -517,7 +517,7 @@ func TestSockaddrUnix_sockaddr(t *testing.T) { slen: 2, // family (uint16) }, { - name: "abstract", + name: "abstract_starting_with_at", sa: &SockaddrUnix{ Name: "@", }, @@ -526,6 +526,16 @@ func TestSockaddrUnix_sockaddr(t *testing.T) { }, slen: 3, // family (uint16) + NULL }, + { + name: "abstract_starting_with_null", + sa: &SockaddrUnix{ + Name: "\x00", + }, + raw: &RawSockaddrUnix{ + Family: AF_UNIX, + }, + slen: 3, // family (uint16) + NULL + }, { name: "named", sa: &SockaddrUnix{ diff --git a/unix/syscall_linux.go b/unix/syscall_linux.go index 6b8a4ad69..a5e1c10e3 100644 --- a/unix/syscall_linux.go +++ b/unix/syscall_linux.go @@ -417,7 +417,8 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { if n > 0 { sl += _Socklen(n) + 1 } - if sa.raw.Path[0] == '@' { + if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) { + // Check sl > 3 so we don't change unnamed socket behavior. sa.raw.Path[0] = 0 // Don't count trailing NUL for abstract address. sl-- diff --git a/unix/syscall_solaris.go b/unix/syscall_solaris.go index b99cfa134..60c8142d4 100644 --- a/unix/syscall_solaris.go +++ b/unix/syscall_solaris.go @@ -128,7 +128,8 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { if n > 0 { sl += _Socklen(n) + 1 } - if sa.raw.Path[0] == '@' { + if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) { + // Check sl > 3 so we don't change unnamed socket behavior. sa.raw.Path[0] = 0 // Don't count trailing NUL for abstract address. sl-- diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index dcaa40977..fb6cfd046 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -970,7 +970,8 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) { if n > 0 { sl += int32(n) + 1 } - if sa.raw.Path[0] == '@' { + if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) { + // Check sl > 3 so we don't change unnamed socket behavior. sa.raw.Path[0] = 0 // Don't count trailing NUL for abstract address. sl--