bcache: fix variable length array abuse in btree_iter

[ Upstream commit 3a861560ccb35f2a4f0a4b8207fa7c2a35fc7f31 ]

btree_iter is used in two ways: either allocated on the stack with a
fixed size MAX_BSETS, or from a mempool with a dynamic size based on the
specific cache set. Previously, the struct had a fixed-length array of
size MAX_BSETS which was indexed out-of-bounds for the dynamically-sized
iterators, which causes UBSAN to complain.

This patch uses the same approach as in bcachefs's sort_iter and splits
the iterator into a btree_iter with a flexible array member and a
btree_iter_stack which embeds a btree_iter as well as a fixed-length
data array.

BUG=b/352002606
TEST=presubmit
RELEASE_NOTE=Fixes CVE-2024-39482 in the Linux kernel

cos-patch: security-moderate
Cc: [email protected]
Closes: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2039368
Change-Id: I417e37b774e79b2f29c15c9eade185d20d3ed50a
Signed-off-by: Matthew Mirvish <[email protected]>
Signed-off-by: Coly Li <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
Signed-off-by: Arnav Kansal <[email protected]>
Reviewed-on: https://cos-review.googlesource.com/c/third_party/kernel/+/75841
Reviewed-by: Kevin Berry <[email protected]>
Tested-by: Cusky Presubmit Bot <[email protected]>
6 files changed