Skip to content

ipRange nil Segfaults prevention #3459

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all 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
49 changes: 45 additions & 4 deletions app/dns/fakedns/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,26 @@
ipRange *gonet.IPNet

config *FakeDnsPool
closed bool // Flag, closed instance
}

func (fkdns *Holder) IsIPInIPPool(ip net.Address) bool {
if fkdns == nil || fkdns.closed {
return false
}
if ip.Family().IsDomain() {
return false
}
if fkdns.ipRange == nil {
return false
}
return fkdns.ipRange.Contains(ip.IP())
}

func (fkdns *Holder) GetFakeIPForDomain3(domain string, ipv4, ipv6 bool) []net.Address {
if fkdns == nil || fkdns.closed || fkdns.ipRange == nil {
return []net.Address{}
}
isIPv6 := fkdns.ipRange.IP.To4() == nil
if (isIPv6 && ipv6) || (!isIPv6 && ipv4) {
return fkdns.GetFakeIPForDomain(domain)
Expand All @@ -46,17 +56,24 @@
}

func (fkdns *Holder) Start() error {
if fkdns == nil || fkdns.closed {
return newError("Holder is closed")
}
if fkdns.config != nil && fkdns.config.IpPool != "" && fkdns.config.LruSize != 0 {
return fkdns.initializeFromConfig()
}
return newError("invalid fakeDNS setting")
}

func (fkdns *Holder) Close() error {
if fkdns == nil || fkdns.closed {
return nil
}
fkdns.domainToIP = nil
fkdns.nextIP = nil
fkdns.ipRange = nil
fkdns.mu = nil
fkdns.closed = true
return nil
}

Expand All @@ -75,7 +92,7 @@
}

func NewFakeDNSHolderConfigOnly(conf *FakeDnsPool) (*Holder, error) {
return &Holder{nil, nil, nil, nil, conf}, nil

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (windows, amd64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (dragonfly, amd64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, arm, 6)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, arm, 7)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (windows, 386)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, mips)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (windows, arm, 7)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, mipsle)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, arm, 5)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, mips64le)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (darwin, arm64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (freebsd, 386)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, mips64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (darwin, amd64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (freebsd, arm64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (freebsd, arm, 6)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / lint

too few values in struct literal of type Holder) (typecheck)

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, 386)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, riscv64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (freebsd, arm, 7)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, loong64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (openbsd, arm, 7)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (android, arm64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (freebsd, amd64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (openbsd, arm64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (openbsd, 386)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (openbsd, arm, 6)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (windows, arm64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, arm64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (openbsd, amd64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, amd64, pie)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, amd64)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / build (linux, arm64, pie)

too few values in struct literal of type Holder

Check failure on line 95 in app/dns/fakedns/fake.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

too few values in struct literal of type Holder
}

func (fkdns *Holder) initializeFromConfig() error {
Expand Down Expand Up @@ -111,6 +128,9 @@

// GetFakeIPForDomain checks and generate a fake IP for a domain name
func (fkdns *Holder) GetFakeIPForDomain(domain string) []net.Address {
if fkdns == nil || fkdns.closed {
return nil
}
fkdns.mu.Lock()
defer fkdns.mu.Unlock()
if v, ok := fkdns.domainToIP.Get(domain); ok {
Expand All @@ -121,8 +141,12 @@
ip = net.IPAddress(fkdns.nextIP.Bytes())

fkdns.nextIP = fkdns.nextIP.Add(fkdns.nextIP, big.NewInt(1))
if !fkdns.ipRange.Contains(fkdns.nextIP.Bytes()) {
fkdns.nextIP = big.NewInt(0).SetBytes(fkdns.ipRange.IP)
if fkdns.ipRange == nil || !fkdns.ipRange.Contains(fkdns.nextIP.Bytes()) {
if fkdns.ipRange != nil {
fkdns.nextIP = big.NewInt(0).SetBytes(fkdns.ipRange.IP)
} else {
break // ipRange nil, cancel
}
}

// if we run for a long time, we may go back to beginning and start seeing the IP in use
Expand All @@ -136,11 +160,28 @@

// GetDomainFromFakeDNS checks if an IP is a fake IP and have corresponding domain name
func (fkdns *Holder) GetDomainFromFakeDNS(ip net.Address) string {
if !ip.Family().IsIP() || !fkdns.ipRange.Contains(ip.IP()) {
// nil-Checks, Panics preventing (Issue: Segfault in net.(*IPNet).Contains)
if fkdns == nil || fkdns.closed || fkdns.ipRange == nil || fkdns.domainToIP == nil {
return ""
}
if ip == nil || !ip.Family().IsIP() {
return ""
}
ipBytes := ip.IP()
if ipBytes == nil {
return ""
}
// ipRange-Check
if fkdns.ipRange == nil || !fkdns.ipRange.Contains(ipBytes) {
return ""
}
if fkdns.domainToIP == nil {
return ""
}
if k, ok := fkdns.domainToIP.GetKeyFromValue(ip); ok {
return k.(string)
if str, ok := k.(string); ok {
return str
}
}
return ""
}
Expand Down
Loading