Inherited restrictions and BPF filtering for io_uring
Subsystems (4)
Jens Axboe
maintainerPATCH v7 1/1 Inherited restrictions and BPF filtering for io_uring
Hi,
Followup to v6 here:
https://lore.kernel.org/io-uring/[email protected]/
Mostly just addressing a bit of feedback, feature wise this is all the same as before. See the changelog section for more details.
Kernel branch can be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git/log/?h=io_uring-bpf-restrictions.4
and a liburing branch with support helpers, man page, and a fairly substantial test case can be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/liburing.git/log/?h=bpf-restrictions
Feedback welcome!
Changes since v6
- Add pdu_size to struct io_uring_bpf_ctx. This will help future proof filters as they can check if they agree with the kernel on what is available, and it eliminates the need to pad the struct out for future expansion (Christian)
- Various code cleanups (Christian)
- Fix for FORTIFY on how the bctx filter struct is cleared.
- Rebase on current for-7.0/io_uring tree.
include/linux/io_uring.h | 14 +- include/linux/io_uring_types.h | 13 + include/linux/sched.h | 1 + include/uapi/linux/io_uring.h | 10 + include/uapi/linux/io_uring/bpf_filter.h | 62 ++++ io_uring/Kconfig | 5 + io_uring/Makefile | 1 + io_uring/bpf_filter.c | 430 +++++++++++++++++++++++ io_uring/bpf_filter.h | 48 +++ io_uring/io_uring.c | 48 +++ io_uring/io_uring.h | 1 + io_uring/net.c | 9 + io_uring/net.h | 6 + io_uring/openclose.c | 9 + io_uring/openclose.h | 3 + io_uring/register.c | 91 +++++ io_uring/tctx.c | 42 ++- kernel/fork.c | 5 + 18 files changed, 788 insertions(+), 10 deletions(-)
--
Jens Axboe
Jens Axboe
maintainerPATCH 1/7 io_uring: add support for BPF filtering for opcode restrictions
Add support for loading classic BPF programs with io_uring to provide fine-grained filtering of SQE operations. Unlike IORING_REGISTER_RESTRICTIONS which only allows bitmap-based allow/deny of opcodes, BPF filters can inspect request attributes and make dynamic decisions.
The filter is registered via IORING_REGISTER_BPF_FILTER with a struct io_uring_bpf:
struct io_uring_bpf_filter {
__u32 opcode; /* io_uring opcode to filter */
__u32 flags;
__u32 filter_len; /* number of BPF instructions */
__u32 resv;
__u64 filter_ptr; /* pointer to BPF filter */
__u64 resv2[5];
};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;
};
};and the filters get supplied a struct io_uring_bpf_ctx:
struct io_uring_bpf_ctx {
__u64 user_data;
__u8 opcode;
__u8 sqe_flags;
__u8 pdu_size;
__u8 pad[5];
};where it's possible to filter on opcode and sqe_flags, with pdu_size indicating how much extra data is being passed in beyond the pad field. This will used for specific finer grained filtering inside an opcode. An example of that for sockets is in one of the following patches. Anything the opcode supports can end up in this struct, populated by the opcode itself, and hence can be filtered for.
Filters have the following semantics:
- Return 1 to allow the request
- Return 0 to deny the request with -EACCES
- Multiple filters can be stacked per opcode. All filters must return 1 for the opcode to be allowed.
- Filters are evaluated in registration order (most recent first)
The implementation uses classic BPF (cBPF) rather than eBPF for as that's required for containers, and since they can be used by any user in the system.
--- include/linux/io_uring_types.h | 9 + include/uapi/linux/io_uring.h | 3 + include/uapi/linux/io_uring/bpf_filter.h | 50 ++++ io_uring/Kconfig | 5 + io_uring/Makefile | 1 + io_uring/bpf_filter.c | 321 +++++++++++++++++++++++ io_uring/bpf_filter.h | 42 +++ io_uring/io_uring.c | 8 + io_uring/register.c | 8 + 9 files changed, 447 insertions(+) create mode 100644 include/uapi/linux/io_uring/bpf_filter.h create mode 100644 io_uring/bpf_filter.c create mode 100644 io_uring/bpf_filter.h
include/linux/io_uring_types.h +9 -0
@@ -219,9 +219,18 @@ struct io_rings {struct io_uring_cqe cqes[]____cacheline_aligned_in_smp ; }; +struct io_bpf_filter ; +struct io_bpf_filters { +refcount_t refs;/* ref for ->bpf_filters */ +spinlock_t lock;/* protects ->bpf_filters modifications */ +struct io_bpf_filter __rcu **filters; +struct rcu_head rcu_head; +}; +struct io_restriction { DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); DECLARE_BITMAP(sqe_op, IORING_OP_LAST); +struct io_bpf_filters *bpf_filters;u8 sqe_flags_allowed;u8 sqe_flags_required;/* IORING_OP_* restrictions exist */
include/uapi/linux/io_uring.h +3 -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,
include/uapi/linux/io_uring/bpf_filter.h new file +50 -0
50 lines not on this page · raw message
io_uring/Kconfig +5 -0
@@ -9,3 +9,8 @@ config IO_URING_ZCRX depends on PAGE_POOL depends on INET depends on NET_RX_BUSY_POLL + +config IO_URING_BPF + def_bool y + depends on BPF + depends on NET
io_uring/Makefile +1 -0
@@ -24,3 +24,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_IO_URING_BPF) += bpf_filter.o
io_uring/bpf_filter.c new file +321 -0
321 lines not on this page · raw message
io_uring/bpf_filter.h new file +42 -0
42 lines not on this page · raw message
io_uring/io_uring.c +8 -0
@@ -94,6 +94,7 @@#include "alloc_cache.h" #include "eventfd.h" #include "wait.h" +#include "bpf_filter.h" #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ IOSQE_IO_HARDLINK | IOSQE_ASYNC)
@@ -1874,6 +1875,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->restrictions.bpf_filters)) { + ret =io_uring_run_bpf_filters (&ctx->restrictions, req); +if (ret) +return io_submit_fail_init (sqe, req, ret); + } +trace_io_uring_submit_req (req);/*
@@ -2161,6 +2168,7 @@ 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); +io_put_bpf_filters (&ctx->restrictions);WARN_ON_ONCE (ctx->nr_req_allocated);
io_uring/register.c +8 -0
@@ -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)
@@ -830,6 +831,13 @@ 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 ; + ret =io_register_bpf_filter (&ctx->restrictions, arg); +break ;default : ret = -EINVAL ;break ;
Signature · 1 line
2.51.0
Jens Axboe
maintainerPATCH 2/7 io_uring/net: allow filtering on IORING_OP_SOCKET data
In reply to Jens AxboeExample population method for the BPF based opcode filtering. This exposes the socket family, type, and protocol to a registered BPF filter. This in turn enables the filter to make decisions based on what was passed in to the IORING_OP_SOCKET request type.
--- include/uapi/linux/io_uring/bpf_filter.h | 7 +++++++ io_uring/bpf_filter.c | 11 +++++++++++ io_uring/net.c | 9 +++++++++ io_uring/net.h | 6 ++++++ 4 files changed, 33 insertions(+)
include/uapi/linux/io_uring/bpf_filter.h +7 -0
@@ -16,6 +16,13 @@ struct io_uring_bpf_ctx {__u8 sqe_flags;__u8 pdu_size;/* size of aux data for filter */ __u8 pad[5 ]; +union { +struct { +__u32 family; +__u32 type; +__u32 protocol; + } socket; + }; };enum {
io_uring/bpf_filter.c +11 -0
@@ -30,6 +30,17 @@ static void io_uring_populate_bpf_ctx(struct io_uring_bpf_ctx *bctx,/* clear residual, anything from pdu_size and below */ memset ((void *) bctx + offsetof(struct io_uring_bpf_ctx , pdu_size),0 ,sizeof (*bctx) - offsetof(struct io_uring_bpf_ctx , pdu_size)); + +/* +* Opcodes can provide a handler fo populating more data into bctx, +* for filters to use. +*/ +switch (req->opcode) { +case IORING_OP_SOCKET : + bctx->pdu_size =sizeof (bctx->socket); +io_socket_bpf_populate (bctx, req); +break ; + } }/*
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 +6 -0
@@ -3,6 +3,7 @@#include <linux/net.h> #include <linux/uio.h> #include <linux/io_uring_types.h> +#include <uapi/linux/io_uring/bpf_filter.h> struct io_async_msghdr {#if defined(CONFIG_NET )
@@ -44,6 +45,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 +66,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
Signature · 1 line
2.51.0
Jens Axboe
maintainerPATCH 3/7 io_uring/bpf_filter: allow filtering on contents of struct open_how
In reply to Jens AxboeThis adds custom filtering for IORING_OP_OPENAT and IORING_OP_OPENAT2, where the open_how flags, mode, and resolve can be checked by filters.
--- include/uapi/linux/io_uring/bpf_filter.h | 5 +++++ io_uring/bpf_filter.c | 6 ++++++ io_uring/openclose.c | 9 +++++++++ io_uring/openclose.h | 3 +++ 4 files changed, 23 insertions(+)
include/uapi/linux/io_uring/bpf_filter.h +5 -0
@@ -22,6 +22,11 @@ struct io_uring_bpf_ctx {__u32 type;__u32 protocol; } socket; +struct { +__u64 flags; +__u64 mode; +__u64 resolve; + } open; }; };
io_uring/bpf_filter.c +6 -0
@@ -12,6 +12,7 @@#include "io_uring.h" #include "bpf_filter.h" #include "net.h" +#include "openclose.h" struct io_bpf_filter {struct bpf_prog *prog;
@@ -40,6 +41,11 @@ static void io_uring_populate_bpf_ctx(struct io_uring_bpf_ctx *bctx, bctx->pdu_size =sizeof (bctx->socket);io_socket_bpf_populate (bctx, req);break ; +case IORING_OP_OPENAT : +case IORING_OP_OPENAT2 : + bctx->pdu_size =sizeof (bctx->open); +io_openat_bpf_populate (bctx, req); +break ; } }
io_uring/openclose.c +9 -0
@@ -85,6 +85,15 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqereturn 0 ; } +void io_openat_bpf_populate (struct io_uring_bpf_ctx *bctx,struct io_kiocb *req) +{ + struct io_open *open = io_kiocb_to_cmd(req, struct io_open); + + bctx->open.flags = open->how.flags; + bctx->open.mode = open->how.mode; + bctx->open.resolve = open->how.resolve; +} +int io_openat_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe) { struct io_open *open = io_kiocb_to_cmd(req, struct io_open);
io_uring/openclose.h +3 -0
@@ -1,11 +1,14 @@// SPDX-License-Identifier: GPL-2.0 +#include "bpf_filter.h" +int __io_close_fixed (struct io_ring_ctx *ctx,unsigned int issue_flags,unsigned int offset);int io_openat_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);int io_openat (struct io_kiocb *req,unsigned int issue_flags);void io_open_cleanup (struct io_kiocb *req); +void io_openat_bpf_populate (struct io_uring_bpf_ctx *bctx,struct io_kiocb *req);int io_openat2_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);int io_openat2 (struct io_kiocb *req,unsigned int issue_flags);
Signature · 1 line
2.51.0
Jens Axboe
maintainerPATCH 4/7 io_uring/bpf_filter: cache lookup table in ctx->bpf_filters
In reply to Jens AxboeCurrently a few pointer dereferences need to be made to both check if BPF filters are installed, and then also to retrieve the actual filter for the opcode. Cache the table in ctx->bpf_filters to avoid that.
Add a bit of debug info on ring exit to show if we ever got this wrong. Small risk of that given that the table is currently only updated in one spot, but once task forking is enabled, that will add one more spot.
--- include/linux/io_uring_types.h | 2 ++ io_uring/bpf_filter.c | 7 ++++--- io_uring/bpf_filter.h | 10 +++++----- io_uring/io_uring.c | 11 +++++++++-- io_uring/register.c | 3 +++ 5 files changed, 23 insertions(+), 10 deletions(-)
include/linux/io_uring_types.h +2 -0
@@ -287,6 +287,8 @@ struct io_ring_ctx {struct task_struct *submitter_task;struct io_rings *rings; +/* cache of ->restrictions.bpf_filters->filters */ +struct io_bpf_filter __rcu **bpf_filters;struct percpu_ref refs;clockid_t clockid;
io_uring/bpf_filter.c +4 -3
@@ -58,14 +58,15 @@ static void io_uring_populate_bpf_ctx(struct io_uring_bpf_ctx *bctx,* __io_uring_run_bpf_filters() returns 0 on success, allow running the * request, and -EACCES when a request is denied. */ -int __io_uring_run_bpf_filters (struct io_restriction *res,struct io_kiocb *req) +int __io_uring_run_bpf_filters (struct io_bpf_filter __rcu **filters, +struct io_kiocb *req) {struct io_bpf_filter *filter;struct io_uring_bpf_ctx bpf_ctx;int ret;/* Fast check for existence of filters outside of RCU */ -if (!rcu_access_pointer (res->bpf_filters->filters[req->opcode])) +if (!rcu_access_pointer (filters[req->opcode]))return 0 ;/*
@@ -73,7 +74,7 @@ int __io_uring_run_bpf_filters(struct io_restriction *res, struct io_kiocb *req)* of what we expect, io_init_req() does this. */ guard (rcu)(); - filter =rcu_dereference (res->bpf_filters->filters[req->opcode]); + filter =rcu_dereference (filters[req->opcode]);if (!filter)return 0 ;else if (filter == &dummy_filter)
io_uring/bpf_filter.h +5 -5
@@ -6,18 +6,18 @@#ifdef CONFIG_IO_URING_BPF -int __io_uring_run_bpf_filters (struct io_restriction *res,struct io_kiocb *req); +int __io_uring_run_bpf_filters (struct io_bpf_filter __rcu **filters,struct io_kiocb *req);int io_register_bpf_filter (struct io_restriction *res,struct io_uring_bpf __user *arg);void io_put_bpf_filters (struct io_restriction *res); -static inline int io_uring_run_bpf_filters (struct io_restriction *res, +static inline int io_uring_run_bpf_filters (struct io_bpf_filter __rcu **filters,struct io_kiocb *req) { -if (res->bpf_filters) -return __io_uring_run_bpf_filters (res, req); +if (filters) +return __io_uring_run_bpf_filters (filters, req);return 0 ; }
@@ -29,7 +29,7 @@ static inline int io_register_bpf_filter(struct io_restriction *res, {return -EINVAL ; } -static inline int io_uring_run_bpf_filters (struct io_restriction *res, +static inline int io_uring_run_bpf_filters (struct io_bpf_filter __rcu **filters,struct io_kiocb *req) {return 0 ;
io_uring/io_uring.c +9 -2
@@ -1875,8 +1875,8 @@ 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->restrictions.bpf_filters)) { - ret =io_uring_run_bpf_filters (&ctx->restrictions, req); +if (unlikely (ctx->bpf_filters)) { + ret =io_uring_run_bpf_filters (ctx->bpf_filters, req);if (ret)return io_submit_fail_init (sqe, req, ret); }
@@ -2168,6 +2168,13 @@ 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.bpf_filters) { +WARN_ON_ONCE (ctx->bpf_filters != + ctx->restrictions.bpf_filters->filters); + }else { +WARN_ON_ONCE (ctx->bpf_filters); + }io_put_bpf_filters (&ctx->restrictions);WARN_ON_ONCE (ctx->nr_req_allocated);
io_uring/register.c +3 -0
@@ -837,6 +837,9 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,if (nr_args !=1 )break ; ret =io_register_bpf_filter (&ctx->restrictions, arg); +if (!ret) +WRITE_ONCE (ctx->bpf_filters, + ctx->restrictions.bpf_filters->filters);break ;default : ret = -EINVAL ;
Signature · 1 line
2.51.0
Jens Axboe
maintainerPATCH 5/7 io_uring/bpf_filter: add ref counts to struct io_bpf_filter
In reply to Jens AxboeIn preparation for allowing inheritance of BPF filters and filter tables, add a reference count to the filter. This allows multiple tables to safely include the same filter.
--- io_uring/bpf_filter.c | 7 +++++++ 1 file changed, 7 insertions(+)
io_uring/bpf_filter.c +7 -0
@@ -15,6 +15,7 @@#include "openclose.h" struct io_bpf_filter { +refcount_t refs;struct bpf_prog *prog;struct io_bpf_filter *next; };
@@ -125,6 +126,11 @@ static void io_free_bpf_filters(struct rcu_head *head)*/ if (f == &dummy_filter)break ; + +/* Someone still holds a ref, stop iterating. */ +if (!refcount_dec_and_test (&f->refs)) +break ; +bpf_prog_destroy (f->prog);kfree (f); f = next;
@@ -298,6 +304,7 @@ int io_register_bpf_filter(struct io_restriction *res, ret = -ENOMEM ; goto err; } +refcount_set (&filter->refs,1 ); filter->prog = prog; res->bpf_filters = filters;
Signature · 1 line
2.51.0
Jens Axboe
maintainerPATCH 6/7 io_uring: add task fork hook
In reply to Jens AxboeCalled when copy_process() is called to copy state to a new child. Right now this is just a stub, but will be used shortly to properly handle fork'ing of task based io_uring restrictions.
--- include/linux/io_uring.h | 14 +++++++++++++- include/linux/sched.h | 1 + io_uring/tctx.c | 25 ++++++++++++++++--------- kernel/fork.c | 5 +++++ 4 files changed, 35 insertions(+), 10 deletions(-)
include/linux/io_uring.h +13 -1
@@ -12,6 +12,7 @@ void __io_uring_free(struct task_struct *tsk);void io_uring_unreg_ringfd (void );const char *io_uring_get_opcode (u8 opcode);bool io_is_uring_fops (struct file *file); +int __io_uring_fork (struct task_struct *tsk);static inline void io_uring_files_cancel (void ) {
@@ -25,9 +26,16 @@ 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); } +static inline int io_uring_fork (struct task_struct *tsk) +{ +if (tsk->io_uring_restrict) +return __io_uring_fork (tsk); + +return 0 ; +}#else static inline void io_uring_task_cancel (void ) {
@@ -46,6 +54,10 @@ static inline bool io_is_uring_fops(struct file *file) {return false; } +static inline int io_uring_fork (struct task_struct *tsk) +{ +return 0 ; +}#endif #endif
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: */
io_uring/tctx.c +16 -9
@@ -54,16 +54,18 @@ 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 ; + } } __cold int io_uring_alloc_task_context(struct task_struct *task,
@@ -351,3 +353,8 @@ int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg,return i ? i : ret; } + +int __io_uring_fork (struct task_struct *tsk) +{ +return 0 ; +}
kernel/fork.c +5 -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,9 @@ __latent_entropy struct task_struct *copy_process(#ifdef CONFIG_IO_URING p->io_uring =NULL ; + retval =io_uring_fork (p); +if (unlikely (retval)) + goto bad_fork_cleanup_delayacct;#endif p->default_timer_slack_ns = current->timer_slack_ns;
@@ -2525,6 +2529,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
Jens Axboe
maintainerPATCH 7/7 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 with ring_fd == -1, like the other "blind" register opcodes which work on the task rather than a specific ring. This allows registration of the same kind of restrictions as can been done on a specific ring, but with the task itself. 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.
Inheriting restrictions include both the classic IORING_REGISTER_RESTRICTIONS based restrictions, as well as the BPF filters that have been registered with the task via IORING_REGISTER_BPF_FILTER.
--- include/linux/io_uring_types.h | 2 + include/uapi/linux/io_uring.h | 7 +++ io_uring/bpf_filter.c | 86 +++++++++++++++++++++++++++++++++- io_uring/bpf_filter.h | 6 +++ io_uring/io_uring.c | 33 +++++++++++++ io_uring/io_uring.h | 1 + io_uring/register.c | 80 +++++++++++++++++++++++++++++++ io_uring/tctx.c | 17 +++++++ 8 files changed, 231 insertions(+), 1 deletion(-)
include/linux/io_uring_types.h +2 -0
@@ -231,6 +231,8 @@ struct io_restriction {DECLARE_BITMAP (register_op,IORING_REGISTER_LAST );DECLARE_BITMAP (sqe_op,IORING_OP_LAST );struct io_bpf_filters *bpf_filters; +/* ->bpf_filters needs COW on modification */ +bool bpf_filters_cow;u8 sqe_flags_allowed;u8 sqe_flags_required;/* IORING_OP_* restrictions exist */
include/uapi/linux/io_uring.h +7 -0
@@ -808,6 +808,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/bpf_filter.c +85 -1
110 lines not on this page · raw message
io_uring/bpf_filter.h +6 -0
@@ -13,6 +13,8 @@ int io_register_bpf_filter(struct io_restriction *res,void io_put_bpf_filters (struct io_restriction *res); +void io_bpf_filter_clone (struct io_restriction *dst,struct io_restriction *src); +static inline int io_uring_run_bpf_filters (struct io_bpf_filter __rcu **filters,struct io_kiocb *req) {
@@ -37,6 +39,10 @@ static inline int io_uring_run_bpf_filters(struct io_bpf_filter __rcu **filters,static inline void io_put_bpf_filters (struct io_restriction *res) { } +static inline void io_bpf_filter_clone (struct io_restriction *dst, +struct io_restriction *src) +{ +}#endif /* CONFIG_IO_URING_BPF */ #endif
io_uring/io_uring.c +33 -0
@@ -2880,6 +2880,32 @@ int io_prepare_config(struct io_ctx_config *config)return 0 ; } +void io_restriction_clone (struct io_restriction *dst,struct io_restriction *src) +{ +memcpy (&dst->register_op, &src->register_op,sizeof (dst->register_op)); +memcpy (&dst->sqe_op, &src->sqe_op,sizeof (dst->sqe_op)); + dst->sqe_flags_allowed = src->sqe_flags_allowed; + dst->sqe_flags_required = src->sqe_flags_required; + dst->op_registered = src->op_registered; + dst->reg_registered = src->reg_registered; + +io_bpf_filter_clone (dst, src); +} + +static void io_ctx_restriction_clone (struct io_ring_ctx *ctx, +struct io_restriction *src) +{ +struct io_restriction *dst = &ctx->restrictions; + +io_restriction_clone (dst, src); +if (dst->bpf_filters) +WRITE_ONCE (ctx->bpf_filters, dst->bpf_filters->filters); +if (dst->op_registered) + ctx->op_restricted =1 ; +if (dst->reg_registered) + ctx->reg_restricted =1 ; +} +static __cold int io_uring_create (struct io_ctx_config *config) {struct io_uring_params *p = &config->p;
@@ -2940,6 +2966,13 @@ 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) +io_ctx_restriction_clone (ctx, current->io_uring_restrict); +/* * 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/io_uring.h +1 -0
@@ -197,6 +197,7 @@ void io_task_refs_refill(struct io_uring_task *tctx);bool __io_alloc_req_refill (struct io_ring_ctx *ctx);void io_activate_pollwq (struct io_ring_ctx *ctx); +void io_restriction_clone (struct io_restriction *dst,struct io_restriction *src);static inline void io_lockdep_assert_cq_locked (struct io_ring_ctx *ctx) {
io_uring/register.c +80 -0
92 lines not on this page · raw message
io_uring/tctx.c +17 -0
@@ -11,6 +11,7 @@#include "io_uring.h" #include "tctx.h" +#include "bpf_filter.h" static struct io_wq *io_init_wq_offload (struct io_ring_ctx *ctx,struct task_struct *task)
@@ -66,6 +67,11 @@ void __io_uring_free(struct task_struct *tsk)kfree (tctx); tsk->io_uring =NULL ; } +if (tsk->io_uring_restrict) { +io_put_bpf_filters (tsk->io_uring_restrict); +kfree (tsk->io_uring_restrict); + tsk->io_uring_restrict =NULL ; + } } __cold int io_uring_alloc_task_context(struct task_struct *task,
@@ -356,5 +362,16 @@ int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg,int __io_uring_fork (struct task_struct *tsk) { +struct io_restriction *res, *src = tsk->io_uring_restrict; + +/* Don't leave it dangling on error */ + tsk->io_uring_restrict =NULL ; + + res =kzalloc (sizeof (*res),GFP_KERNEL_ACCOUNT ); +if (!res) +return -ENOMEM ; + + tsk->io_uring_restrict = res; +io_restriction_clone (res, src);return 0 ; }
Signature · 1 line
2.51.0