]> git.itanic.dy.fi Git - linux-stable/commitdiff
scsi: scsi_debug: Fix some bugs in sdebug_error_write()
authorDan Carpenter <dan.carpenter@linaro.org>
Mon, 6 Nov 2023 14:04:33 +0000 (17:04 +0300)
committerMartin K. Petersen <martin.petersen@oracle.com>
Thu, 9 Nov 2023 02:42:26 +0000 (21:42 -0500)
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.

Fixes: a9996d722b11 ("scsi: scsi_debug: Add interface to manage error injection for a single device")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Link: https://lore.kernel.org/r/7733643d-e102-4581-8d29-769472011c97@moroto.mountain
Reviewed-by: Wenchao Hao <haowenchao2@huawei.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/scsi/scsi_debug.c

index 67922e2c4c1915cb74dd62b7a7704d371efdb1d9..0dd21598f7b6d108f6e19ee1047cd48bd66b867e 100644 (file)
@@ -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;