]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
io_uring: Fix registered ring file refcount leak
authorJann Horn <jannh@google.com>
Wed, 18 Dec 2024 16:56:25 +0000 (17:56 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 27 Dec 2024 12:53:00 +0000 (13:53 +0100)
commit 12d908116f7efd34f255a482b9afc729d7a5fb78 upstream.

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: stable@vger.kernel.org
Fixes: e7a6c00dc77a ("io_uring: add support for registering ring file descriptors")
Signed-off-by: Jann Horn <jannh@google.com>
Link: https://lore.kernel.org/r/20241218-uring-reg-ring-cleanup-v1-1-8f63e999045b@google.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/io_uring.h
io_uring/io_uring.c

index a8f3058448eaa1253fc0354baa834c360be0bcd6..e0dec1bf917a4fb1983d80e8ccc63116cf87e85c 100644 (file)
@@ -50,10 +50,8 @@ bool io_is_uring_fops(struct file *file);
 
 static inline void io_uring_files_cancel(void)
 {
-       if (current->io_uring) {
-               io_uring_unreg_ringfd();
+       if (current->io_uring)
                __io_uring_cancel(false);
-       }
 }
 static inline void io_uring_task_cancel(void)
 {
index 0b136166326789dbb7632433536b0fb879ea51c6..ae7a9545b87734b688ae97cc1b3223dd910c8cd5 100644 (file)
@@ -3085,6 +3085,7 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
 
 void __io_uring_cancel(bool cancel_all)
 {
+       io_uring_unreg_ringfd();
        io_uring_cancel_generic(cancel_all, NULL);
 }