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

net: LookupCNAME returns empty string if entry from /etc/hosts is used #44741

Open
riton opened this issue Mar 2, 2021 · 4 comments · May be fixed by #51004
Open

net: LookupCNAME returns empty string if entry from /etc/hosts is used #44741

riton opened this issue Mar 2, 2021 · 4 comments · May be fixed by #51004
Labels
NeedsInvestigation
Milestone

Comments

@riton
Copy link

@riton riton commented Mar 2, 2021

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

$ go version
go version go1.16 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=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/go/pkg/mod"
GONOPROXY="*.private-domain.fr"
GONOSUMDB="*.private-domain.fr"
GOOS="linux"
GOPATH="/home/user/go"
GOPRIVATE="*.private-domain.fr"
GOPROXY="direct"
GOROOT="/opt/go/go1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/go/go1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build937784123=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I'm trying to resolve a name that is present in my /etc/hosts using LookupCNAME

The simple program I'm using is:

package main                                               
                                                           
import (                                                   
        "fmt"                                              
        "net"                                              
        "os"                                               
)                                                          
                                                           
func main() {                                              
        name, err := net.LookupCNAME(os.Args[1])           
        fmt.Printf("name = %q, err = %v\n", name, err)     
}                                                          

The content of my /etc/hosts is:

140.82.121.4 github.com

(the behavior exists with other domains / adresses)

What did you expect to see?

Same behavior using cgo resolver and pure go resolver:

with cgo resolver`

$ GODEBUG="netdns=3" go run . github.com
go package net: dynamic selection of DNS resolver
go package net: hostLookupOrder() = cgo
name = "github.com.", err = <nil>

with pure go resolver`

$ GODEBUG="netdns=go+3" go run . github.com                 
go package net: GODEBUG setting forcing use of Go's resolver
go package net: hostLookupOrder() = files,dns               
go package net: hostLookupOrder(github.com) = files,dns     
name = "github.com.", err = <nil>                                                        

What did you see instead?

with cgo resolver`

$ GODEBUG="netdns=3" go run . github.com
go package net: dynamic selection of DNS resolver
go package net: hostLookupOrder() = cgo
name = "github.com.", err = <nil>

with pure go resolver`

$ GODEBUG="netdns=go+3" go run . github.com                 
go package net: GODEBUG setting forcing use of Go's resolver
go package net: hostLookupOrder() = files,dns               
go package net: hostLookupOrder(github.com) = files,dns     
name = "", err = <nil>                                                        

Pure GO returns an empty string while cgo resolves the name properly.

Extra informations

It seems that this behavior is from the dnsmessage.Name{} returned from https://github.com/golang/go/blob/go1.16/src/net/dnsclient_unix.go#L569 .

As soon as the /etc/hosts entry is removed, the behavior difference disappears.

Thanks in advance for helping me understand this.

@seankhliao
Copy link
Member

@seankhliao seankhliao commented Mar 2, 2021

I think this works as intended, an entry in /etc/hosts should be equivalent to an A or AAAA record and not CNAME

riton added a commit to ccin2p3/gokrb5 that referenced this issue Mar 3, 2021
* See golang/go#44741
* When "pure go" resolver is used and host is present in /etc/hosts
  net.LookupCNAME() can return an empty string whitout any error
riton added a commit to ccin2p3/gokrb5 that referenced this issue Mar 3, 2021
* See golang/go#44741
* When "pure go" resolver is used and host is present in /etc/hosts
  net.LookupCNAME() can return an empty string whitout any error
@dmitshur
Copy link
Contributor

@dmitshur dmitshur commented Mar 5, 2021

CC @bradfitz, @ianlancetaylor via owners.

@dmitshur dmitshur added the NeedsInvestigation label Mar 5, 2021
@dmitshur dmitshur added this to the Backlog milestone Mar 5, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Feb 3, 2022

Change https://golang.org/cl/382996 mentions this issue: net: add support for /etc/hosts aliases using go resolver

@neild
Copy link
Contributor

@neild neild commented Feb 17, 2022

The result of net.LookupCNAME with the cgo resolver is the contents of the ai_canonname ("canonical name for service location") field of the struct addrinfo returned by getaddrinfo. Many (most? all?) libc resolvers populate this field for results returned based on an entry in /etc/hosts.

With the go resolver, net.LookupCNAME issues an IN A or IN AAAA query and determines the canonical name from the response. The canonical name will be populated even if the name does not have a CNAME record; in this case, the name is just that of the original query.

I think that it makes sense to align the cgo and go resolvers and return a canonical name for a result based on an /etc/hosts entry

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

Successfully merging a pull request may close this issue.

5 participants