From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512290; b=Cv55Qgh4zQa0Op69LTFBUE060UJwJRq5WLuZ9FrRE6A7rXYK+2kpJ2Ly4WDPEDIlxXSr bjvs8C5jGKG3ylPX0druxemPdgKUlt2wmboEvxyeKBE3O59WB5Gy08Cl+1xinT43LR36h i/YTAYasidnLITzak1HhbLrLz/m2pkhyRPDBxOxQKAQQ5lmYwPmbKjN0FctYMT8iAKEat TRJY5VDAyUzks3THAxPTPVMVBytTGPEgPOyprZf15c7O43WZS7Ot9edSmn9pgJl2LP8Pi giUKkXmBJxVmCCv3h74Geg3zZsNfEoFDudKPe6u3ZgnS/MBLDo3d8poL2faXOqpogJA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512290; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=1nJXYIpuK0eXT+iHbEXkEjTtDUMYXfBk5/WA4eORtPw=; b=kcMk1Gauxpc1NDJILf88iSv3sIIGPc3+wZe2wdbKP7J84OGh5GsxxunVLcWLVPV+YkvH FGWQerpO5MpKK1Pi97oZmI03W3tUMPeSVtqtJ8JOExdt7UZh9Mv1MsbQ3YkHDOQE+6BFP gsQyG+NN0fOo4UwTBhqzfkhqOaxm3QRy7UqEt6MnXwKTj611A2/h0RxCO0omfy1AiDQF5 HEHbzz0TuqRq/QLKfAgnG5xhosFiauLWUBnKy1PoMck1b2+9hRViZpawa2ymI7rio+wsi kJOm1l/ZKZ/PwMb54JLMqu30I26TvBa/Wu4h/gMXcXKE6hnmuHt/5XVMG7iF/QYD0pg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=PAPQ9K6N; arc=none smtp.remote-ip=209.85.167.181 Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50130606A330 for ; Sat, 5 Dec 2026 23:11:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 50130606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=PAPQ9K6N Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-53ec2cc397aso1359941b6e.1 for ; Sat, 05 Dec 2026 15:11:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512289; x=1797117089; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=1nJXYIpuK0eXT+iHbEXkEjTtDUMYXfBk5/WA4eORtPw=; b=PAPQ9K6NFR3FLorcXUr2F9x7gATSdt11ZkAGGONsSVkkTtLpJ09/NKvpCZXdPWenIV 1+gQuFJXuGJRbQhLWRxgo5J4VMnPFFOEItviw8iSO7IyTk/6Zy7qF+EK8WuLtW2ItXLJ lAKe43IoK4BN7MLpDnwVA4tG+q5p6VoFAggO1kMOHoeHkyH0wFDCkkEw0qDyQvF3Mkte X+XCKXRhR91HXzEygqXbmx5zWQAd2sbMMzA6yb23Ktny9Ae/K7OQb93FQ8JbOfY9WhAm fuw84BDjb2+zLNKTDs6iw169V4fZFTVAJXCKAz+fQzDYIPTpDKikca4S9lIKJ4CK+R+y XEFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512289; x=1797117089; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=1nJXYIpuK0eXT+iHbEXkEjTtDUMYXfBk5/WA4eORtPw=; b=qGWgKofSIUI9M/KmWdHGo/juibGR+63gMOZdA42NnyqMbml8/ICOIfVU2Rt6rJJXqu /JnRKJr6cGbM4hcZNm1RUHcVw1amC6Vb2roAtseMUrF4rXL3FlaiL7ypk35XpGWi7aTB F0As1DG2/MUa/bXoib29bAYxY/UJDego4Ouv5EzirYMkJJcqw46upic4ZhPZcCADubQN JJXTHe4BUWST4KFBdh6lrZuQsnvRVKCXX6Ma9yIMozZ4P18TXvCAePXtYwNOE/p2BYIZ Uvu5lOKg0gYCDmttAR04B1qWsq63EOWS+vEGPqAQmc3+Et0VHan910/JIE9GNFq0PSYk RCLA== X-Gm-Message-State: AB4q+uGRtwUd0A2ahxLJGYZecsoCX+n5xNQIDMK4LXyyCmmDXfcEQ+7M jSmuyH7tlg+Rhrq7UYA7lZbuwwj45UysKgByanPqhRgHHH3jKXvpCHZFLI3BcQ6yzB5FypDbu4R FBNBEI2c= X-Gm-Gg: AZFdTZd1UaHw7jq7rbdTq6MiqN7AP73TTJ3QQULKydUlZR0JeITS7hYwAvTdXGCnPk4 8yNSSBP0h1fLZXkKcHCrs3Rpp7if7Y4ghgfnafjsmnxw1rZwd20BnWPqmgjd30vnOULSt9pdusJ +YYPhjmUU2cLe8mZP7bC9+zoahogx57HnPja0l5H0/FJHWyzoXtv5iGxcbF/np5hYQiFvDPuWrD vbclLJ+EqRWsU+S83Kxfkq7NPxE5vnowsXGERyY2l/XJ8TKj2eOKHgt+/HV5DL8GwdEIs4j6Z+o 4Q3APa07qvWjkyDZPV1Su/N8YjvEiVXkg+C19OAZdW6YtfG9Yk7C9eSGNSbFq5x5ibl7Z+VJ7g0 qPYAlzidfEgSl8VNHRj5g4D469TaK/v/CKcL20zbs75OU5lryB1KqeSS8L8YGXIMgRWm7papM7X VWFQ+RXAZ76PAD88hp3bZ1sKPUTxh5dgnHFJK7psOG2Hrh3MfYRTNfS6Exg9Szr+b0DnqnHPGs7 vlpodXV8OXCUxh7kwdVaHolAXBAtUT+JKu2EPqEKQBk+Kok44vWgfV6pTt+wHgPlRhhwwzcxeVA 1BGRoygdhPJW6arCWTspxFlXGTV/Z5e8Tq2+MGF5sAYtsP8zVYCw3YpgX6Azx9RvTJkhnOLu+/y HS1u53iCukv8eAXFi1eDLZoKZ3CWhIBTlDC+K X-Received: by 2002:a05:6808:1495:b0:53b:f634:83b2 with SMTP id 5614622812f47-57f201987a6mr6456113b6e.0.1796512289354; Sat, 05 Dec 2026 15:11:29 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57f1f65febbsm4163964b6e.6.2026.12.05.15.11.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:11:28 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.12.y 10/15] io_uring/cancel: always walk links under timeout_lock Date: Sat, 5 Dec 2026 23:11:02 +0000 Message-ID: <20261205231107.4023506-11-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231107.4023506-1-axboe@kernel.dk> References: <20261205231107.4023506-1-axboe@kernel.dk> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Chris Mason 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 Signed-off-by: Jens Axboe [axboe: 6.12 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 20cb8162e13a..98cba5672475 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -204,6 +204,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) @@ -211,16 +212,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