]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
xattr: add xattr_permission_error()
authorChristian Brauner <brauner@kernel.org>
Mon, 16 Feb 2026 13:32:03 +0000 (14:32 +0100)
committerChristian Brauner <brauner@kernel.org>
Mon, 2 Mar 2026 10:06:42 +0000 (11:06 +0100)
Stop repeating the ?: in multiple places and use a simple helper for
this.

Link: https://patch.msgid.link/20260216-work-xattr-socket-v1-7-c2efa4f74cb7@kernel.org
Acked-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/xattr.c

index 64803097e1dc8fd00bb3497a31548e8bafc4d25f..c4db8663c32e07a7d6ae32dabe84c095c8c4eb37 100644 (file)
@@ -106,6 +106,13 @@ int may_write_xattr(struct mnt_idmap *idmap, struct inode *inode)
        return 0;
 }
 
+static inline int xattr_permission_error(int mask)
+{
+       if (mask & MAY_WRITE)
+               return -EPERM;
+       return -ENODATA;
+}
+
 /*
  * Check permissions for extended attribute access.  This is a bit complicated
  * because different namespaces have very different rules.
@@ -135,7 +142,7 @@ xattr_permission(struct mnt_idmap *idmap, struct inode *inode,
         */
        if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
                if (!capable(CAP_SYS_ADMIN))
-                       return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
+                       return xattr_permission_error(mask);
                return 0;
        }
 
@@ -146,7 +153,7 @@ xattr_permission(struct mnt_idmap *idmap, struct inode *inode,
         */
        if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
                if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
-                       return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
+                       return xattr_permission_error(mask);
                if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
                    (mask & MAY_WRITE) &&
                    !inode_owner_or_capable(idmap, inode))