From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512372; b=rtyPE+dNZqh3BxatYMbVm6BGbfT4F/bB4n2n4iO2N+Rj7c+qLTzWNQ2v3Cs5KoT7zTxG ZiHvbLgjtmpuitP5LBqcf9jxNHs/zGjXuc+hU9788+nCnHpHNqzGqdZ/h5FsVDzCfbmcx MLVgR/b//GDjp7iiI2mPYG9isSVj/BanK6b8aqFnLFKtxFC1DcjmaSqWTnuqDb4fH/Yfo 3k0rviZCMyoOwyL0tho+2qAhxFi8tIS8kPoReQ6r4ahcy7EeEGCJ9cONzq2f9fXLp1/I0 dvVAvBXVx9K/8dnNxEreqJdA0Ur1gM5gGU8KUnCf9CnaJJaMhryV1Dr3kc7keMCdhHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512372; 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=Nl3fZCFz0oCIt8rLI1FSNywy+o0SgM1mRxrOXi63L68=; b=auM13Dx1Y/HF+qvmoq034uX9SaukWk1jdNWiVJednT5lHMgSnOK6kUySRUqTdRT/kga5 AfbOqj2SbOw3+OttKpopph16Qhf60PUhy0Xi1kYgrQ7t2SROz0g/Ho76HWHP8KjiQOyug Oh06AE8gEjlfyJWWUsL4aTpWnNKeAw8oUxzcLykSvaz72kau9Pt9Gscz6fyTEcI8qHeu5 tSKlQ23ZMpCet5iBeabvJ39uk7NPj7uYjemJr5FZudzs+XR9FdyWJ+HVUu187TZhggLJP gOue9Oi+YdIhBQCna8FQ/lfj5JIFMozg7aISPPqq9A/ME3K3TtIVyDv8KtTE+P98BRw== 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=nx0C+c1K; arc=none smtp.remote-ip=209.85.210.49 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (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 0E5C7606A330 for ; Sat, 5 Dec 2026 23:12:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0E5C7606A330 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=nx0C+c1K Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-847ac7ccd85so1210131a34.3 for ; Sat, 05 Dec 2026 15:12:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512371; x=1797117171; 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=Nl3fZCFz0oCIt8rLI1FSNywy+o0SgM1mRxrOXi63L68=; b=nx0C+c1KUWG4BVqWYxg/WubTj2lg/Qk1vGAUGw0B+aVdcWIz03Xp5pUh0yxlnYxYhs Hlnvx/U9XO28ot47cf/Km7w5/lWNvi3XI+kA7brojEBMwzTWlDInmV+/jBrgirP70HHP H2P1kHDdIkuw3wmoDGQHWoDOtzt+SC7j6C/+05C/j5t8Btop/wXE3qs+UWyOn7b/49BZ 36bf5jwSSxGqPh5h7Wr4mv9q5cTA2FDrwR07Ot33WlNZfX3qIWizKcA0FC2y5LZVWJla ROcHrzr14i6qR6gU0gMD/C5T6jLTe330N0SGLAUjr1eY05Vh56Mwzetiw7WarOOPDY/O T0FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512371; x=1797117171; 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=Nl3fZCFz0oCIt8rLI1FSNywy+o0SgM1mRxrOXi63L68=; b=2xVRwl8DoZ5MO1x+d1+ebDLXT7eYdoHKRRXLx1IpCXGodDbsV+iX6zdFxh2Y9YTXaj kxvaB1FNCaF1v2Qj0ycuEN4A+Jl9jSP+OIrKrlwsZQDShPwm3dCLyeUAjGcWF6KmIAOU Up5FDJ6+P7s9dd0R/VaQ4HuR3qbfCJMv7AS59jbRWIZWbCxjQiboUtsvqadDvkkmYoqS sPAIoqr24MAaT49zM2oN7Rk3yB0SRAUnQPcRnwlsRiCM8HY+bUHGWyPufa/Bvo+98AHE 5ZlDsfnyXKBpS6A+F6cTvE94jNLSgDyYhYqxj9xOuWaZZSNx0yY7FF1WX4054x3K3B/l DBeQ== X-Gm-Message-State: APuU4Z+8CvJgvr3gQGMslKWvifFWmqMtfTLwKyiUPxS8jIbFFsbzt8RH dp2G/SnHzGVQCVPpHy7uhqXjLLrpN2t9jtRhZZT8ni4JBfzWnkHfGDeayItbaBjF840pbP8CsR/ JFHTvgu0= X-Gm-Gg: AZFdTZf8Qr0uTEkvixZvBSSF8owSECHwHlu1dzCOSAEaNOyw3ET7bRAlkOXA2ImRh3R oeNynDqN2wAZSbS9q/ZL8Y25yuFJcTPCQoL+YQuqQOk8K6+uzWuYherTM/PWQVoaauQBE33zmpA 9Ikb45YBBn0Gzbyzdz0df/dO8gMNeHZ/mBpEnkBWaC03QkSFvA0IS+MiU+8rt24KoRggWAx5OA8 ZtyWLd+IBtzsxifa7MinDOznjn2lmOMRNx1WB7dVTbZBBesXg0LrBF1xd9xVwifHFP6rP93ILde F78dYIVqnRkc7OtHJxAEjwzcX/JGuCra1j2eTpWEjehAeEP2v8ujmo1mcL76mWCeOG3suGiMJji 5kUdVaMW57/NIsC2RlaMlUXoJ6HVPEYBCm9639YQL8ZRwF3zURjNQlazwyVX1/mOpxC2cjoD2Z/ YxIHdnU6cIzCtERTWXGjBNYph3uxzwWPtUkQBkKhjmci5c2C2IlDn10Zb+zfE3zCj8QtmgkND7J Y1WTwe3NXrc8qLc5GoSFucMzkGwR9jiO18Xi86+kn40i+Uobe+qKiLJkCRJwKyU0/L/aZbeK712 LRlnHIQeG4/SxqFM75js0Mn1cAN2VL2oWUUAH/fkjX5X5yfEuVt7XkwvydJDp38KNxODgLKJCIE JmoGSBw/wRtx2YkEmPAs5+jUsCAfO0+ccmCbo X-Received: by 2002:a05:6830:44a7:b0:81c:e293:7d66 with SMTP id 46e09a7af769-863f89ef71dmr6000131a34.21.1796512371097; Sat, 05 Dec 2026 15:12:51 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f7dd42a8sm6279788a34.0.2026.12.05.15.12.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:12:50 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.6.y 09/13] io_uring/cancel: always walk links under timeout_lock Date: Sat, 5 Dec 2026 23:12:29 +0000 Message-ID: <20261205231233.4140988-10-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231233.4140988-1-axboe@kernel.dk> References: <20261205231233.4140988-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.6 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 1dd7bfcfc80a..7a05875d2b7d 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -210,6 +210,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) @@ -217,16 +218,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