scsi: scsi_debug: scsi: scsi_debug: fix some bugs in sdebug_error_write()
Subsystems (2)
PATCH v2 1/2 scsi: scsi_debug: scsi: scsi_debug: fix some bugs in sdebug_error_write()
There are two bug in this code:
1) If count is zero, then it will lead to a NULL dereference. The kmalloc() will successfully allocate zero bytes and the test for "if (buf[0] == '-')" will read beyond the end of the zero size buffer and Oops.
2) The code does not ensure that the user's string is properly NUL terminated which could lead to a read overflow.
---
v2: At first I tried to use strndup_user() but that only accepts NUL
terminated strings and the user string is normally not terminated.
drivers/scsi/scsi_debug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi_debug.c +1 -1
@@ -1019,7 +1019,7 @@ static ssize_t sdebug_error_write(struct file *file, const char __user *ubuf,struct sdebug_err_inject *inject;struct scsi_device *sdev = (struct scsi_device *)file->f_inode->i_private; - buf =kmalloc (count,GFP_KERNEL ); + buf =kzalloc (count +1 ,GFP_KERNEL );if (!buf)return -ENOMEM ;
Signature · 1 line
2.42.0
PATCH v2 2/2 scsi: scsi_debug: delete some bogus error checking
Smatch complains that "dentry" is never initialized. These days everyone initializes all their stack variables to zero so this means that it will trigger a warning every time this function is run.
Really, debugfs functions are not supposed to be checked for errors in normal code. For example, if we updated this code to check the correct variable then it would print a warning if CONFIG_DEBUGFS was disabled. We don't want that. Just delete the check.
---
v2: Add some more text to the commit message about CONFIG_DEBUGFS
drivers/scsi/scsi_debug.c | 7 ------- 1 file changed, 7 deletions(-)
drivers/scsi/scsi_debug.c +0 -7
@@ -1132,7 +1132,6 @@ static const struct file_operations sdebug_target_reset_fail_fops = {static int sdebug_target_alloc (struct scsi_target *starget) {struct sdebug_target_info *targetip; -struct dentry *dentry; targetip =kzalloc (sizeof (struct sdebug_target_info ),GFP_KERNEL );if (!targetip)
@@ -1140,15 +1139,9 @@ static int sdebug_target_alloc(struct scsi_target *starget) targetip->debugfs_entry =debugfs_create_dir (dev_name (&starget->dev), sdebug_debugfs_root); -if (IS_ERR_OR_NULL (targetip->debugfs_entry)) -pr_info ("%s: failed to create debugfs directory for target %s\n" , - __func__,dev_name (&starget->dev));debugfs_create_file ("fail_reset" ,0600 , targetip->debugfs_entry, starget, &sdebug_target_reset_fail_fops); -if (IS_ERR_OR_NULL (dentry)) -pr_info ("%s: failed to create fail_reset file for target %s\n" , - __func__,dev_name (&starget->dev)); starget->hostdata = targetip;
Signature · 1 line
2.42.0
Re: [PATCH v2 2/2] scsi: scsi_debug: delete some bogus error checking
Quoted 35 lines · Dan Carpenter
Smatch complains that "dentry" is never initialized. These days everyone initializes all their stack variables to zero so this means that it will trigger a warning every time this function is run.
Really, debugfs functions are not supposed to be checked for errors in normal code. For example, if we updated this code to check the correct variable then it would print a warning if
CONFIG_DEBUGFSwas disabled. We don't want that. Just delete the check.Fixes f084fe52c640 ("scsi: scsi_debug: Add debugfs interface to fail target reset")Signed-off-by Dan Carpenter <[email protected]>---
v2: Add some more text to the commit message aboutCONFIG_DEBUGFSdrivers/scsi/scsi_debug.c | 7 ------- 1 file changed, 7 deletions(-)drivers/scsi/scsi_debug.c +0
-3@@ -1132,7 +1132,6 @@ static const struct file_operations sdebug_target_reset_fail_fops = {static int sdebug_target_alloc (struct scsi_target *starget) {struct sdebug_target_info *targetip; -struct dentry *dentry; targetip =kzalloc (sizeof (struct sdebug_target_info ),GFP_KERNEL );if (!targetip)@@ -1140,15 +1139,9 @@ static int sdebug_target_alloc(struct scsi_target *starget) targetip->debugfs_entry =debugfs_create_dir (dev_name (&starget->dev), sdebug_debugfs_root); -if (IS_ERR_OR_NULL (targetip->debugfs_entry)) - pr_info("%s: failed to create debugfs directory for target %s\n",
drivers/scsi/scsi_debug.c static int sdebug_target_alloc(struct scsi_target *starget) · line 1147
Quoted 10 lines
drivers/scsi/scsi_debug.c +0
-4- __func__, dev_name(&starget->dev));debugfs_create_file ("fail_reset" ,0600 , targetip->debugfs_entry, starget, &sdebug_target_reset_fail_fops); -if (IS_ERR_OR_NULL (dentry)) -pr_info ("%s: failed to create fail_reset file for target %s\n" , - __func__,dev_name (&starget->dev)); starget->hostdata = targetip;
Looks good, thanks for the fix.
Quoted 22 lines · Dan Carpenter
There are two bug in this code:
1) If count is zero, then it will lead to a NULL dereference. Thekmalloc()will successfully allocate zero bytes and the test for "if (buf[0] == '-')" will read beyond the end of the zero size buffer and Oops.
2) The code does not ensure that the user's string is properly NUL terminated which could lead to a read overflow.Fixes a9996d722b11 ("scsi: scsi_debug: Add interface to manage error injection for a single device")Signed-off-by Dan Carpenter <[email protected]>---
v2: At first I tried to usestrndup_user()but that only accepts NUL
terminated strings and the user string is normally not terminated.drivers/scsi/scsi_debug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/scsi/scsi_debug.c +0
-0
drivers/scsi/scsi_debug.c static ssize_t sdebug_error_write(struct file *file, const char __user *ubuf, · line 1025
Quoted 8 lines
drivers/scsi/scsi_debug.c +1
-1@@ -1019,7 +1019,7 @@ static ssize_t sdebug_error_write(struct file *file, const char __user *ubuf,struct sdebug_err_inject *inject;struct scsi_device *sdev = (struct scsi_device *)file->f_inode->i_private; - buf =kmalloc (count,GFP_KERNEL ); + buf =kzalloc (count +1 ,GFP_KERNEL );if (!buf)return -ENOMEM ;
Looks good,
Dan,
Quoted 7 lines
There are two bug in this code:
1) If count is zero, then it will lead to a NULL dereference. Thekmalloc()will successfully allocate zero bytes and the test for "if (buf[0] == '-')" will read beyond the end of the zero size buffer and Oops.
2) The code does not ensure that the user's string is properly NUL terminated which could lead to a read overflow.
Applied 1+2 to 6.7/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering
Martin K. Petersen
AppliedQuoted 9 lines · Dan Carpenter
There are two bug in this code:
1) If count is zero, then it will lead to a NULL dereference. Thekmalloc()will successfully allocate zero bytes and the test for "if (buf[0] == '-')" will read beyond the end of the zero size buffer and Oops.
2) The code does not ensure that the user's string is properly NUL terminated which could lead to a read overflow.[...]
Applied to 6.7/scsi-fixes, thanks!
[1/2] scsi: scsi_debug: scsi: scsi_debug: fix some bugs in sdebug_error_write()
https://git.kernel.org/mkp/scsi/c/860c3d03bbc3 [2/2] scsi: scsi_debug: delete some bogus error checking https://git.kernel.org/mkp/scsi/c/037fbd3fcfbd
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering