Skip to contentSkip to conversation

brd: fix integer overflow in brd_check_and_reset_par

PATCH In review lixiaokeng 2 participants 2 messages Mar 25, 2021 – Apr 1, 2021

Subsystems (2)
lore
Share & tools
lore

lixiaokeng

author
#0 lore raw

PATCH 1/1 brd: fix integer overflow in brd_check_and_reset_par

The max_part may overflow. For example,

modprobe brd rd_nr=3 rd_size=102400 max_part=1073741824(2^30)

Expected result

NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
ram0               1:0    0   100M  0 disk
ram1               1:256  0   100M  0 disk
ram2               1:512  0   100M  0 disk

Actual result

NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
ram0             259:0    0   100M  0 disk
ram1             259:1    0   100M  0 disk
ram2             259:2    0   100M  0 disk

Fix it.

---
 drivers/block/brd.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/block/brd.c +3 -5
index c43a6ab4b1f3..c91831cd5d2a 100644
@@ -457,21 +457,19 @@ static void brd_del_one(struct brd_device *brd)
 
 static inline void brd_check_and_reset_par(void)
 {
-	if (unlikely(!max_part))
+	if (unlikely(max_part <= 0))
 		max_part = 1;
 
 	/*
 	 * make sure 'max_part' can be divided exactly by (1U << MINORBITS),
 	 * otherwise, it is possiable to get same dev_t when adding partitions.
 	 */
-	if ((1U << MINORBITS) % max_part != 0)
-		max_part = 1UL << fls(max_part);
-
 	if (max_part > DISK_MAX_PARTS) {
 		pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
 			DISK_MAX_PARTS, DISK_MAX_PARTS);
 		max_part = DISK_MAX_PARTS;
-	}
+	} else if ((1U << MINORBITS) % max_part != 0)
+		max_part = 1UL << fls(max_part);
 }
 
 static int __init brd_init(void)
#1 lore raw

friendly ping.

Quoted 53 lines · lixiaokeng

The max_part may overflow. For example,

modprobe brd rd_nr=3 rd_size=102400 max_part=1073741824(2^30)

Expected result

NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
ram0               1:0    0   100M  0 disk
ram1               1:256  0   100M  0 disk
ram2               1:512  0   100M  0 disk

Actual result

NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
ram0             259:0    0   100M  0 disk
ram1             259:1    0   100M  0 disk
ram2             259:2    0   100M  0 disk

Fix it.

---
 drivers/block/brd.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/block/brd.c +3 -5
index c43a6ab4b1f3..c91831cd5d2a 100644
@@ -457,21 +457,19 @@ static void brd_del_one(struct brd_device *brd)
 
 static inline void brd_check_and_reset_par(void)
 {
-	if (unlikely(!max_part))
+	if (unlikely(max_part <= 0))
 		max_part = 1;
 
 	/*
 	 * make sure 'max_part' can be divided exactly by (1U << MINORBITS),
 	 * otherwise, it is possiable to get same dev_t when adding partitions.
 	 */
-	if ((1U << MINORBITS) % max_part != 0)
-		max_part = 1UL << fls(max_part);
-
 	if (max_part > DISK_MAX_PARTS) {
 		pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
 			DISK_MAX_PARTS, DISK_MAX_PARTS);
 		max_part = DISK_MAX_PARTS;
-	}
+	} else if ((1U << MINORBITS) % max_part != 0)
+		max_part = 1UL << fls(max_part);
 }
 
 static int __init brd_init(void)