-
Notifications
You must be signed in to change notification settings - Fork 1
Add override_stat xattr support for virtiofs mounts #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build darwin || linux | ||
|
|
||
| package xattr | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // SetOverrideStatTree walks root and sets user.containers.override_stat | ||
| // on every file and directory. Each entry's real mode (from Lstat) is | ||
| // preserved in the xattr value. Symlinks are skipped — they cannot carry | ||
| // user.* xattrs on Linux, and skipping them prevents setting xattrs | ||
| // outside the mount boundary via symlink traversal. | ||
| // | ||
| // The root path is resolved via [filepath.EvalSymlinks] before walking, | ||
| // and every visited entry is verified to remain under the resolved root. | ||
| // | ||
| // Errors on individual entries are logged at debug level and skipped. | ||
| // Returns an error only if the root itself cannot be accessed. | ||
| // | ||
| // On platforms other than macOS and Linux a no-op stub is provided. | ||
| func SetOverrideStatTree(root string, uid, gid int) error { | ||
| if _, err := os.Lstat(root); err != nil { | ||
| return fmt.Errorf("access root %s: %w", root, err) | ||
| } | ||
|
|
||
| realRoot, err := filepath.EvalSymlinks(root) | ||
| if err != nil { | ||
| return fmt.Errorf("resolve root: %w", err) | ||
| } | ||
| realRoot = filepath.Clean(realRoot) | ||
| rootPrefix := realRoot + string(filepath.Separator) | ||
|
|
||
| return filepath.WalkDir(realRoot, func(path string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return nil // best-effort, skip inaccessible entries | ||
| } | ||
| // Skip symlinks: prevents setting xattrs outside mount boundary, | ||
| // and Linux rejects user.* xattrs on symlinks anyway. | ||
| if d.Type()&fs.ModeSymlink != 0 { | ||
| return nil | ||
| } | ||
| // Boundary check: verify path stays under resolved root. | ||
| cleanPath := filepath.Clean(path) | ||
JAORMX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if cleanPath != realRoot && !strings.HasPrefix(cleanPath, rootPrefix) { | ||
| if d.IsDir() { | ||
| return fs.SkipDir | ||
| } | ||
| return nil | ||
| } | ||
| info, err := d.Info() | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| SetOverrideStat(path, uid, gid, info.Mode()) | ||
| return nil | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build !darwin && !linux | ||
|
|
||
| package xattr | ||
|
|
||
| // SetOverrideStatTree is a no-op on platforms without xattr support. | ||
| func SetOverrideStatTree(_ string, _, _ int) error { return nil } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //go:build darwin || linux | ||
|
|
||
| package xattr | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func TestSetOverrideStatTree_NestedTree(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| sub1 := filepath.Join(root, "a") | ||
| sub2 := filepath.Join(root, "a", "b") | ||
| require.NoError(t, os.MkdirAll(sub2, 0o755)) | ||
|
|
||
| // Create a regular file — it should also get the xattr. | ||
| filePath := filepath.Join(sub1, "file.txt") | ||
| require.NoError(t, os.WriteFile(filePath, []byte("hi"), 0o644)) | ||
|
|
||
| require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) | ||
|
|
||
| // All directories should have the xattr set. | ||
| for _, dir := range []string{root, sub1, sub2} { | ||
| val := readXattrOpt(t, dir) | ||
| assert.Contains(t, val, "1000:1000:", "dir %s should have override xattr", dir) | ||
| } | ||
|
|
||
| // Regular files should also have the xattr set. | ||
| val := readXattrOpt(t, filePath) | ||
| assert.Contains(t, val, "1000:1000:", "file should have override xattr") | ||
| } | ||
|
|
||
| func TestSetOverrideStatTree_SymlinkToExternalDir(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| external := t.TempDir() | ||
| externalSub := filepath.Join(external, "secret") | ||
| require.NoError(t, os.Mkdir(externalSub, 0o755)) | ||
|
|
||
| // Create a symlink inside root pointing to an external directory. | ||
| require.NoError(t, os.Symlink(external, filepath.Join(root, "escape"))) | ||
|
|
||
| require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) | ||
|
|
||
| // The external directory must NOT have the xattr set. | ||
| _, err := unix.Lgetxattr(external, overrideKey, make([]byte, 256)) | ||
| assert.Error(t, err, "external dir should not have override xattr") | ||
| _, err = unix.Lgetxattr(externalSub, overrideKey, make([]byte, 256)) | ||
| assert.Error(t, err, "external subdir should not have override xattr") | ||
| } | ||
|
|
||
| func TestSetOverrideStatTree_SymlinkToFile(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| target := filepath.Join(root, "real.txt") | ||
| require.NoError(t, os.WriteFile(target, []byte("data"), 0o644)) | ||
| require.NoError(t, os.Symlink(target, filepath.Join(root, "link.txt"))) | ||
|
|
||
| require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) | ||
|
|
||
| // The real file gets the xattr (it's a regular file under root). | ||
| val := readXattrOpt(t, target) | ||
| assert.Contains(t, val, "1000:1000:", "real file should have override xattr") | ||
|
|
||
| // The symlink itself should NOT have the xattr. | ||
| link := filepath.Join(root, "link.txt") | ||
| _, err := unix.Lgetxattr(link, overrideKey, make([]byte, 256)) | ||
| assert.Error(t, err, "symlink should not have override xattr") | ||
| } | ||
|
|
||
| func TestSetOverrideStatTree_InaccessibleRoot(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := SetOverrideStatTree("/nonexistent/path/xattr-test", 1000, 1000) | ||
| assert.Error(t, err, "should fail on inaccessible root") | ||
| } | ||
|
|
||
| func TestSetOverrideStatTree_EmptyDir(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| require.NoError(t, SetOverrideStatTree(root, 1000, 1000)) | ||
|
|
||
| // Root dir itself should have the xattr. | ||
| val := readXattrOpt(t, root) | ||
| assert.Contains(t, val, "1000:1000:", "root dir should have override xattr") | ||
| } | ||
|
|
||
| func TestSetOverrideStatTree_RootIsSymlink(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| real := t.TempDir() | ||
| sub := filepath.Join(real, "child") | ||
| require.NoError(t, os.Mkdir(sub, 0o755)) | ||
|
|
||
| // Create a symlink that points to real. The walk should resolve it | ||
| // and set xattrs on the real directory tree. | ||
| link := filepath.Join(t.TempDir(), "link") | ||
| require.NoError(t, os.Symlink(real, link)) | ||
|
|
||
| require.NoError(t, SetOverrideStatTree(link, 1000, 1000)) | ||
|
|
||
| val := readXattrOpt(t, real) | ||
| assert.Contains(t, val, "1000:1000:", "resolved root should have override xattr") | ||
| val = readXattrOpt(t, sub) | ||
| assert.Contains(t, val, "1000:1000:", "child dir should have override xattr") | ||
| } | ||
|
|
||
| func TestSetOverrideStatTree_DifferentUIDGID(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| filePath := filepath.Join(root, "file.txt") | ||
| require.NoError(t, os.WriteFile(filePath, []byte("data"), 0o644)) | ||
|
|
||
| // Use different UID and GID to verify both are written independently. | ||
| require.NoError(t, SetOverrideStatTree(root, 1000, 2000)) | ||
|
|
||
| val := readXattrOpt(t, root) | ||
| assert.Contains(t, val, "1000:2000:", "dir should have uid=1000 gid=2000") | ||
|
|
||
| val = readXattrOpt(t, filePath) | ||
| assert.Contains(t, val, "1000:2000:", "file should have uid=1000 gid=2000") | ||
| } | ||
|
|
||
| // readXattrOpt reads the override_stat xattr and returns its value, or | ||
| // empty string if the xattr is not set. | ||
| func readXattrOpt(t *testing.T, path string) string { | ||
| t.Helper() | ||
| buf := make([]byte, 256) | ||
| n, err := unix.Lgetxattr(path, overrideKey, buf) | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| return string(buf[:n]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.