Skip to content

Commit

Permalink
Fix crash in timesync code
Browse files Browse the repository at this point in the history
When ConnectVSockSync fails in watchWakeupNotifications(),
the returned net.Conn contains a nil value, but is not a nil interface
(ie checking it for `== nil` returns false).
This means we'll try to call `Close()` on the connection, which will
cause a nil pointer dereference.
See https://go.dev/doc/faq#nil_error and https://groups.google.com/g/golang-nuts/c/wnH302gBa4I
for details about this behaviour.

This bug is fixed by returning a nil interface when
`vsockDevice.Connect(uint32(port))` returns an error.
  • Loading branch information
cfergeau authored and praveenkumar committed Nov 22, 2023
1 parent 2a4d725 commit f19a643
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/vf/vsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ func ConnectVsockSync(vm *vz.VirtualMachine, port uint) (net.Conn, error) {
}
vsockDevice := socketDevices[0]

return vsockDevice.Connect(uint32(port))
conn, err := vsockDevice.Connect(uint32(port))
if err != nil {
// we can't `return vsockDevice.Connect()` directly, see https://go.dev/doc/faq#nil_error
// checking the return value for nil won't work as expected if we don't do this
return nil, err
}
return conn, nil
}

// connectVsock proxies connections from a host unix socket to a vsock port
Expand Down

0 comments on commit f19a643

Please sign in to comment.