Skip to content

Commit 33d4a51

Browse files
nikita-vanyasinqmuntal
authored andcommitted
net: respect hosts file when resolving names for Windows
Fixes #57757. Change-Id: I896dae8e5905ae98539ab83c9379fd1c9886d44a Reviewed-on: https://go-review.googlesource.com/c/go/+/467335 Reviewed-by: Mateusz Poliwczak <[email protected]> Run-TryBot: Mateusz Poliwczak <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Joedian Reid <[email protected]> Reviewed-by: Quim Muntal <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 5373959 commit 33d4a51

File tree

10 files changed

+52
-38
lines changed

10 files changed

+52
-38
lines changed

src/internal/syscall/windows/security_windows.go

+6
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,9 @@ type UserInfo4 struct {
126126
}
127127

128128
//sys NetUserGetLocalGroups(serverName *uint16, userName *uint16, level uint32, flags uint32, buf **byte, prefMaxLen uint32, entriesRead *uint32, totalEntries *uint32) (neterr error) = netapi32.NetUserGetLocalGroups
129+
130+
// GetSystemDirectory retrieves the path to current location of the system
131+
// directory, which is typically, though not always, `C:\Windows\System32`.
132+
//
133+
//go:linkname GetSystemDirectory
134+
func GetSystemDirectory() string // Implemented in runtime package.

src/net/conf.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,7 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
238238
// Go resolver was explicitly requested
239239
// or cgo resolver is not available.
240240
// Figure out the order below.
241-
switch c.goos {
242-
case "windows":
243-
// TODO(bradfitz): implement files-based
244-
// lookup on Windows too? I guess /etc/hosts
245-
// kinda exists on Windows. But for now, only
246-
// do DNS.
247-
fallbackOrder = hostLookupDNS
248-
default:
249-
fallbackOrder = hostLookupFilesDNS
250-
}
241+
fallbackOrder = hostLookupFilesDNS
251242
canUseCgo = false
252243
} else if c.netCgo {
253244
// Cgo resolver was explicitly requested.

src/net/dnsclient_unix_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,8 @@ func TestGoLookupIPOrderFallbackToFile(t *testing.T) {
619619
t.Fatal(err)
620620
}
621621
// Redirect host file lookups.
622-
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
623-
testHookHostsPath = "testdata/hosts"
622+
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
623+
hostsFilePath = "testdata/hosts"
624624

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

19721972
tests := []struct {
19731973
name string
@@ -2186,8 +2186,8 @@ func TestRootNS(t *testing.T) {
21862186
}
21872187

21882188
func TestGoLookupIPCNAMEOrderHostsAliasesFilesOnlyMode(t *testing.T) {
2189-
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
2190-
testHookHostsPath = "testdata/aliases"
2189+
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
2190+
hostsFilePath = "testdata/aliases"
21912191
mode := hostLookupFiles
21922192

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

21982198
func TestGoLookupIPCNAMEOrderHostsAliasesFilesDNSMode(t *testing.T) {
2199-
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
2200-
testHookHostsPath = "testdata/aliases"
2199+
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
2200+
hostsFilePath = "testdata/aliases"
22012201
mode := hostLookupFilesDNS
22022202

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

22152215
func TestGoLookupIPCNAMEOrderHostsAliasesDNSFilesMode(t *testing.T) {
2216-
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
2217-
testHookHostsPath = "testdata/aliases"
2216+
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
2217+
hostsFilePath = "testdata/aliases"
22182218
mode := hostLookupDNSFiles
22192219

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

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

25732573
const testName = "test.invalid"
25742574

src/net/hook.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ var (
1313
// if non-nil, overrides dialTCP.
1414
testHookDialTCP func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error)
1515

16-
testHookHostsPath = "/etc/hosts"
17-
testHookLookupIP = func(
16+
testHookLookupIP = func(
1817
ctx context.Context,
1918
fn func(context.Context, string, string) ([]IPAddr, error),
2019
network string,

src/net/hook_plan9.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package net
6+
7+
var (
8+
hostsFilePath = "/etc/hosts"
9+
)

src/net/hook_unix.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import "syscall"
1111
var (
1212
testHookCanceledDial = func() {} // for golang.org/issue/16523
1313

14+
hostsFilePath = "/etc/hosts"
15+
1416
// Placeholders for socket system calls.
1517
socketFunc func(int, int, int) (int, error) = syscall.Socket
1618
connectFunc func(int, syscall.Sockaddr) error = syscall.Connect

src/net/hook_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
)
1111

1212
var (
13+
hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"
14+
1315
// Placeholders for socket system calls.
1416
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
1517
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect

src/net/hosts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var hosts struct {
5151

5252
func readHosts() {
5353
now := time.Now()
54-
hp := testHookHostsPath
54+
hp := hostsFilePath
5555

5656
if now.Before(hosts.expire) && hosts.path == hp && len(hosts.byName) > 0 {
5757
return

src/net/hosts_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ var lookupStaticHostTests = []struct {
5959
}
6060

6161
func TestLookupStaticHost(t *testing.T) {
62-
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
62+
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
6363

6464
for _, tt := range lookupStaticHostTests {
65-
testHookHostsPath = tt.name
65+
hostsFilePath = tt.name
6666
for _, ent := range tt.ents {
6767
testStaticHost(t, tt.name, ent)
6868
}
@@ -128,10 +128,10 @@ var lookupStaticAddrTests = []struct {
128128
}
129129

130130
func TestLookupStaticAddr(t *testing.T) {
131-
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
131+
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
132132

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

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

166-
testHookHostsPath = "testdata/ipv6-hosts"
166+
hostsFilePath = "testdata/ipv6-hosts"
167167
ent = staticHostEntry{"::1", []string{"localhost"}}
168-
testStaticAddr(t, testHookHostsPath, ent)
168+
testStaticAddr(t, hostsFilePath, ent)
169169
// Modify the hosts return by lookupStaticAddr.
170170
hosts := lookupStaticAddr(ent.in)
171171
for i := range hosts {
172172
hosts[i] += "junk"
173173
}
174-
testStaticAddr(t, testHookHostsPath, ent)
174+
testStaticAddr(t, hostsFilePath, ent)
175175
}
176176

177177
var lookupStaticHostAliasesTest = []struct {
@@ -195,9 +195,9 @@ var lookupStaticHostAliasesTest = []struct {
195195
}
196196

197197
func TestLookupStaticHostAliases(t *testing.T) {
198-
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
198+
defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
199199

200-
testHookHostsPath = "testdata/aliases"
200+
hostsFilePath = "testdata/aliases"
201201
for _, ent := range lookupStaticHostAliasesTest {
202202
testLookupStaticHostAliases(t, ent.lookup, absDomainName(ent.res))
203203
}

src/runtime/os_windows.go

+5
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ func initSysDirectory() {
243243
sysDirectoryLen = l + 1
244244
}
245245

246+
//go:linkname windows_GetSystemDirectory internal/syscall/windows.GetSystemDirectory
247+
func windows_GetSystemDirectory() string {
248+
return unsafe.String(&sysDirectory[0], sysDirectoryLen)
249+
}
250+
246251
func windowsLoadSystemLib(name []uint16) uintptr {
247252
return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
248253
}

0 commit comments

Comments
 (0)