Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid race conditions in DNSSnooper's cached domains #2637

Merged
merged 4 commits into from
Jun 23, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 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,11 @@ 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
// values cached values aren't

This comment was marked as abuse.

reverseDNSMutex sync.RWMutex
reverseDNSCache gcache.Cache
decodingErrorCounts map[string]uint64 // for limiting
}
Expand Down Expand Up @@ -101,10 +105,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 +277,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()
}
}
}