Skip to contentSkip to conversation

ocfs2: add check for free bits before allocation in ocfs2_move_extent()

PATCH Superseded Deepanshu Kartikey 2 participants 2 messages Dec 31, 2025 – Jan 4 OCFS2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: add check for free bits before allocation in ocfs2_move_extent()

Add a check to verify the group descriptor has enough free bits before attempting allocation in ocfs2_move_extent(). This prevents a kernel BUG_ON crash in ocfs2_block_group_set_bits() when the move_extents ioctl is called on a crafted or corrupted filesystem.

The existing validation in ocfs2_validate_gd_self() only checks static metadata consistency (bg_free_bits_count <= bg_bits) when the descriptor is first read from disk. However, during move_extents operations, multiple allocations can exhaust the free bits count below the requested allocation size, triggering BUG_ON(le16_to_cpu(bg->bg_free_bits_count) num_bits).

The debug trace shows the issue clearly:
- Block group 32 validated with bg_free_bits_count=427
- Repeated allocations decreased count: 427 -> 171 -> 43 -> ... -> 1
- Final request for 2 bits with only 1 available triggers BUG_ON

By adding an early check in ocfs2_move_extent() before calling ocfs2_block_group_set_bits(), we return -ENOSPC gracefully instead of crashing the kernel.

---
 fs/ocfs2/move_extents.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/ocfs2/move_extents.c +5 -0
index 99637e34d9da..2548a8908a1b 100644
@@ -690,6 +690,11 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 		goto out_commit;
 	}
 
+	if (le16_to_cpu(gd->bg_free_bits_count) < len) {
+		ret = -ENOSPC;
+		goto out_commit;
+	}
+
 	ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
 					 goal_bit, len, 0, 0);
 	if (ret) {
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 11 lines · Deepanshu Kartikey

Add a check to verify the group descriptor has enough free bits before attempting allocation in ocfs2_move_extent(). This prevents a kernel BUG_ON crash in ocfs2_block_group_set_bits() when the move_extents ioctl is called on a crafted or corrupted filesystem.

The existing validation in ocfs2_validate_gd_self() only checks static metadata consistency (bg_free_bits_count <= bg_bits) when the descriptor is first read from disk. However, during move_extents operations, multiple allocations can exhaust the free bits count below the requested allocation size, triggering BUG_ON(le16_to_cpu(bg->bg_free_bits_count) num_bits).

BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits)

Seems '<' is missing.

Quoted 23 lines

The debug trace shows the issue clearly:
- Block group 32 validated with bg_free_bits_count=427
- Repeated allocations decreased count: 427 -> 171 -> 43 -> ... -> 1
- Final request for 2 bits with only 1 available triggers BUG_ON

By adding an early check in ocfs2_move_extent() before calling ocfs2_block_group_set_bits(), we return -ENOSPC gracefully instead of crashing the kernel.

---
 fs/ocfs2/move_extents.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/ocfs2/move_extents.c +0 -0
index 99637e34d9da..2548a8908a1b 100644

fs/ocfs2/move_extents.c static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, · line 697

Quoted 8 lines
fs/ocfs2/move_extents.c +5 -0
@@ -690,6 +690,11 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 		goto out_commit;
 	}
 
+	if (le16_to_cpu(gd->bg_free_bits_count) < len) {
+		ret = -ENOSPC;
+		goto out_commit;
+	}
+

Why not check rightly after ocfs2_find_victim_alloc_group()?

Joseph

Quoted 3 lines
fs/ocfs2/move_extents.c +0 -0
 	ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
 					 goal_bit, len, 0, 0);
 	if (ret) {