Skip to contentSkip to conversation

bcachefs: avoid truncating fiemap extent length

PATCH Applied Mikhail Dmitrichenko 3 participants 4 messages Jul 3 – Jul 5 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: avoid truncating fiemap extent length

No upstream commit exists for this patch.

bkey sizes are stored in sectors as u32, while fiemap reports byte lengths as u64. Shifting k.k->size before widening performs the conversion in 32 bits, so an extent of 4 GiB or larger can wrap before it is passed to fiemap_fill_next_extent().

Compute the byte length after casting the sector count to u64 and reuse it for all bch2_fill_extent() cases.

The same issue was fixed in bcachefs-tools, but there is no Linux upstream commit to backport to 6.12. The affected 6.12 implementation lives in fs/bcachefs/fs.c, while the bcachefs-tools fix touches fs/bcachefs/vfs/fiemap.c.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
 fs/bcachefs/fs.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/bcachefs/fs.c +5 -3
index a41d0d8a2f7b..0dc2466de3f9 100644
@@ -1184,6 +1184,8 @@ static int bch2_fill_extent(struct bch_fs *c,
 			    struct fiemap_extent_info *info,
 			    struct bkey_s_c k, unsigned flags)
 {
+	u64 len = (u64)k.k->size << 9;
+
 	if (bkey_extent_is_direct_data(k.k)) {
 		struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
 		const union bch_extent_entry *entry;
@@ -1212,7 +1214,7 @@ static int bch2_fill_extent(struct bch_fs *c,
 			ret = fiemap_fill_next_extent(info,
 						bkey_start_offset(k.k) << 9,
 						offset << 9,
-						k.k->size << 9, flags|flags2);
+						len, flags | flags2);
 			if (ret)
 				return ret;
 		}
@@ -1221,13 +1223,13 @@ static int bch2_fill_extent(struct bch_fs *c,
 	} else if (bkey_extent_is_inline_data(k.k)) {
 		return fiemap_fill_next_extent(info,
 					       bkey_start_offset(k.k) << 9,
-					       0, k.k->size << 9,
+					       0, len,
 					       flags|
 					       FIEMAP_EXTENT_DATA_INLINE);
 	} else if (k.k->type == KEY_TYPE_reservation) {
 		return fiemap_fill_next_extent(info,
 					       bkey_start_offset(k.k) << 9,
-					       0, k.k->size << 9,
+					       0, len,
 					       flags|
 					       FIEMAP_EXTENT_DELALLOC|
 					       FIEMAP_EXTENT_UNWRITTEN);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 6 lines · Mikhail Dmitrichenko

No upstream commit exists for this patch.

bkey sizes are stored in sectors as u32, while fiemap reports byte lengths as u64. Shifting k.k->size before widening performs the conversion in 32 bits, so an extent of 4 GiB or larger can wrap before it is passed to fiemap_fill_next_extent().

Thanks for the patch, happy to take it if we can get an ack from the bcachefs maintainers.

Signature · 2 lines
Thanks,
Sasha
#2 lore raw
Quoted 14 lines · Sasha Levin
Quoted 6 lines · Mikhail Dmitrichenko

No upstream commit exists for this patch.

bkey sizes are stored in sectors as u32, while fiemap reports byte lengths as u64. Shifting k.k->size before widening performs the conversion in 32 bits, so an extent of 4 GiB or larger can wrap before it is passed to fiemap_fill_next_extent().

Thanks for the patch, happy to take it if we can get an ack from the bcachefs maintainers.

Signature · 2 lines
Thanks,
Sasha

Ack

maintainer Applied
#3 lore raw
Quoted 5 lines · Kent Overstreet
Quoted 2 lines · Sasha Levin

Thanks for the patch, happy to take it if we can get an ack from the bcachefs maintainers.

Ack

Queued for 6.12, thanks!

Signature · 2 lines
Thanks,
Sasha