conn: linux: protect read fds

The -1 protection was removed and the wrong error was returned, causing
us to read from a bogus fd. As well, remove the useless closures that
aren't doing anything, since this is all synchronized anyway.

Fixes: 10533c3 ("all: make conn.Bind.Open return a slice of receive functions")
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-05-20 18:09:55 +02:00
parent 50d779833e
commit bd83f0ac99

View file

@ -148,12 +148,12 @@ again:
var fns []ReceiveFunc var fns []ReceiveFunc
if sock4 != -1 { if sock4 != -1 {
fns = append(fns, bind.makeReceiveIPv4(sock4))
bind.sock4 = sock4 bind.sock4 = sock4
fns = append(fns, bind.receiveIPv4)
} }
if sock6 != -1 { if sock6 != -1 {
fns = append(fns, bind.makeReceiveIPv6(sock6))
bind.sock6 = sock6 bind.sock6 = sock6
fns = append(fns, bind.receiveIPv6)
} }
if len(fns) == 0 { if len(fns) == 0 {
return nil, 0, syscall.EAFNOSUPPORT return nil, 0, syscall.EAFNOSUPPORT
@ -224,20 +224,26 @@ func (bind *LinuxSocketBind) Close() error {
return err2 return err2
} }
func (*LinuxSocketBind) makeReceiveIPv6(sock int) ReceiveFunc { func (bind *LinuxSocketBind) receiveIPv4(buf []byte) (int, Endpoint, error) {
return func(buff []byte) (int, Endpoint, error) { bind.mu.RLock()
var end LinuxSocketEndpoint defer bind.mu.RUnlock()
n, err := receive6(sock, buff, &end) if bind.sock4 == -1 {
return n, &end, err return 0, nil, net.ErrClosed
} }
var end LinuxSocketEndpoint
n, err := receive4(bind.sock4, buf, &end)
return n, &end, err
} }
func (*LinuxSocketBind) makeReceiveIPv4(sock int) ReceiveFunc { func (bind *LinuxSocketBind) receiveIPv6(buf []byte) (int, Endpoint, error) {
return func(buff []byte) (int, Endpoint, error) { bind.mu.RLock()
var end LinuxSocketEndpoint defer bind.mu.RUnlock()
n, err := receive4(sock, buff, &end) if bind.sock6 == -1 {
return n, &end, err return 0, nil, net.ErrClosed
} }
var end LinuxSocketEndpoint
n, err := receive6(bind.sock6, buf, &end)
return n, &end, err
} }
func (bind *LinuxSocketBind) Send(buff []byte, end Endpoint) error { func (bind *LinuxSocketBind) Send(buff []byte, end Endpoint) error {