Skip to content

Commit

Permalink
net: respect hosts file when resolving names for Windows
Browse files Browse the repository at this point in the history
Fixes #57757.

Change-Id: I896dae8e5905ae98539ab83c9379fd1c9886d44a
Reviewed-on: https://go-review.googlesource.com/c/go/+/467335
Reviewed-by: Mateusz Poliwczak <mpoliwczak34@gmail.com>
Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Joedian Reid <joedian@golang.org>
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
  • Loading branch information
nikita-vanyasin authored and qmuntal committed Sep 4, 2023
1 parent 5373959 commit 33d4a51
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 38 deletions.
6 changes: 6 additions & 0 deletions src/internal/syscall/windows/security_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,9 @@ type UserInfo4 struct {
}

//sys NetUserGetLocalGroups(serverName *uint16, userName *uint16, level uint32, flags uint32, buf **byte, prefMaxLen uint32, entriesRead *uint32, totalEntries *uint32) (neterr error) = netapi32.NetUserGetLocalGroups

// GetSystemDirectory retrieves the path to current location of the system
// directory, which is typically, though not always, `C:\Windows\System32`.
//
//go:linkname GetSystemDirectory
func GetSystemDirectory() string // Implemented in runtime package.
11 changes: 1 addition & 10 deletions src/net/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,7 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
// Go resolver was explicitly requested
// or cgo resolver is not available.
// Figure out the order below.
switch c.goos {
case "windows":
// TODO(bradfitz): implement files-based
// lookup on Windows too? I guess /etc/hosts
// kinda exists on Windows. But for now, only
// do DNS.
fallbackOrder = hostLookupDNS
default:
fallbackOrder = hostLookupFilesDNS
}
fallbackOrder = hostLookupFilesDNS
canUseCgo = false
} else if c.netCgo {
// Cgo resolver was explicitly requested.
Expand Down
24 changes: 12 additions & 12 deletions src/net/dnsclient_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ func TestGoLookupIPOrderFallbackToFile(t *testing.T) {
t.Fatal(err)
}
// Redirect host file lookups.
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
testHookHostsPath = "testdata/hosts"
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
hostsFilePath = "testdata/hosts"

for _, order := range []hostLookupOrder{hostLookupFilesDNS, hostLookupDNSFiles} {
name := fmt.Sprintf("order %v", order)
Expand Down Expand Up @@ -1966,8 +1966,8 @@ func TestCVE202133195(t *testing.T) {
DefaultResolver = &r
defer func() { DefaultResolver = originalDefault }()
// Redirect host file lookups.
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
testHookHostsPath = "testdata/hosts"
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
hostsFilePath = "testdata/hosts"

tests := []struct {
name string
Expand Down Expand Up @@ -2186,8 +2186,8 @@ func TestRootNS(t *testing.T) {
}

func TestGoLookupIPCNAMEOrderHostsAliasesFilesOnlyMode(t *testing.T) {
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
testHookHostsPath = "testdata/aliases"
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
hostsFilePath = "testdata/aliases"
mode := hostLookupFiles

for _, v := range lookupStaticHostAliasesTest {
Expand All @@ -2196,8 +2196,8 @@ func TestGoLookupIPCNAMEOrderHostsAliasesFilesOnlyMode(t *testing.T) {
}

func TestGoLookupIPCNAMEOrderHostsAliasesFilesDNSMode(t *testing.T) {
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
testHookHostsPath = "testdata/aliases"
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
hostsFilePath = "testdata/aliases"
mode := hostLookupFilesDNS

for _, v := range lookupStaticHostAliasesTest {
Expand All @@ -2213,8 +2213,8 @@ var goLookupIPCNAMEOrderDNSFilesModeTests = []struct {
}

func TestGoLookupIPCNAMEOrderHostsAliasesDNSFilesMode(t *testing.T) {
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
testHookHostsPath = "testdata/aliases"
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
hostsFilePath = "testdata/aliases"
mode := hostLookupDNSFiles

for _, v := range goLookupIPCNAMEOrderDNSFilesModeTests {
Expand Down Expand Up @@ -2541,7 +2541,7 @@ func TestDNSConfigNoReload(t *testing.T) {
}

func TestLookupOrderFilesNoSuchHost(t *testing.T) {
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
if runtime.GOOS != "openbsd" {
defer setSystemNSS(getSystemNSS(), 0)
setSystemNSS(nssStr(t, "hosts: files"), time.Hour)
Expand All @@ -2568,7 +2568,7 @@ func TestLookupOrderFilesNoSuchHost(t *testing.T) {
if err := os.WriteFile(tmpFile, []byte{}, 0660); err != nil {
t.Fatal(err)
}
testHookHostsPath = tmpFile
hostsFilePath = tmpFile

const testName = "test.invalid"

Expand Down
3 changes: 1 addition & 2 deletions src/net/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ var (
// if non-nil, overrides dialTCP.
testHookDialTCP func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error)

testHookHostsPath = "/etc/hosts"
testHookLookupIP = func(
testHookLookupIP = func(
ctx context.Context,
fn func(context.Context, string, string) ([]IPAddr, error),
network string,
Expand Down
9 changes: 9 additions & 0 deletions src/net/hook_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

var (
hostsFilePath = "/etc/hosts"
)
2 changes: 2 additions & 0 deletions src/net/hook_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import "syscall"
var (
testHookCanceledDial = func() {} // for golang.org/issue/16523

hostsFilePath = "/etc/hosts"

// Placeholders for socket system calls.
socketFunc func(int, int, int) (int, error) = syscall.Socket
connectFunc func(int, syscall.Sockaddr) error = syscall.Connect
Expand Down
2 changes: 2 additions & 0 deletions src/net/hook_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

var (
hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"

// Placeholders for socket system calls.
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect
Expand Down
2 changes: 1 addition & 1 deletion src/net/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var hosts struct {

func readHosts() {
now := time.Now()
hp := testHookHostsPath
hp := hostsFilePath

if now.Before(hosts.expire) && hosts.path == hp && len(hosts.byName) > 0 {
return
Expand Down
26 changes: 13 additions & 13 deletions src/net/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ var lookupStaticHostTests = []struct {
}

func TestLookupStaticHost(t *testing.T) {
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)

for _, tt := range lookupStaticHostTests {
testHookHostsPath = tt.name
hostsFilePath = tt.name
for _, ent := range tt.ents {
testStaticHost(t, tt.name, ent)
}
Expand Down Expand Up @@ -128,10 +128,10 @@ var lookupStaticAddrTests = []struct {
}

func TestLookupStaticAddr(t *testing.T) {
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)

for _, tt := range lookupStaticAddrTests {
testHookHostsPath = tt.name
hostsFilePath = tt.name
for _, ent := range tt.ents {
testStaticAddr(t, tt.name, ent)
}
Expand All @@ -151,27 +151,27 @@ func testStaticAddr(t *testing.T, hostsPath string, ent staticHostEntry) {
func TestHostCacheModification(t *testing.T) {
// Ensure that programs can't modify the internals of the host cache.
// See https://golang.org/issues/14212.
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)

testHookHostsPath = "testdata/ipv4-hosts"
hostsFilePath = "testdata/ipv4-hosts"
ent := staticHostEntry{"localhost", []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}}
testStaticHost(t, testHookHostsPath, ent)
testStaticHost(t, hostsFilePath, ent)
// Modify the addresses return by lookupStaticHost.
addrs, _ := lookupStaticHost(ent.in)
for i := range addrs {
addrs[i] += "junk"
}
testStaticHost(t, testHookHostsPath, ent)
testStaticHost(t, hostsFilePath, ent)

testHookHostsPath = "testdata/ipv6-hosts"
hostsFilePath = "testdata/ipv6-hosts"
ent = staticHostEntry{"::1", []string{"localhost"}}
testStaticAddr(t, testHookHostsPath, ent)
testStaticAddr(t, hostsFilePath, ent)
// Modify the hosts return by lookupStaticAddr.
hosts := lookupStaticAddr(ent.in)
for i := range hosts {
hosts[i] += "junk"
}
testStaticAddr(t, testHookHostsPath, ent)
testStaticAddr(t, hostsFilePath, ent)
}

var lookupStaticHostAliasesTest = []struct {
Expand All @@ -195,9 +195,9 @@ var lookupStaticHostAliasesTest = []struct {
}

func TestLookupStaticHostAliases(t *testing.T) {
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)

testHookHostsPath = "testdata/aliases"
hostsFilePath = "testdata/aliases"
for _, ent := range lookupStaticHostAliasesTest {
testLookupStaticHostAliases(t, ent.lookup, absDomainName(ent.res))
}
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ func initSysDirectory() {
sysDirectoryLen = l + 1
}

//go:linkname windows_GetSystemDirectory internal/syscall/windows.GetSystemDirectory
func windows_GetSystemDirectory() string {
return unsafe.String(&sysDirectory[0], sysDirectoryLen)
}

func windowsLoadSystemLib(name []uint16) uintptr {
return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
}
Expand Down

0 comments on commit 33d4a51

Please sign in to comment.