Skip to content

Commit 85022d8

Browse files
author
Ming Lei
committed
io_uring: Fix registered ring file refcount leak
JIRA: https://issues.redhat.com/browse/RHEL-106845 commit 12d9081 Author: Jann Horn <[email protected]> Date: Wed Dec 18 17:56:25 2024 +0100 io_uring: Fix registered ring file refcount leak Currently, io_uring_unreg_ringfd() (which cleans up registered rings) is only called on exit, but __io_uring_free (which frees the tctx in which the registered ring pointers are stored) is also called on execve (via begin_new_exec -> io_uring_task_cancel -> __io_uring_cancel -> io_uring_cancel_generic -> __io_uring_free). This means: A process going through execve while having registered rings will leak references to the rings' `struct file`. Fix it by zapping registered rings on execve(). This is implemented by moving the io_uring_unreg_ringfd() from io_uring_files_cancel() into its callee __io_uring_cancel(), which is called from io_uring_task_cancel() on execve. This could probably be exploited *on 32-bit kernels* by leaking 2^32 references to the same ring, because the file refcount is stored in a pointer-sized field and get_file() doesn't have protection against refcount overflow, just a WARN_ONCE(); but on 64-bit it should have no impact beyond a memory leak. Cc: [email protected] Fixes: e7a6c00 ("io_uring: add support for registering ring file descriptors") Signed-off-by: Jann Horn <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Ming Lei <[email protected]>
1 parent 902f18c commit 85022d8

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

include/linux/io_uring.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ bool io_is_uring_fops(struct file *file);
1515

1616
static inline void io_uring_files_cancel(void)
1717
{
18-
if (current->io_uring) {
19-
io_uring_unreg_ringfd();
18+
if (current->io_uring)
2019
__io_uring_cancel(false);
21-
}
2220
}
2321
static inline void io_uring_task_cancel(void)
2422
{

io_uring/io_uring.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,7 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
32253225

32263226
void __io_uring_cancel(bool cancel_all)
32273227
{
3228+
io_uring_unreg_ringfd();
32283229
io_uring_cancel_generic(cancel_all, NULL);
32293230
}
32303231

0 commit comments

Comments
 (0)