Skip to content

Commit

Permalink
syscall: allow \x00-prefixed unix abstract socket to use full path le…
Browse files Browse the repository at this point in the history
…ngth of 108

Fixes golang#70893
  • Loading branch information
albertsundjaja committed Dec 18, 2024
1 parent 95b433e commit 74527fe
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/net/unixsock_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ func TestUnixAutobindClose(t *testing.T) {
ln.Close()
}

func TestUnixAbstractLongNameeNullStart(t *testing.T) {
addr := "\x00abstract_test"
rsu := syscall.RawSockaddrUnix{}
paddedAddr := make([]byte, len(rsu.Path))
copy(paddedAddr, "\x00abstract_test")
addr = string(paddedAddr)

la, err := ResolveUnixAddr("unix", addr)
if err != nil {
t.Fatal(err)
}
c, err := ListenUnix("unix", la)
if err != nil {
t.Fatal(err)
}
defer c.Close()
}

func TestUnixgramLinuxAbstractLongName(t *testing.T) {
if !testableNetwork("unixgram") {
t.Skip("abstract unix socket long name test")
Expand Down
23 changes: 23 additions & 0 deletions src/net/unixsock_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"internal/syscall/windows"
"os"
"reflect"
"syscall"
"testing"
)

Expand Down Expand Up @@ -69,6 +70,28 @@ func TestUnixConnLocalWindows(t *testing.T) {
}
}

func TestUnixAbstractLongNameNullStart(t *testing.T) {
if !windows.SupportUnixSocket() {
t.Skip("unix test")
}

addr := "\x00abstract_test"
rsu := syscall.RawSockaddrUnix{}
paddedAddr := make([]byte, len(rsu.Path))
copy(paddedAddr, "\x00abstract_test")
addr = string(paddedAddr)

la, err := ResolveUnixAddr("unix", addr)
if err != nil {
t.Fatal(err)
}
c, err := ListenUnix("unix", la)
if err != nil {
t.Fatal(err)
}
defer c.Close()
}

func TestModeSocket(t *testing.T) {
if !windows.SupportUnixSocket() {
t.Skip("unix test")
Expand Down
6 changes: 3 additions & 3 deletions src/syscall/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
if n > len(sa.raw.Path) {
return nil, 0, EINVAL
}
if n == len(sa.raw.Path) && name[0] != '@' {
isAbstract := n > 0 && (name[0] == '@' || name[0] == '\x00')
if n == len(sa.raw.Path) && !isAbstract {
return nil, 0, EINVAL
}
sa.raw.Family = AF_UNIX
Expand All @@ -566,8 +567,7 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
if n > 0 {
sl += _Socklen(n) + 1
}
if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) {
// Check sl > 3 so we don't change unnamed socket behavior.
if isAbstract {
sa.raw.Path[0] = 0
// Don't count trailing NUL for abstract address.
sl--
Expand Down
6 changes: 3 additions & 3 deletions src/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) {
if n > len(sa.raw.Path) {
return nil, 0, EINVAL
}
if n == len(sa.raw.Path) && name[0] != '@' {
isAbstract := n > 0 && (name[0] == '@' || name[0] == '\x00')
if n == len(sa.raw.Path) && !isAbstract {
return nil, 0, EINVAL
}
sa.raw.Family = AF_UNIX
Expand All @@ -868,8 +869,7 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) {
if n > 0 {
sl += int32(n) + 1
}
if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) {
// Check sl > 3 so we don't change unnamed socket behavior.
if isAbstract {
sa.raw.Path[0] = 0
// Don't count trailing NUL for abstract address.
sl--
Expand Down

0 comments on commit 74527fe

Please sign in to comment.