]> Cypherpunks repositories - gostls13.git/commitdiff
net: respect hosts file when resolving names for Windows
authorNikita Vaniasin <nikita.vanyasin@gmail.com>
Mon, 28 Aug 2023 13:26:32 +0000 (15:26 +0200)
committerQuim Muntal <quimmuntal@gmail.com>
Mon, 4 Sep 2023 07:21:38 +0000 (07:21 +0000)
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>
src/internal/syscall/windows/security_windows.go
src/net/conf.go
src/net/dnsclient_unix_test.go
src/net/hook.go
src/net/hook_plan9.go [new file with mode: 0644]
src/net/hook_unix.go
src/net/hook_windows.go
src/net/hosts.go
src/net/hosts_test.go
src/runtime/os_windows.go

index 4a2dfc0c73393fa86acf948a486ed2db50c19cd6..c8c5cbed74736008a5acae01cef679ef23e30a89 100644 (file)
@@ -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.
index ff3ec20c8a83a650a9e775dcce2a80d9e9eb38d0..08c2e7e33d5e2d89fb1a64464acdc9a823ffcd4c 100644 (file)
@@ -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.
index 8d50d8dee0659b53fab6590e2cf07ffb09e0bb2a..0da36303cc8887cfbd70f10b0b55f7624cc24fc7 100644 (file)
@@ -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)
@@ -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
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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)
@@ -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"
 
index 35c660b4a346e44787d899788da5b43f5aa72502..eded34d48abe4e8d0f91ed5e133097572423537d 100644 (file)
@@ -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,
diff --git a/src/net/hook_plan9.go b/src/net/hook_plan9.go
new file mode 100644 (file)
index 0000000..6020d32
--- /dev/null
@@ -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"
+)
index 89cb404c835a8fe5ddb557943890562c38a59da8..bad92939b94fb041dee8cf573caf3428729199d0 100644 (file)
@@ -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
index 946e1efa8df45bd47f98ed4ff2111768c7fe70fa..f7c5b5af90fe354f9d4238dade86aa0e08481e35 100644 (file)
@@ -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
index 56e667414446507818a591c6a02e11b3bec28755..73e6fcc7a4ae7dfbab02a44d6b4fb0e531beb8c9 100644 (file)
@@ -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
index b3f189e641373a7eca430db401cd0db9772365c7..5f22920765bd7678c385e84975425500319e4951 100644 (file)
@@ -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)
                }
@@ -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)
                }
@@ -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 {
@@ -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))
        }
index 6686a9053476d3edd032dda22b2c2cee802fc6d2..081a4a23d04b3eda372f70ad878efae9f93cd5a6 100644 (file)
@@ -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)
 }