Skip to contentSkip to conversation

ocfs2: validate allocator type to prevent BUG_ON in ocfs2_block_group_search

PATCH In review Deepanshu Kartikey 2 participants 3 messages Jan 10 – Jan 23 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ocfs2: validate allocator type to prevent BUG_ON in ocfs2_block_group_search

ocfs2_is_cluster_bitmap() checks whether an inode is the global bitmap by comparing ip_blkno with osb->bitmap_blkno. A corrupted filesystem can have either the superblock's bitmap_blkno field corrupted to point to the inode allocator block, or the inode allocator's i_blkno field corrupted to match bitmap_blkno. In either case, ocfs2_is_cluster_bitmap() returns true for the inode allocator.

When the code later calls ocfs2_block_group_search(), it triggers BUG_ON(ocfs2_is_cluster_bitmap(inode)) causing a kernel panic.

Call trace:
  ocfs2_block_group_search+0x1c7/0x2c0 fs/ocfs2/suballoc.c:1611
  ocfs2_search_chain+0x38a/0x1010 fs/ocfs2/suballoc.c:1764
  ocfs2_claim_suballoc_bits+0x3a4/0x650 fs/ocfs2/suballoc.c:1978
  ocfs2_claim_new_inode+0x95/0x130 fs/ocfs2/suballoc.c:2137
  ocfs2_mknod_locked+0x129/0x510 fs/ocfs2/namei.c:568
  ocfs2_mknod+0x5c7/0x11d0 fs/ocfs2/namei.c:802
  ocfs2_create+0x136/0x170 fs/ocfs2/namei.c:852

Add validation in ocfs2_reserve_suballoc_bits() to check that the allocator inode type matches the expected type:
- Global bitmap allocator must be the cluster bitmap
- Other allocators (inode, extent) must NOT be the cluster bitmap

This follows the existing pattern of validating OCFS2_CHAIN_FL in the same function and uses ocfs2_error() for graceful error handling.

---
v2: Fix commit message - ocfs2_is_cluster_bitmap() checks ip_blkno
against bitmap_blkno, not a flag (Joseph Qi)

---
 fs/ocfs2/suballoc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/ocfs2/suballoc.c +20 -0
index 8e6e5235b30c..fb72c062a8d5 100644
@@ -813,6 +813,26 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
 		goto bail;
 	}
 
+	/*
+	 * Validate allocator type matches expected bitmap type.
+	 * Global bitmap must have BITMAP flag, other allocators must not.
+	 * This prevents a corrupted filesystem from triggering BUG_ON
+	 * in ocfs2_block_group_search() or ocfs2_cluster_group_search().
+	 */
+	if (type == GLOBAL_BITMAP_SYSTEM_INODE) {
+		if (!ocfs2_is_cluster_bitmap(alloc_inode)) {
+			status = ocfs2_error(alloc_inode->i_sb,
+					     "Global bitmap %llu missing bitmap flag\n",
+					     (unsigned long long)le64_to_cpu(fe->i_blkno));
+			goto bail;
+		}
+	} else if (ocfs2_is_cluster_bitmap(alloc_inode)) {
+		status = ocfs2_error(alloc_inode->i_sb,
+				     "Allocator %llu has invalid bitmap flag\n",
+				     (unsigned long long)le64_to_cpu(fe->i_blkno));
+		goto bail;
+	}
+
 	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
 		le32_to_cpu(fe->id1.bitmap1.i_used);
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 72 lines · Deepanshu Kartikey

ocfs2_is_cluster_bitmap() checks whether an inode is the global bitmap by comparing ip_blkno with osb->bitmap_blkno. A corrupted filesystem can have either the superblock's bitmap_blkno field corrupted to point to the inode allocator block, or the inode allocator's i_blkno field corrupted to match bitmap_blkno. In either case, ocfs2_is_cluster_bitmap() returns true for the inode allocator.

When the code later calls ocfs2_block_group_search(), it triggers BUG_ON(ocfs2_is_cluster_bitmap(inode)) causing a kernel panic.

Call trace:
  ocfs2_block_group_search+0x1c7/0x2c0 fs/ocfs2/suballoc.c:1611
  ocfs2_search_chain+0x38a/0x1010 fs/ocfs2/suballoc.c:1764
  ocfs2_claim_suballoc_bits+0x3a4/0x650 fs/ocfs2/suballoc.c:1978
  ocfs2_claim_new_inode+0x95/0x130 fs/ocfs2/suballoc.c:2137
  ocfs2_mknod_locked+0x129/0x510 fs/ocfs2/namei.c:568
  ocfs2_mknod+0x5c7/0x11d0 fs/ocfs2/namei.c:802
  ocfs2_create+0x136/0x170 fs/ocfs2/namei.c:852

Add validation in ocfs2_reserve_suballoc_bits() to check that the allocator inode type matches the expected type:
- Global bitmap allocator must be the cluster bitmap
- Other allocators (inode, extent) must NOT be the cluster bitmap

This follows the existing pattern of validating OCFS2_CHAIN_FL in the same function and uses ocfs2_error() for graceful error handling.

