timeout immediate arg
cover timeout immediate arg
Allow the user to pass the timeout value inside the SQE instead of pointing to a timespec, people asked for it as it makes user space simpler. More details description is in Patch 2.
Pavel Begunkov (2):
io_uring/timeout: READ_ONCE sqe->addr
io_uring/timeout: immediate timeout arg
include/uapi/linux/io_uring.h | 5 +++++ io_uring/timeout.c | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/2 io_uring/timeout: READ_ONCE sqe->addr
We should use READ_ONCE when reading from a SQE, make sure timeout gets a stable timespec address.
--- io_uring/timeout.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/timeout.c +1 -1
@@ -557,7 +557,7 @@ static int __io_timeout_prep(struct io_kiocb *req, data->req = req; data->flags = flags; -if (get_timespec64 (&data->ts,u64_to_user_ptr (sqe->addr))) +if (get_timespec64 (&data->ts,u64_to_user_ptr (READ_ONCE (sqe->addr))))return -EFAULT ;if (data->ts.tv_sec <0 || data->ts.tv_nsec <0 )
Signature · 1 line
2.53.0
PATCH 2/2 io_uring/timeout: immediate timeout arg
In reply to Pavel BegunkovOne the things the user has always keep in mind is that any user pointers they put into an SQE is not going to be read by the kernel until submission happens, and the user has to ensure the pointee stays alive until then. For example, this snippet:
void prep_timeout(struct io_uring_sqe *sqe) {
struct __kernel_timespec ts = {...};
prep_timeout(sqe, &ts);
}void submit() {
sqe = get_sqe();
prep_timeout(sqe);
io_uring_submit();
}Would lead to UAF for the on stack variable ts. Instead of passing the timeout value as a pointer allow to store it immediately in the SQE. The user has to set a new flag called IORING_TIMEOUT_IMMEDIATE_ARG, in which case sqe->addr will be interpreted as the timeout value in ns. It only works with relative timeouts and rejected if set together with IORING_TIMEOUT_ABS out of concerns of not having enough range in u64 to represent a good long term API.
--- include/uapi/linux/io_uring.h | 5 +++++ io_uring/timeout.c | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-)
include/uapi/linux/io_uring.h +5 -0
@@ -340,6 +340,10 @@ enum io_uring_op {/* * sqe->timeout_flags +* +* IORING_TIMEOUT_IMMEDIATE_ARG: If set, sqe->addr stores the timeout +* value in nanoseconds instead of +* pointing to a timespec. */ #define IORING_TIMEOUT_ABS (1U << 0)#define IORING_TIMEOUT_UPDATE (1U << 1)
@@ -348,6 +352,7 @@ enum io_uring_op {#define IORING_LINK_TIMEOUT_UPDATE (1U << 4)#define IORING_TIMEOUT_ETIME_SUCCESS (1U << 5)#define IORING_TIMEOUT_MULTISHOT (1U << 6) +#define IORING_TIMEOUT_IMMEDIATE_ARG (1U << 7)#define IORING_TIMEOUT_CLOCK_MASK (IORING_TIMEOUT_BOOTTIME | IORING_TIMEOUT_REALTIME)#define IORING_TIMEOUT_UPDATE_MASK (IORING_TIMEOUT_UPDATE | IORING_LINK_TIMEOUT_UPDATE)/*
io_uring/timeout.c +9 -2
@@ -528,7 +528,8 @@ static int __io_timeout_prep(struct io_kiocb *req, flags =READ_ONCE (sqe->timeout_flags);if (flags & ~(IORING_TIMEOUT_ABS |IORING_TIMEOUT_CLOCK_MASK |IORING_TIMEOUT_ETIME_SUCCESS | -IORING_TIMEOUT_MULTISHOT )) +IORING_TIMEOUT_MULTISHOT | +IORING_TIMEOUT_IMMEDIATE_ARG ))return -EINVAL ;/* more than one clock specified is invalid, obviously */ if (hweight32 (flags &IORING_TIMEOUT_CLOCK_MASK ) >1 )
@@ -557,8 +558,14 @@ static int __io_timeout_prep(struct io_kiocb *req, data->req = req; data->flags = flags; -if (get_timespec64 (&data->ts,u64_to_user_ptr (READ_ONCE (sqe->addr)))) +if (flags &IORING_TIMEOUT_IMMEDIATE_ARG ) { +if (flags &IORING_TIMEOUT_ABS ) +return -EINVAL ; + data->ts =ns_to_timespec64 (READ_ONCE (sqe->addr)); + }else if (get_timespec64 (&data->ts, +u64_to_user_ptr (READ_ONCE (sqe->addr)))) {return -EFAULT ; + }if (data->ts.tv_sec <0 || data->ts.tv_nsec <0 )return -EINVAL ;
Signature · 1 line
2.53.0
Re: [PATCH 1/2] io_uring/timeout: READ_ONCE sqe->addr
In reply to Pavel Begunkovio_uring/timeout.c static int __io_timeout_prep(struct io_kiocb *req, · line 561
Quoted 7 lines · Pavel Begunkov
io_uring/timeout.c +1
-1@@ -557,7 +557,7 @@ static int __io_timeout_prep(struct io_kiocb *req, data->req = req; data->flags = flags; -if (get_timespec64 (&data->ts,u64_to_user_ptr (sqe->addr))) +if (get_timespec64 (&data->ts,u64_to_user_ptr (READ_ONCE (sqe->addr))))return -EFAULT ;
Should io_timeout_remove_prep() get the same update? Otherwise looks good.
Jens Axboe
maintainerRe: [PATCH 1/2] io_uring/timeout: READ_ONCE sqe->addr
Quoted 11 lines · Keith Busch
io_uring/timeout.c static int __io_timeout_prep(struct io_kiocb *req, · line 561
Quoted 7 lines · Pavel Begunkov
io_uring/timeout.c +1
-1@@ -557,7 +557,7 @@ static int __io_timeout_prep(struct io_kiocb *req, data->req = req; data->flags = flags; -if (get_timespec64 (&data->ts,u64_to_user_ptr (sqe->addr))) +if (get_timespec64 (&data->ts,u64_to_user_ptr (READ_ONCE (sqe->addr))))return -EFAULT ;Should
io_timeout_remove_prep()get the same update? Otherwise looks good.
Yep looks like. Just went hunting if we missed this in other spots, and only other one I can find is in io_uring_cmd_getsockname().
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerRe: [PATCH 2/2] io_uring/timeout: immediate timeout arg
In reply to Pavel BegunkovQuoted 73 lines · Pavel Begunkov
One the things the user has always keep in mind is that any user pointers they put into an SQE is not going to be read by the kernel until submission happens, and the user has to ensure the pointee stays alive until then. For example, this snippet:
void prep_timeout(struct io_uring_sqe *sqe) { struct __kernel_timespec ts = {...}; prep_timeout(sqe, &ts); }void submit() { sqe = get_sqe(); prep_timeout(sqe); io_uring_submit(); }Would lead to UAF for the on stack variable ts. Instead of passing the timeout value as a pointer allow to store it immediately in the SQE. The user has to set a new flag called
IORING_TIMEOUT_IMMEDIATE_ARG, in which casesqe->addrwill be interpreted as the timeout value in ns. It only works with relative timeouts and rejected if set together withIORING_TIMEOUT_ABSout of concerns of not having enough range in u64 to represent a good long term API.Signed-off-by Pavel Begunkov <[email protected]>--- include/uapi/linux/io_uring.h | 5 +++++ io_uring/timeout.c | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-)include/uapi/linux/io_uring.h +5
-0@@ -340,6 +340,10 @@ enum io_uring_op {/* * sqe->timeout_flags +* +* IORING_TIMEOUT_IMMEDIATE_ARG: If set, sqe->addr stores the timeout +* value in nanoseconds instead of +* pointing to a timespec. */ #define IORING_TIMEOUT_ABS (1U << 0)#define IORING_TIMEOUT_UPDATE (1U << 1)@@ -348,6 +352,7 @@ enum io_uring_op {#define IORING_LINK_TIMEOUT_UPDATE (1U << 4)#define IORING_TIMEOUT_ETIME_SUCCESS (1U << 5)#define IORING_TIMEOUT_MULTISHOT (1U << 6) +#define IORING_TIMEOUT_IMMEDIATE_ARG (1U << 7)#define IORING_TIMEOUT_CLOCK_MASK (IORING_TIMEOUT_BOOTTIME | IORING_TIMEOUT_REALTIME)#define IORING_TIMEOUT_UPDATE_MASK (IORING_TIMEOUT_UPDATE | IORING_LINK_TIMEOUT_UPDATE)/* io_uring/timeout.c +3
-2@@ -528,7 +528,8 @@ static int __io_timeout_prep(struct io_kiocb *req, flags =READ_ONCE (sqe->timeout_flags);if (flags & ~(IORING_TIMEOUT_ABS |IORING_TIMEOUT_CLOCK_MASK |IORING_TIMEOUT_ETIME_SUCCESS | -IORING_TIMEOUT_MULTISHOT )) +IORING_TIMEOUT_MULTISHOT | +IORING_TIMEOUT_IMMEDIATE_ARG ))return -EINVAL ;/* more than one clock specified is invalid, obviously */ if (hweight32 (flags &IORING_TIMEOUT_CLOCK_MASK ) >1 )@@ -557,8 +558,14 @@ static int __io_timeout_prep(struct io_kiocb *req, data->req = req; data->flags = flags; -if (get_timespec64 (&data->ts,u64_to_user_ptr (READ_ONCE (sqe->addr)))) +if (flags &IORING_TIMEOUT_IMMEDIATE_ARG ) {
io_uring/timeout.c static int __io_timeout_prep(struct io_kiocb *req, · line 571
Quoted 10 lines
io_uring/timeout.c +6
-0+if (flags &IORING_TIMEOUT_ABS ) +return -EINVAL ; + data->ts =ns_to_timespec64 (READ_ONCE (sqe->addr)); + }else if (get_timespec64 (&data->ts, +u64_to_user_ptr (READ_ONCE (sqe->addr)))) {return -EFAULT ; + }if (data->ts.tv_sec <0 || data->ts.tv_nsec <0 )return -EINVAL ;
Looks good to me on the feature side, makes sense. But like the 1/2 patch, this one needs to update the remove side as well to support the immediate arg.
Signature · 1 line
Jens Axboe
Re: [PATCH 1/2] io_uring/timeout: READ_ONCE sqe->addr
In reply to Keith BuschQuoted 11 lines · Keith Busch
io_uring/timeout.c static int __io_timeout_prep(struct io_kiocb *req, · line 561
Quoted 7 lines · Pavel Begunkov
io_uring/timeout.c +1
-1@@ -557,7 +557,7 @@ static int __io_timeout_prep(struct io_kiocb *req, data->req = req; data->flags = flags; -if (get_timespec64 (&data->ts,u64_to_user_ptr (sqe->addr))) +if (get_timespec64 (&data->ts,u64_to_user_ptr (READ_ONCE (sqe->addr))))return -EFAULT ;Should
io_timeout_remove_prep()get the same update? Otherwise looks good.
good catch, it misses it as well
Signature · 1 line
Pavel Begunkov