[PATCH RFC v3 61/90] fs: accept a cellfd for MOUNT_ATTR_IDMAP
From: Josef Bacik
Date: Thu Nov 05 2026 - 10:29:04 EST
The builder of a cell has to idmap the mounts that it puts into the tree
of the cell. But it cannot name the ids that the kernel chose for the
cell.
Accept a cellfd in mount_attr.userns_fd. It means "idmap this mount for
the cell": on-disk ids from 0 upwards map to the ids of the cell. This
works while the cell is being built, once its ids have been set.
mount_setattr() normally requires CAP_SYS_ADMIN over the user namespace.
That check stops a caller from mapping file ownership to ids that it does
not control. Here the ids belong to the cell and nobody else can hold
them, so the check is not needed. The checks that can_idmap_mount() makes
on the filesystem are unchanged.
Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
---
fs/namespace.c | 13 +++++++++++++
include/linux/cell.h | 7 +++++++
kernel/cell/boundary.c | 25 +++++++++++++++++++++++++
3 files changed, 45 insertions(+)
diff --git a/fs/namespace.c b/fs/namespace.c
index 30167f8863bf..e2406d7f7bcc 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -5207,6 +5207,19 @@ static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
if (fd_empty(f))
return -EBADF;
+ /*
+ * "For this cell." The ids on the caller's side of the mapping are
+ * the cell's own, so there is nobody to ask about them. What is asked
+ * about the filesystem is the same as ever.
+ */
+ mnt_userns = cell_idmap_userns(fd_file(f));
+ if (mnt_userns) {
+ if (IS_ERR(mnt_userns))
+ return PTR_ERR(mnt_userns);
+ kattr->mnt_userns = mnt_userns;
+ return 0;
+ }
+
if (!proc_ns_file(fd_file(f)))
return -EINVAL;
diff --git a/include/linux/cell.h b/include/linux/cell.h
index 2107cd3c994b..f204c47ee783 100644
--- a/include/linux/cell.h
+++ b/include/linux/cell.h
@@ -249,4 +249,11 @@ bool cell_may_change_cgroup(struct cgroup *cgrp);
static inline bool cell_may_change_cgroup(struct cgroup *cgrp) { return true; }
#endif
+/* A mount is idmapped for a cell. */
+#ifdef CONFIG_CELLS
+struct user_namespace *cell_idmap_userns(const struct file *file);
+#else
+static inline struct user_namespace *cell_idmap_userns(const struct file *f) { return NULL; }
+#endif
+
#endif /* _LINUX_CELL_H */
diff --git a/kernel/cell/boundary.c b/kernel/cell/boundary.c
index 3f5cc898dc48..3891dfe899f2 100644
--- a/kernel/cell/boundary.c
+++ b/kernel/cell/boundary.c
@@ -8,6 +8,31 @@
#include "internal.h"
+/**
+ * cell_idmap_userns - the user namespace to idmap a mount with, by cellfd
+ * @file: what was passed in mount_attr.userns_fd
+ *
+ * A cellfd there means "for this cell". It works while the cell is being built
+ * and once its ids have been set.
+ *
+ * Return: the user namespace of the cell with a reference, NULL if @file is no
+ * cellfd, or an ERR_PTR().
+ */
+struct user_namespace *cell_idmap_userns(const struct file *file)
+{
+ struct cell *cell = cell_from_file(file);
+
+ if (!cell)
+ return NULL;
+
+ guard(mutex)(&cell->lock);
+ if (cell->state != CELL_BUILDING)
+ return ERR_PTR(-EBUSY);
+ if (!cell->ids_count)
+ return ERR_PTR(-EINVAL);
+ return get_user_ns(cell->user_ns);
+}
+
/**
* cell_may_migrate - may a task be moved to a cgroup?
* @task: the task
--
2.55.0