Skip to contentSkip to conversation

st: fix potential null pointer dereference.

PATCH In review Maurizio Lombardi 4 participants 4 messages Nov 18, 2015 – Nov 24, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 st: fix potential null pointer dereference.

If cdev_add() returns an error, the code calls cdev_del() passing the STm->cdevs[rew] pointer as parameter; the problem is that the pointer has not been initialized yet.

This patch fixes the problem by moving the STm->cdevs[rew] pointer initialization before the call to cdev_add().
It also sets STm->devs[rew] and STm->cdevs[rew] to NULL in case of failure.

---
 drivers/scsi/st.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/scsi/st.c +3 -2
index e0a1e52..2e52295 100644
@@ -4083,6 +4083,7 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 	}
 	cdev->owner = THIS_MODULE;
 	cdev->ops = &st_fops;
+	STm->cdevs[rew] = cdev;
 
 	error = cdev_add(cdev, cdev_devno, 1);
 	if (error) {
@@ -4091,7 +4092,6 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 		pr_err("st%d: Device not attached.\n", dev_num);
 		goto out_free;
 	}
-	STm->cdevs[rew] = cdev;
 
 	i = mode << (4 - ST_NBR_MODE_BITS);
 	snprintf(name, 10, "%s%s%s", rew ? "n" : "",
@@ -4110,8 +4110,9 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 	return 0;
 out_free:
 	cdev_del(STm->cdevs[rew]);
-	STm->cdevs[rew] = NULL;
 out:
+	STm->cdevs[rew] = NULL;
+	STm->devs[rew] = NULL;
 	return error;
 }
 
Signature · 1 line
Maurizio Lombardi
Reviewed-by on v2 1/1
#1 lore raw
Quoted 35 lines · Maurizio Lombardi

If cdev_add() returns an error, the code calls cdev_del() passing the STm->cdevs[rew] pointer as parameter; the problem is that the pointer has not been initialized yet.

This patch fixes the problem by moving the STm->cdevs[rew] pointer initialization before the call to cdev_add().
It also sets STm->devs[rew] and STm->cdevs[rew] to NULL in case of failure.

---
 drivers/scsi/st.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/scsi/st.c +1 -1
index e0a1e52..2e52295 100644
@@ -4083,6 +4083,7 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 	}
 	cdev->owner = THIS_MODULE;
 	cdev->ops = &st_fops;
+	STm->cdevs[rew] = cdev;
 
 	error = cdev_add(cdev, cdev_devno, 1);
 	if (error) {
@@ -4091,7 +4092,6 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 		pr_err("st%d: Device not attached.\n", dev_num);
 		goto out_free;
 	}
-	STm->cdevs[rew] = cdev;
 
 	i = mode << (4 - ST_NBR_MODE_BITS);
 	snprintf(name, 10, "%s%s%s", rew ? "n" : "",

drivers/scsi/st.c static int create_one_cdev(struct scsi_tape *tape, int mode, int rew) · line 4115

Quoted 7 lines
drivers/scsi/st.c +2 -1
@@ -4110,8 +4110,9 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
 	return 0;
 out_free:
 	cdev_del(STm->cdevs[rew]);
-	STm->cdevs[rew] = NULL;
 out:
+	STm->cdevs[rew] = NULL;
+	STm->devs[rew] = NULL;
Reviewed-by on v2 1/1
#2 lore raw
In reply to Maurizio Lombardi
Quoted 45 lines · Maurizio Lombardi

If cdev_add() returns an error, the code calls cdev_del() passing the STm->cdevs[rew] pointer as parameter; the problem is that the pointer has not been initialized yet.

This patch fixes the problem by moving the STm->cdevs[rew] pointer initialization before the call to cdev_add().
It also sets STm->devs[rew] and STm->cdevs[rew] to NULL in case of failure.

---
 drivers/scsi/st.c | 5 +++--

 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/scsi/st.c +1 -0
index e0a1e52..2e52295 100644
@@ -4083,6 +4083,7 @@ static int create_one_cdev(struct scsi_tape
 *tape, int mode, int rew)
  	}
  	cdev->owner = THIS_MODULE;
  	cdev->ops = &st_fops;
+	STm->cdevs[rew] = cdev;


  error = cdev_add(cdev, cdev_devno, 1);
  if (error) {

drivers/scsi/st.c +0 -1
@@ -4091,7 +4092,6 @@ static int create_one_cdev(struct scsi_tape
 *tape, int mode, int rew)
  		pr_err("st%d: Device not attached.\n", dev_num);
  		goto out_free;
  	}
-	STm->cdevs[rew] = cdev;


  i = mode << (4 - ST_NBR_MODE_BITS); snprintf(name, 10, "%s%s%s", rew ? "n" : "",

drivers/scsi/st.c +0 -0
@@ -4110,8 +4110,9 @@ static int create_one_cdev(struct scsi_tape
 *tape, int mode, int rew)
  	return 0;

 out_free:
  cdev_del(STm->cdevs[rew]);

drivers/scsi/st.c +0 -1
-	STm->cdevs[rew] = NULL;

 out:

drivers/scsi/st.c +2 -0
+	STm->cdevs[rew] = NULL;
+	STm->devs[rew] = NULL;
Quoted 3 lines

  return error;
 }

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Acked-by on v2 1/1
#3 lore raw
In reply to Maurizio Lombardi
Quoted 7 lines · Maurizio Lombardi

If cdev_add() returns an error, the code calls cdev_del() passing the STm->cdevs[rew] pointer as parameter; the problem is that the pointer has not been initialized yet.

Quoted 6 lines

This patch fixes the problem by moving the STm->cdevs[rew] pointer initialization before the call to cdev_add().
It also sets STm->devs[rew] and STm->cdevs[rew] to NULL in case of failure.

Thanks,
Kai

--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html