Skip to content

Commit 8b0ba61

Browse files
stephensmalleybrauner
authored andcommitted
fs/xattr.c: fix simple_xattr_list to always include security.* xattrs
The vfs has long had a fallback to obtain the security.* xattrs from the LSM when the filesystem does not implement its own listxattr, but shmem/tmpfs and kernfs later gained their own xattr handlers to support other xattrs. Unfortunately, as a side effect, tmpfs and kernfs-based filesystems like sysfs no longer return the synthetic security.* xattr names via listxattr unless they are explicitly set by userspace or initially set upon inode creation after policy load. coreutils has recently switched from unconditionally invoking getxattr for security.* for ls -Z via libselinux to only doing so if listxattr returns the xattr name, breaking ls -Z of such inodes. Before: $ getfattr -m.* /run/initramfs <no output> $ getfattr -m.* /sys/kernel/fscaps <no output> $ setfattr -n user.foo /run/initramfs $ getfattr -m.* /run/initramfs user.foo After: $ getfattr -m.* /run/initramfs security.selinux $ getfattr -m.* /sys/kernel/fscaps security.selinux $ setfattr -n user.foo /run/initramfs $ getfattr -m.* /run/initramfs security.selinux user.foo Link: https://lore.kernel.org/selinux/CAFqZXNtF8wDyQajPCdGn=iOawX4y77ph0EcfcqcUUj+T87FKyA@mail.gmail.com/ Link: https://lore.kernel.org/selinux/[email protected]/ Signed-off-by: Stephen Smalley <[email protected]> Link: https://lore.kernel.org/[email protected] Fixes: b09e0fa ("tmpfs: implement generic xattr support") Signed-off-by: Christian Brauner <[email protected]>
1 parent b443265 commit 8b0ba61

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

fs/xattr.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,15 @@ static bool xattr_is_trusted(const char *name)
14281428
return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
14291429
}
14301430

1431+
static bool xattr_is_maclabel(const char *name)
1432+
{
1433+
const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
1434+
1435+
return !strncmp(name, XATTR_SECURITY_PREFIX,
1436+
XATTR_SECURITY_PREFIX_LEN) &&
1437+
security_ismaclabel(suffix);
1438+
}
1439+
14311440
/**
14321441
* simple_xattr_list - list all xattr objects
14331442
* @inode: inode from which to get the xattrs
@@ -1460,6 +1469,17 @@ ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
14601469
if (err)
14611470
return err;
14621471

1472+
err = security_inode_listsecurity(inode, buffer, remaining_size);
1473+
if (err < 0)
1474+
return err;
1475+
1476+
if (buffer) {
1477+
if (remaining_size < err)
1478+
return -ERANGE;
1479+
buffer += err;
1480+
}
1481+
remaining_size -= err;
1482+
14631483
read_lock(&xattrs->lock);
14641484
for (rbp = rb_first(&xattrs->rb_root); rbp; rbp = rb_next(rbp)) {
14651485
xattr = rb_entry(rbp, struct simple_xattr, rb_node);
@@ -1468,6 +1488,10 @@ ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
14681488
if (!trusted && xattr_is_trusted(xattr->name))
14691489
continue;
14701490

1491+
/* skip MAC labels; these are provided by LSM above */
1492+
if (xattr_is_maclabel(xattr->name))
1493+
continue;
1494+
14711495
err = xattr_list_one(&buffer, &remaining_size, xattr->name);
14721496
if (err)
14731497
break;

0 commit comments

Comments
 (0)