scsi: csiostor: fix inverted sscanf() check in csio_store_dbg_level
PATCH 1/1 scsi: csiostor: fix inverted sscanf() check in csio_store_dbg_level
csio_store_dbg_level() has an inverted return check on sscanf():
the existing check treats a truthy return as an error, so every valid write to the "dbg_level" sysfs attribute was rejected with -EINVAL. This has been broken since the driver was first added.
checkpatch also flags this same line for a related issue:
WARNING: unchecked sscanf return value
WARNING: Prefer kstrto<type> to single variable sscanf
Replace the sscanf()/isdigit() combo with kstrtou32(buf, 16, ...), so this class of inversion bug can't happen again. It also validates the whole string instead of stopping at the first non-digit. This makes the isdigit() pre-check redundant, so it's removed along with the now unused <linux/ctype.h> include.
Base 16 is used instead of base 0 (which would only recognize hex with an explicit "0x" prefix) to match csio_show_dbg_level(), which always prints the value as bare hex via "%x". This keeps read and write symmetric: writing back what was just read always restores the same value.
---
Hi all,
While looking at this file I noticed checkpatch flags csio_store_dbg_level() with:
WARNING: unchecked sscanf return value
WARNING: Prefer kstrto<type> to single variable sscanf
Digging into it, the sscanf() check is actually inverted, so csio_store_dbg_level() has been rejecting every valid write since the driver was added. This patch fixes that with kstrtou32(), using base 16 (instead of 0) so that copy-pasting the value printed by csio_show_dbg_level() (which is always printed as bare hex) back into the store function round-trips correctly.
While tracing this I also noticed that log_level doesn't seem to be read anywhere else in the driver to gate any behaviour -- I couldn't find a consumer for it. I'm not proposing to remove the dbg_level sysfs attribute as part of this fix, just flagging it in case it's dead code worth a separate cleanup, if the maintainers agree.
I wasn't able to test this patch, only compile it. Thanks and regards.
--- drivers/scsi/csiostor/csio_scsi.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/scsi/csiostor/csio_scsi.c +4 -6
@@ -34,7 +34,6 @@#include <linux/device.h> #include <linux/delay.h> -#include <linux/ctype.h> #include <linux/kernel.h> #include <linux/slab.h> #include <linux/string.h>
@@ -1441,12 +1440,11 @@ csio_store_dbg_level(struct device *dev,struct csio_lnode *ln =shost_priv (class_to_shost (dev));struct csio_hw *hw =csio_lnode_to_hw (ln);uint32_t dbg_level =0 ; +int ret; -if (!isdigit (buf[0 ])) -return -EINVAL ; - -if (sscanf (buf,"%i" , &dbg_level)) -return -EINVAL ; + ret =kstrtou32 (buf,16 , &dbg_level); +if (ret) +return ret; ln->params.log_level = dbg_level; hw->params.log_level = dbg_level;
Signature · 1 line
2.48.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Medium] The sysfs store function csio_store_dbg_level returns 0 on success instead of the number of bytes consumed (count), which will cause user-space writers to enter an infinite loop. --
--- Patch [1]: [PATCH] scsi: csiostor: fix inverted sscanf() check in csio_store_dbg_level --- commit e7ce8f094df22bd78f194c7117aa937439236e6d
Author: Rodrigo Gobbi <[email protected]>
scsi: csiostor: fix inverted sscanf() check in csio_store_dbg_level
csio_store_dbg_level() has an inverted return check on sscanf():
the existing check treats a truthy return as an error, so every valid write to the "dbg_level" sysfs attribute was rejected with -EINVAL. This has been broken since the driver was first added.
Quoted 4 lines
drivers/scsi/csiostor/csio_scsi.c +0
-0
[ ... ]
Quoted 7 lines
drivers/scsi/csiostor/csio_scsi.c +1
-1@@ -1441,12 +1440,11 @@ csio_store_dbg_level(struct device *dev,struct csio_lnode *ln =shost_priv (class_to_shost (dev));struct csio_hw *hw =csio_lnode_to_hw (ln);uint32_t dbg_level =0 ; +int ret; -if (!isdigit (buf[0 ]))
drivers/scsi/csiostor/csio_scsi.c csio_store_dbg_level(struct device *dev, · line 1450
Quoted 10 lines
drivers/scsi/csiostor/csio_scsi.c +3
-4-return -EINVAL ; - -if (sscanf (buf,"%i" , &dbg_level)) -return -EINVAL ; + ret =kstrtou32 (buf,16 , &dbg_level); +if (ret) +return ret; ln->params.log_level = dbg_level; hw->params.log_level = dbg_level;
[Severity: Medium]
This is a pre-existing issue, but does csio_store_dbg_level() need to return the number of bytes consumed (count) instead of 0 on success?
According to the sysfs API contract, a successful store callback must return the number of bytes consumed. Returning 0 instructs the VFS that 0 bytes were written, which causes user-space utilities like echo to indefinitely retry writing the remaining buffer, resulting in an infinite loop.
Since this patch fixes the parsing bug that previously rejected all valid numeric input, it looks like the success path will now be reachable for the first time:
drivers/scsi/csiostor/csio_scsi.c:csio_store_dbg_level() {
...
ln->params.log_level = dbg_level;
hw->params.log_level = dbg_level;
return 0;
}Could this return value be updated to count to prevent the infinite loop?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1