From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512326; b=CjdWzVsqTrGKQhKw1AjVDx8n2ApQnp2rXoJ2ryPT0/wbVmg6Nk6mw4h70gzWz8to2tva OYrPEMXoq6x559IEEtibkuYoC6FGYrH2YmmKredFcZwtL2L6wWCrqtgEmO1rFSBK+XKrT KEYgYhiyopR0G4XhlT6hd5VcxobTyeLMaotfampM+7vh/Owjdm4rd4P48X97wF87aC1Ia z4LMVXvcbmeGCY9hy0keHNSsgYGfzDMS4CKO0jXSLw0MLQ1k1CiNbZHkyyfUiIYmH09fO 6f8nRqRFD7nfnlbmzQbFbQQ7QRWPYLeW+ntVu8OHf7x6ynTXQM3onbd06/quY8I5lpg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512326; 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=pSf+UIsZZ/cjaR+sb0Oqmf7zMydc06jgDIIem9eNta8=; b=XaoQDCU3f5hZKtevtXNvhO2UxAUl9qKL3LHn8BCsfSUeKhAKPOl48M3oMVUKxhWAokFV YW2POUyaQV8NuPHlgsT9eQpIkxGK3BddForaUayYu4bPlLGgo9M0rPFQKE8qT/wR8ZRN5 1KwHy/h1DFL5FDSUQaKqxDkTdM+2BJKqvrH9FhCXWb8sM8Xw7SFFaG/oz8O9N9/99Hp5u Pir3qURlk+8y/YEISt+ZTrEXkzFiJaECcVsnu1FMFBp0+m/sSWd188mtyWGhFvqO3dUHY Ayqb11x9M9wVgyVDe5EoYErNJfy8qTlqoZjkjMsZDwKJc3agqaOUXKGqYALUE6wBEeg== 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=oQ4pNdo0; arc=none smtp.remote-ip=209.85.167.180 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (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 8F5A5606A330 for ; Sat, 5 Dec 2026 23:12:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8F5A5606A330 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=oQ4pNdo0 Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-57309bd40b1so949306b6e.3 for ; Sat, 05 Dec 2026 15:12:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512325; x=1797117125; 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=pSf+UIsZZ/cjaR+sb0Oqmf7zMydc06jgDIIem9eNta8=; b=oQ4pNdo0x4xmpuZ7+ydSKxQQbqIFlo/lK9dWxrfQSlg0xT5JS/zCYsI+GzxHJljmzj XahnFR+3NwjTkkdd87mHPy5EPLYCzgkyGESKWhUm6nBlWfWIaqKOR0/rKn+SlUmO/OHB VxX527vy6eFVxrSmZiciaq+bDnbCDkxEGuzZzf9DoUqxJGPhiep0K8ci74hP2n+tIrsx AAGY9+GysIShgSDQPi6fQJZDaGMHM7D5cQCufKf7Qe5KUK2sD6SkB+KrC1J/ztXkE2CM ay+gZbm2CWnBbrzzGSh4LSTp4fA9yj1feHSD1OjpzZ/6cIj90UiWfn5stjOkqdZtDM04 x6iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512325; x=1797117125; 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=pSf+UIsZZ/cjaR+sb0Oqmf7zMydc06jgDIIem9eNta8=; b=Y49qSFPgjP1TfINFsgl0FNdbzs7oiG+ztMBBj/PdIWfwY9dQWcYjFxWQCIf3zwW9Vj Oz2+VInNbO9rJNH5OXanF1dpr5PVn/o0C2M1AbzZCtEDITmA02SHexCrAsS/NMsOZMZ3 Wi33Ge2hkXxI2fVfeQdXKpOTupiRej89BNXnv+7pxsuIOY0QtqvVs2Zqs0j8FEQAWsyH mhIQYITwk8XMdWKwFkGP1GxFxte/4BknidU6SgVxuNXToYISqWYAvYUSDVtz6g9WlzrG 86/ors0YbR3uqnCUEiq6T0ZnSIr64f+JkBKkT+CPfh/CmXNSrFzSZDnZ7FHX72bg2kD6 smbw== X-Gm-Message-State: APuU4Z/kMXEiQy2sjtX8DLIjVjLvMfBu2yUHlrPAPb98Iy1fOcpHhxc9 kAvKezQ3+kGwL4PJ16QqTwlhONF32tOprWowv9P8oTOxZIPMXkOXW1LNe+a7MAZhPw4= X-Gm-Gg: AZFdTZerSBB3h0jn8WrQhcc+3j4VJ6WtCwj/Fm9MUry3tGyMB7SVxGWOBcfs+gk/wEd NZ1CwCx4Y2tbXi1C0oUq9YgZailNlSveR7KCcpB8/lBkaZAxFO1EeSiU5g2bxDfuH1MdddclQbk rVCtIMBbJeRAbok7xJ2GBTvsQGDwBivXY/Yy6UKb2YbPrlsZfIqcl4LtUCxoSabeR/hSRSCpRvm LYV4ThDdJkHEskwEUrUWfRzT5D1vBPFGKbgW+5ZKp9DIPDDG2U4ZndkOX9egsc0K79WUmptvkDl 2s4l15K8z7vW5U47V5xFUzAzNduEeNZMXyp68CC5wIhpZ00mhh+1vyEAMDHWfn1W/dPWjtUsxJ5 ucMZGOYGAqOo/9GCZrl5JBUO3lBblvVFHJzDNxS4Re6t1XmCzhRrZ+WgZn7bK4LwIE03G9UA7qg 62w8js4q+rbG58C4pe3gjGKkMy6MBoLOkNmfRYjJusYfauECWEDDKlSXUIvwdGvM0+lLwpwRYI0 Q21MAXabbHEAKFBiMmkJiBOi3oAhrjUiaYorXJzel/IzgB+8T3wmIOGo6sCNIS5k0YCaXtzTQip 2cS8c7FIZR8WFoKZ5XvJNGPFw2ISIpBHfnfUnctCnSsrS0XXYipQN/Z9EU1gFdIFvLcK77oj5H0 ID7mXSFf/VrnIDhjKj2smcbVTAM9xVJP6Oc/B7Q== X-Received: by 2002:a05:6808:3199:b0:574:9463:551d with SMTP id 5614622812f47-57f207ead69mr5898531b6e.45.1796512325651; Sat, 05 Dec 2026 15:12:05 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57f1f6fe2ecsm4086916b6e.14.2026.12.05.15.12.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:12:04 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.18.y 08/14] io_uring/cancel: always walk links under timeout_lock Date: Sat, 5 Dec 2026 23:11:41 +0000 Message-ID: <20261205231147.4095031-9-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231147.4095031-1-axboe@kernel.dk> References: <20261205231147.4095031-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 92bee62c09ad..b4d31de286fe 100644 --- a/io_uring/cancel.c +++ b/io_uring/cancel.c @@ -432,6 +432,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) @@ -439,16 +440,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