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=1796512291; b=OLdYvXhCFo6ar7JBTTgVxUGhT7gkc4j1iJ5CEPRgp570umk5rkassssPKCGSHMcT8GCZ vnyG+FC50O5JA+uA7338VJZMWAOz8NrKLCKEuSmpcld15W3tG72/B2t2qUEN7maesh1zA nLdo51dW4PDDxVlCzOLbI6fhdzL6MNujGR29g0AE7tVLy3rJjCpMUBLADhJbKeBWIF71c ta9ofJup1WKUMR56OiSnf9BQmuL7+X59IsaRauF7mV+O1DINyrk6nm4GLtr/Z6mAXV928 PpHx2lxkIFt9sKrJkGAPC9PKbMysQlO+kvqo+NVmeg4X3ZyK0XDMrWLSNb4VyaI37ag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512291; 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=wa1okNC8ppW9NpI49PvcFDQUrurKIA2x8akbAqBkKNOnBftrvEZaKafGqNvDxEJvWqKC /1Nzm5gvHQxbqqhuJjHzJoXlEsYqei0TU333tzTtzU0u64rkygCwlMce2RNhIsN/ckNfF bORJ2D1MtkHpXuDvaMeXd+SwtCnxniwJyn6IsT5e3ULwzW2L/sg04cW7NYAFb/ZS0cbZy YtSV69GL3PGAhY8ZAALDex7AOeRO1mfwVDksAFktK311qGCei4+rn4hFxE6spMe+CLNqj 6lf0TkqslgAO4w2+DdgTYxYmAUhkGCigc5BPH1QXxM71zyB5vf/rsCxeol1RjhdLcpA== 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 500A4606A31F for ; Sat, 5 Dec 2026 23:11:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 500A4606A31F 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-55650fe1f0dso1237310b6e.2 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=asBtGpmbMYNlzHfIIBG2r7Xg5otWxetegbfvTqv90yWIWFQAbJUFeLt7knZO4IYmeb BOF/MpHK8yhIv72lRxkViLAQv4E+A25MdlStgqN3UUR1o7TUJIQoprWTBb2c4gzs3rbJ 2y/iOprDMBKfPTIwsxkbSWgqCGLtOPevVUl5RVaz4mHUTzJz8GzFLpM8iHhqPhs8FKcQ /Mx1CYtm36GdNgk+o4GP/l8Tey9MHcO8/F+5dLzqXFRotq7MS1bycyU3JGI0Li2l6C9y rWe6+GMV2VkIFmnk9HJiUj+mMZYeJCWqImfFP/kEqiw0VNdeva58cuBocIop0uobf90U rasQ== X-Gm-Message-State: AB4q+uHcdVGWgdJng8ua/X9PgJulMTF+IWdbtJICizbSPuHy21zpT+GR DV0AuPa+d1IOD7Vc03DLPnjg7r9+Nu8gF+rWly2JHBRytrrk28W6JRwdX++YhM7yvft3JObK5f6 xL+oAK70= X-Gm-Gg: AZFdTZftExPZE1EqxgoHtXERht2jT7bWFZ0rRguo1NvsUKZpMzCR4eTLcv9SZgv8VcF SrKYyRBAOcaOK7rYFQOkgYTIWZl+GqrZMNYaMlh6WuIKxPPn74cdjcWeinoP7sDQZuXgeGXcmJO bEjuJd6U6hG9i/Qf+PhE2OyyzPXPf218kLJ4ULvj0AeLWlqV08INYsj8w4qdgfZbozn+8hTAsIT mqjfs2roiAbXudR7lF6bh+CWcW56NJOLvsybHJ03Kq76faE9EAugvF0Tgr3CSqCflYkpHQNNuz7 FuS8n8RdGIkh1qRGAURnJvjnAT0F+T5sXuNy8B/IjTBca0IEAxarY7XIYciqBMg+YK0UCVssxx4 kwEkc+57ZwLuyDou5zeQfB+i4lhGr7rJAdIBJcXmoGJ32WFdmXP4N9i7rGa/+ZQWbJUyie1OT8i QKPbBasUcxJnTXWKQtPWZufqNStP5rLIXt2skZWBIbLLVcxl3cSRux5YD9vq1fNrCbirkXdNY2S J+RL9PvEj2RyobBPFH8OA+7JArGROGAAftJCHhA0AQMyWx+Gr/4+DgXypTz/JlEQp47kRakS/Wy XbOMF0TxNVxotGLSFQCFcS4nK+aDsbwryekUTX9GhmqxZ8+Svc5EiBK0M63pZuGd0VRixkRJp12 GyguwtDSZY92guw5bjkigBfm6+z/6UkmVIoxe 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: 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 [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