Skip to contentSkip to conversation

scsi: core: Use struct_size() helper in kmalloc()

PATCH New Gustavo A. R. Silva 1 participant 1 message Jan 25, 2022 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: core: Use struct_size() helper in kmalloc()

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/scsi/scsi.c:390:27: warning: using sizeof on a flexible structure

---
 drivers/scsi/scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi.c +1 -1
index 211aace69c22..949cb530e360 100644
@@ -387,7 +387,7 @@ static struct scsi_vpd *scsi_get_vpd_buf(struct scsi_device *sdev, u8 page)
 	int vpd_len = SCSI_VPD_PG_LEN, result;
 
 retry_pg:
-	vpd_buf = kmalloc(sizeof(*vpd_buf) + vpd_len, GFP_KERNEL);
+	vpd_buf = kmalloc(struct_size(vpd_buf, data, vpd_len), GFP_KERNEL);
 	if (!vpd_buf)
 		return NULL;
 
Signature · 1 line
2.27.0