ocfs2: validate cl_bpc in ocfs2_block_group_alloc to prevent divide-by-zero
PATCH 1/1 ocfs2: validate cl_bpc in ocfs2_block_group_alloc to prevent divide-by-zero
The chain allocator field cl_bpc (blocks per cluster) is read from disk and used in division operations without validation. A corrupted filesystem image with cl_bpc=0 causes a divide-by-zero crash in the kernel:
divide error: 0000 [#1] PREEMPT SMP KASAN
RIP: 0010:ocfs2_bg_discontig_add_extent fs/ocfs2/suballoc.c:335 [inline] RIP: 0010:ocfs2_block_group_fill+0x5bd/0xa70 fs/ocfs2/suballoc.c:386 Call Trace: ocfs2_block_group_alloc+0x7e9/0x1330 fs/ocfs2/suballoc.c:703 ocfs2_reserve_suballoc_bits+0x20a6/0x4640 fs/ocfs2/suballoc.c:834 ocfs2_reserve_new_inode+0x4f4/0xcc0 fs/ocfs2/suballoc.c:1074 ocfs2_mknod+0x83c/0x2050 fs/ocfs2/namei.c:306
This patch adds validation in ocfs2_block_group_alloc() to ensure cl_bpc matches the expected value calculated from the superblock's cluster size and block size. This validation follows the same pattern used elsewhere in OCFS2 to verify on-disk structures against known-good values derived from the superblock parameters.
The check is performed early in the allocation path, before any resources are allocated or transactions started, ensuring clean error propagation. If validation fails, the filesystem is marked read-only and the operation returns -EUCLEAN (Structure needs cleaning), prompting the administrator to run fsck.ocfs2.
The validation catches both:
- Zero values that cause divide-by-zero crashes
- Non-zero but incorrect values indicating filesystem corruption or mismatched filesystem geometry
With this fix, mounting a corrupted filesystem produces:
OCFS2: ERROR (device loop0): ocfs2_block_group_alloc: Chain allocator
74 has corrupted cl_bpc: ondisk=0 expected=16
OCFS2: File system is now read-only.
Instead of a kernel crash.
--- fs/ocfs2/suballoc.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
fs/ocfs2/suballoc.c +12 -0
@@ -667,10 +667,22 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,u16 alloc_rec;struct buffer_head *bg_bh =NULL ;struct ocfs2_group_desc *bg; +u16 cl_bpc, expected_bpc;BUG_ON (ocfs2_is_cluster_bitmap (alloc_inode)); cl = &fe->id2.i_chain; + cl_bpc =le16_to_cpu (cl->cl_bpc); + expected_bpc =1 << (osb->s_clustersize_bits - alloc_inode->i_sb->s_blocksize_bits); +if (cl_bpc != expected_bpc) { +ocfs2_error (alloc_inode->i_sb, +"Chain allocator %llu has corrupted cl_bpc: ondisk=%u expected=%u\n" , + (unsigned long long )le64_to_cpu (fe->i_blkno), + cl_bpc, expected_bpc); + status = -EUCLEAN ; + goto bail; + } + status =ocfs2_reserve_clusters_with_limit (osb,le16_to_cpu (cl->cl_cpg), max_block, flags, &ac);
Signature · 1 line
2.43.0
Joseph Qi
maintainerQuoted 51 lines · Deepanshu Kartikey
The chain allocator field
cl_bpc(blocks per cluster) is read from disk and used in division operations without validation. A corrupted filesystem image with cl_bpc=0 causes a divide-by-zero crash in the kernel:divide error: 0000 [#1] PREEMPT SMP KASAN
RIP: 0010:ocfs2_bg_discontig_add_extent fs/ocfs2/suballoc.c:335 [inline] RIP: 0010:ocfs2_block_group_fill+0x5bd/0xa70 fs/ocfs2/suballoc.c:386 Call Trace: ocfs2_block_group_alloc+0x7e9/0x1330 fs/ocfs2/suballoc.c:703 ocfs2_reserve_suballoc_bits+0x20a6/0x4640 fs/ocfs2/suballoc.c:834 ocfs2_reserve_new_inode+0x4f4/0xcc0 fs/ocfs2/suballoc.c:1074 ocfs2_mknod+0x83c/0x2050 fs/ocfs2/namei.c:306This patch adds validation in
ocfs2_block_group_alloc()to ensurecl_bpcmatches the expected value calculated from the superblock's cluster size and block size. This validation follows the same pattern used elsewhere in OCFS2 to verify on-disk structures against known-good values derived from the superblock parameters.The check is performed early in the allocation path, before any resources are allocated or transactions started, ensuring clean error propagation. If validation fails, the filesystem is marked read-only and the operation returns
-EUCLEAN(Structure needs cleaning), prompting the administrator to run fsck.ocfs2.The validation catches both:
- Zero values that cause divide-by-zero crashes
- Non-zero but incorrect values indicating filesystem corruption or mismatched filesystem geometryWith this fix, mounting a corrupted filesystem produces:
OCFS2: ERROR (device loop0):ocfs2_block_group_alloc: Chain allocator
74 has corruptedcl_bpc: ondisk=0 expected=16
OCFS2: File system is now read-only.Instead of a kernel crash.
Reported-by [email protected]Tested-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/ocfs2/suballoc.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)fs/ocfs2/suballoc.c +0
-0
fs/ocfs2/suballoc.c static int ocfs2_block_group_alloc(struct ocfs2_super *osb, · line 676
Quoted 10 lines
fs/ocfs2/suballoc.c +3
-0@@ -667,10 +667,22 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,u16 alloc_rec;struct buffer_head *bg_bh =NULL ;struct ocfs2_group_desc *bg; +u16 cl_bpc, expected_bpc;BUG_ON (ocfs2_is_cluster_bitmap (alloc_inode)); cl = &fe->id2.i_chain; + cl_bpc =le16_to_cpu (cl->cl_bpc); + expected_bpc =1 << (osb->s_clustersize_bits - alloc_inode->i_sb->s_blocksize_bits);
'cl_bpc' is 'bits per cluster'.
fs/ocfs2/suballoc.c static int ocfs2_block_group_alloc(struct ocfs2_super *osb, · line 685
Quoted 9 lines
fs/ocfs2/suballoc.c +9
-0+if (cl_bpc != expected_bpc) { +ocfs2_error (alloc_inode->i_sb, +"Chain allocator %llu has corrupted cl_bpc: ondisk=%u expected=%u\n" , + (unsigned long long )le64_to_cpu (fe->i_blkno), + cl_bpc, expected_bpc); + status = -EUCLEAN ; + goto bail; + } +
Why not check this in ocfs2_validate_inode_block()? This may benifit all paths.
Thanks,
Joseph
Quoted 3 lines
fs/ocfs2/suballoc.c +0
-0status =ocfs2_reserve_clusters_with_limit (osb,le16_to_cpu (cl->cl_cpg), max_block, flags, &ac);
Hi Joseph,
Thank you for the review and suggestion!
You're absolutely right that moving the validation to ocfs2_validate_inode_block()
would be cleaner and benefit all code paths. I'll revise the patch to add the validation there, checking for OCFS2_CHAIN_FL before validating cl_bpc.
I'll send v2 shortly.
Thanks,
Deepanshu