[PATCH 08/14] xattr: switch xattr_permission() to switch statement

Christian Brauner posted 14 patches 1 month ago
[PATCH 08/14] xattr: switch xattr_permission() to switch statement
Posted by Christian Brauner 1 month ago
Simplify the codeflow by using a switch statement that switches on
S_IFMT.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/xattr.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index c4db8663c32e..328ed7558dfc 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -152,12 +152,20 @@ xattr_permission(struct mnt_idmap *idmap, struct inode *inode,
 	 * privileged users can write attributes.
 	 */
 	if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
-		if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
-			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))
+		switch (inode->i_mode & S_IFMT) {
+		case S_IFREG:
+			break;
+		case S_IFDIR:
+			if (!(inode->i_mode & S_ISVTX))
+				break;
+			if (!(mask & MAY_WRITE))
+				break;
+			if (inode_owner_or_capable(idmap, inode))
+				break;
 			return -EPERM;
+		default:
+			return xattr_permission_error(mask);
+		}
 	}
 
 	return inode_permission(idmap, inode, mask);

-- 
2.47.3
Re: [PATCH 08/14] xattr: switch xattr_permission() to switch statement
Posted by Jan Kara 2 weeks, 5 days ago
On Mon 16-02-26 14:32:04, Christian Brauner wrote:
> Simplify the codeflow by using a switch statement that switches on
> S_IFMT.
> 
> Signed-off-by: Christian Brauner <brauner@kernel.org>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/xattr.c | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/xattr.c b/fs/xattr.c
> index c4db8663c32e..328ed7558dfc 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -152,12 +152,20 @@ xattr_permission(struct mnt_idmap *idmap, struct inode *inode,
>  	 * privileged users can write attributes.
>  	 */
>  	if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
> -		if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
> -			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))
> +		switch (inode->i_mode & S_IFMT) {
> +		case S_IFREG:
> +			break;
> +		case S_IFDIR:
> +			if (!(inode->i_mode & S_ISVTX))
> +				break;
> +			if (!(mask & MAY_WRITE))
> +				break;
> +			if (inode_owner_or_capable(idmap, inode))
> +				break;
>  			return -EPERM;
> +		default:
> +			return xattr_permission_error(mask);
> +		}
>  	}
>  
>  	return inode_permission(idmap, inode, mask);
> 
> -- 
> 2.47.3
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR