From 40c6ab6d538aed459c930dd293cac2e0513de811 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Fri, 27 Feb 2026 21:08:44 +0000 Subject: [PATCH] runtime/v2: avoid symlink for bundle work dir on Windows Creating symlinks on Windows requires administrator privileges or Developer Mode, so NewBundle fails with "A required privilege is not held by the client" when containerd creates a task without them. Replace the direct os.Symlink and os.Readlink calls with platform specific helpers. On non-Windows platforms they keep the existing symlink behaviour. On Windows, createWorkLink creates a directory junction using go-winio's reparse point encoding, which does not need elevated privileges. os.Readlink resolves both symlinks and junctions, so resolveWorkLink uses it on both platforms. createWorkLink closes the directory handle before removing the half-created junction directory on failure. The handle is opened without sharing, so removing the directory while it is still open would fail. Signed-off-by: Craig Gumbley Signed-off-by: Eric Curtin Assisted-by: OpenCode (anthropic/claude-sonnet-5-5) --- core/runtime/v2/bundle.go | 6 +- core/runtime/v2/bundle_link_unix.go | 35 ++++++++ core/runtime/v2/bundle_link_windows.go | 94 +++++++++++++++++++++ core/runtime/v2/bundle_link_windows_test.go | 55 ++++++++++++ 4 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 core/runtime/v2/bundle_link_unix.go create mode 100644 core/runtime/v2/bundle_link_windows.go create mode 100644 core/runtime/v2/bundle_link_windows_test.go diff --git a/core/runtime/v2/bundle.go b/core/runtime/v2/bundle.go index 5f50325957..81ae69b6bd 100644 --- a/core/runtime/v2/bundle.go +++ b/core/runtime/v2/bundle.go @@ -102,8 +102,8 @@ func NewBundle(ctx context.Context, root, state, id string, spec typeurl.Any) (b } } paths = append(paths, work) - // symlink workdir - if err := os.Symlink(work, filepath.Join(b.Path, "work")); err != nil { + // create work link (symlink on unix, junction on windows) + if err := createWorkLink(work, b.Path); err != nil { return nil, err } // Spec may be nil for some sandboxers that do not initialize the spec, for @@ -144,7 +144,7 @@ type Bundle struct { // Delete a bundle atomically func (b *Bundle) Delete() error { - work, werr := os.Readlink(filepath.Join(b.Path, "work")) + work, werr := resolveWorkLink(b.Path) rootfs := filepath.Join(b.Path, "rootfs") if err := mount.UnmountRecursive(rootfs, 0); err != nil { return fmt.Errorf("unmount rootfs %s: %w", rootfs, err) diff --git a/core/runtime/v2/bundle_link_unix.go b/core/runtime/v2/bundle_link_unix.go new file mode 100644 index 0000000000..b67bf144f3 --- /dev/null +++ b/core/runtime/v2/bundle_link_unix.go @@ -0,0 +1,35 @@ +//go:build !windows + +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package v2 + +import ( + "os" + "path/filepath" +) + +// createWorkLink creates a symlink from bundlePath/work -> work. +func createWorkLink(work, bundlePath string) error { + return os.Symlink(work, filepath.Join(bundlePath, "work")) +} + +// resolveWorkLink reads the symlink at bundlePath/work to recover the +// working directory path. +func resolveWorkLink(bundlePath string) (string, error) { + return os.Readlink(filepath.Join(bundlePath, "work")) +} diff --git a/core/runtime/v2/bundle_link_windows.go b/core/runtime/v2/bundle_link_windows.go new file mode 100644 index 0000000000..03085ae5e3 --- /dev/null +++ b/core/runtime/v2/bundle_link_windows.go @@ -0,0 +1,94 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package v2 + +import ( + "os" + "path/filepath" + + winio "github.com/Microsoft/go-winio" + "golang.org/x/sys/windows" +) + +// createWorkLink creates a directory junction from bundlePath/work -> work. +// Junctions do not require administrator privileges or Developer Mode, +// unlike symlinks on Windows. +func createWorkLink(work, bundlePath string) error { + target, err := filepath.Abs(work) + if err != nil { + return err + } + + link := filepath.Join(bundlePath, "work") + if err := os.Mkdir(link, 0711); err != nil { + return err + } + + linkPtr, err := windows.UTF16PtrFromString(link) + if err != nil { + os.Remove(link) + return err + } + + handle, err := windows.CreateFile( + linkPtr, + windows.GENERIC_WRITE, + 0, + nil, + windows.OPEN_EXISTING, + windows.FILE_FLAG_BACKUP_SEMANTICS, + 0, + ) + if err != nil { + os.Remove(link) + return err + } + + rp := winio.ReparsePoint{ + Target: target, + IsMountPoint: true, + } + data := winio.EncodeReparsePoint(&rp) + + var bytesReturned uint32 + err = windows.DeviceIoControl( + handle, + windows.FSCTL_SET_REPARSE_POINT, + &data[0], + uint32(len(data)), + nil, + 0, + &bytesReturned, + nil, + ) + // The handle is opened without sharing, so it must be closed before the + // directory can be removed on failure. + windows.CloseHandle(handle) + if err != nil { + os.Remove(link) + return err + } + + return nil +} + +// resolveWorkLink reads the junction/symlink at bundlePath/work to recover +// the working directory path. os.Readlink works for both symlinks and +// junctions on Windows. +func resolveWorkLink(bundlePath string) (string, error) { + return os.Readlink(filepath.Join(bundlePath, "work")) +} diff --git a/core/runtime/v2/bundle_link_windows_test.go b/core/runtime/v2/bundle_link_windows_test.go new file mode 100644 index 0000000000..3398989b4a --- /dev/null +++ b/core/runtime/v2/bundle_link_windows_test.go @@ -0,0 +1,55 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package v2 + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCreateWorkLinkJunction(t *testing.T) { + dir := t.TempDir() + + work := filepath.Join(dir, "root", "default", "test-id") + bundlePath := filepath.Join(dir, "state", "default", "test-id") + + require.NoError(t, os.MkdirAll(work, 0711), "failed to create work dir") + require.NoError(t, os.MkdirAll(bundlePath, 0700), "failed to create bundle dir") + + // Create the junction + require.NoError(t, createWorkLink(work, bundlePath), "failed to create work link") + + // Verify the junction target resolves back to work + resolved, err := resolveWorkLink(bundlePath) + require.NoError(t, err, "failed to resolve work link") + + absWork, err := filepath.Abs(work) + require.NoError(t, err, "failed to get absolute path") + assert.Equal(t, absWork, resolved, "resolved work link should match absolute work path") + + // Write a file through the junction and read from the target + testContent := []byte("hello") + require.NoError(t, os.WriteFile(filepath.Join(bundlePath, "work", "testfile"), testContent, 0644), "failed to write through junction") + + got, err := os.ReadFile(filepath.Join(work, "testfile")) + require.NoError(t, err, "failed to read from work dir") + assert.Equal(t, testContent, got, "content written through junction should be readable from target") +}