From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512402; b=yBpLyTFfQ8S37XM62QtiJFpFufLHeSsUMDcGGiJOnGXlf+cyj0TgKNDQtfVQnqLZdvtd 0SHVDu4GZ496ZrcCpNUj+DkZuhSMBgfyBDFmoI3JG+BWjHNN5rXr9PbZ6rpSsKXl/McSn E0JiUci3DGVefqkVQqMRhxGBW62zkZ34g4llEbxbXTlK59SMDQGt2OC8xZFkhwMmhJt1A 6ekLFsabgrmwaxPz36A+4ywUIQUtFwbecVWt4UZ0LfrF924hFE05XJUBcCVkfVUnwvTIe WTrvCIuNQzMSmhN/6rcpCIyqxquCldWQcWMSl4J2euaVG0KTmRv2pdJIE68ZTXGItfw== 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=PGLxzx0unfSysvabd8+FFsUz70yNYlXnNjkDScxUYsV78uyOHAqceCP2FCD3g6f9yHQ7 A66qMqDSWmixBwb6CE4FLl0oyiiR7gAIsXARHyXmQpgB5t9+rQ8hKR1tWtCjMRcNai1Em qevuRsBsYPxCpgzlTkDIgkwvZj2XZWj6WykRVulovVAGz4k/hrrWU1fC4fNDwHXCl2+IL zERqTqyDLlhcb8Hzz4KukxuwPMaJcszVFlEnLVs6FcGg6SqMWZFvlIcAgXo6nedJWa2oj ZS7ClheTQlV6qECtacgUp2BaPoT5VMlFXH3zyQwhLCRyksjJzGlWrP2L/afCTTTym3g== 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.47 Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) (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 EA03B606A31F for ; Sat, 5 Dec 2026 23:13:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EA03B606A31F 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-f47.google.com with SMTP id 006d021491bc7-6ddef424972so1774871eaf.3 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=hc/4fcMwyb0cUV4m4iD9kSDKzyTvoO/z4TUXw9b4RQ3JNd3rW5PFa0aLqw39bbV1fN rS5DaRH3ADp3ONfw3L9TGI9y/XvhL6vANIeF2GA6rveEYPz924XFr0/GE6SkQ0RYBEmU BlLbTg85JZgdXRnB5/z5SwL1Yt+aZKfCENeX+OlpmvxtCaG5UNVcIKkaOYP3N+9ItIYu +eLdjtZF+cnQ415C8Oo5cGF9OQipAQBgYjIb9rzIauXqjHUFHb1QjlNJ1CCowBGn1UVu kh4Ho2H6/8X2B4Envtg0RvE3L9JSMlE8YNW3qLEhqM4ZXawjtWXE07Jj3YSJBcQRtW2u Rz6Q== X-Gm-Message-State: APuU4Z9pCKE00eIuMHV3yuxwn57wjNjf2jGGSolLxRSRvG+vwmhLiHjt i/PcHRU3Ym7K+27NUsjEns92bS4xqoaL4LPFgD1uakMaf7wm1XOVE+34AtGBDKDvS/c= X-Gm-Gg: AZFdTZcMTS/QwO3yxg7T7eI0yx1VggZgCG4nODSgyX1uorpH99+7tScd2XAJJBCdiUT XIVN4Pm1F5AiAGqKaXXEI1bCVE6PDuexJYj0V3ATmcHIRyEDl9wgCJU1YCqHXAIA/aTQPtdNwWU kybv8Gm5LhoYXSi+tlS03lf8V3JWUCOH8scW+h9npU6rUAoAvzW1h6+hHnFEhRvuaxMBfJjszXB yHpuVmRhuEfeLrOLIGPUTAMUBUraMN/UqmvltE3OnEY8Qo9m19T1igL+4b7m/jsD5cOe2+itdn5 9PF41xJThhToJT2NzVYBSlCAGqXU7HMoHmYhppRd3BEF5Zr9p88YRm+ZivV8R8SW/7u+FztkWCX pKhVEZ4u8Szz9mo1yEIGgoFlGqslDGOufDWmmY0lW+zIGWLV3x/e3BUVUaVIB6htKjOFAxdbMzt ce3Jbam20ZQvlbSqCXUszZxidnChxs+HYZlsSzw42qYiGguNLBnSholoEf+0ama8G+LkQtH4czY cScC2L+RsQ3uxe1EB+DG1AAjbEP+7NI9gKH082mC+M/nIJzv0iDsdeVLMdsoveA3A1Wzk/xLUQu im1iuTzbQRLPRMxsXO29Vp92iJfyu5xrzHZ+moP//zv0r4WeCsWObKpVjX+Jv6L1G9rIsTnhS56 s1mNEUFe9gs9wOQTCYdZn78uDEXFR35bkUKS6 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: io-uring@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