scsi: core: Use scnprintf() for avoiding potential buffer overflow
authorTakashi Iwai <tiwai@suse.de>
Sun, 15 Mar 2020 09:42:40 +0000 (10:42 +0100)
committerMartin K. Petersen <martin.petersen@oracle.com>
Tue, 17 Mar 2020 17:36:12 +0000 (13:36 -0400)
Since snprintf() returns the would-be-output size instead of the actual
output size, the succeeding calls may go beyond the given buffer limit.
Fix it by replacing with scnprintf().

Link: https://lore.kernel.org/r/20200315094241.9086-8-tiwai@suse.de
Cc: "James E . J . Bottomley" <jejb@linux.ibm.com>
Cc: "Martin K . Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org
Reviewed-by: Bart van Assche <bvanassche@acm.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/scsi/scsi_sysfs.c

index c3a30ba4ae088618bef599c53e73df985e6e5d23..163dbcb741c1c7d43819cecf33a28ef45df37604 100644 (file)
@@ -1045,14 +1045,14 @@ sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
                        name = sdev_bflags_name[i];
 
                if (name)
-                       len += snprintf(buf + len, PAGE_SIZE - len,
-                                       "%s%s", len ? " " : "", name);
+                       len += scnprintf(buf + len, PAGE_SIZE - len,
+                                        "%s%s", len ? " " : "", name);
                else
-                       len += snprintf(buf + len, PAGE_SIZE - len,
-                                       "%sINVALID_BIT(%d)", len ? " " : "", i);
+                       len += scnprintf(buf + len, PAGE_SIZE - len,
+                                        "%sINVALID_BIT(%d)", len ? " " : "", i);
        }
        if (len)
-               len += snprintf(buf + len, PAGE_SIZE - len, "\n");
+               len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
        return len;
 }
 static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);