Skip to contentSkip to conversation

dm-stripe: fix a possible integer overflow

PATCH In review Mikulas Patocka 3 participants 4 messages Jul 7, 2025 Device mapper

lore
Share & tools
lore

Mikulas Patocka

author maintainer
#0 lore raw

PATCH 1/1 dm-stripe: fix a possible integer overflow

There's a possible integer overflow in stripe_io_hints if we have too large chunk size. Test if the overflow happened, and if it did, don't set limits->io_min and limits->io_opt;

---
 drivers/md/dm-stripe.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/md/dm-stripe.c +7 -3
Index: linux-2.6/drivers/md/dm-stripe.c
===================================================================
@@ -456,10 +456,14 @@ static void stripe_io_hints(struct dm_ta
 			    struct queue_limits *limits)
 {
 	struct stripe_c *sc = ti->private;
-	unsigned int chunk_size = sc->chunk_size << SECTOR_SHIFT;
+	unsigned int io_min = sc->chunk_size << SECTOR_SHIFT;
+	unsigned int io_opt = io_min * sc->stripes;
 
-	limits->io_min = chunk_size;
-	limits->io_opt = chunk_size * sc->stripes;
+	if (io_min >> SECTOR_SHIFT == sc->chunk_size &&
+	    io_opt / sc->stripes == io_min) {
+		limits->io_min = io_min;
+		limits->io_opt = io_opt;
+	}
 }
 
 static struct target_type stripe_target = {
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Mikulas Patocka

There's a possible integer overflow in stripe_io_hints if we have too large chunk size. Test if the overflow happened, and if it did, don't set limits->io_min and limits->io_opt;

Seems fine, I cc'ed Martin as I asked him about this earlier:

And this will conflict with https://lore.kernel.org/linux-xfs/[email protected]/T/#t, as to be expected

Quoted 27 lines
---
  drivers/md/dm-stripe.c |   10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)

Index: linux-2.6/drivers/md/dm-stripe.c
===================================================================

drivers/md/dm-stripe.c +7 -3
@@ -456,10 +456,14 @@ static void stripe_io_hints(struct dm_ta
  			    struct queue_limits *limits)
  {
  	struct stripe_c *sc = ti->private;
-	unsigned int chunk_size = sc->chunk_size << SECTOR_SHIFT;
+	unsigned int io_min = sc->chunk_size << SECTOR_SHIFT;
+	unsigned int io_opt = io_min * sc->stripes;
 
-	limits->io_min = chunk_size;
-	limits->io_opt = chunk_size * sc->stripes;
+	if (io_min >> SECTOR_SHIFT == sc->chunk_size &&
+	    io_opt / sc->stripes == io_min) {
+		limits->io_min = io_min;
+		limits->io_opt = io_opt;
+	}
  }
 
  static struct target_type stripe_target = {
#2 lore raw
In reply to Mikulas Patocka
Quoted 20 lines · Mikulas Patocka

There's a possible integer overflow in stripe_io_hints if we have too large chunk size. Test if the overflow happened, and if it did, don't set limits->io_min and limits->io_opt;

---
  drivers/md/dm-stripe.c |   10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)

Index: linux-2.6/drivers/md/dm-stripe.c
===================================================================

drivers/md/dm-stripe.c +0 -1
@@ -456,10 +456,14 @@ static void stripe_io_hints(struct dm_ta
  			    struct queue_limits *limits)
  {
  	struct stripe_c *sc = ti->private;
-	unsigned int chunk_size = sc->chunk_size << SECTOR_SHIFT;

drivers/md/dm-stripe.c static void stripe_io_hints(struct dm_ta · line 466

Quoted 10 lines
drivers/md/dm-stripe.c +7 -2
+	unsigned int io_min = sc->chunk_size << SECTOR_SHIFT;
+	unsigned int io_opt = io_min * sc->stripes;
 
-	limits->io_min = chunk_size;
-	limits->io_opt = chunk_size * sc->stripes;
+	if (io_min >> SECTOR_SHIFT == sc->chunk_size &&
+	    io_opt / sc->stripes == io_min) {
+		limits->io_min = io_min;
+		limits->io_opt = io_opt;
+	}

Hi Mikulas,

    What about using check_xxx_overflow() ? something like:

    if (check_shl_overflow(sc->chunk_size, SECTOR_SHIFT, &io_min)) return;

    if (check_mul_overflow(io_min, sc->stripes, &io_opt)) return;

    limits->io_min = io_min;
    limits->io_opt = io_opt;

Quoted 5 lines
drivers/md/dm-stripe.c +0 -0
  }
 
  static struct target_type stripe_target = {
author maintainer
#3 lore raw
Quoted 43 lines · Dongsheng Yang
Quoted 20 lines · Mikulas Patocka

There's a possible integer overflow in stripe_io_hints if we have too large chunk size. Test if the overflow happened, and if it did, don't set limits->io_min and limits->io_opt;

---
  drivers/md/dm-stripe.c |   10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)

Index: linux-2.6/drivers/md/dm-stripe.c
===================================================================
--- linux-2.6.orig/drivers/md/dm-stripe.c 2025-07-06 15:02:23.000000000
+0200
+++ linux-2.6/drivers/md/dm-stripe.c 2025-07-07 12:32:49.000000000 +0200

drivers/md/dm-stripe.c +0 -0
@@ -456,10 +456,14 @@ static void stripe_io_hints(struct dm_ta
  			    struct queue_limits *limits)
  {
  	struct stripe_c *sc = ti->private;

drivers/md/dm-stripe.c static void stripe_io_hints(struct dm_ta · line 466

Quoted 10 lines
drivers/md/dm-stripe.c +7 -2
-	unsigned int chunk_size = sc->chunk_size << SECTOR_SHIFT;
+	unsigned int io_min = sc->chunk_size << SECTOR_SHIFT;
+	unsigned int io_opt = io_min * sc->stripes;
  -	limits->io_min = chunk_size;
-	limits->io_opt = chunk_size * sc->stripes;
+	if (io_min >> SECTOR_SHIFT == sc->chunk_size &&
+	    io_opt / sc->stripes == io_min) {
+		limits->io_min = io_min;
+		limits->io_opt = io_opt;
+	}

Hi Mikulas,

    What about using check_xxx_overflow() ? something like:

    if (check_shl_overflow(sc->chunk_size, SECTOR_SHIFT, &io_min)) return;

Quoted 10 lines

    if (check_mul_overflow(io_min, sc->stripes, &io_opt)) return;

    limits->io_min = io_min;
    limits->io_opt = io_opt;

Quoted 4 lines
drivers/md/dm-stripe.c +0 -0
  }
    static struct target_type stripe_target = {
 

Yes, that's a good idea.

I changed the patch to use these macros.

Mikulas