Skip to contentSkip to conversation

io_uring/bpf-ops: reject re-registration of an already-bound ops

PATCH Applied Woraphat Khiaodaeng 4 participants 5 messages Jul 17 – Jul 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring/bpf-ops: reject re-registration of an already-bound ops

io_install_bpf() only rejects a second registration on the ctx side (ctx->bpf_ops) and sets the per-map back-pointer ops->priv unconditionally. The struct_ops link path never advances a map past BPF_STRUCT_OPS_STATE_READY, so the same io_uring_bpf_ops map can be registered more than once, and bpf_io_reg() re-resolves the target ring via fget(ops->ring_fd) on every call. A caller can therefore point the same ring_fd at a different io_ring_ctx between two BPF_LINK_CREATE calls.

The second registration passes the ctx->bpf_ops check (the new ctx has none) and overwrites ops->priv, orphaning the first ctx. Teardown (io_eject_bpf()/bpf_io_unreg()) only reaches a ctx through ops->priv, so the orphaned ctx is never torn down: its ctx->loop_step keeps pointing into the struct_ops trampoline, which is freed once the map is gone. A later io_uring_enter() on the orphaned ring then calls the dangling ctx->loop_step from io_run_loop() -- a use-after-free of freed executable memory, reachable by a task with CAP_BPF + CAP_PERFMON.

Reject registration when ops->priv is already set, as hid_bpf_reg() does for its struct_ops.

Fixes 98f37634b12b ("io_uring/bpf-ops: implement bpf ops registration")
---
 io_uring/bpf-ops.c | 2 ++
 1 file changed, 2 insertions(+)
io_uring/bpf-ops.c +2 -0
index 5a50f0675..cf2bd068e 100644
@@ -168,6 +168,8 @@ static int io_install_bpf(struct io_ring_ctx *ctx, struct io_uring_bpf_ops *ops)
 
 	if (ctx->bpf_ops)
 		return -EBUSY;
+	if (ops->priv)
+		return -EBUSY;
 	if (WARN_ON_ONCE(!ops->loop_step))
 		return -EINVAL;
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Woraphat Khiaodaeng

io_install_bpf() only rejects a second registration on the ctx side (ctx->bpf_ops) and sets the per-map back-pointer ops->priv unconditionally. The struct_ops link path never advances a map past BPF_STRUCT_OPS_STATE_READY, so the same io_uring_bpf_ops map can be registered more than once, and bpf_io_reg() re-resolves the target ring via fget(ops->ring_fd) on every call. A caller can therefore point the same ring_fd at a different io_ring_ctx between two BPF_LINK_CREATE calls.

The second registration passes the ctx->bpf_ops check (the new ctx has none) and overwrites ops->priv, orphaning the first ctx. Teardown (io_eject_bpf()/bpf_io_unreg()) only reaches a ctx through ops->priv, so the orphaned ctx is never torn down: its ctx->loop_step keeps pointing into the struct_ops trampoline, which is freed once the map is gone. A later io_uring_enter() on the orphaned ring then calls the dangling ctx->loop_step from io_run_loop() -- a use-after-free of freed executable memory, reachable by a task with CAP_BPF + CAP_PERFMON.

Quoted 5 lines

Reject registration when ops->priv is already set, as hid_bpf_reg() does for its struct_ops.

Fixes 98f37634b12b ("io_uring/bpf-ops: implement bpf ops registration")

Feel free to add,

can you send a testcase to add to liburing?

Quoted 20 lines
---
 io_uring/bpf-ops.c | 2 ++
 1 file changed, 2 insertions(+)
io_uring/bpf-ops.c +2 -0
index 5a50f0675..cf2bd068e 100644
@@ -168,6 +168,8 @@ static int io_install_bpf(struct io_ring_ctx *ctx, struct io_uring_bpf_ops *ops)
 
 	if (ctx->bpf_ops)
 		return -EBUSY;
+	if (ops->priv)
+		return -EBUSY;
 	if (WARN_ON_ONCE(!ops->loop_step))
 		return -EINVAL;
 
Signature · 1 line
2.43.0
Signature · 1 line
Gabriel Krisman Bertazi
#2 lore raw

PATCH 1/1 test: add io_uring bpf-ops double-registration regression test

Exercise the io_uring BPF struct_ops registration path fixed by the kernel commit "io_uring/bpf-ops: reject re-registration of an already-bound ops".

The same io_uring_bpf_ops struct_ops map is registered twice. Between the two BPF_LINK_CREATEs the ring_fd is repointed at a second ring, so the second registration targets a different io_ring_ctx whose ->bpf_ops is NULL and passes the per-ctx check; only the per-map ops->priv guard rejects it. The test verifies the second registration is refused with -EBUSY. On a vulnerable kernel it is accepted, the first ctx is orphaned and its loop_step dangles into the freed struct_ops trampoline.

