[RFC PATCH v9 21/29] proc: Implement ->set_nsexec() fs_context operation
From: Jarkko Sakkinen
Date: Mon Nov 30 2026 - 22:13:17 EST
proc_init_fs_context() binds ctx->pid_ns to task_active_pid_ns(current)
and resets fc->user_ns to ctx->pid_ns->user_ns, overriding the user
namespace delegated by FSCONFIG_SET_NSEXEC and leaving the context
attached to the caller's PID namespace.
Wire up proc_set_nsexec() to verify that the target nsexec's PID
namespace is a descendant of the caller's active PID namespace and
rebind ctx->pid_ns and fc->user_ns accordingly, and mark proc with
FS_NSEXEC_MOUNT.
Co-developed-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
fs/proc/root.c | 61 +++++++++++++++++++++++++++++++++++++-------------
1 file changed, 46 insertions(+), 15 deletions(-)
diff --git a/fs/proc/root.c b/fs/proc/root.c
index 7fbbe92bf73a..8ec022fbede4 100644
--- a/fs/proc/root.c
+++ b/fs/proc/root.c
@@ -15,6 +15,8 @@
#include <linux/sched/stat.h>
#include <linux/module.h>
#include <linux/bitops.h>
+#include <linux/nsexec.h>
+#include <linux/nsproxy.h>
#include <linux/user_namespace.h>
#include <linux/fs_context.h>
#include <linux/mount.h>
@@ -30,7 +32,7 @@ struct proc_fs_context {
struct pid_namespace *pid_ns;
unsigned int mask;
enum proc_hidepid hidepid;
- int gid;
+ kgid_t gid;
enum proc_pidonly pidonly;
};
@@ -42,7 +44,7 @@ enum proc_param {
};
static const struct fs_parameter_spec proc_fs_parameters[] = {
- fsparam_u32("gid", Opt_gid),
+ fsparam_gid("gid", Opt_gid),
fsparam_string("hidepid", Opt_hidepid),
fsparam_string("subset", Opt_subset),
fsparam_file_or_string("pidns", Opt_pidns),
@@ -178,7 +180,9 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
switch (opt) {
case Opt_gid:
- ctx->gid = result.uint_32;
+ if (!kgid_has_mapping(fc->user_ns, result.gid))
+ return invalfc(fc, "Invalid gid");
+ ctx->gid = result.gid;
break;
case Opt_hidepid:
@@ -206,6 +210,10 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
errorfc(fc, "cannot reconfigure pidns for existing procfs");
return -EBUSY;
}
+ if (fc->nsexec_bound) {
+ errorfc(fc, "cannot override pidns on nsexec-bound procfs");
+ return -EBUSY;
+ }
err = proc_parse_pidns_param(fc, param, &result);
if (err)
return err;
@@ -224,8 +232,7 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
}
static int proc_apply_options(struct proc_fs_info *fs_info,
- struct fs_context *fc,
- struct user_namespace *user_ns)
+ struct fs_context *fc)
{
struct proc_fs_context *ctx = fc->fs_private;
@@ -234,8 +241,11 @@ static int proc_apply_options(struct proc_fs_info *fs_info,
ctx->pidonly != fs_info->pidonly)
return invalf(fc, "proc: subset=pid cannot be changed\n");
- if (ctx->mask & (1 << Opt_gid))
- fs_info->pid_gid = make_kgid(user_ns, ctx->gid);
+ if (ctx->mask & (1 << Opt_gid)) {
+ if (!kgid_has_mapping(fc->user_ns, ctx->gid))
+ return invalfc(fc, "Invalid gid");
+ fs_info->pid_gid = ctx->gid;
+ }
if (ctx->mask & (1 << Opt_hidepid))
fs_info->hide_pid = ctx->hidepid;
if (ctx->mask & (1 << Opt_subset))
@@ -261,7 +271,8 @@ static int proc_fill_super(struct super_block *s, struct fs_context *fc)
fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
fs_info->mounter_cred = get_cred(fc->cred);
- ret = proc_apply_options(fs_info, fc, current_user_ns());
+ s->s_fs_info = fs_info;
+ ret = proc_apply_options(fs_info, fc);
if (ret)
return ret;
@@ -273,8 +284,6 @@ static int proc_fill_super(struct super_block *s, struct fs_context *fc)
s->s_magic = PROC_SUPER_MAGIC;
s->s_op = &proc_sops;
s->s_time_gran = 1;
- s->s_fs_info = fs_info;
-
if (fs_info->pidonly == PROC_PIDONLY_ON)
s->s_iflags |= SB_I_RESTRICTED_VARIANT;
@@ -315,7 +324,7 @@ static int proc_reconfigure(struct fs_context *fc)
sync_filesystem(sb);
- return proc_apply_options(fs_info, fc, current_user_ns());
+ return proc_apply_options(fs_info, fc);
}
static int proc_get_tree(struct fs_context *fc)
@@ -323,6 +332,21 @@ static int proc_get_tree(struct fs_context *fc)
return get_tree_nodev(fc, proc_fill_super);
}
+static int proc_set_nsexec(struct fs_context *fc, struct nsexec *nsexec)
+{
+ struct proc_fs_context *ctx = fc->fs_private;
+ struct pid_namespace *target = nsexec->ns->pid_ns_for_children;
+
+ if (!pidns_is_ancestor(target, task_active_pid_ns(current)))
+ return invalfc(fc, "cannot set pidns to non-descendant pidns");
+
+ put_pid_ns(ctx->pid_ns);
+ ctx->pid_ns = get_pid_ns(target);
+ put_user_ns(fc->user_ns);
+ fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
+ return 0;
+}
+
static void proc_fs_context_free(struct fs_context *fc)
{
struct proc_fs_context *ctx = fc->fs_private;
@@ -334,6 +358,7 @@ static void proc_fs_context_free(struct fs_context *fc)
static const struct fs_context_operations proc_fs_context_ops = {
.free = proc_fs_context_free,
.parse_param = proc_parse_param,
+ .set_nsexec = proc_set_nsexec,
.get_tree = proc_get_tree,
.reconfigure = proc_reconfigure,
};
@@ -346,9 +371,13 @@ static int proc_init_fs_context(struct fs_context *fc)
if (!ctx)
return -ENOMEM;
- ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
- put_user_ns(fc->user_ns);
- fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
+ if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
+ ctx->pid_ns = get_pid_ns(proc_sb_info(fc->root->d_sb)->pid_ns);
+ } else {
+ ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
+ put_user_ns(fc->user_ns);
+ fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
+ }
fc->fs_private = ctx;
fc->ops = &proc_fs_context_ops;
return 0;
@@ -371,7 +400,9 @@ static struct file_system_type proc_fs_type = {
.init_fs_context = proc_init_fs_context,
.parameters = proc_fs_parameters,
.kill_sb = proc_kill_sb,
- .fs_flags = FS_USERNS_MOUNT | FS_USERNS_MOUNT_RESTRICTED | FS_DISALLOW_NOTIFY_PERM,
+ .fs_flags = FS_USERNS_MOUNT | FS_NSEXEC_MOUNT |
+ FS_USERNS_MOUNT_RESTRICTED |
+ FS_DISALLOW_NOTIFY_PERM,
};
void __init proc_root_init(void)
--
2.47.3