diff mbox series

[v2,03/10] fs: tweak fsuidgid_has_mapping()

Message ID 20211130121032.3753852-4-brauner@kernel.org (mailing list archive)
State New, archived
Headers show
Series Extend and tweak mapping support | expand

Commit Message

Christian Brauner Nov. 30, 2021, 12:10 p.m. UTC
From: Christian Brauner <christian.brauner@ubuntu.com>

If the caller's fs{g,u}id aren't mapped in the mount's idmapping we can
return early and skip the check whether the mapped fs{g,u}id also have a
mapping in the filesystem's idmapping. If the fs{g,u}id aren't mapped in
the mount's idmapping they consequently can't be mapped in the
filesystem's idmapping. So there's no point in checking that.

Link: https://lore.kernel.org/r/20211123114227.3124056-4-brauner@kernel.org (v1)
Cc: Seth Forshee <sforshee@digitalocean.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Al Viro <viro@zeniv.linux.org.uk>
CC: linux-fsdevel@vger.kernel.org
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
---
/* v2 */
unchanged
---
 include/linux/fs.h | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

Comments

Seth Forshee (DigitalOcean) Dec. 2, 2021, 5:12 p.m. UTC | #1
On Tue, Nov 30, 2021 at 01:10:25PM +0100, Christian Brauner wrote:
> From: Christian Brauner <christian.brauner@ubuntu.com>
> 
> If the caller's fs{g,u}id aren't mapped in the mount's idmapping we can
> return early and skip the check whether the mapped fs{g,u}id also have a
> mapping in the filesystem's idmapping. If the fs{g,u}id aren't mapped in
> the mount's idmapping they consequently can't be mapped in the
> filesystem's idmapping. So there's no point in checking that.
> 
> Link: https://lore.kernel.org/r/20211123114227.3124056-4-brauner@kernel.org (v1)
> Cc: Seth Forshee <sforshee@digitalocean.com>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Al Viro <viro@zeniv.linux.org.uk>
> CC: linux-fsdevel@vger.kernel.org
> Reviewed-by: Amir Goldstein <amir73il@gmail.com>
> Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>

Reviewed-by: Seth Forshee <sforshee@digitalocean.com>

> ---
> /* v2 */
> unchanged
> ---
>  include/linux/fs.h | 14 +++++++++++---
>  1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index f5d1ae0a783a..28ab20ce0adc 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -1695,10 +1695,18 @@ static inline void inode_fsgid_set(struct inode *inode,
>  static inline bool fsuidgid_has_mapping(struct super_block *sb,
>  					struct user_namespace *mnt_userns)
>  {
> -	struct user_namespace *s_user_ns = sb->s_user_ns;
> +	struct user_namespace *fs_userns = sb->s_user_ns;
> +	kuid_t kuid;
> +	kgid_t kgid;
>  
> -	return kuid_has_mapping(s_user_ns, mapped_fsuid(mnt_userns)) &&
> -	       kgid_has_mapping(s_user_ns, mapped_fsgid(mnt_userns));
> +	kuid = mapped_fsuid(mnt_userns);
> +	if (!uid_valid(kuid))
> +		return false;
> +	kgid = mapped_fsgid(mnt_userns);
> +	if (!gid_valid(kgid))
> +		return false;
> +	return kuid_has_mapping(fs_userns, kuid) &&
> +	       kgid_has_mapping(fs_userns, kgid);
>  }
>  
>  extern struct timespec64 current_time(struct inode *inode);
> -- 
> 2.30.2
>
diff mbox series

Patch

diff --git a/include/linux/fs.h b/include/linux/fs.h
index f5d1ae0a783a..28ab20ce0adc 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1695,10 +1695,18 @@  static inline void inode_fsgid_set(struct inode *inode,
 static inline bool fsuidgid_has_mapping(struct super_block *sb,
 					struct user_namespace *mnt_userns)
 {
-	struct user_namespace *s_user_ns = sb->s_user_ns;
+	struct user_namespace *fs_userns = sb->s_user_ns;
+	kuid_t kuid;
+	kgid_t kgid;
 
-	return kuid_has_mapping(s_user_ns, mapped_fsuid(mnt_userns)) &&
-	       kgid_has_mapping(s_user_ns, mapped_fsgid(mnt_userns));
+	kuid = mapped_fsuid(mnt_userns);
+	if (!uid_valid(kuid))
+		return false;
+	kgid = mapped_fsgid(mnt_userns);
+	if (!gid_valid(kgid))
+		return false;
+	return kuid_has_mapping(fs_userns, kuid) &&
+	       kgid_has_mapping(fs_userns, kgid);
 }
 
 extern struct timespec64 current_time(struct inode *inode);