]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
wintun: take mutex first always
authorJason A. Donenfeld <Jason@zx2c4.com>
Mon, 2 Sep 2019 03:32:28 +0000 (21:32 -0600)
committerJason A. Donenfeld <Jason@zx2c4.com>
Mon, 2 Sep 2019 03:32:28 +0000 (21:32 -0600)
This prevents an ABA deadlock with setupapi's internal locks.

tun/wintun/wintun_windows.go

index c654b021f534143eb09ce5822d0f7c51a933da26..e2348c0a9ef50073d950a79e8020f65fe38b1d02 100644 (file)
@@ -196,6 +196,15 @@ func (pool Pool) GetInterface(ifname string) (*Interface, error) {
 // interesting complications with its usage. This function returns the network
 // interface ID and a flag if reboot is required.
 func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wintun *Interface, rebootRequired bool, err error) {
+       mutex, err := pool.takeNameMutex()
+       if err != nil {
+               return
+       }
+       defer func() {
+               windows.ReleaseMutex(mutex)
+               windows.CloseHandle(mutex)
+       }()
+
        // Create an empty device info set for network adapter device class.
        devInfoList, err := setupapi.SetupDiCreateDeviceInfoListEx(&deviceClassNetGUID, 0, "")
        if err != nil {
@@ -281,15 +290,6 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
                return
        }
 
-       mutex, err := pool.takeNameMutex()
-       if err != nil {
-               return
-       }
-       defer func() {
-               windows.ReleaseMutex(mutex)
-               windows.CloseHandle(mutex)
-       }()
-
        defer func() {
                if err != nil {
                        // The interface failed to install, or the interface ID was unobtainable. Clean-up.