Skip to content

Commit c41e8bf

Browse files
committed
merge #609 into opencontainers/umoci:main
Aleksa Sarai (1): tests: fix failures on SELinux-enabled systems LGTMs: cyphar
2 parents bca0881 + df3a23e commit c41e8bf

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

internal/system/xattr_unix_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ import (
2828
"golang.org/x/sys/unix"
2929
)
3030

31+
// specialXattrs come from the OCI xattr remapping logic in oci/layer/xattr.go.
32+
// These are xattrs that will be auto-set by the system and we should ignore
33+
// their existence when doing xattr-related operations (for our tests, this
34+
// especially means to take care of them when checking sets of xattrs that
35+
// exist on the filesystem).
36+
var specialXattrs = map[string]struct{}{
37+
"security.selinux": {},
38+
"system.nfs4_acl": {},
39+
}
40+
3141
func TestClearxattrFilter(t *testing.T) {
3242
dir := t.TempDir()
3343

@@ -38,6 +48,12 @@ func TestClearxattrFilter(t *testing.T) {
3848
path := file.Name()
3949
defer os.RemoveAll(path) //nolint:errcheck
4050

51+
autosetXattrs, err := Llistxattr(path)
52+
require.NoErrorf(t, err, "llistxattr %q", path)
53+
for _, xattr := range autosetXattrs {
54+
require.Contains(t, specialXattrs, xattr, "auto-set xattrs must be part of special list")
55+
}
56+
4157
xattrs := []struct {
4258
name, value string
4359
forbidden bool
@@ -65,11 +81,16 @@ func TestClearxattrFilter(t *testing.T) {
6581
}
6682
require.NoErrorf(t, err, "lsetxattr %q=%q on %q", xattr.name, xattr.value, path)
6783
}
84+
// If we are running on an SELinux-enabled system, all new files get a
85+
// security.selinux xattr that gets auto-set and cannot be removed so we
86+
// need to include it in the expected set.
87+
expectAllXattrNames := append(setXattrNames, autosetXattrs...)
88+
expectRemainingXattrNames := append(forbiddenXattrNames, autosetXattrs...)
6889

6990
// Check they're all present.
7091
allXattrList, err := Llistxattr(path)
7192
require.NoErrorf(t, err, "llistxattr %q", path)
72-
assert.ElementsMatch(t, setXattrNames, allXattrList, "all xattrs should be present after setting")
93+
assert.ElementsMatch(t, expectAllXattrNames, allXattrList, "all xattrs should be present after setting")
7394

7495
// Now clear them.
7596
err = Lclearxattrs(path, func(xattrName string) bool {
@@ -79,9 +100,9 @@ func TestClearxattrFilter(t *testing.T) {
79100
require.NoErrorf(t, err, "lclearxattrs %q (forbidden=%v)", path, forbiddenXattrs)
80101

81102
// Check that only the forbidden ones remain.
82-
forbiddenXattrList, err := Llistxattr(path)
103+
remainingXattrList, err := Llistxattr(path)
83104
require.NoErrorf(t, err, "llistxattr %q", path)
84-
assert.NotElementsMatch(t, setXattrNames, forbiddenXattrList, "there should be a different set of xattrs after clearing")
85-
assert.ElementsMatch(t, forbiddenXattrNames, forbiddenXattrList, "only explicitly forbidden xattrs should be allowed to remain after clearing")
86-
assert.NotEmpty(t, forbiddenXattrList, "there should be some remaining xattrs after clearing")
105+
assert.NotElementsMatch(t, expectAllXattrNames, remainingXattrList, "there should be a different set of xattrs after clearing")
106+
assert.ElementsMatch(t, expectRemainingXattrNames, remainingXattrList, "only explicitly forbidden xattrs should be allowed to remain after clearing")
107+
assert.NotEmpty(t, remainingXattrList, "there should be some remaining xattrs after clearing")
87108
}

oci/layer/xattr_linux_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ import (
3737
"github.com/opencontainers/umoci/pkg/fseval"
3838
)
3939

40+
func isForbiddenXattr(xattrName string) bool {
41+
found, ok := getXattrFilter(xattrName)
42+
return ok && found == forbiddenXattrFilter{}
43+
}
44+
4045
func getAllXattrs(t *testing.T, path string) map[string]string {
4146
names, err := system.Llistxattr(path)
4247
require.NoErrorf(t, err, "fetch all xattrs for %q", path)
@@ -201,6 +206,14 @@ func testUnpackGenerateRoundTrip_ComplexXattr_OverlayfsRootfs(t *testing.T, tarX
201206
fullPath := filepath.Join(dir, path)
202207

203208
xattrs := getAllXattrs(t, fullPath)
209+
// Strip out any forbidden xattrs from the returned set.
210+
// TODO: This is a little ugly -- ideally we would create
211+
// copies of the necessary maps with special xattrs added.
212+
for name := range xattrs {
213+
if isForbiddenXattr(name) {
214+
delete(xattrs, name)
215+
}
216+
}
204217

205218
if test.expectRemap {
206219
assert.Equalf(t, de.remapXattrs, xattrs, "UnpackEntry(%q): expected to see %#v remapped properly", path, de.xattrs)
@@ -212,7 +225,6 @@ func testUnpackGenerateRoundTrip_ComplexXattr_OverlayfsRootfs(t *testing.T, tarX
212225
require.NoErrorf(t, err, "isOverlayWhiteout(%q)", path)
213226
assert.Falsef(t, isWo, "isOverlayWhiteout(%q): regular entries with overlayfs xattrs should not end up being unpacked with overlayfs whiteout xattrs", path)
214227
} else {
215-
xattrs := getAllXattrs(t, fullPath)
216228
assert.Equalf(t, de.xattrs, xattrs, "UnpackEntry(%q): expected to see %#v not be remapped", path, de.xattrs)
217229
assert.NotEqualf(t, de.remapXattrs, xattrs, "UnpackEntry(%q): expected to see %#v not be remapped", path, de.xattrs)
218230
}
@@ -348,6 +360,14 @@ func TestUnpackGenerateRoundTrip_MockedSELinux(t *testing.T) {
348360
if value, ok := de.autoXattrs[forbiddenTestXattr]; ok {
349361
wantXattrs[forbiddenTestXattr] = value
350362
}
363+
// If we are running on an SELinux system (or even more
364+
// unlikely, on top of nfsv4) we need to also allow any masked
365+
// xattrs auto-applied by the system.
366+
for gotXattr, value := range xattrs {
367+
if isForbiddenXattr(gotXattr) {
368+
wantXattrs[gotXattr] = value
369+
}
370+
}
351371
assert.Equalf(t, wantXattrs, xattrs, "UnpackEntry(%q): expected to only see specific subset of applied xattrs", path)
352372
}
353373

0 commit comments

Comments
 (0)