Skip to content

Commit 52a8fe8

Browse files
zhaohemsmb49
authored andcommitted
md/bitmap: don't set sb values if can't pass sanity check
BugLink: https://bugs.launchpad.net/bugs/1982409 [ Upstream commit e68cb83 ] If bitmap area contains invalid data, kernel will crash then mdadm triggers "Segmentation fault". This is cluster-md speical bug. In non-clustered env, mdadm will handle broken metadata case. In clustered array, only kernel space handles bitmap slot info. But even this bug only happened in clustered env, current sanity check is wrong, the code should be changed. How to trigger: (faulty injection) dd if=/dev/zero bs=1M count=1 oflag=direct of=/dev/sda dd if=/dev/zero bs=1M count=1 oflag=direct of=/dev/sdb mdadm -C /dev/md0 -b clustered -e 1.2 -n 2 -l mirror /dev/sda /dev/sdb mdadm -Ss echo aaa > magic.txt == below modifying slot 2 bitmap data == dd if=magic.txt of=/dev/sda seek=16384 bs=1 count=3 <== destroy magic dd if=/dev/zero of=/dev/sda seek=16436 bs=1 count=4 <== ZERO chunksize mdadm -A /dev/md0 /dev/sda /dev/sdb == kernel crashes. mdadm outputs "Segmentation fault" == Reason of kernel crash: In md_bitmap_read_sb (called by md_bitmap_create), bad bitmap magic didn't block chunksize assignment, and zero value made DIV_ROUND_UP_SECTOR_T() trigger "divide error". Crash log: kernel: md: md0 stopped. kernel: md/raid1:md0: not clean -- starting background reconstruction kernel: md/raid1:md0: active with 2 out of 2 mirrors kernel: dlm: ... ... kernel: md-cluster: Joined cluster 44810aba-38bb-e6b8-daca-bc97a0b254aa slot 1 kernel: md0: invalid bitmap file superblock: bad magic kernel: md_bitmap_copy_from_slot can't get bitmap from slot 2 kernel: md-cluster: Could not gather bitmaps from slot 2 kernel: divide error: 0000 [#1] SMP NOPTI kernel: CPU: 0 PID: 1603 Comm: mdadm Not tainted 5.14.6-1-default kernel: Hardware name: QEMU Standard PC (i440FX + PIIX, 1996) kernel: RIP: 0010:md_bitmap_create+0x1d1/0x850 [md_mod] kernel: RSP: 0018:ffffc22ac0843ba0 EFLAGS: 00010246 kernel: ... ... kernel: Call Trace: kernel: ? dlm_lock_sync+0xd0/0xd0 [md_cluster 77fe..7a0] kernel: md_bitmap_copy_from_slot+0x2c/0x290 [md_mod 24ea..d3a] kernel: load_bitmaps+0xec/0x210 [md_cluster 77fe..7a0] kernel: md_bitmap_load+0x81/0x1e0 [md_mod 24ea..d3a] kernel: do_md_run+0x30/0x100 [md_mod 24ea..d3a] kernel: md_ioctl+0x1290/0x15a0 [md_mod 24ea....d3a] kernel: ? mddev_unlock+0xaa/0x130 [md_mod 24ea..d3a] kernel: ? blkdev_ioctl+0xb1/0x2b0 kernel: block_ioctl+0x3b/0x40 kernel: __x64_sys_ioctl+0x7f/0xb0 kernel: do_syscall_64+0x59/0x80 kernel: ? exit_to_user_mode_prepare+0x1ab/0x230 kernel: ? syscall_exit_to_user_mode+0x18/0x40 kernel: ? do_syscall_64+0x69/0x80 kernel: entry_SYSCALL_64_after_hwframe+0x44/0xae kernel: RIP: 0033:0x7f4a15fa722b kernel: ... ... kernel: ---[ end trace 8afa7612f559c868 ]--- kernel: RIP: 0010:md_bitmap_create+0x1d1/0x850 [md_mod] Reported-by: kernel test robot <lkp@intel.com> Reported-by: Dan Carpenter <dan.carpenter@oracle.com> Acked-by: Guoqing Jiang <guoqing.jiang@linux.dev> Signed-off-by: Heming Zhao <heming.zhao@suse.com> Signed-off-by: Song Liu <song@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Kamal Mostafa <kamal@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 11248a6 commit 52a8fe8

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

drivers/md/md-bitmap.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,6 @@ static int md_bitmap_read_sb(struct bitmap *bitmap)
642642
daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
643643
write_behind = le32_to_cpu(sb->write_behind);
644644
sectors_reserved = le32_to_cpu(sb->sectors_reserved);
645-
/* Setup nodes/clustername only if bitmap version is
646-
* cluster-compatible
647-
*/
648-
if (sb->version == cpu_to_le32(BITMAP_MAJOR_CLUSTERED)) {
649-
nodes = le32_to_cpu(sb->nodes);
650-
strlcpy(bitmap->mddev->bitmap_info.cluster_name,
651-
sb->cluster_name, 64);
652-
}
653645

654646
/* verify that the bitmap-specific fields are valid */
655647
if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
@@ -671,6 +663,16 @@ static int md_bitmap_read_sb(struct bitmap *bitmap)
671663
goto out;
672664
}
673665

666+
/*
667+
* Setup nodes/clustername only if bitmap version is
668+
* cluster-compatible
669+
*/
670+
if (sb->version == cpu_to_le32(BITMAP_MAJOR_CLUSTERED)) {
671+
nodes = le32_to_cpu(sb->nodes);
672+
strlcpy(bitmap->mddev->bitmap_info.cluster_name,
673+
sb->cluster_name, 64);
674+
}
675+
674676
/* keep the array size field of the bitmap superblock up to date */
675677
sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
676678

@@ -703,9 +705,9 @@ static int md_bitmap_read_sb(struct bitmap *bitmap)
703705

704706
out:
705707
kunmap_atomic(sb);
706-
/* Assigning chunksize is required for "re_read" */
707-
bitmap->mddev->bitmap_info.chunksize = chunksize;
708708
if (err == 0 && nodes && (bitmap->cluster_slot < 0)) {
709+
/* Assigning chunksize is required for "re_read" */
710+
bitmap->mddev->bitmap_info.chunksize = chunksize;
709711
err = md_setup_cluster(bitmap->mddev, nodes);
710712
if (err) {
711713
pr_warn("%s: Could not setup cluster service (%d)\n",
@@ -716,18 +718,18 @@ static int md_bitmap_read_sb(struct bitmap *bitmap)
716718
goto re_read;
717719
}
718720

719-
720721
out_no_sb:
721-
if (test_bit(BITMAP_STALE, &bitmap->flags))
722-
bitmap->events_cleared = bitmap->mddev->events;
723-
bitmap->mddev->bitmap_info.chunksize = chunksize;
724-
bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
725-
bitmap->mddev->bitmap_info.max_write_behind = write_behind;
726-
bitmap->mddev->bitmap_info.nodes = nodes;
727-
if (bitmap->mddev->bitmap_info.space == 0 ||
728-
bitmap->mddev->bitmap_info.space > sectors_reserved)
729-
bitmap->mddev->bitmap_info.space = sectors_reserved;
730-
if (err) {
722+
if (err == 0) {
723+
if (test_bit(BITMAP_STALE, &bitmap->flags))
724+
bitmap->events_cleared = bitmap->mddev->events;
725+
bitmap->mddev->bitmap_info.chunksize = chunksize;
726+
bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
727+
bitmap->mddev->bitmap_info.max_write_behind = write_behind;
728+
bitmap->mddev->bitmap_info.nodes = nodes;
729+
if (bitmap->mddev->bitmap_info.space == 0 ||
730+
bitmap->mddev->bitmap_info.space > sectors_reserved)
731+
bitmap->mddev->bitmap_info.space = sectors_reserved;
732+
} else {
731733
md_bitmap_print_sb(bitmap);
732734
if (bitmap->cluster_slot < 0)
733735
md_cluster_stop(bitmap->mddev);

0 commit comments

Comments
 (0)