mikispag / dns-over-tls-forwarder

A simple, fast DNS-over-TLS forwarding server with hybrid LRU/MFA caching written in Go.
MIT License
48 stars 9 forks source link

Race Condition: connections can be put in the pool while in use #2

Closed empijei closed 4 years ago

empijei commented 4 years ago

8000024 introduced a race condition in forwardMessage:

    msgChan := make(chan *dns.Msg, 2)
    go exchangeMessages(cloudFlareConn, q, msgChan)
    go exchangeMessages(googleConn, q, msgChan)

    m = <-msgChan
    if m != nil {
        return m
    }
    return <-msgChan

This code makes the function return as soon as one exchangeMessage is done, but the other is still running and has a reference to its connection.

The deferred func then puts back both connections in the pool, while one is still in use and could be picked up by another requester and corrupt the messages.

exchangeMessages should be the one in charge of handling the pools.

I can write something to address this issue.