The test reuses the existing bpf test infrastructure and only checks the registration result; it never triggers the dangling call.

---
 test/Makefile                   |   2 +-
 test/bpf-progs/double_reg.bpf.c |  16 ++++
 test/bpf_double_reg.c           | 158 ++++++++++++++++++++++++++++++++
 3 files changed, 175 insertions(+), 1 deletion(-)
 create mode 100644 test/bpf-progs/double_reg.bpf.c
 create mode 100644 test/bpf_double_reg.c
test/Makefile +1 -1
index d88a428..5f6794d 100644
@@ -335,7 +335,7 @@ ifdef CONFIG_HAVE_CXX
 endif
 all_targets += sq-full-cpp.t
 
-bpf_test_srcs := bpf_nops.c bpf_cp.c
+bpf_test_srcs := bpf_nops.c bpf_cp.c bpf_double_reg.c
 bpf_progs := $(patsubst bpf_%.c, %.bpf.c, $(bpf_test_srcs))
 bpf_test_targets :=
 
test/bpf-progs/double_reg.bpf.c new file +16 -0
new file mode 100644
index 0000000..20617d0
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include "../bpf_defs.h"
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
+
+/* Minimal loop_step: the test only exercises registration, never runs it. */
+SEC("struct_ops.s/reg_loop_step")
+int BPF_PROG(reg_loop_step, struct io_ring_ctx *ring, struct iou_loop_params *ls)
+{
+	return IOU_LOOP_STOP;
+}
+
+SEC(".struct_ops.link")
+struct io_uring_bpf_ops reg_ops = {
+	.loop_step = (void *)reg_loop_step,
+};
test/bpf_double_reg.c new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Woraphat Khiaodaeng
Quoted 18 lines · Woraphat Khiaodaeng

io_install_bpf() only rejects a second registration on the ctx side (ctx->bpf_ops) and sets the per-map back-pointer ops->priv unconditionally. The struct_ops link path never advances a map past BPF_STRUCT_OPS_STATE_READY, so the same io_uring_bpf_ops map can be registered more than once, and bpf_io_reg() re-resolves the target ring via fget(ops->ring_fd) on every call. A caller can therefore point the same ring_fd at a different io_ring_ctx between two BPF_LINK_CREATE calls.

The second registration passes the ctx->bpf_ops check (the new ctx has none) and overwrites ops->priv, orphaning the first ctx. Teardown (io_eject_bpf()/bpf_io_unreg()) only reaches a ctx through ops->priv, so the orphaned ctx is never torn down: its ctx->loop_step keeps pointing into the struct_ops trampoline, which is freed once the map is gone. A later io_uring_enter() on the orphaned ring then calls the dangling ctx->loop_step from io_run_loop() -- a use-after-free of freed executable memory, reachable by a task with CAP_BPF + CAP_PERFMON.

Quoted 5 lines

Reject registration when ops->priv is already set, as hid_bpf_reg() does for its struct_ops.

Fixes 98f37634b12b ("io_uring/bpf-ops: implement bpf ops registration")

Thanks for the patch. As mentioned, the patch is simple and should be safe to apply.

Quoted 17 lines
---
  io_uring/bpf-ops.c | 2 ++
  1 file changed, 2 insertions(+)
io_uring/bpf-ops.c +2 -0
index 5a50f0675..cf2bd068e 100644
@@ -168,6 +168,8 @@ static int io_install_bpf(struct io_ring_ctx *ctx, struct io_uring_bpf_ops *ops)
 
  	if (ctx->bpf_ops)
  		return -EBUSY;
+	if (ops->priv)
+		return -EBUSY;
  	if (WARN_ON_ONCE(!ops->loop_step))
  		return -EINVAL;
 
Signature · 1 line
Pavel Begunkov
maintainer Applied
#4 lore raw
In reply to Woraphat Khiaodaeng
Quoted 10 lines · Woraphat Khiaodaeng

io_install_bpf() only rejects a second registration on the ctx side (ctx->bpf_ops) and sets the per-map back-pointer ops->priv unconditionally. The struct_ops link path never advances a map past BPF_STRUCT_OPS_STATE_READY, so the same io_uring_bpf_ops map can be registered more than once, and bpf_io_reg() re-resolves the target ring via fget(ops->ring_fd) on every call. A caller can therefore point the same ring_fd at a different io_ring_ctx between two BPF_LINK_CREATE calls.

[...]

Applied, thanks!

[1/1] io_uring/bpf-ops: reject re-registration of an already-bound ops

      commit: 83d2280089bd45e5f782011275b5c7414d85cb2b

Best regards,

Signature · 1 line
Jens Axboe