Skip to contentSkip to conversation

scsi: integer overflow in megadev_ioctl()

PATCH In review Chen.Yu 2 participants 3 messages Dec 13, 2013 – Dec 14, 2013

Subsystems (2)
lore
Share & tools
lore

Chen.Yu

author
#0 lore raw

PATCH 1/1 scsi: integer overflow in megadev_ioctl()

From: "Chen.Yu" <[email protected]>

There is a potential integer overflow in megadev_ioctl() if userspace passes in a large u32 variable uioc.adapno. The int variable adapno would < 0, leading to an error array access for hdb_soft_state[adapno], or an error copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,..)

---
 drivers/scsi/megaraid.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/scsi/megaraid.c +12 -3
index 816db12..0b90c54 100644
@@ -3099,7 +3099,10 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		/*
 		 * Which adapter
 		 */
-		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
+		adapno = GETADAP(uioc.adapno);
+		if( adapno < 0 )
+			return (-EINVAL);
+		if( adapno >= hba_count )
 			return (-ENODEV);
 
 		if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
@@ -3113,7 +3116,10 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		/*
 		 * Which adapter
 		 */
-		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
+		adapno = GETADAP(uioc.adapno);
+		if( adapno < 0 )
+			return (-EINVAL);
+		if( adapno >= hba_count )
 			return (-ENODEV);
 
 		adapter = hba_soft_state[adapno];
@@ -3160,7 +3166,10 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		/*
 		 * Which adapter
 		 */
-		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
+		adapno = GETADAP(uioc.adapno);
+		if( adapno < 0 )
+			return (-EINVAL);
+		if( adapno >= hba_count )
 			return (-ENODEV);
 
 		adapter = hba_soft_state[adapno];
Signature · 1 line
1.8.3.2
#1 lore raw
Quoted 21 lines · Chen.Yu

From: "Chen.Yu" <[email protected]>

There is a potential integer overflow in megadev_ioctl() if userspace passes in a large u32 variable uioc.adapno. The int variable adapno would < 0, leading to an error array access for hdb_soft_state[adapno], or an error copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,..)

---
 drivers/scsi/megaraid.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/scsi/megaraid.c +0 -0
index 816db12..0b90c54 100644

drivers/scsi/megaraid.c megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) · line 3106

Quoted 9 lines
drivers/scsi/megaraid.c +4 -1
@@ -3099,7 +3099,10 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		/*
 		 * Which adapter
 		 */
-		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
+		adapno = GETADAP(uioc.adapno);
+		if( adapno < 0 )
+			return (-EINVAL);
+		if( adapno >= hba_count )
 			return (-ENODEV);

This relies on implementation-defined behaviour when converting an unsigned integer to signed integer. A simpler and more robust fix is to make the local variable 'adapno' unsigned.

Signature · 2 lines
Måns Rullgård
[email protected]
author
#2 lore raw

I agree that the simpler fix is to change the type of 'adapno' to u32, which is the type of uioc.adapno to u32.

2013/12/14 Måns Rullgård <[email protected]>:

Quoted 37 lines
Quoted 21 lines · Chen.Yu

From: "Chen.Yu" <[email protected]>

There is a potential integer overflow in megadev_ioctl() if userspace passes in a large u32 variable uioc.adapno. The int variable adapno would < 0, leading to an error array access for hdb_soft_state[adapno], or an error copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,..)

---
 drivers/scsi/megaraid.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/scsi/megaraid.c +0 -0
index 816db12..0b90c54 100644

drivers/scsi/megaraid.c megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) · line 3106

Quoted 9 lines
drivers/scsi/megaraid.c +4 -1
@@ -3099,7 +3099,10 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
              /*
               * Which adapter
               */
-             if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
+             adapno = GETADAP(uioc.adapno);
+             if( adapno < 0 )
+                     return (-EINVAL);
+             if( adapno >= hba_count )
                      return (-ENODEV);

This relies on implementation-defined behaviour when converting an unsigned integer to signed integer. A simpler and more robust fix is to make the local variable 'adapno' unsigned.

Quoted 3 lines

--
Måns Rullgård
[email protected]

--