Skip to content

Commit ec02fe2

Browse files
isilenceaxboe
authored andcommitted
io_uring/bpf-ops: restrict ctx access to BPF
BPF programs should have no need in looking into struct io_ring_ctx, if anything, most of such cases would be anti patterns like looking up ring indices directly via the context. Replace it with a new empty structure, which is just an alias to struct io_ring_ctx. It'll create a new BTF type and fail verification if a BPF program tries to access it (beyond the first byte). It'll also give more flexibility for the future, and otherwise it can be made aligned with io_ring_ctx as before with struct groups if ever needed or extended in a different way. Fixes: d0e437b ("io_uring/bpf-ops: implement loop_step with BPF struct_ops") Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://patch.msgid.link/5f6ca3649e9e0bae8667db4357e28dd00cd07901.1780394491.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 29bef99 commit ec02fe2

5 files changed

Lines changed: 21 additions & 6 deletions

File tree

include/linux/io_uring_types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ enum {
290290
IO_RING_F_IOWQ_LIMITS_SET = BIT(12),
291291
};
292292

293+
struct iou_ctx {};
294+
293295
struct io_ring_ctx {
294296
/* const or read-mostly hot data */
295297
struct {
@@ -366,7 +368,7 @@ struct io_ring_ctx {
366368
struct io_alloc_cache rw_cache;
367369
struct io_alloc_cache cmd_cache;
368370

369-
int (*loop_step)(struct io_ring_ctx *ctx,
371+
int (*loop_step)(struct iou_ctx *,
370372
struct iou_loop_params *);
371373

372374
/*

io_uring/bpf-ops.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ static const struct btf_type *loop_params_type;
1414

1515
__bpf_kfunc_start_defs();
1616

17-
__bpf_kfunc int bpf_io_uring_submit_sqes(struct io_ring_ctx *ctx, u32 nr)
17+
__bpf_kfunc int bpf_io_uring_submit_sqes(struct iou_ctx *loop_ctx, u32 nr)
1818
{
19+
struct io_ring_ctx *ctx = io_loop_demangle_ctx(loop_ctx);
20+
1921
return io_submit_sqes(ctx, nr);
2022
}
2123

2224
__bpf_kfunc
23-
__u8 *bpf_io_uring_get_region(struct io_ring_ctx *ctx, __u32 region_id,
25+
__u8 *bpf_io_uring_get_region(struct iou_ctx *loop_ctx, __u32 region_id,
2426
const size_t rdwr_buf_size)
2527
{
28+
struct io_ring_ctx *ctx = io_loop_demangle_ctx(loop_ctx);
2629
struct io_mapped_region *r;
2730

2831
lockdep_assert_held(&ctx->uring_lock);
@@ -58,7 +61,7 @@ static const struct btf_kfunc_id_set bpf_io_uring_kfunc_set = {
5861
.set = &io_uring_kfunc_set,
5962
};
6063

61-
static int io_bpf_ops__loop_step(struct io_ring_ctx *ctx,
64+
static int io_bpf_ops__loop_step(struct iou_ctx *ctx,
6265
struct iou_loop_params *lp)
6366
{
6467
return IOU_LOOP_STOP;

io_uring/bpf-ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ enum {
1111
};
1212

1313
struct io_uring_bpf_ops {
14-
int (*loop_step)(struct io_ring_ctx *ctx, struct iou_loop_params *lp);
14+
int (*loop_step)(struct iou_ctx *, struct iou_loop_params *lp);
1515

1616
__u32 ring_fd;
1717
void *priv;

io_uring/loop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static int __io_run_loop(struct io_ring_ctx *ctx)
4949
if (unlikely(!ctx->loop_step))
5050
return -EFAULT;
5151

52-
step_res = ctx->loop_step(ctx, &lp);
52+
step_res = ctx->loop_step(io_loop_mangle_ctx(ctx), &lp);
5353
if (step_res == IOU_LOOP_STOP)
5454
break;
5555
if (step_res != IOU_LOOP_CONTINUE)

io_uring/loop.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ static inline bool io_has_loop_ops(struct io_ring_ctx *ctx)
2424

2525
int io_run_loop(struct io_ring_ctx *ctx);
2626

27+
static inline struct iou_ctx *io_loop_mangle_ctx(struct io_ring_ctx *ctx)
28+
{
29+
return (struct iou_ctx *)ctx;
30+
}
31+
32+
static inline struct io_ring_ctx *io_loop_demangle_ctx(struct iou_ctx *ctx)
33+
{
34+
return (struct io_ring_ctx *)ctx;
35+
}
36+
2737
#endif

0 commit comments

Comments
 (0)