Skip to content

Commit 76d3cce

Browse files
matrizzoaxboe
authored andcommitted
io_uring: add a sysctl to disable io_uring system-wide
Introduce a new sysctl (io_uring_disabled) which can be either 0, 1, or 2. When 0 (the default), all processes are allowed to create io_uring instances, which is the current behavior. When 1, io_uring creation is disabled (io_uring_setup() will fail with -EPERM) for unprivileged processes not in the kernel.io_uring_group group. When 2, calls to io_uring_setup() fail with -EPERM regardless of privilege. Signed-off-by: Matteo Rizzo <matteorizzo@google.com> [JEM: modified to add io_uring_group] Signed-off-by: Jeff Moyer <jmoyer@redhat.com> Link: https://lore.kernel.org/r/x49y1i42j1z.fsf@segfault.boston.devel.redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 32f5dea commit 76d3cce

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Documentation/admin-guide/sysctl/kernel.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,35 @@ this allows system administrators to override the
450450
``IA64_THREAD_UAC_NOPRINT`` ``prctl`` and avoid logs being flooded.
451451

452452

453+
io_uring_disabled
454+
=================
455+
456+
Prevents all processes from creating new io_uring instances. Enabling this
457+
shrinks the kernel's attack surface.
458+
459+
= ======================================================================
460+
0 All processes can create io_uring instances as normal. This is the
461+
default setting.
462+
1 io_uring creation is disabled (io_uring_setup() will fail with
463+
-EPERM) for unprivileged processes not in the io_uring_group group.
464+
Existing io_uring instances can still be used. See the
465+
documentation for io_uring_group for more information.
466+
2 io_uring creation is disabled for all processes. io_uring_setup()
467+
always fails with -EPERM. Existing io_uring instances can still be
468+
used.
469+
= ======================================================================
470+
471+
472+
io_uring_group
473+
==============
474+
475+
When io_uring_disabled is set to 1, a process must either be
476+
privileged (CAP_SYS_ADMIN) or be in the io_uring_group group in order
477+
to create an io_uring instance. If io_uring_group is set to -1 (the
478+
default), only processes with the CAP_SYS_ADMIN capability may create
479+
io_uring instances.
480+
481+
453482
kexec_load_disabled
454483
===================
455484

io_uring/io_uring.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,31 @@ static void io_queue_sqe(struct io_kiocb *req);
150150

151151
struct kmem_cache *req_cachep;
152152

153+
static int __read_mostly sysctl_io_uring_disabled;
154+
static int __read_mostly sysctl_io_uring_group = -1;
155+
156+
#ifdef CONFIG_SYSCTL
157+
static struct ctl_table kernel_io_uring_disabled_table[] = {
158+
{
159+
.procname = "io_uring_disabled",
160+
.data = &sysctl_io_uring_disabled,
161+
.maxlen = sizeof(sysctl_io_uring_disabled),
162+
.mode = 0644,
163+
.proc_handler = proc_dointvec_minmax,
164+
.extra1 = SYSCTL_ZERO,
165+
.extra2 = SYSCTL_TWO,
166+
},
167+
{
168+
.procname = "io_uring_group",
169+
.data = &sysctl_io_uring_group,
170+
.maxlen = sizeof(gid_t),
171+
.mode = 0644,
172+
.proc_handler = proc_dointvec,
173+
},
174+
{},
175+
};
176+
#endif
177+
153178
struct sock *io_uring_get_socket(struct file *file)
154179
{
155180
#if defined(CONFIG_UNIX)
@@ -4070,9 +4095,30 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
40704095
return io_uring_create(entries, &p, params);
40714096
}
40724097

4098+
static inline bool io_uring_allowed(void)
4099+
{
4100+
int disabled = READ_ONCE(sysctl_io_uring_disabled);
4101+
kgid_t io_uring_group;
4102+
4103+
if (disabled == 2)
4104+
return false;
4105+
4106+
if (disabled == 0 || capable(CAP_SYS_ADMIN))
4107+
return true;
4108+
4109+
io_uring_group = make_kgid(&init_user_ns, sysctl_io_uring_group);
4110+
if (!gid_valid(io_uring_group))
4111+
return false;
4112+
4113+
return in_group_p(io_uring_group);
4114+
}
4115+
40734116
SYSCALL_DEFINE2(io_uring_setup, u32, entries,
40744117
struct io_uring_params __user *, params)
40754118
{
4119+
if (!io_uring_allowed())
4120+
return -EPERM;
4121+
40764122
return io_uring_setup(entries, params);
40774123
}
40784124

@@ -4666,6 +4712,10 @@ static int __init io_uring_init(void)
46664712
offsetof(struct io_kiocb, cmd.data),
46674713
sizeof_field(struct io_kiocb, cmd.data), NULL);
46684714

4715+
#ifdef CONFIG_SYSCTL
4716+
register_sysctl_init("kernel", kernel_io_uring_disabled_table);
4717+
#endif
4718+
46694719
return 0;
46704720
};
46714721
__initcall(io_uring_init);

0 commit comments

Comments
 (0)