diff mbox series

drivers/scsi/qla4xxx: use scnprintf() instead of snprintf()

Message ID 1611127365-45929-1-git-send-email-abaci-bugfix@linux.alibaba.com (mailing list archive)
State Changes Requested
Headers show
Series drivers/scsi/qla4xxx: use scnprintf() instead of snprintf() | expand

Commit Message

Abaci Team Jan. 20, 2021, 7:22 a.m. UTC
Fix the following coccicheck warning:

./drivers/scsi/qla4xxx/ql4_attr.c: WARNING: use scnprintf or
sprintf

The snprintf() function returns the number of characters which would
have been printed if there were enough space, but the scnprintf()
returns the number of characters which were actually printed.  If
the buffer is not large enough, then using snprintf() would result
in a read overflow and an information leak.

Reported-by: Abaci Robot<abaci@linux.alibaba.com>
Signed-off-by: Jiapeng Zhong <abaci-bugfix@linux.alibaba.com>
---
 drivers/scsi/qla4xxx/ql4_attr.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

Comments

James Bottomley Jan. 20, 2021, 3:36 p.m. UTC | #1
On Wed, 2021-01-20 at 15:22 +0800, Jiapeng Zhong wrote:
> Fix the following coccicheck warning:
> 
> ./drivers/scsi/qla4xxx/ql4_attr.c: WARNING: use scnprintf or
> sprintf
> 
> The snprintf() function returns the number of characters which would
> have been printed if there were enough space, but the scnprintf()
> returns the number of characters which were actually printed.  If
> the buffer is not large enough, then using snprintf() would result
> in a read overflow and an information leak.
> 
> Reported-by: Abaci Robot<abaci@linux.alibaba.com>
> Signed-off-by: Jiapeng Zhong <abaci-bugfix@linux.alibaba.com>
> ---
>  drivers/scsi/qla4xxx/ql4_attr.c | 22 +++++++++++-----------
>  1 file changed, 11 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/scsi/qla4xxx/ql4_attr.c
> b/drivers/scsi/qla4xxx/ql4_attr.c
> index ec43528..1a16017 100644
> --- a/drivers/scsi/qla4xxx/ql4_attr.c
> +++ b/drivers/scsi/qla4xxx/ql4_attr.c
> @@ -170,7 +170,7 @@ void qla4_8xxx_free_sysfs_attr(struct
> scsi_qla_host *ha)
>  			char *buf)
>  {
>  	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
> -	return snprintf(buf, PAGE_SIZE, "%s\n", ha->serial_number);
> +	return scnprintf(buf, PAGE_SIZE, "%s\n", ha->serial_number);

This is the wrong ABI to be replacing anything sysfs with, it should be
sysfs_emit()

James
diff mbox series

Patch

diff --git a/drivers/scsi/qla4xxx/ql4_attr.c b/drivers/scsi/qla4xxx/ql4_attr.c
index ec43528..1a16017 100644
--- a/drivers/scsi/qla4xxx/ql4_attr.c
+++ b/drivers/scsi/qla4xxx/ql4_attr.c
@@ -170,7 +170,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 			char *buf)
 {
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
-	return snprintf(buf, PAGE_SIZE, "%s\n", ha->serial_number);
+	return scnprintf(buf, PAGE_SIZE, "%s\n", ha->serial_number);
 }
 
 static ssize_t
@@ -178,7 +178,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 			   char *buf)
 {
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
-	return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fw_info.iscsi_major,
+	return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fw_info.iscsi_major,
 			ha->fw_info.iscsi_minor);
 }
 
@@ -187,7 +187,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 			    char *buf)
 {
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
-	return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d.%02d\n",
+	return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d.%02d\n",
 			ha->fw_info.bootload_major, ha->fw_info.bootload_minor,
 			ha->fw_info.bootload_patch, ha->fw_info.bootload_build);
 }
@@ -197,7 +197,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 		      char *buf)
 {
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
-	return snprintf(buf, PAGE_SIZE, "0x%08X\n", ha->board_id);
+	return scnprintf(buf, PAGE_SIZE, "0x%08X\n", ha->board_id);
 }
 
 static ssize_t
@@ -207,7 +207,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
 
 	qla4xxx_get_firmware_state(ha);
-	return snprintf(buf, PAGE_SIZE, "0x%08X%8X\n", ha->firmware_state,
+	return scnprintf(buf, PAGE_SIZE, "0x%08X%8X\n", ha->firmware_state,
 			ha->addl_fw_state);
 }
 
@@ -220,7 +220,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 	if (is_qla40XX(ha))
 		return -ENOSYS;
 
-	return snprintf(buf, PAGE_SIZE, "0x%04X\n", ha->phy_port_cnt);
+	return scnprintf(buf, PAGE_SIZE, "0x%04X\n", ha->phy_port_cnt);
 }
 
 static ssize_t
@@ -232,7 +232,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 	if (is_qla40XX(ha))
 		return -ENOSYS;
 
-	return snprintf(buf, PAGE_SIZE, "0x%04X\n", ha->phy_port_num);
+	return scnprintf(buf, PAGE_SIZE, "0x%04X\n", ha->phy_port_num);
 }
 
 static ssize_t
@@ -244,7 +244,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 	if (is_qla40XX(ha))
 		return -ENOSYS;
 
-	return snprintf(buf, PAGE_SIZE, "0x%04X\n", ha->iscsi_pci_func_cnt);
+	return scnprintf(buf, PAGE_SIZE, "0x%04X\n", ha->iscsi_pci_func_cnt);
 }
 
 static ssize_t
@@ -253,7 +253,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 {
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
 
-	return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_name);
+	return scnprintf(buf, PAGE_SIZE, "%s\n", ha->model_name);
 }
 
 static ssize_t
@@ -261,7 +261,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 			  char *buf)
 {
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
-	return snprintf(buf, PAGE_SIZE, "%s %s\n", ha->fw_info.fw_build_date,
+	return scnprintf(buf, PAGE_SIZE, "%s %s\n", ha->fw_info.fw_build_date,
 			ha->fw_info.fw_build_time);
 }
 
@@ -309,7 +309,7 @@  void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
 {
 	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
 	qla4xxx_about_firmware(ha);
-	return snprintf(buf, PAGE_SIZE, "%u.%u secs\n", ha->fw_uptime_secs,
+	return scnprintf(buf, PAGE_SIZE, "%u.%u secs\n", ha->fw_uptime_secs,
 			ha->fw_uptime_msecs);
 }