Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: stable@vger.kernel.org
Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org
Subject: [PATCH 6.6.y 09/13] io_uring/cancel: always walk links under timeout_lock
Date: Sat,  5 Dec 2026 23:12:29 +0000	[thread overview]
Message-ID: <20261205231233.4140988-10-axboe@kernel.dk> (raw)
In-Reply-To: <20261205231233.4140988-1-axboe@kernel.dk>

From: Chris Mason <mason@kernel.org>

Commit a6b53de207d3472f209e71fd4990be3dd3133d1f upstream.

The exit of a single thread that races with the close of its
IORING_SETUP_DEFER_TASKRUN ring can cause a use-after-free of a linked
timeout, if io-wq is the first to issue the request that the timeout is
linked to.

When io-wq calls io_match_task_safe(), ->timeout_lock alone protects the
walk of the link chain against the removal of a linked timeout. The
function takes the lock only if the head has REQ_F_LINK_TIMEOUT. But the
first issue sets that flag. If that happens in an io-wq worker,
cancellation can find the request as the current work of the worker
before the flag is set:

    io_match_task_safe()          io-wq worker, timer, task_work
    ====================          ==============================
    REQ_F_LINK_TIMEOUT is clear
    io_match_linked()
      req = head->link
                                  __io_issue_sqe()
                                    __io_prep_linked_timeout()
                                    io_queue_linked_timeout()
                                  io_link_timeout_fn()
                                  io_req_task_link_timeout()
                                  __io_req_caches_free()
      req = req->link

The fix used here is to take ->timeout_lock for every walk, whatever the
flags of the head say.

Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
[axboe: 6.6 has io_match_task_safe() in io_uring.c]
---
 io_uring/io_uring.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 1dd7bfcfc80a..7a05875d2b7d 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -210,6 +210,7 @@ static bool io_match_linked(struct io_kiocb *head)
 bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
 			bool cancel_all)
 {
+	struct io_ring_ctx *ctx = head->ctx;
 	bool matched;
 
 	if (task && head->task != task)
@@ -217,16 +218,13 @@ bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
 	if (cancel_all)
 		return true;
 
-	if (head->flags & REQ_F_LINK_TIMEOUT) {
-		struct io_ring_ctx *ctx = head->ctx;
-
-		/* protect against races with linked timeouts */
-		raw_spin_lock_irq(&ctx->timeout_lock);
-		matched = io_match_linked(head);
-		raw_spin_unlock_irq(&ctx->timeout_lock);
-	} else {
-		matched = io_match_linked(head);
-	}
+	/*
+	 * Protect against races with linked timeouts. REQ_F_LINK_TIMEOUT can't
+	 * tell if there is one: an io-wq worker may set it during the walk.
+	 */
+	raw_spin_lock_irq(&ctx->timeout_lock);
+	matched = io_match_linked(head);
+	raw_spin_unlock_irq(&ctx->timeout_lock);
 	return matched;
 }
 
-- 
2.55.0


  parent reply	other threads:[~2026-12-05 23:12 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 23:12 [PATCH 6.6.y 00/13] io_uring fixes for 6.6-stable Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 01/13] io_uring/net: don't charge the folio offset to truesize for managed frags Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 02/13] io_uring/net: don't copy msg_name on a recvmsg retry Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 03/13] io_uring/tctx: add separate lock for list of tctx's in ctx Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 04/13] io_uring: free the task context after a grace period Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 05/13] io_uring: free the ring " Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 06/13] io_uring/filetable: don't install O_PATH files Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 07/13] io_uring: make ctx->timeout_lock a raw spinlock Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 08/13] io_uring/timeout: flush timeouts outside of the timeout lock Jens Axboe
2026-12-05 23:12 ` Jens Axboe [this message]
2026-12-05 23:12 ` [PATCH 6.6.y 10/13] io_uring/net: restore the cmsg buffer on recvmsg retry Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 11/13] io_uring_poll: kill the no longer necessary barrier after poll_wait() Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 12/13] io_uring: close poll activation race when enabling rings Jens Axboe
2026-12-05 23:12 ` [PATCH 6.6.y 13/13] io_uring/cancel: arm local work wakeups only for submitter Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205231233.4140988-10-axboe@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=gregkh@linuxfoundation.org \
    --cc=io-uring@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox