In the Linux kernel, the following vulnerability has been resolved:
f2fs: fix to do sanity check on sitbitmapsize
w/ below testcase, resize will generate a corrupted image which contains inconsistent metadata, so when mounting such image, it will trigger kernel panic:
touch img truncate -s $((512102410241024)) img mkfs.f2fs -f img $((25610241024)) resize.f2fs -s -i img -t $((10241024*1024)) mount img /mnt/f2fs
------------[ cut here ]------------ kernel BUG at fs/f2fs/segment.h:863! Oops: invalid opcode: 0000 [#1] SMP PTI CPU: 11 UID: 0 PID: 3922 Comm: mount Not tainted 6.15.0-rc1+ #191 PREEMPT(voluntary) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 RIP: 0010:f2fsrameta_pages+0x47c/0x490
Call Trace: f2fsbuildsegmentmanager+0x11c3/0x2600 f2fsfillsuper+0xe97/0x2840 mountbdev+0xf4/0x140 legacygettree+0x2b/0x50 vfsgettree+0x29/0xd0 pathmount+0x487/0xaf0 _x64sysmount+0x116/0x150 dosyscall64+0x82/0x190 entrySYSCALL64afterhwframe+0x76/0x7e RIP: 0033:0x7fdbfde1bcfe
The reaseon is:
siti->bitmapsize is 192, so size of sit bitmap is 192*8=1536, at maximum there are 1536 sit blocks, however MAINSEGS is 261893, so that sitblkcnt is 4762, buildsitentries() -> currentsitaddr() tries to access out-of-boundary in sitbitmap at offset from [1536, 4762), once sitbitmap and sitbitmapmirror is not the same, it will trigger f2fsbug_on().
Let's add sanity check in f2fssanitycheck_ckpt() to avoid panic.