md: bound raid_disks by max_disks for v0.90 metadata
PATCH 1/1 md: bound raid_disks by max_disks for v0.90 metadata
md: bound raid_disks by max_disks for v0.90 metadata
commit 1a33a379e6 ("md: reject v0.90 arrays with too many devices") bounded sb->raid_disks in super_90_load(), so a crafted superblock can no longer describe more devices than a v0.90 superblock is able to hold. mddev->raid_disks however is not only loaded from the superblock.
Both the raid_disks sysfs attribute and MD_SET_ARRAY_INFO write it directly while the array has no personality, and neither enforces any upper bound. super_90_sync() then lays out the in-superblock device table using mddev->raid_disks and walks past the end of the 4 KiB superblock page.
mddev->max_disks already carries the limit that the selected metadata format can represent, and update_raid_disks() already enforces it, but it is only initialized when a superblock is read from a member device. An array that has just been created through sysfs or MD_SET_ARRAY_INFO has max_disks cleared, so the check is skipped.
Set max_disks when v0.90 metadata is selected in metadata_store(), and reject values the selected format cannot represent in raid_disks_store(). Both orderings are then covered: the limit is installed either before the value is written or together with the metadata choice. Clamp super_90_sync() as well, so an array which already carries a larger value cannot walk past sb->disks[].
v1.x metadata sets max_disks to (4096-256)/2 and is therefore not affected by the v0.90 limit.
drivers/md/md.c +12 -3
@@ -1610,7 +1610,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) {mdp_super_t *sb;struct md_rdev *rdev2; -int next_spare = mddev->raid_disks; +int next_spare =min_t (int, mddev->raid_disks,MD_SB_DISKS );/* make rdev->sb match mddev data.. *
@@ -1643,7 +1643,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) sb->ctime =clamp_t (time64_t, mddev->ctime,0 ,U32_MAX ); sb->level = mddev->level; sb->size = mddev->dev_sectors /2 ; - sb->raid_disks = mddev->raid_disks; + sb->raid_disks =min_t (int, mddev->raid_disks,MD_SB_DISKS ); sb->md_minor = mddev->md_minor; sb->not_persistent =0 ; sb->utime =clamp_t (time64_t, mddev->utime,0 ,U32_MAX );
@@ -1727,7 +1727,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) d->state |= (1 <<MD_DISK_FAILFAST ); }/* now set the "removed" and "faulty" bits on any missing devices */ -for (i=0 ; i < mddev->raid_disks ; i++) { +for (i=0 ; i <min_t (int, mddev->raid_disks,MD_SB_DISKS ) ; i++) {mdp_disk_t *d = &sb->disks[i];if (d->state ==0 && d->number ==0 ) { d->number = i;
@@ -4441,6 +4441,8 @@ raid_disks_store(struct mddev *mddev, const char *buf, size_t len) mddev->delta_disks = n - olddisks; mddev->raid_disks = n; mddev->reshape_backwards = (mddev->delta_disks <0 ); + }else if (mddev->max_disks && n >= mddev->max_disks) { + err = -EINVAL ; }else mddev->raid_disks = n; out_unlock:
@@ -5029,6 +5031,13 @@ metadata_store(struct mddev *mddev, const char *buf, size_t len) err = -ENOENT ;if (major >=ARRAY_SIZE (super_types) || super_types[major].name ==NULL ) goto out_unlock; + err = -EINVAL ; +if (major ==0 && mddev->raid_disks >=MD_SB_DISKS ) + goto out_unlock; +if (major ==0 ) + mddev->max_disks =MD_SB_DISKS ; +else + mddev->max_disks = (4096 -256 ) /2 ; mddev->major_version = major; mddev->minor_version = minor; mddev->persistent =1 ;
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] md: out-of-bounds access to sb->disks[] in super_90_sync() - [Medium] md: missing raid_disks check…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] md: out-of-bounds access to sb->disks[] in super_90_sync()
- [Medium] md: missing raid_disks check and max_disks init in md_set_array_info()
- [Medium] md: max_disks check bypass and int overflow in raid_disks_store()
- [Low] md: incomplete max_disks check and stale max_disks in metadata_store()
- [Low] md: 10-char SHA in Fixes trailer and duplicated subject in changelog
--
commit 0588d69c6a4d85b9a67f6669d8e19860ad381e67
Author: Yang Luohan <[email protected]>
Quoted 28 lines
md: bound
raid_disksbymax_disksfor v0.90 metadatamd: bound
raid_disksbymax_disksfor v0.90 metadatacommit
1a33a379e6("md: reject v0.90 arrays with too many devices") boundedsb->raid_disksinsuper_90_load(), so a crafted superblock can no longer describe more devices than a v0.90 superblock is able to hold.mddev->raid_diskshowever is not only loaded from the superblock.Both the
raid_diskssysfs attribute andMD_SET_ARRAY_INFOwrite it directly while the array has no personality, and neither enforces any upper bound.super_90_sync()then lays out the in-superblock device table usingmddev->raid_disksand walks past the end of the 4 KiB superblock page.
mddev->max_disksalready carries the limit that the selected metadata format can represent, andupdate_raid_disks()already enforces it, but it is only initialized when a superblock is read from a member device. An array that has just been created through sysfs orMD_SET_ARRAY_INFOhasmax_diskscleared, so the check is skipped.Set
max_diskswhen v0.90 metadata is selected inmetadata_store(), and reject values the selected format cannot represent inraid_disks_store(). Both orderings are then covered: the limit is installed either before the value is written or together with the metadata choice. Clampsuper_90_sync()as well, so an array which already carries a larger value cannot walk past sb->disks[].
Quoted 5 lines
v1.x metadata sets
max_disksto (4096-256)/2 and is therefore not affected by the v0.90 limit.Fixes 1a33a379e6 ("md: reject v0.90 arrays with too many devices")Signed-off-by Yang Luohan <[email protected]>
[Severity: Low]
The Fixes: tag uses a 10-character SHA instead of at least 12 characters:
The subject line is also duplicated at the top of the commit message body.
[Severity: Medium]
The commit message notes that MD_SET_ARRAY_INFO writes mddev->raid_disks directly and leaves max_disks cleared, but md_set_array_info() is not updated in this patch.
When info->raid_disks != 0, md_set_array_info() assigns mddev->raid_disks = info->raid_disks without checking it against 0 or MD_SB_DISKS. When info->raid_disks == 0, it sets major_version, minor_version and persistent while leaving mddev->max_disks == 0, so a subsequent sysfs write to raid_disks_store() still bypasses the max_disks check.
Quoted 23 lines
drivers/md/md.c +2
-2@@ -1630,7 +1630,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) {mdp_super_t *sb;struct md_rdev *rdev2; -int next_spare = mddev->raid_disks; +int next_spare =min_t (int, mddev->raid_disks,MD_SB_DISKS );/* make rdev->sb match mddev data.. * @@ -1663,7 +1663,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) sb->ctime =clamp_t (time64_t, mddev->ctime,0 ,U32_MAX ); sb->level = mddev->level; sb->size = mddev->dev_sectors /2 ; - sb->raid_disks = mddev->raid_disks; + sb->raid_disks =min_t (int, mddev->raid_disks,MD_SB_DISKS ); sb->md_minor = mddev->md_minor; sb->not_persistent =0 ; sb->utime =clamp_t (time64_t, mddev->utime,0 ,U32_MAX );
drivers/md/md.c static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) · line 1753
Quoted 8 lines
drivers/md/md.c +1
-1@@ -1747,7 +1747,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) d->state |= (1 <<MD_DISK_FAILFAST ); }/* now set the "removed" and "faulty" bits on any missing devices */ -for (i=0 ; i < mddev->raid_disks ; i++) { +for (i=0 ; i <min_t (int, mddev->raid_disks,MD_SB_DISKS ) ; i++) {mdp_disk_t *d = &sb->disks[i];if (d->state ==0 && d->number ==0 ) { d->number = i;
[Severity: High]
Can super_90_sync() still access sb->disks[] out of bounds in the rdev_for_each() loop and at sb->this_disk = sb->disks[rdev->desc_nr]?
sb->disks has MD_SB_DISKS (27) entries (indices 0..26), followed by sb->this_disk at the end of the 4 KiB rdev->sb_page:
drivers/md/md.c:super_90_sync() {
...
rdev_for_each(rdev2, mddev) {
...
if (is_active)
desc_nr = rdev2->raid_disk;
else
desc_nr = next_spare++;
rdev2->desc_nr = desc_nr;
d = &sb->disks[rdev2->desc_nr];
...
sb->this_disk = sb->disks[rdev->desc_nr];
}When mddev->raid_disks >= MD_SB_DISKS (or when mddev->raid_disks plus the number of spare or faulty devices in mddev->disks exceeds MD_SB_DISKS), next_spare++ reaches >= MD_SB_DISKS, and rdev2->raid_disk for active devices is not bounded against MD_SB_DISKS either. Indexing sb->disks[rdev2->desc_nr] and sb->disks[rdev->desc_nr] then reads and writes past sb->disks[] and past the 4 KiB superblock page.
drivers/md/md.c raid_disks_store(struct mddev *mddev, const char *buf, size_t len) · line 4480
Quoted 9 lines
drivers/md/md.c +2
-0@@ -4473,6 +4473,8 @@ raid_disks_store(struct mddev *mddev, const char *buf, size_t len) mddev->delta_disks = n - olddisks; mddev->raid_disks = n; mddev->reshape_backwards = (mddev->delta_disks <0 ); + }else if (mddev->max_disks && n >= mddev->max_disks) { + err = -EINVAL ; }else mddev->raid_disks = n; out_unlock:
[Severity: Medium]
Can raid_disks_store() bypass the max_disks check or overflow mddev->raid_disks?
Because the new check is placed after the mddev->reshape_position != MaxSector branch, writing to raid_disks while mddev->pers == NULL and reshape_position != MaxSector assigns mddev->raid_disks = n without checking mddev->max_disks.
In addition, n is parsed as an unsigned int while mddev->raid_disks is a signed int. If n > INT_MAX is written while mddev->max_disks == 0, mddev->raid_disks becomes negative, which then passes the signed mddev->raid_disks >= MD_SB_DISKS check in metadata_store() and causes min_t(int, mddev->raid_disks, MD_SB_DISKS) in super_90_sync() to evaluate to a negative number.
Quoted 4 lines
drivers/md/md.c +0
-0@@ -5067,6 +5069,13 @@ metadata_store(struct mddev *mddev, const char *buf, size_t len) err = -ENOENT ;if (major >=ARRAY_SIZE (super_types) || super_types[major].name ==NULL ) goto out_unlock;
drivers/md/md.c metadata_store(struct mddev *mddev, const char *buf, size_t len) · line 5081
Quoted 10 lines
drivers/md/md.c +7
-0+ err = -EINVAL ; +if (major ==0 && mddev->raid_disks >=MD_SB_DISKS ) + goto out_unlock; +if (major ==0 ) + mddev->max_disks =MD_SB_DISKS ; +else + mddev->max_disks = (4096 -256 ) /2 ; mddev->major_version = major; mddev->minor_version = minor; mddev->persistent =1 ;
[Severity: Low]
Should metadata_store() also validate mddev->raid_disks against the new max_disks when major != 0, and reset mddev->max_disks = 0 in the "none" and "external:" branches?
Right now, writing raid_disks >= (4096 - 256) / 2 before selecting v1.x metadata is not rejected here, and switching metadata_version from 0.90 or 1.x to "none" or "external:" leaves the previous max_disks limit set on the array.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1