wireguard-go/device
Martin Basovnik 12269c2761 device: fix possible deadlock in close method
There is a possible deadlock in `device.Close()` when you try to close
the device very soon after its start. The problem is that two different
methods acquire the same locks in different order:

1. device.Close()
 - device.ipcMutex.Lock()
 - device.state.Lock()

2. device.changeState(deviceState)
 - device.state.Lock()
 - device.ipcMutex.Lock()

Reproducer:

    func TestDevice_deadlock(t *testing.T) {
    	d := randDevice(t)
    	d.Close()
    }

Problem:

    $ go clean -testcache && go test -race -timeout 3s -run TestDevice_deadlock ./device | grep -A 10 sync.runtime_SemacquireMutex
    sync.runtime_SemacquireMutex(0xc000117d20?, 0x94?, 0x0?)
            /usr/local/opt/go/libexec/src/runtime/sema.go:77 +0x25
    sync.(*Mutex).lockSlow(0xc000130518)
            /usr/local/opt/go/libexec/src/sync/mutex.go:171 +0x213
    sync.(*Mutex).Lock(0xc000130518)
            /usr/local/opt/go/libexec/src/sync/mutex.go:90 +0x55
    golang.zx2c4.com/wireguard/device.(*Device).Close(0xc000130500)
            /Users/martin.basovnik/git/basovnik/wireguard-go/device/device.go:373 +0xb6
    golang.zx2c4.com/wireguard/device.TestDevice_deadlock(0x0?)
            /Users/martin.basovnik/git/basovnik/wireguard-go/device/device_test.go:480 +0x2c
    testing.tRunner(0xc00014c000, 0x131d7b0)
    --
    sync.runtime_SemacquireMutex(0xc000130564?, 0x60?, 0xc000130548?)
            /usr/local/opt/go/libexec/src/runtime/sema.go:77 +0x25
    sync.(*Mutex).lockSlow(0xc000130750)
            /usr/local/opt/go/libexec/src/sync/mutex.go:171 +0x213
    sync.(*Mutex).Lock(0xc000130750)
            /usr/local/opt/go/libexec/src/sync/mutex.go:90 +0x55
    sync.(*RWMutex).Lock(0xc000130750)
            /usr/local/opt/go/libexec/src/sync/rwmutex.go:147 +0x45
    golang.zx2c4.com/wireguard/device.(*Device).upLocked(0xc000130500)
            /Users/martin.basovnik/git/basovnik/wireguard-go/device/device.go:179 +0x72
    golang.zx2c4.com/wireguard/device.(*Device).changeState(0xc000130500, 0x1)

Signed-off-by: Martin Basovnik <martin.basovnik@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2023-12-11 16:38:47 +01:00
..
allowedips.go global: bump copyright year 2023-02-07 20:39:29 -03:00
allowedips_rand_test.go global: bump copyright year 2023-02-07 20:39:29 -03:00
allowedips_test.go global: bump copyright year 2023-02-07 20:39:29 -03:00
bind_test.go global: buff -> buf 2023-03-13 17:55:53 +01:00
channels.go device: move Queue{In,Out}boundElement Mutex to container type 2023-10-10 15:07:36 +02:00
constants.go global: bump copyright year 2023-02-07 20:39:29 -03:00
cookie.go global: bump copyright year 2023-02-07 20:39:29 -03:00
cookie_test.go global: bump copyright year 2023-02-07 20:39:29 -03:00
device.go device: fix possible deadlock in close method 2023-12-11 16:38:47 +01:00
device_test.go global: buff -> buf 2023-03-13 17:55:53 +01:00
devicestate_string.go device: remove deviceStateNew 2021-02-09 15:39:19 +01:00
endpoint_test.go global: bump copyright year 2023-02-07 20:39:29 -03:00
indextable.go global: bump copyright year 2023-02-07 20:39:29 -03:00
ip.go global: bump copyright year 2023-02-07 20:39:29 -03:00
kdf_test.go global: bump copyright year 2023-02-07 20:39:29 -03:00
keypair.go global: bump copyright year 2023-02-07 20:39:29 -03:00
logger.go global: bump copyright year 2023-02-07 20:39:29 -03:00
mobilequirks.go device: change Peer.endpoint locking to reduce contention 2023-12-11 16:34:09 +01:00
noise-helpers.go device: uniformly check ECDH output for zeros 2023-02-16 16:33:14 +01:00
noise-protocol.go device: uniformly check ECDH output for zeros 2023-02-16 16:33:14 +01:00
noise-types.go global: bump copyright year 2023-02-07 20:39:29 -03:00
noise_test.go device: uniformly check ECDH output for zeros 2023-02-16 16:33:14 +01:00
peer.go device: change Peer.endpoint locking to reduce contention 2023-12-11 16:34:09 +01:00
pools.go device: move Queue{In,Out}boundElement Mutex to container type 2023-10-10 15:07:36 +02:00
pools_test.go conn, device, tun: implement vectorized I/O plumbing 2023-03-10 14:52:13 +01:00
queueconstants_android.go device: ratchet up max segment size on android 2023-10-22 02:12:13 +02:00
queueconstants_default.go conn: inch BatchSize toward being non-dynamic 2023-03-10 14:52:22 +01:00
queueconstants_ios.go global: bump copyright year 2023-02-07 20:39:29 -03:00
queueconstants_windows.go global: bump copyright year 2023-02-07 20:39:29 -03:00
race_disabled_test.go global: bump copyright year 2023-02-07 20:39:29 -03:00
race_enabled_test.go global: bump copyright year 2023-02-07 20:39:29 -03:00
receive.go device: do atomic 64-bit add outside of vector loop 2023-12-11 16:35:57 +01:00
send.go device: move Queue{In,Out}boundElement Mutex to container type 2023-10-10 15:07:36 +02:00
sticky_default.go global: remove old-style build tags 2021-10-12 12:02:10 -06:00
sticky_linux.go device: change Peer.endpoint locking to reduce contention 2023-12-11 16:34:09 +01:00
timers.go device: change Peer.endpoint locking to reduce contention 2023-12-11 16:34:09 +01:00
tun.go global: bump copyright year 2023-02-07 20:39:29 -03:00
uapi.go device: change Peer.endpoint locking to reduce contention 2023-12-11 16:34:09 +01:00