[PATCH] scsi: core: fix parsing of scsi {add,remove}-single-device

From: Tony Battersby
Date: Fri Jul 21 2023 - 11:12:27 EST


When parsing the "scsi add-single-device" and
"scsi remove-single-device" commands written to /proc/scsi/scsi, make
sure the parser doesn't skip over the NUL string terminator and read
past the end of the user-supplied string.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Tony Battersby <tonyb@xxxxxxxxxxxxxxx>
---
drivers/scsi/scsi_proc.c | 48 +++++++++++++++++++++++++++++++---------
1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c
index 4a6eb1741be0..b27c8da83e62 100644
--- a/drivers/scsi/scsi_proc.c
+++ b/drivers/scsi/scsi_proc.c
@@ -406,7 +406,7 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
size_t length, loff_t *ppos)
{
int host, channel, id, lun;
- char *buffer, *p;
+ char *buffer, *end, *p;
int err;

if (!buf || length > PAGE_SIZE)
@@ -421,10 +421,14 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
goto out;

err = -EINVAL;
- if (length < PAGE_SIZE)
- buffer[length] = '\0';
- else if (buffer[PAGE_SIZE-1])
- goto out;
+ if (length < PAGE_SIZE) {
+ end = buffer + length;
+ *end = '\0';
+ } else {
+ end = buffer + PAGE_SIZE - 1;
+ if (*end)
+ goto out;
+ }

/*
* Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
@@ -432,11 +436,22 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
*/
if (!strncmp("scsi add-single-device", buffer, 22)) {
p = buffer + 23;
+ if (p >= end)
+ goto out;

host = simple_strtoul(p, &p, 0);
- channel = simple_strtoul(p + 1, &p, 0);
- id = simple_strtoul(p + 1, &p, 0);
- lun = simple_strtoul(p + 1, &p, 0);
+ if (++p >= end)
+ goto out;
+
+ channel = simple_strtoul(p, &p, 0);
+ if (++p >= end)
+ goto out;
+
+ id = simple_strtoul(p, &p, 0);
+ if (++p >= end)
+ goto out;
+
+ lun = simple_strtoul(p, &p, 0);

err = scsi_add_single_device(host, channel, id, lun);

@@ -446,11 +461,22 @@ static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
*/
} else if (!strncmp("scsi remove-single-device", buffer, 25)) {
p = buffer + 26;
+ if (p >= end)
+ goto out;

host = simple_strtoul(p, &p, 0);
- channel = simple_strtoul(p + 1, &p, 0);
- id = simple_strtoul(p + 1, &p, 0);
- lun = simple_strtoul(p + 1, &p, 0);
+ if (++p >= end)
+ goto out;
+
+ channel = simple_strtoul(p, &p, 0);
+ if (++p >= end)
+ goto out;
+
+ id = simple_strtoul(p, &p, 0);
+ if (++p >= end)
+ goto out;
+
+ lun = simple_strtoul(p, &p, 0);

err = scsi_remove_single_device(host, channel, id, lun);
}
--
2.25.1