Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syscall: allow \x00-prefixed unix abstract socket to use full path length #70901

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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