md/raid1: Prevent excessively large memory allocations
Subsystems (2)
PATCH 1/1 md/raid1: Prevent excessively large memory allocations
Validate mddev->raid_disks before memory allocation to prevent integer wrap-around or allocating an unreasonably large chunk of memory that exceeds KMALLOC_MAX_SIZE boundaries.
--- drivers/md/raid1.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/md/raid1.c +10 -0
@@ -3050,6 +3050,16 @@ static struct r1conf *setup_conf(struct mddev *mddev)struct md_rdev *rdev;size_t r1bio_size;int err = -ENOMEM ; +size_t calculated_size; + +/* prevent baseline integer wrap-around anomalies first*/ +/* Safely calculate size; overflows automatically saturate to SIZE_MAX */ + + calculated_size =size_add (sizeof (struct r1conf ), +size_mul (mddev->raid_disks,sizeof (struct r1bio ))); + +if (mddev->raid_disks <=0 || calculated_size >=KMALLOC_MAX_SIZE ) +return ERR_PTR (-EINVAL ); conf = kzalloc_obj(struct r1conf);if (!conf)
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] md/raid1: incorrect allocation size calculation in setup_conf() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] md/raid1: incorrect allocation size calculation in setup_conf() --
commit c8cc718382c763b1d6426dcf4d4968c6557c8e89
Author: Gowtham S Bhushan <[email protected]>
md/raid1: Prevent excessively large memory allocations
This commit adds a bounds check on mddev->raid_disks in setup_conf() to prevent integer overflow and allocations exceeding KMALLOC_MAX_SIZE.
Quoted 8 lines
drivers/md/raid1.c +0
-0@@ -3020,6 +3020,16 @@ static struct r1conf *setup_conf(struct mddev *mddev)struct md_rdev *rdev;size_t r1bio_size;int err = -ENOMEM ;
drivers/md/raid1.c static struct r1conf *setup_conf(struct mddev *mddev) · line 3032
Quoted 10 lines
drivers/md/raid1.c +10
-0+size_t calculated_size; + +/* prevent baseline integer wrap-around anomalies first*/ +/* Safely calculate size; overflows automatically saturate to SIZE_MAX */ + + calculated_size =size_add (sizeof (struct r1conf ), +size_mul (mddev->raid_disks,sizeof (struct r1bio ))); + +if (mddev->raid_disks <=0 || calculated_size >=KMALLOC_MAX_SIZE ) +return ERR_PTR (-EINVAL );
[Severity: Medium]
Does calculated_size match the actual allocations in drivers/md/raid1.c that scale with mddev->raid_disks?
struct r1conf is a fixed-size structure with no flexible array member, and struct r1bio is not allocated as an array of mddev->raid_disks elements. The per-disk allocations are:
drivers/md/raid1.c:setup_conf() {
...
conf->mirrors = kzalloc(array3_size(sizeof(struct raid1_info),
mddev->raid_disks, 2),
GFP_KERNEL);
...
r1bio_size = offsetof(struct r1bio, bios[mddev->raid_disks * 2]);
conf->r1bio_pool = mempool_create_kmalloc_pool(NR_RAID_BIOS, r1bio_size);
...
}and during resync initialization via raid1_sync_request() -> init_resync() -> mempool_init() -> r1buf_pool_alloc():
drivers/md/raid1.c:r1buf_pool_alloc() {
...
rps = kmalloc_objs(struct resync_pages, conf->raid_disks * 2, gfp_flags);
...
}Because calculated_size only checks mddev->raid_disks * sizeof(struct r1bio), several allocations can still exceed KMALLOC_MAX_SIZE and trigger the WARN_ON_ONCE_GFP(size > KMALLOC_MAX_SIZE, flags) warning in mm/slub.c:
1. On 64-bit systems with 4KB pages, sizeof(struct r1bio) is 80 bytes while 2 * sizeof(struct resync_pages) is 272 bytes per disk. A raid_disks value such as 20,000 passes setup_conf() (~1.6 MB) and then requests ~5.44 MB in r1buf_pool_alloc(), exceeding a 4 MB KMALLOC_MAX_SIZE.
2. On 32-bit systems, sizeof(struct r1bio) is 48 bytes while
2 * sizeof(struct raid1_info) is 56 or 64 bytes per disk. A raid_disks value such as 70,000 or 80,000 passes calculated_size < KMALLOC_MAX_SIZE and then exceeds a 4 MB KMALLOC_MAX_SIZE when allocating conf->mirrors in setup_conf().
Also, since mddev->raid_disks is a signed int, calling size_mul(mddev->raid_disks, ...) before checking mddev->raid_disks <= 0 implicitly converts negative values to a large size_t first.
Could mddev->raid_disks <= 0 be checked first, and the upper bound be validated against the actual allocation sizes (or a reasonable maximum device count)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 md/raid1: reject out-of-range raid_disks in setup_conf()
In reply to Gowtham S Bhushanmddev->raid_disks can be set from user space with SET_ARRAY_INFO and is not bounded before RUN_ARRAY reaches raid1_run(). With a very large value, setup_conf() asks the allocator for more than KMALLOC_MAX_SIZE when allocating conf->mirrors, and the allocator warns:
WARNING: mm/slub.c:5459 at ___kmalloc_large_node setup_conf+0x15d/0x700 raid1_run+0x109/0x250 md_run+0x6c3/0xec0 do_md_run+0x27/0x3b0 md_ioctl+0x163f/0x1ca0
The array fails to start, but machines running with panic_on_warn crash and syzbot keeps reporting it.
Fail early instead. Version 1.x metadata can describe at most 1920 devices, so use that as the upper bound, and test the sign before the value is used. At that limit the allocations that scale with raid_disks (conf->mirrors, the r1bio mempool element and the resync buffers) stay well below KMALLOC_MAX_SIZE.
--- drivers/md/raid1.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/md/raid1.c +12 -0
@@ -48,6 +48,13 @@ static void allow_barrier(struct r1conf *conf, sector_t sector_nr);static void lower_barrier (struct r1conf *conf,sector_t sector_nr);static void raid1_free (struct mddev *mddev,void *priv); +/* +* Version 1.x md metadata keeps a 256-byte header and then 2 bytes per +* device in a 4096-byte block, so no array described by it can have more +* devices than this. Arrays created through the ioctl alone have no +* on-disk limit, so apply the same cap to them. +*/ +#define RAID1_MAX_DISKS ((4096 - 256) / 2)#define RAID_1_10_NAME "raid1"#include "raid1-10.c"
@@ -3051,6 +3058,11 @@ static struct r1conf *setup_conf(struct mddev *mddev)size_t r1bio_size;int err = -ENOMEM ; +/* raid_disks comes from user space and sizes the allocations below */ + +if (mddev->raid_disks <=0 || mddev->raid_disks >RAID1_MAX_DISKS ) +return ERR_PTR (-EINVAL ); + conf = kzalloc_obj(struct r1conf);if (!conf) goto abort;
Signature · 1 line
2.56.0
yu kuai
maintainerRe: [PATCH v2] md/raid1: reject out-of-range raid_disks in setup_conf()
Hi,
Quoted 49 lines · Gowtham S Bhushan
mddev->raid_diskscan be set from user space withSET_ARRAY_INFOand is not bounded beforeRUN_ARRAYreachesraid1_run(). With a very large value,setup_conf()asks the allocator for more thanKMALLOC_MAX_SIZEwhen allocatingconf->mirrors, and the allocator warns:WARNING: mm/slub.c:5459 at ___kmalloc_large_node setup_conf+0x15d/0x700 raid1_run+0x109/0x250 md_run+0x6c3/0xec0 do_md_run+0x27/0x3b0 md_ioctl+0x163f/0x1ca0The array fails to start, but machines running with
panic_on_warncrash and syzbot keeps reporting it.Fail early instead. Version 1.x metadata can describe at most 1920 devices, so use that as the upper bound, and test the sign before the value is used. At that limit the allocations that scale with
raid_disks(conf->mirrors, the r1bio mempool element and the resync buffers) stay well belowKMALLOC_MAX_SIZE.Reported-by [email protected]Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Gowtham S Bhushan <[email protected]>--- drivers/md/raid1.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)drivers/md/raid1.c +7
-0@@ -48,6 +48,13 @@ static void allow_barrier(struct r1conf *conf, sector_t sector_nr);static void lower_barrier (struct r1conf *conf,sector_t sector_nr);static void raid1_free (struct mddev *mddev,void *priv); +/* +* Version 1.x md metadata keeps a 256-byte header and then 2 bytes per +* device in a 4096-byte block, so no array described by it can have more +* devices than this. Arrays created through the ioctl alone have no +* on-disk limit, so apply the same cap to them. +*/ +#define RAID1_MAX_DISKS ((4096 - 256) / 2)#define RAID_1_10_NAME "raid1"#include "raid1-10.c"
drivers/md/raid1.c static struct r1conf *setup_conf(struct mddev *mddev) · line 3064
Quoted 7 lines
drivers/md/raid1.c +4
-0@@ -3051,6 +3058,11 @@ static struct r1conf *setup_conf(struct mddev *mddev)size_t r1bio_size;int err = -ENOMEM ; +/* raid_disks comes from user space and sizes the allocations below */ + +if (mddev->raid_disks <=0 || mddev->raid_disks >RAID1_MAX_DISKS ) +return ERR_PTR (-EINVAL );
There is still the same kmalloc in raid1_reshape().
Quoted 4 lines
drivers/md/raid1.c +1
-0+ conf = kzalloc_obj(struct r1conf);if (!conf) goto abort;
Signature · 2 lines
Thanks, Kuai
yu kuai
maintainerHi,
Quoted 21 lines · Gowtham S Bhushan
Validate
mddev->raid_disksbefore memory allocation to prevent integer wrap-around or allocating an unreasonably large chunk of memory that exceedsKMALLOC_MAX_SIZEboundaries.Signed-off-by Gowtham S Bhushan <[email protected]>--- drivers/md/raid1.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/md/raid1.c +0
-0
drivers/md/raid1.c static struct r1conf *setup_conf(struct mddev *mddev) · line 3059
Quoted 10 lines
drivers/md/raid1.c +7
-0@@ -3050,6 +3050,16 @@ static struct r1conf *setup_conf(struct mddev *mddev)struct md_rdev *rdev;size_t r1bio_size;int err = -ENOMEM ; +size_t calculated_size; + +/* prevent baseline integer wrap-around anomalies first*/ +/* Safely calculate size; overflows automatically saturate to SIZE_MAX */ + + calculated_size =size_add (sizeof (struct r1conf ), +size_mul (mddev->raid_disks,sizeof (struct r1bio )));
This is not the only place, raid1_reshape() has the same memory allocation.
Quoted 6 lines
drivers/md/raid1.c +3
-0+ +if (mddev->raid_disks <=0 || calculated_size >=KMALLOC_MAX_SIZE ) +return ERR_PTR (-EINVAL ); conf = kzalloc_obj(struct r1conf);if (!conf)
Signature · 2 lines
Thanks, Kuai