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

nil ptr in defaults.go:152 msg.IsEdns0 #1614

Open
ignoramous opened this issue Nov 3, 2024 · 8 comments
Open

nil ptr in defaults.go:152 msg.IsEdns0 #1614

ignoramous opened this issue Nov 3, 2024 · 8 comments

Comments

@ignoramous
Copy link

ignoramous commented Nov 3, 2024

A crash report from our app points to a nil ptr whilst iterating over Extra RRs:

dns/defaults.go

Lines 147 to 157 in b77d1ed

func (dns *Msg) IsEdns0() *OPT {
// RFC 6891, Section 6.1.1 allows the OPT record to appear
// anywhere in the additional record section, but it's usually at
// the end so start there.
for i := len(dns.Extra) - 1; i >= 0; i-- {
if dns.Extra[i].Header().Rrtype == TypeOPT {
return dns.Extra[i].(*OPT)
}
}
return nil
}

11-03 05:50:57.349 32129 20730 E LibLogger: panic({0x763892d0c0?, 0x7638f537d0?})
11-03 05:50:57.349 32129 20730 E LibLogger:     /home/jitpack/golang/go/src/runtime/panic.go:785 +0x124
11-03 05:50:57.349 32129 20730 E LibLogger: github.com/miekg/dns.(*Msg).IsEdns0(0x40008b9710)
11-03 05:50:57.349 32129 20730 E LibLogger:     /tmp/gomobile-work-1841608193/pkg/mod/github.com/miekg/dns@v1.1.62/defaults.go:152 +0x60
11-03 05:50:57.349 32129 20730 E LibLogger: github.com/celzero/firestack/intra/xdns.ensureEDNS0(0x40008b9710)
11-03 05:50:57.349 32129 20730 E LibLogger:     /home/jitpack/build/intra/xdns/dnsutil.go:480 +0x20
11-03 05:50:57.349 32129 20730 E LibLogger: github.com/celzero/firestack/intra/xdns.AddEDNS0PaddingIfNoneFound(0x40008b9710)
11-03 05:50:57.349 32129 20730 E LibLogger:     /home/jitpack/build/intra/xdns/dnsutil.go:517 +0x24
11-03 05:50:57.349 32129 20730 E LibLogger: github.com/celzero/firestack/intra/doh.padQuery(0x40008b9710)
11-03 05:50:57.349 32129 20730 E LibLogger:     /home/jitpack/build/intra/doh/padding.go:35 +0x40
11-03 05:50:57.349 32129 20730 E LibLogger: github.com/celzero/firestack/intra/doh.(*transport).doDoh(0x40003dc160, {0x4000f84bdc, 0x4}, 0x40008b9710)
11-03 05:50:57.349 32129 20730 E LibLogger:     /home/jitpack/build/intra/doh/doh.go:372 +0x44
11-03 05:50:57.349 32129 20730 E LibLogger: github.com/celzero/firestack/intra/doh.(*transport).Query(0x40003dc160, {0x4000f84bd8, 0x8}, 0x40008b9710, 0x4000ec2600)
11-03 05:50:57.349 32129 20730 E LibLogger:     /home/jitpack/build/intra/doh/doh.go:679 +0xa8
11-03 05:50:57.349 32129 20730 E LibLogger: github.com/celzero/firestack/intra/dnsx.Req({0x7638a462f0, 0x40003dc160}, {0x4000f84bd8, 0x8}, 0x40008b9710, 0x4000ec2600)
11-03 05:50:57.349 32129 20730 E LibLogger:     /home/jitpack/build/intra/dnsx/alg.go:1129 +0x12c

Unsure if it is incorrect use of the lib by our code, or something that's indicative of other issues (as I see miekg/dns codebase accessing struct fields ex: Extra[x].Header() all too often).

(from: celzero/firestack#111)

@ignoramous ignoramous changed the title nil ptr in defaults.go:52 msg.IsEdns0 nil ptr in defaults.go:152 msg.IsEdns0 Nov 3, 2024
@ignoramous
Copy link
Author

If it helps, managed to grab a str(query) from a recent crash log:

;; opcode: QUERY, status: NOERROR, id: 0
;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version 0; flags:; udp: 4096
; PADDING: 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

;; QUESTION SECTION:
;github.com.	IN	 HTTPS

@miekg
Copy link
Owner

miekg commented Nov 22, 2024

thanks for that. This indeed needs a fix. Maybe I have the cycles this weekend

@miekg
Copy link
Owner

miekg commented Jan 24, 2025

(I obviously didn't have time this weekend, and also not soon)

so there is an OPT rr (PADDING), and then some if dns.Extra[i].Header().Rrtype == TypeOPT {

would nil....odd. This also happens if you (manually) in a Msg and call the above code? (PADDING is relative new, might be something fishy there)

@ignoramous
Copy link
Author

Unsure how the DNS msg is crafted. The app is handling the DNS queries forwarded to it by dnsmasq (on Android). If I were to hazard a guess, these queries were sent by Chrome.

would nil....odd

Yeah, no matter what checks we added (in our code), we couldn't prevent the nil ptr .

@miekg
Copy link
Owner

miekg commented Jan 24, 2025

odd

Do you happen to see which things nilled in dns.Extra[i].Header().Rrtype, i.e. the Extra[i] or does Header() return a nil?

@miekg
Copy link
Owner

miekg commented Jan 24, 2025

and... tcpdump or some such?!

@ignoramous
Copy link
Author

ignoramous commented Jan 29, 2025

and... tcpdump or some such?!

Unfortunately, no. The logs are sent by app users as bug reports from Android devices in the field, if that makes sense. We've hit this nil ptr may be twice or thrice in our testing too but it is so so rare.

Do you happen to see which things nil'd in dns.Extra[i].Header().Rrtype, i.e. the Extra[i] or does Header() return a nil?

We added nil checks on dns, on dns.Extra[i], a "deep" nil check on dns.Extra[i].Header() ... but nothing stopped the crashes as the nil ptr would manifest elsewhere (the error logs after desperate fixes we tried are linked to from here).1 I didn't open bugs for those, as the nils were emanating from our code & the padding stuff we were doing with OPT Extras. After many unsuccessful attempts, out of exasperation, we switched to Msg.IsEdns0() as the first thing our code did (AddEDNS0Padding -> ensureEDNS0 -> Msg.IsEdns0), and sure enough, hit a nil ptr, except the bug now manifested squarely in the lib and not our code.

Footnotes

  1. Commits (in no particular order): a / b / c / d / e / f

@miekg
Copy link
Owner

miekg commented Jan 29, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants