Skip to content

Commit f52d69d

Browse files
committed
Safer use of filepath.EvalSymlinks() on Windows
The behavior of function `path/filepath.EvalSymlinks()` has changed in Go v1.23: - https://go-review.googlesource.com/c/go/+/565136 - https://go.dev/doc/go1.23#minor_library_changes - https://tip.golang.org/doc/godebug As a consequences, starting with Podman 5.3.0, when installing on Windows (WSL) using scoop, Podman fails to start because it fails to find helper binaries. Scoop copies Podman binaries in a folder of type Junction and `EvalSymlinks` returns an error. The problem is described in #24557. To address this problem we are checking if a path is a `Symlink` before calling `EvalSymlinks` and, if it's not (hardlinks, mount points or canonical files), we are calling `path/filepath.Clean` for consistency. In fact `path/filepath.EvalSymlinks`, after evaluating a symlink target, calls `Clean` too. Signed-off-by: Mario Loriedo <[email protected]>
1 parent eea2866 commit f52d69d

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

pkg/machine/machine_windows.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,36 @@ func FindExecutablePeer(name string) (string, error) {
251251
return "", err
252252
}
253253

254-
exe, err = filepath.EvalSymlinks(exe)
254+
exe, err = EvalSymlinksOrClean(exe)
255255
if err != nil {
256256
return "", err
257257
}
258258

259259
return filepath.Join(filepath.Dir(exe), name), nil
260260
}
261261

262+
func EvalSymlinksOrClean(filePath string) (string, error) {
263+
fileInfo, err := os.Lstat(filePath)
264+
if err != nil {
265+
return "", err
266+
}
267+
if fileInfo.Mode()&fs.ModeSymlink != 0 {
268+
// Only call filepath.EvalSymlinks if it is a symlink.
269+
// Starting with v1.23, EvalSymlinks returns an error for mount points.
270+
// See https://go-review.googlesource.com/c/go/+/565136 for reference.
271+
filePath, err = filepath.EvalSymlinks(filePath)
272+
if err != nil {
273+
return "", err
274+
}
275+
} else {
276+
// Call filepath.Clean when filePath is not a symlink. That's for
277+
// consistency with the symlink case (filepath.EvalSymlinks calls
278+
// Clean after evaluating filePath).
279+
filePath = filepath.Clean(filePath)
280+
}
281+
return filePath, nil
282+
}
283+
262284
func GetWinProxyStateDir(name string, vmtype define.VMType) (string, error) {
263285
dir, err := env.GetDataDir(vmtype)
264286
if err != nil {

pkg/machine/machine_windows_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//go:build windows
2+
3+
package machine
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
// CreateNewItemWithPowerShell creates a new item using PowerShell.
16+
// It's an helper to easily create junctions on Windows (as well as other file types).
17+
// It constructs a PowerShell command to create a new item at the specified path with the given item type.
18+
// If a target is provided, it includes it in the command.
19+
//
20+
// Parameters:
21+
// - t: The testing.T instance.
22+
// - path: The path where the new item will be created.
23+
// - itemType: The type of the item to be created (e.g., "File", "SymbolicLink", "Junction").
24+
// - target: The target for the new item, if applicable.
25+
func CreateNewItemWithPowerShell(t *testing.T, path string, itemType string, target string) {
26+
var pwshCmd string
27+
pwshCmd = "New-Item -Path " + path + " -ItemType " + itemType
28+
if target != "" {
29+
pwshCmd += " -Target " + target
30+
}
31+
cmd := exec.Command("pwsh", "-Command", pwshCmd)
32+
cmd.Stdout = os.Stdout
33+
cmd.Stderr = os.Stderr
34+
err := cmd.Run()
35+
require.NoError(t, err)
36+
}
37+
38+
// TestEvalSymlinksOrClean tests the EvalSymlinksOrClean function.
39+
// In particular it verifies that EvalSymlinksOrClean behaves as
40+
// filepath.EvalSymlink before Go 1.23 - with the exception of
41+
// files under a mount point (juntion) that aren't resolved
42+
// anymore.
43+
// The old behavior of filepath.EvalSymlinks can be tested with
44+
// the directive "//go:debug winsymlink=0" and replacing EvalSymlinksOrClean()
45+
// with filepath.EvalSymlink().
46+
func TestEvalSymlinksOrClean(t *testing.T) {
47+
// Create a temporary directory to store the normal file
48+
normalFileDir := t.TempDir()
49+
50+
// Create a temporary directory to store the (hard/sym)link files
51+
linkFilesDir := t.TempDir()
52+
53+
// Create a temporary directory where the mount point will be created
54+
mountPointDir := t.TempDir()
55+
56+
// Create a normal file
57+
normalFile := filepath.Join(normalFileDir, "testFile")
58+
CreateNewItemWithPowerShell(t, normalFile, "File", "")
59+
60+
// Create a symlink file
61+
symlinkFile := filepath.Join(linkFilesDir, "testSymbolicLink")
62+
CreateNewItemWithPowerShell(t, symlinkFile, "SymbolicLink", normalFile)
63+
64+
// Create a hardlink file
65+
hardlinkFile := filepath.Join(linkFilesDir, "testHardLink")
66+
CreateNewItemWithPowerShell(t, hardlinkFile, "HardLink", normalFile)
67+
68+
// Create a mount point file
69+
mountPoint := filepath.Join(mountPointDir, "testJunction")
70+
mountPointFile := filepath.Join(mountPoint, "testFile")
71+
CreateNewItemWithPowerShell(t, mountPoint, "Junction", normalFileDir)
72+
73+
// Replaces the backslashes with forward slashes in the normal file path
74+
normalFileWithBadSeparators := filepath.ToSlash(normalFile)
75+
76+
tests := []struct {
77+
name string
78+
filePath string
79+
want string
80+
}{
81+
{
82+
name: "Normal file",
83+
filePath: normalFile,
84+
want: normalFile,
85+
},
86+
{
87+
name: "File under a mount point (juntion)",
88+
filePath: mountPointFile,
89+
want: mountPointFile,
90+
},
91+
{
92+
name: "Symbolic link",
93+
filePath: symlinkFile,
94+
want: normalFile,
95+
},
96+
{
97+
name: "Hard link",
98+
filePath: hardlinkFile,
99+
want: hardlinkFile,
100+
},
101+
{
102+
name: "Bad separators in path",
103+
filePath: normalFileWithBadSeparators,
104+
want: normalFile,
105+
},
106+
}
107+
108+
for _, tt := range tests {
109+
t.Run(tt.name, func(t *testing.T) {
110+
got, err := EvalSymlinksOrClean(tt.filePath)
111+
require.NoError(t, err)
112+
assert.Equal(t, tt.want, got)
113+
})
114+
}
115+
}

0 commit comments

Comments
 (0)