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.

Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: Tomas Henzl <thenzl@redhat.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
This commit is contained in:
Maurizio Lombardi 2015-11-18 15:32:44 +01:00 коммит произвёл Martin K. Petersen
Родитель a35bb4458e
Коммит ab08ee1439
1 изменённых файлов: 3 добавлений и 2 удалений

Просмотреть файл

@ -4083,6 +4083,7 @@ static int create_one_cdev(struct scsi_tape *tape, int mode, int rew)
} }
cdev->owner = THIS_MODULE; cdev->owner = THIS_MODULE;
cdev->ops = &st_fops; cdev->ops = &st_fops;
STm->cdevs[rew] = cdev;
error = cdev_add(cdev, cdev_devno, 1); error = cdev_add(cdev, cdev_devno, 1);
if (error) { 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); pr_err("st%d: Device not attached.\n", dev_num);
goto out_free; goto out_free;
} }
STm->cdevs[rew] = cdev;
i = mode << (4 - ST_NBR_MODE_BITS); i = mode << (4 - ST_NBR_MODE_BITS);
snprintf(name, 10, "%s%s%s", rew ? "n" : "", 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; return 0;
out_free: out_free:
cdev_del(STm->cdevs[rew]); cdev_del(STm->cdevs[rew]);
STm->cdevs[rew] = NULL;
out: out:
STm->cdevs[rew] = NULL;
STm->devs[rew] = NULL;
return error; return error;
} }