Skip to content

Commit

Permalink
Merge pull request #2637 from weaveworks/2636-DNSSnooper-race
Browse files Browse the repository at this point in the history
Avoid race conditions in DNSSnooper's cached domains
  • Loading branch information
Alfonso Acosta authored Jun 23, 2017
2 parents 7e3c8b8 + 4006040 commit 40bb28a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions probe/endpoint/dns_snooper_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"fmt"
"math"
"sync"
"time"

log "github.com/Sirupsen/logrus"
Expand All @@ -23,8 +24,10 @@ const (

// DNSSnooper is a snopper of DNS queries
type DNSSnooper struct {
stop chan struct{}
pcapHandle *pcap.Handle
stop chan struct{}
pcapHandle *pcap.Handle
// gcache is goroutine-safe, but the cached values aren't
reverseDNSMutex sync.RWMutex
reverseDNSCache gcache.Cache
decodingErrorCounts map[string]uint64 // for limiting
}
Expand Down Expand Up @@ -101,10 +104,11 @@ func (s *DNSSnooper) CachedNamesForIP(ip string) []string {
if err != nil {
return result
}

s.reverseDNSMutex.RLock()
for domain := range domains.(map[string]struct{}) {
result = append(result, domain)
}
s.reverseDNSMutex.RUnlock()

return result
}
Expand Down Expand Up @@ -272,7 +276,9 @@ func (s *DNSSnooper) processDNSMessage(dns *layers.DNS) {
s.reverseDNSCache.Set(ip, map[string]struct{}{newDomain: {}})
} else {
// TODO: Be smarter about the expiration of entries with pre-existing associated domains
s.reverseDNSMutex.Lock()
existingDomains.(map[string]struct{})[newDomain] = struct{}{}
s.reverseDNSMutex.Unlock()
}
}
}

0 comments on commit 40bb28a

Please sign in to comment.