delve/service/internal/sameuser/sameuser_linux_test.go
Alessandro Arzilli 513751573e
service: fix sameuser check (#2642)
Change the socket search to check both the remote and local fields of the
socket match the socket we want to find.

Sockets are identified by the 4-uple

	local_addr, local_port, remote_addr, remote_port

Two socket can differ by a single one of this four elements.
It is possible for the same local_port to be used by two different sockets,
as long as they are connecting to different remote addresses (or remote
ports).

An example of this bug in action can be seen at:

https://github.com/golang/vscode-go/runs/3141270564?check_suite_focus=true

There the server starts listening on 127.0.0.1:46011 and rejects a valid
client connection by finding the following socket:

60: 0100007F:DD82 0100007F:962D 06 00000000:00000000 03:00000133 00000000     0        0 0 3 0000000000000000

the local address of this socket is 0100007F:DD82 (127.0.0.1:56706), and the
remote address is 0100007F:962D (127.0.0.1:38445).

The reported error is:

	closing connection from different user (127.0.0.1:56706): connections to localhost are only accepted from the same UNIX user for security reasons

note how the local port does match the socket line (56706) but the remote
port is wrong (38445 instead of 46011).

Note also that the state of this socket is 06, or TIME_WAIT, which would be
impossible if this was the right socket, since the right socket would still
be open.

Fixes https://github.com/golang/vscode-go/issues/1555
2021-08-10 11:40:39 +02:00

69 lines
2.7 KiB
Go

//+build linux
package sameuser
import (
"net"
"testing"
)
func TestSameUserForRemoteAddr(t *testing.T) {
uid = 149098
var proc string
readFile = func(string) ([]byte, error) {
return []byte(proc), nil
}
for _, tt := range []struct {
name string
proc string
localAddr, remoteAddr *net.TCPAddr
want bool
}{
{
name: "ipv4-same",
proc: ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
21: 0100007F:E682 0100007F:0FC8 01 00000000:00000000 00:00000000 00000000 149098 0 8420541 2 0000000000000000 20 0 0 10 -1 `,
localAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4040},
remoteAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 59010},
want: true,
},
{
name: "ipv4-not-found",
proc: ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
21: 0100007F:E682 0100007F:0FC8 01 00000000:00000000 00:00000000 00000000 149098 0 8420541 2 0000000000000000 20 0 0 10 -1 `,
localAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4040},
remoteAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 2342},
want: false,
},
{
name: "ipv4-different-uid",
proc: ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
21: 0100007F:E682 0100007F:0FC8 01 00000000:00000000 00:00000000 00000000 149097 0 8420541 2 0000000000000000 20 0 0 10 -1 `,
localAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4040},
remoteAddr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 59010},
want: false,
},
{
name: "ipv6-same",
proc: ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
5: 00000000000000000000000001000000:D3E4 00000000000000000000000001000000:0FC8 01 00000000:00000000 00:00000000 00000000 149098 0 8425526 2 0000000000000000 20 0 0 10 -1
6: 00000000000000000000000001000000:0FC8 00000000000000000000000001000000:D3E4 01 00000000:00000000 00:00000000 00000000 149098 0 8424744 1 0000000000000000 20 0 0 10 -1`,
localAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 4040},
remoteAddr: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 54244},
want: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
proc = tt.proc
// The returned error is for reporting only.
same, _ := sameUserForRemoteAddr(tt.localAddr, tt.remoteAddr)
if got, want := same, tt.want; got != want {
t.Errorf("sameUserForRemoteAddr(%v, %v) = %v, want %v", tt.localAddr, tt.remoteAddr, got, want)
}
})
}
}