io_uring/bpf-ops: reject re-registration of an already-bound ops
Subsystems (2)
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.
--- io_uring/bpf-ops.c | 2 ++ 1 file changed, 2 insertions(+)
io_uring/bpf-ops.c +2 -0
@@ -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
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-pointerops->privunconditionally. Thestruct_opslink path never advances a map pastBPF_STRUCT_OPS_STATE_READY, so the sameio_uring_bpf_opsmap can be registered more than once, andbpf_io_reg()re-resolves the target ring viafget(ops->ring_fd)on every call. A caller can therefore point the samering_fdat a differentio_ring_ctxbetween twoBPF_LINK_CREATEcalls.The second registration passes the
ctx->bpf_opscheck (the new ctx has none) and overwritesops->priv, orphaning the first ctx. Teardown (io_eject_bpf()/bpf_io_unreg()) only reaches a ctx throughops->priv, so the orphaned ctx is never torn down: itsctx->loop_stepkeeps pointing into thestruct_opstrampoline, which is freed once the map is gone. A laterio_uring_enter()on the orphaned ring then calls the danglingctx->loop_stepfromio_run_loop()-- a use-after-free of freed executable memory, reachable by a task withCAP_BPF+CAP_PERFMON.
Quoted 5 lines
Reject registration when
ops->privis already set, ashid_bpf_reg()does for itsstruct_ops.Fixes 98f37634b12b ("io_uring/bpf-ops: implement bpf ops registration")Signed-off-by Woraphat Khiaodaeng <[email protected]>
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@@ -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
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
@@ -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
@@ -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
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-pointerops->privunconditionally. Thestruct_opslink path never advances a map pastBPF_STRUCT_OPS_STATE_READY, so the sameio_uring_bpf_opsmap can be registered more than once, andbpf_io_reg()re-resolves the target ring viafget(ops->ring_fd)on every call. A caller can therefore point the samering_fdat a differentio_ring_ctxbetween twoBPF_LINK_CREATEcalls.The second registration passes the
ctx->bpf_opscheck (the new ctx has none) and overwritesops->priv, orphaning the first ctx. Teardown (io_eject_bpf()/bpf_io_unreg()) only reaches a ctx throughops->priv, so the orphaned ctx is never torn down: itsctx->loop_stepkeeps pointing into thestruct_opstrampoline, which is freed once the map is gone. A laterio_uring_enter()on the orphaned ring then calls the danglingctx->loop_stepfromio_run_loop()-- a use-after-free of freed executable memory, reachable by a task withCAP_BPF+CAP_PERFMON.
Quoted 5 lines
Reject registration when
ops->privis already set, ashid_bpf_reg()does for itsstruct_ops.Fixes 98f37634b12b ("io_uring/bpf-ops: implement bpf ops registration")Signed-off-by Woraphat Khiaodaeng <[email protected]>
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@@ -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
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-pointerops->privunconditionally. Thestruct_opslink path never advances a map pastBPF_STRUCT_OPS_STATE_READY, so the sameio_uring_bpf_opsmap can be registered more than once, andbpf_io_reg()re-resolves the target ring viafget(ops->ring_fd)on every call. A caller can therefore point the samering_fdat a differentio_ring_ctxbetween twoBPF_LINK_CREATEcalls.[...]
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