Inherited restrictions and BPF filtering
Subsystems (6)
Jens Axboe
maintainerRFC PATCH v3 1/1 Inherited restrictions and BPF filtering
Hi,
Followup to v2 here:
https://lore.kernel.org/io-uring/[email protected]/
While this is a followup, it takes a different approach to the problem. What remains is task inheritance - if a set of restrictions are registered with a task, any children will get it too.
What's new is adding basic support for BPF filters, so that anything can be filtered. You can add filters for each opcode, and several of them as well. As the filtering is done after the prep phase, it's even possible to support filtering based on user structs that are copied in to the kernel. For now, only IORING_OP_SOCKET is done, and allows filtering on domain/type/protocol. This is done as an example. A sample filter for that could look like:
SEC("io_uring_filter")
int socket_filter(struct io_uring_bpf_ctx *ctx)
{
/* Only allow AF_INET and AF_INET6 */
if (ctx->socket.family != AF_INET && ctx->socket.family != AF_INET6)
return 0; /* Reject */
/* Only allow SOCK_STREAM (TCP) */
if (ctx->socket.type != SOCK_STREAM)
return 0; /* Reject */
/* Only allow IPPROTO_TCP or default (0) */
if (ctx->socket.protocol != IPPROTO_TCP && ctx->socket.protocol != 0)
return 0; /* Reject */
return 1; /* Accept */
}to restrict certain families, types, or protocols.
Just supports SQE opcodes for this kind of filtering, but easily extendable to cover REGISTER opcodes as well, including arguments.
Sending this out as an RFC for comments. I think this provides most of the functionality needed to filter basically anything. There's still some rough edges here, notably the BPF support, as I really don't know what I'm doing there. But it works for testing, at least... I don't have a liburing branch for this just yet, let me know if you want some test/sample code and I'll be happy to toss it over the wall. I'll add a liburing branch over the weekend for easier experimentation.
Sample based on the above filter:
axboe@m2max-kvm ~> ./io_uring_bpf_loader io_uring_bpf_filter.c.bpf.o io_uring BPF Socket Filter Test (C-based)
==========================================
io_uring initialized
BPF program loaded successfully from io_uring_bpf_filter.c.bpf.o, fd=4 BPF filter registered for opcode 45
Running tests...
Testing AF_INET TCP (explicit): PASSED (fd=5)
Testing AF_INET TCP (default): PASSED (fd=5)
Testing AF_INET6 TCP (explicit): PASSED (fd=5)
Testing AF_INET6 TCP (default): PASSED (fd=5)
Testing AF_INET UDP: PASSED (correctly rejected)
Testing AF_INET RAW: PASSED (correctly rejected)
Testing AF_UNIX: PASSED (correctly rejected)
Testing AF_INET TCP socket with UDP proto: PASSED (correctly rejected)
or running t/io_uring with IORING_OP_NOP and a filter set. This filter just allows the opcode, but it's still run on each NOP issued:
axboe@m2max-kvm ~/g/fio (master)> sudo taskset -c 0 t/io_uring -N1 -n1 -E ~/noop_filter.bpf.c.o -B0 -F0 trim.json submitter=0, tid=2287, file=trim.json, nfiles=1, node=-1
BPF program loaded successfully from /home/axboe/noop_filter.bpf.c.o, fd=5
BPF filter registered for opcode 0
polled=1, fixedbufs=0, register_files=0, buffered=0, QD=128
Engine=io_uring, sq_ring=128, cq_ring=128
IOPS=13.89M, IOS/call=32/32
IOPS=13.90M, IOS/call=32/32
[...]
Comments welcome! Kernel branch can be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git/log/?h=io_uring-bpf-restrictions
and sits on top of for-7.0/io_uring
include/linux/bpf.h | 1 + include/linux/bpf_types.h | 4 + include/linux/io_uring.h | 2 +- include/linux/io_uring_types.h | 20 +++- include/linux/sched.h | 1 + include/uapi/linux/bpf.h | 1 + include/uapi/linux/io_uring.h | 46 +++++++ io_uring/Makefile | 1 + io_uring/bpf_filter.c | 212 +++++++++++++++++++++++++++++++++ io_uring/bpf_filter.h | 41 +++++++ io_uring/io_uring.c | 33 ++++- io_uring/net.c | 9 ++ io_uring/net.h | 5 + io_uring/register.c | 133 +++++++++++++++++++-- io_uring/register.h | 2 + io_uring/tctx.c | 26 ++-- kernel/bpf/syscall.c | 9 ++ kernel/fork.c | 4 + 18 files changed, 527 insertions(+), 23 deletions(-)
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 1/3 io_uring: move ctx->restrictions to be dynamically allocated
In preparation for being able to share restrictions, move them to allocated data rather than be embedded in the ring. This makes it more obvious when they will have potentially different lifetimes.
--- include/linux/io_uring_types.h | 4 +++- io_uring/io_uring.c | 12 ++++++----- io_uring/register.c | 37 +++++++++++++++++++++++++++------- io_uring/register.h | 2 ++ 4 files changed, 42 insertions(+), 13 deletions(-)
include/linux/io_uring_types.h +3 -1
@@ -220,6 +220,7 @@ struct io_rings { };struct io_restriction { +refcount_t refs; DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); DECLARE_BITMAP(sqe_op, IORING_OP_LAST);u8 sqe_flags_allowed;
@@ -342,6 +343,8 @@ struct io_ring_ctx {struct io_alloc_cache rw_cache;struct io_alloc_cache cmd_cache; +struct io_restriction *restrictions; +/* * Any cancelable uring_cmd is added to this list in * ->uring_cmd() by io_uring_cmd_insert_cancelable()
@@ -413,7 +416,6 @@ struct io_ring_ctx {/* Keep this last, we don't need it for the fast path */ struct wait_queue_head poll_wq; -struct io_restriction restrictions;/* Stores zcrx object pointers of type struct io_zcrx_ifq */ struct xarray zcrx_ctxs;
io_uring/io_uring.c +7 -5
@@ -2058,15 +2058,15 @@ static inline bool io_check_restriction(struct io_ring_ctx *ctx, {if (!ctx->op_restricted)return true; -if (!test_bit (req->opcode, ctx->restrictions.sqe_op)) +if (!test_bit (req->opcode, ctx->restrictions->sqe_op))return false; -if ((sqe_flags & ctx->restrictions.sqe_flags_required) != - ctx->restrictions.sqe_flags_required) +if ((sqe_flags & ctx->restrictions->sqe_flags_required) != + ctx->restrictions->sqe_flags_required)return false; -if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | - ctx->restrictions.sqe_flags_required)) +if (sqe_flags & ~(ctx->restrictions->sqe_flags_allowed | + ctx->restrictions->sqe_flags_required))return false;return true;
@@ -2850,6 +2850,8 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)percpu_ref_exit (&ctx->refs);free_uid (ctx->user);io_req_caches_free (ctx); +if (ctx->restrictions) +io_put_restrictions (ctx->restrictions);WARN_ON_ONCE (ctx->nr_req_allocated);
io_uring/register.c +30 -7
@@ -163,9 +163,28 @@ static __cold int io_parse_restrictions(void __user *arg, unsigned int nr_args,return ret; } +void io_put_restrictions (struct io_restriction *res) +{ +if (refcount_dec_and_test (&res->refs)) +kfree (res); +} + +static struct io_restriction *io_alloc_restrictions (void ) +{ +struct io_restriction *res; + + res =kzalloc (sizeof (*res),GFP_KERNEL ); +if (!res) +return ERR_PTR (-ENOMEM ); + +refcount_set (&res->refs,1 ); +return res; +} +static __cold int io_register_restrictions (struct io_ring_ctx *ctx,void __user *arg,unsigned int nr_args) { +struct io_restriction *res;int ret;/* Restrictions allowed only if rings started disabled */
@@ -173,19 +192,23 @@ static __cold int io_register_restrictions(struct io_ring_ctx *ctx,return -EBADFD ;/* We allow only a single restrictions registration */ -if (ctx->restrictions.op_registered || ctx->restrictions.reg_registered) +if (ctx->restrictions)return -EBUSY ; - ret =io_parse_restrictions (arg, nr_args, &ctx->restrictions); -/* Reset all restrictions if an error happened */ + res =io_alloc_restrictions (); +if (IS_ERR (res)) +return PTR_ERR (res); + + ret =io_parse_restrictions (arg, nr_args, res);if (ret <0 ) { -memset (&ctx->restrictions,0 ,sizeof (ctx->restrictions)); +io_put_restrictions (res);return ret; } -if (ctx->restrictions.op_registered) +if (res->op_registered) ctx->op_restricted =1 ; -if (ctx->restrictions.reg_registered) +if (res->reg_registered) ctx->reg_restricted =1 ; + ctx->restrictions = res;return 0 ; }
@@ -637,7 +660,7 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,if (ctx->reg_restricted && !(ctx->flags &IORING_SETUP_R_DISABLED )) { opcode =array_index_nospec (opcode,IORING_REGISTER_LAST ); -if (!test_bit (opcode, ctx->restrictions.register_op)) +if (!test_bit (opcode, ctx->restrictions->register_op))return -EACCES ; }
io_uring/register.h +2 -0
@@ -6,4 +6,6 @@ int io_eventfd_unregister(struct io_ring_ctx *ctx);int io_unregister_personality (struct io_ring_ctx *ctx,unsigned id);struct file *io_uring_register_get_file (unsigned int fd,bool registered); +void io_put_restrictions (struct io_restriction *res); +#endif
Signature · 1 line
2.51.0
Jens Axboe
maintainerPATCH 2/3 io_uring: add support for BPF filtering for opcode restrictions
In reply to Jens AxboeThis adds support for loading BPF programs with io_uring, which can restrict the opcodes performed. Unlike IORING_REGISTER_RESTRICTIONS, using BPF programs allow fine grained control over both the opcode in question, as well as other data associated with the request. Initially only IORING_OP_SOCKET is supported.
--- include/linux/bpf.h | 1 + include/linux/bpf_types.h | 4 + include/linux/io_uring_types.h | 16 +++ include/uapi/linux/bpf.h | 1 + include/uapi/linux/io_uring.h | 37 ++++++ io_uring/Makefile | 1 + io_uring/bpf_filter.c | 212 +++++++++++++++++++++++++++++++++ io_uring/bpf_filter.h | 41 +++++++ io_uring/io_uring.c | 7 ++ io_uring/net.c | 9 ++ io_uring/net.h | 5 + io_uring/register.c | 33 ++++- kernel/bpf/syscall.c | 9 ++ 13 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 io_uring/bpf_filter.c create mode 100644 io_uring/bpf_filter.h
include/linux/bpf.h +1 -0
@@ -31,6 +31,7 @@#include <linux/static_call.h> #include <linux/memcontrol.h> #include <linux/cfi.h> +#include <linux/io_uring_types.h> #include <asm/rqspinlock.h> struct bpf_verifier_env ;
include/linux/bpf_types.h +4 -0
@@ -83,6 +83,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_SYSCALL, bpf_syscall,BPF_PROG_TYPE (BPF_PROG_TYPE_NETFILTER , netfilter, struct bpf_nf_ctx, struct bpf_nf_ctx)#endif +#ifdef CONFIG_IO_URING +BPF_PROG_TYPE (BPF_PROG_TYPE_IO_URING , io_uring_filter, + struct io_uring_bpf_ctx, struct io_uring_bpf_ctx) +#endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)BPF_MAP_TYPE (BPF_MAP_TYPE_PERCPU_ARRAY , percpu_array_map_ops)
include/linux/io_uring_types.h +16 -0
@@ -219,6 +219,17 @@ struct io_rings {struct io_uring_cqe cqes[]____cacheline_aligned_in_smp ; }; +#ifdef CONFIG_BPF +extern const struct bpf_prog_ops io_uring_filter_prog_ops; +extern const struct bpf_verifier_ops io_uring_filter_verifier_ops; +#endif + +struct io_bpf_filter ; +struct io_bpf_filters { +spinlock_t lock; +struct io_bpf_filter __rcu **bpf_filters; +}; +struct io_restriction {refcount_t refs; DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
@@ -229,6 +240,10 @@ struct io_restriction {bool op_registered;/* IORING_REGISTER_* restrictions exist */ bool reg_registered; +/* BPF filter restrictions exists */ +bool bpf_registered; +struct io_bpf_filters filters; +struct rcu_head rcu_head; };struct io_submit_link {
@@ -265,6 +280,7 @@ struct io_ring_ctx { unsigned int drain_next: 1; unsigned int op_restricted: 1; unsigned int reg_restricted: 1; + unsigned int bpf_restricted: 1; unsigned int off_timeout_used: 1; unsigned int drain_active: 1; unsigned int has_evfd: 1;
include/uapi/linux/bpf.h +1 -0
@@ -1072,6 +1072,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ BPF_PROG_TYPE_NETFILTER , +BPF_PROG_TYPE_IO_URING , __MAX_BPF_PROG_TYPE };
include/uapi/linux/io_uring.h +37 -0
@@ -700,6 +700,9 @@ enum io_uring_register_op {/* auxiliary zcrx configuration, see enum zcrx_ctrl_op */ IORING_REGISTER_ZCRX_CTRL =36 , +/* register bpf filtering programs */ +IORING_REGISTER_BPF_FILTER =37 , +/* this goes last */ IORING_REGISTER_LAST,
@@ -1113,6 +1116,40 @@ struct zcrx_ctrl { }; }; +struct io_uring_bpf_ctx { +__u8 opcode; +__u8 sqe_flags; +__u8 pad[6 ]; +__u64 user_data; +union { +struct { +__u32 family; +__u32 type; +__u32 protocol; + } socket; + }; +}; + +struct io_uring_bpf_filter { +__u32 opcode;/* io_uring opcode to filter */ +__u32 flags; +__s32 prog_fd;/* BPF program fd */ +__u32 reserved[3 ]; +}; + +enum { +IO_URING_BPF_CMD_FILTER =1 , +}; + +struct io_uring_bpf { +__u16 cmd_type;/* IO_URING_BPF_* values */ +__u16 cmd_flags;/* none so far */ +__u32 resv; +union { +struct io_uring_bpf_filter filter; + }; +}; + #ifdef __cplusplus }#endif
io_uring/Makefile +1 -0
@@ -22,3 +22,4 @@ obj-$(CONFIG_NET_RX_BUSY_POLL) += napi.o obj-$(CONFIG_NET) += net.o cmd_net.o obj-$(CONFIG_PROC_FS) += fdinfo.o obj-$(CONFIG_IO_URING_MOCK_FILE) += mock_file.o +obj-$(CONFIG_BPF) += bpf_filter.o
io_uring/bpf_filter.c new file +212 -0
212 lines not on this page · raw message
io_uring/bpf_filter.h new file +41 -0
41 lines not on this page · raw message
io_uring/io_uring.c +7 -0
@@ -93,6 +93,7 @@#include "rw.h" #include "alloc_cache.h" #include "eventfd.h" +#include "bpf_filter.h" #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ IOSQE_IO_HARDLINK | IOSQE_ASYNC)
@@ -2261,6 +2262,12 @@ static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,if (unlikely (ret))return io_submit_fail_init (sqe, req, ret); +if (unlikely (ctx->bpf_restricted)) { + ret =io_uring_run_bpf_filters (ctx, req); +if (ret) +return io_submit_fail_init (sqe, req, ret); + } +trace_io_uring_submit_req (req);/*
io_uring/net.c +9 -0
@@ -1699,6 +1699,15 @@ int io_accept(struct io_kiocb *req, unsigned int issue_flags)return IOU_COMPLETE ; } +void io_socket_bpf_populate (struct io_uring_bpf_ctx *bctx,struct io_kiocb *req) +{ + struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket); + + bctx->socket.family = sock->domain; + bctx->socket.type = sock->type; + bctx->socket.protocol = sock->protocol; +} +int io_socket_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe) { struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
io_uring/net.h +5 -0
@@ -44,6 +44,7 @@ int io_accept(struct io_kiocb *req, unsigned int issue_flags);int io_socket_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);int io_socket (struct io_kiocb *req,unsigned int issue_flags); +void io_socket_bpf_populate (struct io_uring_bpf_ctx *bctx,struct io_kiocb *req);int io_connect_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);int io_connect (struct io_kiocb *req,unsigned int issue_flags);
@@ -64,4 +65,8 @@ void io_netmsg_cache_free(const void *entry);static inline void io_netmsg_cache_free (const void *entry) { } +static inline void io_socket_bpf_populate (struct io_uring_bpf_ctx *bctx, +struct io_kiocb *req) +{ +}#endif
io_uring/register.c +32 -1
@@ -33,6 +33,7 @@#include "memmap.h" #include "zcrx.h" #include "query.h" +#include "bpf_filter.h" #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ IORING_REGISTER_LAST + IORING_OP_LAST)
@@ -163,10 +164,19 @@ static __cold int io_parse_restrictions(void __user *arg, unsigned int nr_args,return ret; } +static void io_free_restrictions (struct rcu_head *head) +{ +struct io_restriction *res; + + res = container_of(head, struct io_restriction, rcu_head); +io_uring_put_bpf_filters (res); +kfree (res); +} +void io_put_restrictions (struct io_restriction *res) {if (refcount_dec_and_test (&res->refs)) -kfree (res); +call_rcu (&res->rcu_head, io_free_restrictions); }static struct io_restriction *io_alloc_restrictions (void )
@@ -178,6 +188,7 @@ static struct io_restriction *io_alloc_restrictions(void)return ERR_PTR (-ENOMEM );refcount_set (&res->refs,1 ); +spin_lock_init (&res->filters.lock);return res; }
@@ -853,6 +864,26 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,case IORING_REGISTER_ZCRX_CTRL : ret =io_zcrx_ctrl (ctx, arg, nr_args);break ; +case IORING_REGISTER_BPF_FILTER : + ret = -EINVAL ; +if (nr_args !=1 ) +break ; +#ifdef CONFIG_BPF +if (!ctx->restrictions) { +struct io_restriction *res; + + res =io_alloc_restrictions (); +if (IS_ERR (res)) { + ret =PTR_ERR (res); +break ; + } + ctx->restrictions = res; + } + ret =io_register_bpf_filter (ctx->restrictions, arg); +if (ctx->restrictions->bpf_registered) + ctx->bpf_restricted =1 ; +#endif +break ;default : ret = -EINVAL ;break ;
kernel/bpf/syscall.c +9 -0
@@ -2752,6 +2752,10 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,if (expected_attach_type ==BPF_NETFILTER )return 0 ;return -EINVAL ; +case BPF_PROG_TYPE_IO_URING : +if (expected_attach_type) +return -EINVAL ; +return 0 ;case BPF_PROG_TYPE_SYSCALL :case BPF_PROG_TYPE_EXT :if (expected_attach_type)
@@ -2934,6 +2938,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) }if (type !=BPF_PROG_TYPE_SOCKET_FILTER && type !=BPF_PROG_TYPE_CGROUP_SKB && + type !=BPF_PROG_TYPE_IO_URING && !bpf_cap) goto put_token;
@@ -4403,6 +4408,10 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,if (attach_type !=BPF_NETFILTER )return -EINVAL ;return 0 ; +case BPF_PROG_TYPE_IO_URING : +if (attach_type !=0 ) +return -EINVAL ; +return 0 ;case BPF_PROG_TYPE_PERF_EVENT :case BPF_PROG_TYPE_TRACEPOINT :if (attach_type !=BPF_PERF_EVENT )
Signature · 1 line
2.51.0
Re: [PATCH 2/3] io_uring: add support for BPF filtering for opcode restrictions
Quoted 5 lines · Jens Axboe
This adds support for loading BPF programs with
io_uring, which can restrict the opcodes performed. UnlikeIORING_REGISTER_RESTRICTIONS, using BPF programs allow fine grained control over both the opcode in question, as well as other data associated with the request. Initially onlyIORING_OP_SOCKETis supported.
A minor nit...
[...]
io_uring/bpf_filter.c line 97
Quoted 6 lines
io_uring/bpf_filter.c new file +6
-0+/* +* Run registered filters for a given opcode. Return of 0 means that the +* request should be allowed. +*/ +int __io_uring_run_bpf_filters (struct io_restriction *res,struct io_kiocb *req) +{
That comment seems to contradict the actual logic in this function, as well as the example BPF program in the cover letter. So s/allowed/blocked/?
Thanks,
jon
Jens Axboe
maintainerRe: [PATCH 2/3] io_uring: add support for BPF filtering for opcode restrictions
Quoted 12 lines · Jonathan Corbet
Quoted 5 lines · Jens Axboe
This adds support for loading BPF programs with
io_uring, which can restrict the opcodes performed. UnlikeIORING_REGISTER_RESTRICTIONS, using BPF programs allow fine grained control over both the opcode in question, as well as other data associated with the request. Initially onlyIORING_OP_SOCKETis supported.A minor nit...
[...]
Quoted 10 lines
io_uring/bpf_filter.c line 97
Quoted 6 lines
io_uring/bpf_filter.c new file +6
-0+/* +* Run registered filters for a given opcode. Return of 0 means that the +* request should be allowed. +*/ +int __io_uring_run_bpf_filters (struct io_restriction *res,struct io_kiocb *req) +{That comment seems to contradict the actual logic in this function, as well as the example BPF program in the cover letter. So s/allowed/blocked/?
Are you talking about __io_uring_run_bpf_filters() or the filters themselves? For the former, 0 does indeed mean "yep let it rip", for the filters it's 0/1 where 0 is deny and 1 is allow. I should probably make the comment more explicit on that front...
Signature · 1 line
Jens Axboe
Re: [PATCH 2/3] io_uring: add support for BPF filtering for opcode restrictions
Quoted 24 lines · Jens Axboe
Quoted 12 lines · Jonathan Corbet
Quoted 5 lines · Jens Axboe
This adds support for loading BPF programs with
io_uring, which can restrict the opcodes performed. UnlikeIORING_REGISTER_RESTRICTIONS, using BPF programs allow fine grained control over both the opcode in question, as well as other data associated with the request. Initially onlyIORING_OP_SOCKETis supported.A minor nit...
[...]
Quoted 10 lines
io_uring/bpf_filter.c line 97
Quoted 6 lines
io_uring/bpf_filter.c new file +6
-0+/* +* Run registered filters for a given opcode. Return of 0 means that the +* request should be allowed. +*/ +int __io_uring_run_bpf_filters (struct io_restriction *res,struct io_kiocb *req) +{That comment seems to contradict the actual logic in this function, as well as the example BPF program in the cover letter. So s/allowed/blocked/?
Quoted 4 lines
Are you talking about
__io_uring_run_bpf_filters()or the filters themselves? For the former, 0 does indeed mean "yep let it rip", for the filters it's 0/1 where 0 is deny and 1 is allow. I should probably make the comment more explicit on that front...
Ah, yes, I got confused between the two, sorry for the noise.
jon
Jens Axboe
maintainerRe: [PATCH 2/3] io_uring: add support for BPF filtering for opcode restrictions
Quoted 26 lines · Jonathan Corbet
Quoted 24 lines · Jens Axboe
Quoted 22 lines · Jonathan Corbet
Quoted 5 lines · Jens Axboe
This adds support for loading BPF programs with
io_uring, which can restrict the opcodes performed. UnlikeIORING_REGISTER_RESTRICTIONS, using BPF programs allow fine grained control over both the opcode in question, as well as other data associated with the request. Initially onlyIORING_OP_SOCKETis supported.A minor nit...
[...]
io_uring/bpf_filter.c line 97
Quoted 6 lines
io_uring/bpf_filter.c new file +6
-0+/* +* Run registered filters for a given opcode. Return of 0 means that the +* request should be allowed. +*/ +int __io_uring_run_bpf_filters (struct io_restriction *res,struct io_kiocb *req) +{That comment seems to contradict the actual logic in this function, as well as the example BPF program in the cover letter. So s/allowed/blocked/?
Quoted 6 lines
Quoted 4 lines
Are you talking about
__io_uring_run_bpf_filters()or the filters themselves? For the former, 0 does indeed mean "yep let it rip", for the filters it's 0/1 where 0 is deny and 1 is allow. I should probably make the comment more explicit on that front...Ah, yes, I got confused between the two, sorry for the noise.
It's useful, I expanded the comment now:
/*
* Run registered filters for a given opcode. For filters, a return of 0 denies
* execution of the request, a return of 1 allows it. If any filter for an
* opcode returns 0, filter processing is stopped, and the request is denied.
* This also stops the processing of filters.
*
* __io_uring_run_bpf_filters() returns 0 on success, allow running the
* request, and -EACCES when a request is denied.
*/
I am making one more change in this patch though - to deny by default. If restrictions are registered with BPF, we should probably have the same logic as the classic restrictions, where if an opcode isn't explicitly enabled, it is denied. That makes it easier to future proof a filter set. I do want to do that without needing a dummy BPF program attached to each one, however...
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 3/3 io_uring: allow registration of per-task restrictions
In reply to Jens AxboeCurrently io_uring supports restricting operations on a per-ring basis. To use those, the ring must be setup in a disabled state by setting IORING_SETUP_R_DISABLED. Then restrictions can be set for the ring, and the ring can then be enabled.
This commit adds support for IORING_REGISTER_RESTRICTIONS_TASK, which allows to register the same kind of restrictions, but with the task itself rather than with a specific ring. Once done, any ring created will inherit these restrictions.
If a restriction filter is registered with a task, then it's inherited on fork for its children. Children may only further restrict operations, not extend them.
--- include/linux/io_uring.h | 2 +- include/linux/sched.h | 1 + include/uapi/linux/io_uring.h | 9 +++++ io_uring/io_uring.c | 14 ++++++++ io_uring/register.c | 65 +++++++++++++++++++++++++++++++++++ io_uring/tctx.c | 26 +++++++++----- kernel/fork.c | 4 +++ 7 files changed, 111 insertions(+), 10 deletions(-)
include/linux/io_uring.h +1 -1
@@ -25,7 +25,7 @@ static inline void io_uring_task_cancel(void) }static inline void io_uring_free (struct task_struct *tsk) { -if (tsk->io_uring) +if (tsk->io_uring || tsk->io_uring_restrict)__io_uring_free (tsk); }#else
include/linux/sched.h +1 -0
@@ -1190,6 +1190,7 @@ struct task_struct {#ifdef CONFIG_IO_URING struct io_uring_task *io_uring; +struct io_restriction *io_uring_restrict;#endif /* Namespaces: */
include/uapi/linux/io_uring.h +9 -0
@@ -703,6 +703,8 @@ enum io_uring_register_op {/* register bpf filtering programs */ IORING_REGISTER_BPF_FILTER =37 , +IORING_REGISTER_RESTRICTIONS_TASK =38 , +/* this goes last */ IORING_REGISTER_LAST,
@@ -808,6 +810,13 @@ struct io_uring_restriction {__u32 resv2[3 ]; }; +struct io_uring_task_restriction { +__u16 flags; +__u16 nr_res; +__u32 resv[3 ]; + __DECLARE_FLEX_ARRAY(struct io_uring_restriction, restrictions); +}; +struct io_uring_clock_register {__u32 clockid;__u32 __resv[3 ];
io_uring/io_uring.c +14 -0
@@ -3623,6 +3623,20 @@ static __cold int io_uring_create(struct io_ctx_config *config)else ctx->notify_method =TWA_SIGNAL ; +/* +* If the current task has restrictions enabled, then copy them to +* our newly created ring and mark it as registered. +*/ +if (current->io_uring_restrict) { +struct io_restriction *res = current->io_uring_restrict; + +refcount_inc (&res->refs); + ctx->restrictions = res; + ctx->op_restricted = res->op_registered; + ctx->reg_restricted = res->reg_registered; + ctx->bpf_restricted = res->bpf_registered; + } +/* * This is just grabbed for accounting purposes. When a process exits, * the mm is exited and dropped before the files, hence we need to hang
io_uring/register.c +65 -0
77 lines not on this page · raw message
io_uring/tctx.c +17 -9
@@ -11,6 +11,8 @@#include "io_uring.h" #include "tctx.h" +#include "register.h" +#include "bpf_filter.h" static struct io_wq *io_init_wq_offload (struct io_ring_ctx *ctx,struct task_struct *task)
@@ -54,16 +56,22 @@ void __io_uring_free(struct task_struct *tsk)* node is stored in the xarray. Until that gets sorted out, attempt * an iteration here and warn if any entries are found. */ -xa_for_each (&tctx->xa, index, node) { -WARN_ON_ONCE (1 ); -break ; - } -WARN_ON_ONCE (tctx->io_wq); -WARN_ON_ONCE (tctx->cached_refs); +if (tctx) { +xa_for_each (&tctx->xa, index, node) { +WARN_ON_ONCE (1 ); +break ; + } +WARN_ON_ONCE (tctx->io_wq); +WARN_ON_ONCE (tctx->cached_refs); -percpu_counter_destroy (&tctx->inflight); -kfree (tctx); - tsk->io_uring =NULL ; +percpu_counter_destroy (&tctx->inflight); +kfree (tctx); + tsk->io_uring =NULL ; + } +if (tsk->io_uring_restrict) { +io_put_restrictions (tsk->io_uring_restrict); + tsk->io_uring_restrict =NULL ; + } } __cold int io_uring_alloc_task_context(struct task_struct *task,
kernel/fork.c +4 -0
@@ -97,6 +97,7 @@#include <linux/kasan.h> #include <linux/scs.h> #include <linux/io_uring.h> +#include <linux/io_uring_types.h> #include <linux/bpf.h> #include <linux/stackprotector.h> #include <linux/user_events.h>
@@ -2129,6 +2130,8 @@ __latent_entropy struct task_struct *copy_process(#ifdef CONFIG_IO_URING p->io_uring =NULL ; +if (p->io_uring_restrict) +refcount_inc (&p->io_uring_restrict->refs);#endif p->default_timer_slack_ns = current->timer_slack_ns;
@@ -2525,6 +2528,7 @@ __latent_entropy struct task_struct *copy_process(mpol_put (p->mempolicy);#endif bad_fork_cleanup_delayacct: +io_uring_free (p);delayacct_tsk_free (p); bad_fork_cleanup_count:dec_rlimit_ucounts (task_ucounts (p),UCOUNT_RLIMIT_NPROC ,1 );
Signature · 1 line
2.51.0