From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512326; b=1Yrp8IihiKJu7UW/jxTBBFcEp3yCIq9zFeTOUEM+YShlgpus067GsDg8lFqaPVDRMoKe fYEtsj5aRMHkfGa0iURm3f2Xson7OOgn53BiCEaIL/Z4rUiOiYNGjIWHdoXs9Yi42LPWB L5ehGEPg8FyuoTWwCDYaUK4xqlipGwQNp8Et1hUqbgxBErLhGHPDDOnOLghFUJzcusbag yR5We1oLEK/VIy2mV0dS/D1Ph9oaseIzRchLh3s6HOk/2HjF2OGWa8Ns+658e0MDlffBV Rl4ht78NajJh+RK/f7NvUUaLnO5cF3a9xn2y68xwnmnSpZjbCkIdPJgpmwfM+LPgdUw== 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=L0iVEh79v5VuV9l9lFoPuOi9hx4IeDgpdkse0m/cV2o5tl6RrBKNo745sbLItifaxWWo y+gNN73Mpy0bXC6q8EoFg06GDojF2PTxqvyWZuGVZs3gi2iTu56/eEuLTOzDp2mECUYu4 m31Z0LjKkGeQymU0T89Q888AcVG6t0XO30gklk9uwsnLVLSkRwU1pmvBHPZRCSRk+vkHA IZSF7foAHaKCmqpAM4KR/Easj+CnYKdbV2xIoJV81LhdE/WGpofInm0x9uyUu47JUwcAD UkPgcPSW502N7ELqVQQwa8eqGro8gjhoZt/WcWfCDWLdxdVSbCBxxZlreQDquKXX41w== 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.210.52 Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.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 75755606A31F for ; Sat, 5 Dec 2026 23:12:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 75755606A31F 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-ot1-f52.google.com with SMTP id 46e09a7af769-8633b750a12so1040482a34.2 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=IXQmwZK0j86g+ebAhUTDJb5Rc+mEwtN9ZUyMOmwq2u7ejbZBc+L6oSznJMmeevqFXW jXYQEquLevcl3DGuBlJrwU2Kuv7RZ/MPPYgAJSgXWkUszMHHAx0Y5xvmWWxMaNHhfXcq l0c1uOpi7z50cavThAA7hOx8ZD1plntyAXMNGilEm6AC5GnZzNHR6kMyKD9uhgXOGDpn fqCdwx9AGnkVFiAeZgcBRwLj8DAN7FOKqX7QFSNP3fi+9ljmWoYfR089jR+9NA34o2dm OIP8B4uCh1F1Z3vKeyOquKpBllZwvOlSxneuAzWmEQkX6KnBoUeK9bxXZ/oSX+0g8bh6 8ZnA== X-Gm-Message-State: APuU4Z9fLepMXHzJv/AJn834AKThXgEWKq45sXy3J3ZQvplqXHRVFDBS b/OVXUVG2dN8If/inKVYyp1xajTAqt3lc2WZ2j/GsXGgthklFlUtR/Dz/Ucv6YRhDG/VyXR8uiY oIZQmHm8= X-Gm-Gg: AZFdTZfMTkFYxOyMuWExyV/MLZcGj5IMW83y/xW+uRRZ3L7cCOPr7wdYgeI/BnJNCAP QTTFT7Y14t+jHL40+JLI3l3iadpjj5undyUvrvq5qJnmtumrkc5KKWdB0Fciszj6qS8dHIWQpu7 Nrdm/yRI00x5B5OsCpuUSBMNs5rBVDrf6eZK3eZH2pzrRfT7hJq80ae/+2jFaODQJjocyujMllW s3QcZlibZ3etLv5CY2qFaMZbAknjvMDzjDJEcklW/06JT7E5qaVtvr812Mntr4FrNLmHoLyxNl8 Bd+meraBGFljcr88QcF7HRnpy7ZKijd3hxVWm+ATRnI+ju+MPCiwyWXDOqQwJNsYIeLV8gWbh+X BWDxcWif9f5k4eYWYgAEiRVctAa5b/TPuvDB8Ai3aHAh9D8tTHusQV0yESxxJdm2/OJweNfyXtT 6gTlaMVQ2FG5nJ2j6BpLOs6QDZiAqq69w4uOLkPNGyNIJDN3MVuZrzJCR+Ctyp4+hudNpdJ2IpU 4WJhqk9mjqkmsz6PNHcEiYBptfeHguECh/hyJzeyA0xbMd1LeU7eyNnGgG7zuvSgql7SQzGwqYf MxLiG/OvsxLIUyG50fThXHrfauTR1w9JF/GGEjWuMAh1mF15C9xqu2LMru/LnqrVImgk9q3Mzlj i56zIuncXtl7pBKAFr9/BqfA7g/mYAmid4kL/cw== 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: 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 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