Skip to contentSkip to conversation

scsi: lpfc: Fix a possible null pointer dereference

PATCH Superseded Huai-Yuan Liu 2 participants 2 messages Jun 20, 2024

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 scsi: lpfc: Fix a possible null pointer dereference

In function lpfc_xcvr_data_show, the memory allocation with kmalloc might fail, thereby making rdp_context a null pointer. In the following context and functions that use this pointer, there are dereferencing operations, leading to null pointer dereference.

To fix this issue, a null pointer check should be added. If it is null, just return len.

Fixes 479b0917e447 ("scsi: lpfc: Create a sysfs entry called lpfc_xcvr_data for transceiver info")

---
V2:
* In patch V2, we have removed the unnecessary 'out of memory' message. Thank Bart Van Assche for helpful advice.
V3:
* In patch V3, we return len directly instead of goto out_free_rdp. Thanks to Justin Tee for his suuestion.

---
 drivers/scsi/lpfc/lpfc_attr.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/scsi/lpfc/lpfc_attr.c +2 -0
index a46c73e8d7c4..7d1e38ea9e52 100644
@@ -1907,6 +1907,8 @@ lpfc_xcvr_data_show(struct device *dev, struct device_attribute *attr,
 
 	/* Get transceiver information */
 	rdp_context = kmalloc(sizeof(*rdp_context), GFP_KERNEL);
+	if (!rdp_context)
+		return len;
 
 	rc = lpfc_get_sfp_info_wait(phba, rdp_context);
 	if (rc) {
Signature · 1 line
2.34.1
#1 lore raw

Hi Huai-Yuan,

Sorry for noticing this later, but a return len while len is 0 would result in a silent $(cat /sys/class/scsi_host/host*/lpfc_xcvr_data).

Perhaps, it's better to log something to at least notify the user why SFP information was not able to be collected.

How about something like this?

        /* Get transceiver information */
        rdp_context = kmalloc(sizeof(*rdp_context), GFP_KERNEL);

+ if (!rdp_context) {
+ len = scnprintf(buf, PAGE_SIZE - len,
+ "SFP info NA: alloc failure\n");
+ return len;
+ }

        rc = lpfc_get_sfp_info_wait(phba, rdp_context);
        if (rc) {

Thanks,
Justin Tee