go icon indicating copy to clipboard operation
go copied to clipboard

net: netgo resolver cannot lookup 254 characters FQDN

Open yosida95 opened this issue 2 years ago • 3 comments

What version of Go are you using (go version)?

$ go version
go version go1.19 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/yosida95/proj/bin"
GOCACHE="/home/yosida95/.cache/go-build"
GOENV="/home/yosida95/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/yosida95/proj/pkg/mod"
GOOS="linux"
GOPATH="/home/yosida95/proj"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/go/1.19"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/go/1.19/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3904822855=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Lookup for 254 characters FQDN, maximum length as per the DNS spec.

https://go.dev/play/p/QMYRUTEvL2Y

In the above play I use LookupTXT, but other Lookup* functions are also affected by this bug when built with -tags netgo.

What did you expect to see?

Successful response or NXDOMAIN

What did you see instead?

&net.DNSError{Err:"cannot unmarshal DNS message", Server: ""}

In dnsclient_unix.go, *dnsConfig.nameList rejects 254 characters FQDN instead of rejecting 254 characters non-FQDN.

https://github.com/golang/go/blob/43456202a1e55da55666fac9d56ace7654a65b64/src/net/dnsclient_unix.go#L484-L485

It also means *dnsConfig.nameList accepts 254 characters non-FQDN and generates invalid 255 characters FQDN.

https://github.com/golang/go/blob/43456202a1e55da55666fac9d56ace7654a65b64/src/net/dnsclient_unix.go#L495-L503

On the other hand, isDomainName does correct FQDN handling.

https://github.com/golang/go/blob/43456202a1e55da55666fac9d56ace7654a65b64/src/net/dnsclient.go#L90

yosida95 avatar Aug 05 '22 07:08 yosida95

LookupHost has even a different behaviour than the LookupTXT:

func main() {
        suffix := ".example.net."
        name := strings.Repeat("verylong.", 27)[:254-len(suffix)] + suffix
        fmt.Println(len(name), name)

        d, err := net.LookupHost(name)
        fmt.Println(d, err)
}
[mateusz@arch isstes]$ GODEBUG=netdns=go+3 go run main.go
254 verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylon.example.net.
go package net: confVal.netCgo = false  netGo = true
go package net: GODEBUG setting forcing use of Go's resolver
go package net: hostLookupOrder(verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylong.verylon.example.net) = files,dns
[] <nil>

mateusz834 avatar Aug 05 '22 11:08 mateusz834

Change https://go.dev/cl/421674 mentions this issue: net: dns fix 254 char fqdn

gopherbot avatar Aug 05 '22 12:08 gopherbot

cc @neild

seankhliao avatar Aug 05 '22 19:08 seankhliao