From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512402; b=TSGmIo5A2gBgte0m71xsuqWy2SxdHcbEOLeyqRXEZpTQdUV8w6JBrUe6rQpnS/jIGiEJ pXWU1Lh02eVSBSs8XPY8aX2Sq8WB4bHbgqJFoT4Gm30b0y4DT/4AcnU21EdqFREsEcKfx bDF2K5KgydAOQD/v6lnNVwNVPQH5BE3KHI5u57iAyW+0+tBUcLzeg8gmyNpMXxxSueHZ1 4KXelBBv1PP5Hgzt5Fv2pc45WMnJlczkWor+cwO9z0bSkYXYlFRLlRui3jDSjlUT0tzrH I0bjJ8v+5QMtq7Vlj34TERZhYDAoZIxbvsH28ZdS6ksXEE1yD0sjZb7s2B9UB7CiNLA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512402; 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=8mvadvUesyim7s7JC9aL0i/xlb4O9QLJ0Pbw7hz3Ezc=; b=rEuZGNvlSOalsp30DKEqjhAc1SjahUzmaETSBvyIkt6O4mPJZk93071CUhc+Bw4QoBQB iEYd/aEXGI6zMPC/F1LNFonzfDrAAA0QIxNtxLxsd4iUoHDuF0DxORy4STLalX7/i0ie9 88gi/05nawO0gGTXyPj2/DTEO7+qZEleBaLPqaMaRn8PVHSjuB1Qn56dPYJbDlwt1sYoO vg/M0lVDBGzdiiR8E/wHOqtuM5e65pBW+4yRze+F9sITEbbG5+7GWMd70y+71svq+Jfzd rjvmiXWnpkI8k0CrO4S5yYgIZLb/5xdryZ70wRVE1ljSsnPFcZLmMRwQ5l2bvo4+aSw== 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=p7e4XL1N; arc=none smtp.remote-ip=209.85.161.52 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (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 EDC8C606A322 for ; Sat, 5 Dec 2026 23:13:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EDC8C606A322 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=p7e4XL1N Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-71ad5f20016so1253913eaf.1 for ; Sat, 05 Dec 2026 15:13:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512401; x=1797117201; 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=8mvadvUesyim7s7JC9aL0i/xlb4O9QLJ0Pbw7hz3Ezc=; b=p7e4XL1NuVrInuMNKk1fggZuMhPfrzhpzp3Vd6tfrp3u2OmFr5aEwMJuVWhHTQjyTF QhLv9iHpO9cO8ShiMT8HY0JLeWc0qJvtC5B/vAohiQdRDAKksZjr2foEWWf8zAH6RWyX 4WMrPfDqSseAoyzG4paW01DjUBCZl17UXTAXAqEX5e2RL1I+usAAT7DGvTEqemJoJ9SV QfelE8L+QO9V/mnUDj9O28V+ZV3EonR3x3sfQA7DcjP3KFo7RMpUGrV7j+Ot5y01VZJy dCRoD6c7GRK+wdXfl0pHD6JrBcsZVHnPvLq+9KA/6tGpq0ORhPGXzDZLzcIDvAkOkRsh RKBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512401; x=1797117201; 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=8mvadvUesyim7s7JC9aL0i/xlb4O9QLJ0Pbw7hz3Ezc=; b=NPp+gAV766pg/+jCqyevU4ec3GQG5Ze+d9dWgkPjX0O1xIzEDA66bVbM5Mi8yL9pe4 wcMBGVHvD+Az6lMffDQ5xbGtiiGqAnnQELW3bPFz5BLxwCbpF7uBSj8pocpDwQLTfic6 RXlynDVpK1aYVH8PtfNi7tAIxxCCaXgrZX4Fix/G++4vFcHHL+R6eWhjszliNxQrMnZ+ N0h28CLHbfGfFu6ujmbzyAhSv8LY4+5dHTALmsBRdlu5K0CWoUTwabEjvxarQim4mZhR ol2TOKgxYTuwgKwJvNKjwdQP4sApCryMCxn0Pvyr0Z/hBUPDP6uksCjbp7CP4qSAe7HS a5xg== X-Gm-Message-State: APuU4Z+9SFb0EK60H1bhWZGXd7Cjo9czXaNeHV9PxDKk76TBIY7kjiEW 3D6ZnbVp8+K3emCkYiRPU3qf8hI3sIiIMOEODq9WZnxS1+bToI3SZEZXUdpwNHzoHJMnsrQ59SS /05PdRq4= X-Gm-Gg: AZFdTZdlJf9XWBA7E7CkfNuHmN+XdGKHVYigKRfkvt2v0xjhlkX2vLKpqzCE1U/3ZRE EVOLgYblrljyPQtWSnjYx17LtKwPijhjPbONKwIdViyceEutM8m0bwdZjtOovj5j5tKROKOMjYg U0zWL+F2njr1l1ALF5f4EUSafitC51UY9XG7lsXdvlwJA6eDQp8Q3nGdu2Q9M03ASk53Dx/MZOR REA5BmRQzCNqknkuC30va36gVGwtPypfVoiZYMF1kZUdTmAE5Z5z9PcXxkg5h2H3p/HQEcrVwFb nGX3gA+HVbdKD0+I9TL8Jnw90yqs5D1gyzraZtbsiWsP0mQoCIhAtyfkk6+79Be0AHFOtN0SOyn QLEdaEgsIuJVGH/GjRjvuV4NcO1G7FtIwfeZ3t6FEpDbtEGOoMMvKC5Ro5R9pZ8Zo95Tgwl6R78 u4hknw331y/pUItPhgDrJgfpyDsT1uiD+ZRaOGwmMTiETOdWiElOCZvkFcxcu+sySELL+FISBaw R6CJomYl/r83FTFtzrDII3Q5+0uUw1KqeOHhBc/I46qaZSnIDUmP84FD3FN1QWim2LwdD+jT3XT 0Gr5gUiEP4V7iQKgJo4T853lCwO6nXBatBSk+qsm2Nz8KDgYWK1ky8/LazgMpjlHPF/XAWrFcrR ThgXzUgAnKaqMI44bnzLBeF/kR5CJ6Dfukrps X-Received: by 2002:a05:6820:8589:10b0:703:4a6a:5111 with SMTP id 006d021491bc7-71bf22e2c57mr4020045eaf.89.1796512400951; Sat, 05 Dec 2026 15:13:20 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f8331ff6sm6269737a34.22.2026.12.05.15.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:13:20 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 7.3.y 07/15] io_uring/cancel: always walk links under timeout_lock Date: Sat, 5 Dec 2026 23:12:57 +0000 Message-ID: <20261205231305.4158103-8-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231305.4158103-1-axboe@kernel.dk> References: <20261205231305.4158103-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 --- io_uring/cancel.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/io_uring/cancel.c b/io_uring/cancel.c index 6be224ab53dc..b2fa12354499 100644 --- a/io_uring/cancel.c +++ b/io_uring/cancel.c @@ -438,6 +438,7 @@ static bool io_match_linked(struct io_kiocb *head) bool io_match_task_safe(struct io_kiocb *head, struct io_uring_task *tctx, bool cancel_all) { + struct io_ring_ctx *ctx = head->ctx; bool matched; if (tctx && head->tctx != tctx) @@ -445,16 +446,13 @@ bool io_match_task_safe(struct io_kiocb *head, struct io_uring_task *tctx, 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