---
v2: Fix commit message - ocfs2_is_cluster_bitmap() checks ip_blkno
against bitmap_blkno, not a flag (Joseph Qi)

---
 fs/ocfs2/suballoc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/ocfs2/suballoc.c +20 -1
index 8e6e5235b30c..fb72c062a8d5 100644
@@ -813,6 +813,26 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
                goto bail;
        }
 
+       /*
+        * Validate allocator type matches expected bitmap type.
+        * Global bitmap must have BITMAP flag, other allocators must not.
+        * This prevents a corrupted filesystem from triggering BUG_ON
+        * in ocfs2_block_group_search() or ocfs2_cluster_group_search().
+        */
+       if (type == GLOBAL_BITMAP_SYSTEM_INODE) {
+               if (!ocfs2_is_cluster_bitmap(alloc_inode)) {
+                       status = ocfs2_error(alloc_inode->i_sb,
+                                            "Global bitmap %llu missing bitmap flag\n",
+                                            (unsigned long long)le64_to_cpu(fe->i_blkno));
+                       goto bail;
+               }
+       } else if (ocfs2_is_cluster_bitmap(alloc_inode)) {
+               status = ocfs2_error(alloc_inode->i_sb,
+                                    "Allocator %llu has invalid bitmap flag\n",
+                                    (unsigned long long)le64_to_cpu(fe->i_blkno));
+               goto bail;
+       }
+
        free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
                le32_to_cpu(fe->id1.bitmap1.i_used);
 
--
Quoted 2 lines

2.43.0

Hi Joseph,

Just wanted to follow up on this patch. Do you have any further comments or suggestions?

Thanks,
Deepanshu

maintainer
#2 lore raw
Quoted 80 lines · Deepanshu Kartikey
Quoted 72 lines · Deepanshu Kartikey

ocfs2_is_cluster_bitmap() checks whether an inode is the global bitmap by comparing ip_blkno with osb->bitmap_blkno. A corrupted filesystem can have either the superblock's bitmap_blkno field corrupted to point to the inode allocator block, or the inode allocator's i_blkno field corrupted to match bitmap_blkno. In either case, ocfs2_is_cluster_bitmap() returns true for the inode allocator.

When the code later calls ocfs2_block_group_search(), it triggers BUG_ON(ocfs2_is_cluster_bitmap(inode)) causing a kernel panic.

Call trace:
  ocfs2_block_group_search+0x1c7/0x2c0 fs/ocfs2/suballoc.c:1611
  ocfs2_search_chain+0x38a/0x1010 fs/ocfs2/suballoc.c:1764
  ocfs2_claim_suballoc_bits+0x3a4/0x650 fs/ocfs2/suballoc.c:1978
  ocfs2_claim_new_inode+0x95/0x130 fs/ocfs2/suballoc.c:2137
  ocfs2_mknod_locked+0x129/0x510 fs/ocfs2/namei.c:568
  ocfs2_mknod+0x5c7/0x11d0 fs/ocfs2/namei.c:802
  ocfs2_create+0x136/0x170 fs/ocfs2/namei.c:852

Add validation in ocfs2_reserve_suballoc_bits() to check that the allocator inode type matches the expected type:
- Global bitmap allocator must be the cluster bitmap
- Other allocators (inode, extent) must NOT be the cluster bitmap

This follows the existing pattern of validating OCFS2_CHAIN_FL in the same function and uses ocfs2_error() for graceful error handling.

---
v2: Fix commit message - ocfs2_is_cluster_bitmap() checks ip_blkno
against bitmap_blkno, not a flag (Joseph Qi)

---
 fs/ocfs2/suballoc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/ocfs2/suballoc.c +20 -1
index 8e6e5235b30c..fb72c062a8d5 100644
@@ -813,6 +813,26 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
                goto bail;
        }
 
+       /*
+        * Validate allocator type matches expected bitmap type.
+        * Global bitmap must have BITMAP flag, other allocators must not.
+        * This prevents a corrupted filesystem from triggering BUG_ON
+        * in ocfs2_block_group_search() or ocfs2_cluster_group_search().
+        */
+       if (type == GLOBAL_BITMAP_SYSTEM_INODE) {
+               if (!ocfs2_is_cluster_bitmap(alloc_inode)) {
+                       status = ocfs2_error(alloc_inode->i_sb,
+                                            "Global bitmap %llu missing bitmap flag\n",
+                                            (unsigned long long)le64_to_cpu(fe->i_blkno));
+                       goto bail;
+               }
+       } else if (ocfs2_is_cluster_bitmap(alloc_inode)) {
+               status = ocfs2_error(alloc_inode->i_sb,
+                                    "Allocator %llu has invalid bitmap flag\n",
+                                    (unsigned long long)le64_to_cpu(fe->i_blkno));
+               goto bail;
+       }
+
        free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
                le32_to_cpu(fe->id1.bitmap1.i_used);
 
--
Quoted 2 lines

2.43.0

Hi Joseph,

Quoted 3 lines

Just wanted to follow up on this patch. Do you have any further comments or suggestions?

Hi,since the inode block is read from disk, it seems we'd better validate it in ocfs2_validate_inode_block().

Thanks,
Joseph