Fix bad error handling in sg.c
Subsystems (2)
PATCH 1/1 Fix bad error handling in sg.c
Hi,
I'm taking a part in the development of SRP (Scsi RDMA Protocol). While running tests on SRP, I think I found a bug in the scsi layer.
I got a NULL derefrence in cdev_del+1 when called from sg_remove. By looking at the code of sg_add, sg_alloc and sg_remove (all in drivers/scsi/sg.c) I found out that sg_add is calling sg_alloc but if it fails afterwards it does not deallocate the space that was allocated in sg_alloc and the redundant entry has NULL in cdev. When sg_remove is being called, it tries to perform cdev_del to this NULL cdev and fails.
I think that the following patch solves this problem.
drivers/scsi/sg.c +9 -1
@@ -1402,6 +1402,7 @@ sg_add(struct class_device *cl_dev, struSg_device *sdp =NULL ;struct cdev * cdev =NULL ;int error, k; +unsigned long iflags; disk =alloc_disk (1 );if (!disk) {
@@ -1429,7 +1430,7 @@ sg_add(struct class_device *cl_dev, stru error =cdev_add (cdev,MKDEV (SCSI_GENERIC_MAJOR , k),1 );if (error) - goto out; + goto cdev_add_err; sdp->cdev = cdev;if (sg_sysfs_valid) {
@@ -1456,6 +1457,13 @@ sg_add(struct class_device *cl_dev, strureturn 0 ; +cdev_add_err: +write_lock_irqsave (&sg_dev_arr_lock, iflags); +kfree (sg_dev_arr[k]); + sg_dev_arr[k] =NULL ; + sg_nr_dev--; +write_unlock_irqrestore (&sg_dev_arr_lock, iflags); + out:put_disk (disk);if (cdev)
Signature · 1 line
Ishai Rabinovitz
Quoted 5 lines · Ishai Rabinovitz
Hi,
I'm taking a part in the development of SRP (Scsi RDMA Protocol). While running tests on SRP, I think I found a bug in the scsi layer.
Quoted 8 lines
I got a NULL derefrence in cdev_del+1 when called from
sg_remove. By looking at the code ofsg_add,sg_allocandsg_remove(all indrivers/scsi/sg.c) I found out thatsg_addis callingsg_allocbut if it fails afterwards it does not deallocate the space that was allocated insg_allocand the redundant entry has NULL in cdev. Whensg_removeis being called, it tries to performcdev_delto this NULL cdev and fails.I think that the following patch solves this problem.
Ishai,
Both the analysis and the patch look correct. Thanks.
Quoted 1 line
Signed-off-by Ishai Rabinovitz <[email protected]>
Quoted 36 lines
Index:
ishai/linux-2.6.17/drivers/scsi/sg.c
===================================================================drivers/scsi/sg.c +9
-1@@ -1402,6 +1402,7 @@ sg_add(struct class_device *cl_dev, struSg_device *sdp =NULL ;struct cdev * cdev =NULL ;int error, k; +unsigned long iflags; disk =alloc_disk (1 );if (!disk) {@@ -1429,7 +1430,7 @@ sg_add(struct class_device *cl_dev, stru error =cdev_add (cdev,MKDEV (SCSI_GENERIC_MAJOR , k),1 );if (error) - goto out; + goto cdev_add_err; sdp->cdev = cdev;if (sg_sysfs_valid) {@@ -1456,6 +1457,13 @@ sg_add(struct class_device *cl_dev, strureturn 0 ; +cdev_add_err: +write_lock_irqsave (&sg_dev_arr_lock, iflags); +kfree (sg_dev_arr[k]); + sg_dev_arr[k] =NULL ; + sg_nr_dev--; +write_unlock_irqrestore (&sg_dev_arr_lock, iflags); + out:put_disk (disk);if (cdev)