From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C01D942A781; Sat, 28 Feb 2026 17:46:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772300795; cv=none; b=sCd6z7/FQ3UaYuM+jN3tt+qjTyQ0dBdewqNjQ9eDL2+VXLQYFfI8F4k7VI8VAYVptu5ktD66alaa6OSibfSrZ2vd+2X0D3Nrfb3jGSUolg3avMrPtzVdF5hlWIAfRaxGooQwzTUOSq4zxRddQLmZS1Lk3N3NQwzidG1KipkFS8o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772300795; c=relaxed/simple; bh=gbqZ++Duhupp4YG/TedHWSOdtQHqxkp2I7pBN0yhjOE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rYyQUilkHgWYxa3pJBeXAjvVLlQUX1WeelLh8C7ZBPyPuV9FE7EHQe03AnbPGIMG4QP2yyQfp4euZN+s76CNv/QI9Wulfsj71BMBmf0byFvBbKaywRjThD/t4H1ovFCFscmSv92xxrvRDSaP0yqIguqH+vkcUw0fTRy/CUgjyf4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UIamdP1Z; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UIamdP1Z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF189C19423; Sat, 28 Feb 2026 17:46:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772300795; bh=gbqZ++Duhupp4YG/TedHWSOdtQHqxkp2I7pBN0yhjOE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UIamdP1Z1u9nMN3+muvIkyRwMRE/6Qvyi9V4hL6XRU4HOZ2EUrjgKwtsJkW0keNOG Ml1SHXWCOQpLvPlNHdZkZscKatq9kAcd1gvx4fyXvxubCrgE4dSXwYfQ0bijmw97Co LC6/b8SNeZv6zZyy8IWtER2ciZrpkU6e+iOUz1W+qEC/tArQ/RZdrwpUEHr2mXvYx1 I8SP8Vb+fg97oMaKc4phS6IZv8fQ3oPlAPCuSGNKzMyyg6vo/WhyoYi39NXMIInMgY CEgCGXFs5O5bm6MyUobck0euxxogsJNZLBwbolfSy7y7m8/4+EeOhOPQ+bRDIiDXyB uaf8+jxr/aT2g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kai Aizen , Pavel Begunkov , Jens Axboe , Sasha Levin Subject: [PATCH 6.19 828/844] io_uring/zcrx: fix user_ref race between scrub and refill paths Date: Sat, 28 Feb 2026 12:32:21 -0500 Message-ID: <20260228173244.1509663-829-sashal@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260228173244.1509663-1-sashal@kernel.org> References: <20260228173244.1509663-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Kai Aizen [ Upstream commit 003049b1c4fb8aabb93febb7d1e49004f6ad653b ] The io_zcrx_put_niov_uref() function uses a non-atomic check-then-decrement pattern (atomic_read followed by separate atomic_dec) to manipulate user_refs. This is serialized against other callers by rq_lock, but io_zcrx_scrub() modifies the same counter with atomic_xchg() WITHOUT holding rq_lock. On SMP systems, the following race exists: CPU0 (refill, holds rq_lock) CPU1 (scrub, no rq_lock) put_niov_uref: atomic_read(uref) - 1 // window opens atomic_xchg(uref, 0) - 1 return_niov_freelist(niov) [PUSH #1] // window closes atomic_dec(uref) - wraps to -1 returns true return_niov(niov) return_niov_freelist(niov) [PUSH #2: DOUBLE-FREE] The same niov is pushed to the freelist twice, causing free_count to exceed nr_iovs. Subsequent freelist pushes then perform an out-of-bounds write (a u32 value) past the kvmalloc'd freelist array into the adjacent slab object. Fix this by replacing the non-atomic read-then-dec in io_zcrx_put_niov_uref() with an atomic_try_cmpxchg loop that atomically tests and decrements user_refs. This makes the operation safe against concurrent atomic_xchg from scrub without requiring scrub to acquire rq_lock. Fixes: 34a3e60821ab ("io_uring/zcrx: implement zerocopy receive pp memory provider") Cc: stable@vger.kernel.org Signed-off-by: Kai Aizen [pavel: removed a warning and a comment] Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- io_uring/zcrx.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c index d41aa01a26d31..93da8933a91fa 100644 --- a/io_uring/zcrx.c +++ b/io_uring/zcrx.c @@ -337,10 +337,14 @@ static inline atomic_t *io_get_user_counter(struct net_iov *niov) static bool io_zcrx_put_niov_uref(struct net_iov *niov) { atomic_t *uref = io_get_user_counter(niov); + int old; + + old = atomic_read(uref); + do { + if (unlikely(old == 0)) + return false; + } while (!atomic_try_cmpxchg(uref, &old, old - 1)); - if (unlikely(!atomic_read(uref))) - return false; - atomic_dec(uref); return true; } -- 2.51.0