diff mbox series

[v1,2/3] fs/mnt_idmapping: introduce an invalid_mnt_idmap

Message ID 20240906143453.179506-2-aleksandr.mikhalitsyn@canonical.com (mailing list archive)
State New
Headers show
Series [v1,1/3] fs/fuse: introduce and use fuse_simple_idmap_request() helper | expand

Commit Message

Alexander Mikhalitsyn Sept. 6, 2024, 2:34 p.m. UTC
Link: https://lore.kernel.org/linux-fsdevel/20240904-baugrube-erhoben-b3c1c49a2645@brauner/
Suggested-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
---
 fs/mnt_idmapping.c            | 22 ++++++++++++++++++++--
 include/linux/mnt_idmapping.h |  1 +
 2 files changed, 21 insertions(+), 2 deletions(-)

Comments

Christian Brauner Sept. 9, 2024, 12:57 p.m. UTC | #1
On Fri, Sep 06, 2024 at 04:34:52PM GMT, Alexander Mikhalitsyn wrote:
> Link: https://lore.kernel.org/linux-fsdevel/20240904-baugrube-erhoben-b3c1c49a2645@brauner/
> Suggested-by: Christian Brauner <brauner@kernel.org>
> Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> ---
>  fs/mnt_idmapping.c            | 22 ++++++++++++++++++++--
>  include/linux/mnt_idmapping.h |  1 +
>  2 files changed, 21 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/mnt_idmapping.c b/fs/mnt_idmapping.c
> index 3c60f1eaca61..cbca6500848e 100644
> --- a/fs/mnt_idmapping.c
> +++ b/fs/mnt_idmapping.c
> @@ -32,6 +32,15 @@ struct mnt_idmap nop_mnt_idmap = {
>  };
>  EXPORT_SYMBOL_GPL(nop_mnt_idmap);
>  
> +/*
> + * Carries the invalid idmapping of a full 0-4294967295 {g,u}id range.
> + * This means that all {g,u}ids are mapped to INVALID_VFS{G,U}ID.
> + */
> +struct mnt_idmap invalid_mnt_idmap = {
> +	.count	= REFCOUNT_INIT(1),
> +};
> +EXPORT_SYMBOL_GPL(invalid_mnt_idmap);
> +
>  /**
>   * initial_idmapping - check whether this is the initial mapping
>   * @ns: idmapping to check
> @@ -75,6 +84,8 @@ vfsuid_t make_vfsuid(struct mnt_idmap *idmap,
>  
>  	if (idmap == &nop_mnt_idmap)
>  		return VFSUIDT_INIT(kuid);
> +	if (idmap == &invalid_mnt_idmap)
> +		return INVALID_VFSUID;

Could possibly deserve an:

if (unlikely(idmap == &invalid_mnt_idmap))
	return INVALID_VFSUID;

and technically I guess we could also do:

if (likely(idmap == &nop_mnt_idmap))
	return VFSUIDT_INIT(kuid);

but not that relevant for this patch.
Christian Brauner Sept. 9, 2024, 1:01 p.m. UTC | #2
On Mon, Sep 09, 2024 at 02:57:51PM GMT, Christian Brauner wrote:
> On Fri, Sep 06, 2024 at 04:34:52PM GMT, Alexander Mikhalitsyn wrote:
> > Link: https://lore.kernel.org/linux-fsdevel/20240904-baugrube-erhoben-b3c1c49a2645@brauner/
> > Suggested-by: Christian Brauner <brauner@kernel.org>
> > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > ---
> >  fs/mnt_idmapping.c            | 22 ++++++++++++++++++++--
> >  include/linux/mnt_idmapping.h |  1 +
> >  2 files changed, 21 insertions(+), 2 deletions(-)
> > 
> > diff --git a/fs/mnt_idmapping.c b/fs/mnt_idmapping.c
> > index 3c60f1eaca61..cbca6500848e 100644
> > --- a/fs/mnt_idmapping.c
> > +++ b/fs/mnt_idmapping.c
> > @@ -32,6 +32,15 @@ struct mnt_idmap nop_mnt_idmap = {
> >  };
> >  EXPORT_SYMBOL_GPL(nop_mnt_idmap);
> >  
> > +/*
> > + * Carries the invalid idmapping of a full 0-4294967295 {g,u}id range.
> > + * This means that all {g,u}ids are mapped to INVALID_VFS{G,U}ID.
> > + */
> > +struct mnt_idmap invalid_mnt_idmap = {
> > +	.count	= REFCOUNT_INIT(1),
> > +};
> > +EXPORT_SYMBOL_GPL(invalid_mnt_idmap);
> > +
> >  /**
> >   * initial_idmapping - check whether this is the initial mapping
> >   * @ns: idmapping to check
> > @@ -75,6 +84,8 @@ vfsuid_t make_vfsuid(struct mnt_idmap *idmap,
> >  
> >  	if (idmap == &nop_mnt_idmap)
> >  		return VFSUIDT_INIT(kuid);
> > +	if (idmap == &invalid_mnt_idmap)
> > +		return INVALID_VFSUID;
> 
> Could possibly deserve an:
> 
> if (unlikely(idmap == &invalid_mnt_idmap))
> 	return INVALID_VFSUID;
> 
> and technically I guess we could also do:
> 
> if (likely(idmap == &nop_mnt_idmap))
> 	return VFSUIDT_INIT(kuid);
> 
> but not that relevant for this patch.

Forgot:

Reviewed-by: Christian Brauner <brauner@kernel.org>
Alexander Mikhalitsyn Sept. 9, 2024, 4:09 p.m. UTC | #3
On Mon, Sep 9, 2024 at 2:57 PM Christian Brauner <brauner@kernel.org> wrote:
>
> On Fri, Sep 06, 2024 at 04:34:52PM GMT, Alexander Mikhalitsyn wrote:
> > Link: https://lore.kernel.org/linux-fsdevel/20240904-baugrube-erhoben-b3c1c49a2645@brauner/
> > Suggested-by: Christian Brauner <brauner@kernel.org>
> > Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
> > ---
> >  fs/mnt_idmapping.c            | 22 ++++++++++++++++++++--
> >  include/linux/mnt_idmapping.h |  1 +
> >  2 files changed, 21 insertions(+), 2 deletions(-)
> >
> > diff --git a/fs/mnt_idmapping.c b/fs/mnt_idmapping.c
> > index 3c60f1eaca61..cbca6500848e 100644
> > --- a/fs/mnt_idmapping.c
> > +++ b/fs/mnt_idmapping.c
> > @@ -32,6 +32,15 @@ struct mnt_idmap nop_mnt_idmap = {
> >  };
> >  EXPORT_SYMBOL_GPL(nop_mnt_idmap);
> >
> > +/*
> > + * Carries the invalid idmapping of a full 0-4294967295 {g,u}id range.
> > + * This means that all {g,u}ids are mapped to INVALID_VFS{G,U}ID.
> > + */
> > +struct mnt_idmap invalid_mnt_idmap = {
> > +     .count  = REFCOUNT_INIT(1),
> > +};
> > +EXPORT_SYMBOL_GPL(invalid_mnt_idmap);
> > +
> >  /**
> >   * initial_idmapping - check whether this is the initial mapping
> >   * @ns: idmapping to check
> > @@ -75,6 +84,8 @@ vfsuid_t make_vfsuid(struct mnt_idmap *idmap,
> >
> >       if (idmap == &nop_mnt_idmap)
> >               return VFSUIDT_INIT(kuid);
> > +     if (idmap == &invalid_mnt_idmap)
> > +             return INVALID_VFSUID;
>
> Could possibly deserve an:
>
> if (unlikely(idmap == &invalid_mnt_idmap))
>         return INVALID_VFSUID;
>
> and technically I guess we could also do:
>
> if (likely(idmap == &nop_mnt_idmap))
>         return VFSUIDT_INIT(kuid);
>
> but not that relevant for this patch.

Yeah, I'm happy to submit that change (if you don't mind) a bit later
when this will be merged,
cause in this case we can add this likely()/unlikely() at once for
both nop_mnt_idmap/invalid_mnt_idmap.

Kind regards,
Alex
diff mbox series

Patch

diff --git a/fs/mnt_idmapping.c b/fs/mnt_idmapping.c
index 3c60f1eaca61..cbca6500848e 100644
--- a/fs/mnt_idmapping.c
+++ b/fs/mnt_idmapping.c
@@ -32,6 +32,15 @@  struct mnt_idmap nop_mnt_idmap = {
 };
 EXPORT_SYMBOL_GPL(nop_mnt_idmap);
 
+/*
+ * Carries the invalid idmapping of a full 0-4294967295 {g,u}id range.
+ * This means that all {g,u}ids are mapped to INVALID_VFS{G,U}ID.
+ */
+struct mnt_idmap invalid_mnt_idmap = {
+	.count	= REFCOUNT_INIT(1),
+};
+EXPORT_SYMBOL_GPL(invalid_mnt_idmap);
+
 /**
  * initial_idmapping - check whether this is the initial mapping
  * @ns: idmapping to check
@@ -75,6 +84,8 @@  vfsuid_t make_vfsuid(struct mnt_idmap *idmap,
 
 	if (idmap == &nop_mnt_idmap)
 		return VFSUIDT_INIT(kuid);
+	if (idmap == &invalid_mnt_idmap)
+		return INVALID_VFSUID;
 	if (initial_idmapping(fs_userns))
 		uid = __kuid_val(kuid);
 	else
@@ -112,6 +123,8 @@  vfsgid_t make_vfsgid(struct mnt_idmap *idmap,
 
 	if (idmap == &nop_mnt_idmap)
 		return VFSGIDT_INIT(kgid);
+	if (idmap == &invalid_mnt_idmap)
+		return INVALID_VFSGID;
 	if (initial_idmapping(fs_userns))
 		gid = __kgid_val(kgid);
 	else
@@ -140,6 +153,8 @@  kuid_t from_vfsuid(struct mnt_idmap *idmap,
 
 	if (idmap == &nop_mnt_idmap)
 		return AS_KUIDT(vfsuid);
+	if (idmap == &invalid_mnt_idmap)
+		return INVALID_UID;
 	uid = map_id_up(&idmap->uid_map, __vfsuid_val(vfsuid));
 	if (uid == (uid_t)-1)
 		return INVALID_UID;
@@ -167,6 +182,8 @@  kgid_t from_vfsgid(struct mnt_idmap *idmap,
 
 	if (idmap == &nop_mnt_idmap)
 		return AS_KGIDT(vfsgid);
+	if (idmap == &invalid_mnt_idmap)
+		return INVALID_GID;
 	gid = map_id_up(&idmap->gid_map, __vfsgid_val(vfsgid));
 	if (gid == (gid_t)-1)
 		return INVALID_GID;
@@ -296,7 +313,7 @@  struct mnt_idmap *alloc_mnt_idmap(struct user_namespace *mnt_userns)
  */
 struct mnt_idmap *mnt_idmap_get(struct mnt_idmap *idmap)
 {
-	if (idmap != &nop_mnt_idmap)
+	if (idmap != &nop_mnt_idmap && idmap != &invalid_mnt_idmap)
 		refcount_inc(&idmap->count);
 
 	return idmap;
@@ -312,7 +329,8 @@  EXPORT_SYMBOL_GPL(mnt_idmap_get);
  */
 void mnt_idmap_put(struct mnt_idmap *idmap)
 {
-	if (idmap != &nop_mnt_idmap && refcount_dec_and_test(&idmap->count))
+	if (idmap != &nop_mnt_idmap && idmap != &invalid_mnt_idmap &&
+	    refcount_dec_and_test(&idmap->count))
 		free_mnt_idmap(idmap);
 }
 EXPORT_SYMBOL_GPL(mnt_idmap_put);
diff --git a/include/linux/mnt_idmapping.h b/include/linux/mnt_idmapping.h
index cd4d5c8781f5..b1b219bc3422 100644
--- a/include/linux/mnt_idmapping.h
+++ b/include/linux/mnt_idmapping.h
@@ -9,6 +9,7 @@  struct mnt_idmap;
 struct user_namespace;
 
 extern struct mnt_idmap nop_mnt_idmap;
+extern struct mnt_idmap invalid_mnt_idmap;
 extern struct user_namespace init_user_ns;
 
 typedef struct {