]> www.infradead.org Git - users/willy/xarray.git/commitdiff
scsi: hpsa: Remove deprecated and unnecessary strncpy()
authorThorsten Blum <thorsten.blum@linux.dev>
Fri, 14 Feb 2025 11:43:02 +0000 (12:43 +0100)
committerMartin K. Petersen <martin.petersen@oracle.com>
Wed, 19 Feb 2025 02:34:08 +0000 (21:34 -0500)
While replacing strncpy() with strscpy(), Bart Van Assche pointed out
that the code occurs inside sysfs write callbacks, which already uses
NUL-terminated strings. This allows the string to be passed directly to
sscanf() without requiring a temporary copy.

Remove the deprecated and unnecessary strncpy() and the corresponding
local variables, and pass the buffer buf directly to sscanf().

Replace sscanf() with kstrtoint() to silence checkpatch warnings.

Compile-tested only.

Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Cc: Kees Cook <kees@kernel.org>
Cc: David Laight <david.laight.linux@gmail.com>
Suggested-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Link: https://lore.kernel.org/r/20250214114302.86001-2-thorsten.blum@linux.dev
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/scsi/hpsa.c

index 84d8de07b7aeed61584665c7678b643930cdee82..bb65954b1b1e9e6465e0ffe348fb23caa06248f6 100644 (file)
@@ -453,17 +453,13 @@ static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
                                         struct device_attribute *attr,
                                         const char *buf, size_t count)
 {
-       int status, len;
+       int status;
        struct ctlr_info *h;
        struct Scsi_Host *shost = class_to_shost(dev);
-       char tmpbuf[10];
 
        if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
                return -EACCES;
-       len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
-       strncpy(tmpbuf, buf, len);
-       tmpbuf[len] = '\0';
-       if (sscanf(tmpbuf, "%d", &status) != 1)
+       if (kstrtoint(buf, 10, &status))
                return -EINVAL;
        h = shost_to_hba(shost);
        h->acciopath_status = !!status;
@@ -477,17 +473,13 @@ static ssize_t host_store_raid_offload_debug(struct device *dev,
                                         struct device_attribute *attr,
                                         const char *buf, size_t count)
 {
-       int debug_level, len;
+       int debug_level;
        struct ctlr_info *h;
        struct Scsi_Host *shost = class_to_shost(dev);
-       char tmpbuf[10];
 
        if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
                return -EACCES;
-       len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
-       strncpy(tmpbuf, buf, len);
-       tmpbuf[len] = '\0';
-       if (sscanf(tmpbuf, "%d", &debug_level) != 1)
+       if (kstrtoint(buf, 10, &debug_level))
                return -EINVAL;
        if (debug_level < 0)
                debug_level = 0;