From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512251; b=bfTYSecAyzr5p/GIA1gvMKQFiUUBpzZCNKeXC1p4MBOpqa2WwsvL3Qff6+bUkRRhJPG4 PiX/Duji3XXEoMXxaeYSXYznNGt125VXkTFAhwVOW6n5M6qljziYj5PbhVbJftiDgRw+Y +IFusogSIyyrknb/dkI7UVyA3swAgB59+TbCncFAP8AkHMU77pjfLh6+3W0ANCBGk8k96 qMWgtPh+pi9aNpb7vLxE7766RLDEHcbTkmRmWwmHHRU+L1w7lm4XlLfVkhNtrGuDKMJX8 UuqGAQHtFuvfWnklDJ2phY9pTMQZ+9yi/Wtafs3w0DJJozP6slK5J5gzvh13CE9iSwQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512251; 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=NwPK6diCbtvWSwJ5/u6TE4ZlvcPH4Ni1z+9q07mclts=; b=ZJLOnsa1hgTuw2ZnqsJ1XryZZJBxjFwAMuRTQAuQVzqZkiTTtidDYlExbKXXkLVeXaij aHhxF8gR/pQWJYlJ7AYUbUEkl2J1Jx41M2Ni5oBtrk/YHoDPDftCV7gfFMDRjQ4+bnC4y ksbp57tvqgUExPcOWCK31suN2HQOR0btja1WHAgx1y5bJv+SqM+uz/RUZjuoJpb0/1tKt 1RJch4nUbdKVAXtf1J3FAN2GjrSUHBc26vokm+6tbBuhDQbLTkoNF1Niwx7I650eoSV/H dkG7TaKOKyi5ZTiMNZdXrRHv3N/g/nxzFgnIPs9KiNJKca276NrXu80Ihl9DZ8JMA/A== 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=aZBenUXH; arc=none smtp.remote-ip=209.85.210.47 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.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 2D1F8606A31F for ; Sat, 5 Dec 2026 23:10:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2D1F8606A31F 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=aZBenUXH Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-8253bc5a613so1480568a34.0 for ; Sat, 05 Dec 2026 15:10:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512250; x=1797117050; 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=NwPK6diCbtvWSwJ5/u6TE4ZlvcPH4Ni1z+9q07mclts=; b=aZBenUXHbJ9xlZtV0Wxp+Qsy/aKfvT69hPhuzmmwffECsotLP7KKvZ+DHEexwT5D6c exGAzVl6DTE/JEqMQBVkGRzGFEhYVm1+XEzQmpmjl7FjHU/+1amwW1VHn/s6oEn+12xV DKbZbnNp3rRu6iubyUrJjgqG2wE5bPw/ZOfk3ehe24kg3P/VvIvVqYwYii1oDb7ziez9 klQ4lzlQ20kXCVtB73tJxPBRwiD0Sb4BfMgC/ZuUJzvK2F6TnBqOZURDbmrtpL81HPxU 4viCem78uShS6ZCnKiwCjUX/cg9FrN/1Y/VUlNR7zAnssNIF3c80SvPyZ/AZp1sx4p+S KNxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512250; x=1797117050; 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=NwPK6diCbtvWSwJ5/u6TE4ZlvcPH4Ni1z+9q07mclts=; b=cOGpyN9FDti2CxB7g0tK7lMk/VDSIPyUb+lsT2Vn0W7lnXjk5xSar4gZ4XtAa0muXk Zcfy5lqnGB+9daPYPZigQmK/Cw+jzK2xGVy5Yto61voUoJIm0s9KUHICVhRHgSYJKBnb LnFft1N2CFURfDHkDPwmMqOlYXvYZSvUTRJLY1SvnRYK2WPI7Q22Pr7fLFVtoKFeAPqr VdznP0fFU80TLgGhwVl3Nuxlq6zWW7Lf7/qPofo7QV6vaQ5SWF19vXaaqvBYWvzdZuHe RjrsvgVPmnj2Q4BA4mTfH4tYInL6ZzWaA8Z1ap03UhvP/cYWES7gQu74B8qEAIIEHagh v7yg== X-Gm-Message-State: APuU4Z+OgFAHQY6MMSp8br96HIl3VWy22ZFwbx+qBTkSSlVl8pxdsm6K CWnPQeTqTND9bvCMsZX1YlqrXhbQKcKoNwrIgTUihRBG9lBkw9BldFH+GvHPEFNUz35dNBQRFj0 UAxqgcqg= X-Gm-Gg: AZFdTZdNYNpJYXsRbMRd7vj81o1DxC/wyv3U4UGlx26F6CcbSF1Yzdd6DW4Nx2WIalY l19epSCtfecSMkH0zkvY5tvqafmbWxTNoslJK5Yn0jvfavJSBbUTZ4kBc0We0B0rJBKKKUw4KHp v++R1MMKvOvbVpZtWFyxeoiNFKIskVHA26RqMIPmXq53Cn1ECabihUUpYjgvuFqvFK693o5S+sU C6PGAlGlJ5pLET9j4SXpdStGIdC/0/XPdlaXI+IHhf8BJDsfZ0AQWn7q4NiX9aHqDnFzS12AzJx BYHhGyxblcciZFrQOclc7HEkv2e4SGtqGC2GfCAWSkBG5FZyonwAS8QeZgX+tkjeyRyCjKaKbhc +gJPVQTd2+mW9IdRScSPF91w80LZA9I4lZ5i2od6voHhq4sJUuhiLjTtjfIJDBOngRgR/gMP/h2 1/QaG8u/8iBigsqfmoYViXg33Pp92QPjXb1JVYwHz1s2TcMAl6G1E7GDnGgXwilMTZrRKR0MDxI oNruM0ew3JfLYfWiJNxjfEroFltk4fRxOcj4dpvoZkEUrLIvmOc0IXhemT4y8V2BxvuIUhpD1vw oEXxxbIEhRbCzeVu18K6ahg5UlQMqIWi+Cf0bo60ZmXALGIf/MO+5hUXIO/2WoFYoz4YWRsCVnN h85T5iR2lBK5NhOiQVmk/W6BkxH7gL9rbF8Du X-Received: by 2002:a05:6830:3155:b0:822:6f0d:d04d with SMTP id 46e09a7af769-863f89a54c7mr6150397a34.16.1796512250252; Sat, 05 Dec 2026 15:10:50 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f8313082sm6309624a34.16.2026.12.05.15.10.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:10:29 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.1.y 8/9] io_uring/cancel: always walk links under timeout_lock Date: Sat, 5 Dec 2026 23:10:11 +0000 Message-ID: <20261205231012.3977993-9-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231012.3977993-1-axboe@kernel.dk> References: <20261205231012.3977993-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 [axboe: 6.1 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 3ccc108cf835..3464997b90a8 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -187,6 +187,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) @@ -194,16 +195,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