Skip to contentSkip to conversation

scsi: target: tcmu: Use struct_size() helper in kmalloc()

PATCH Applied Gustavo A. R. Silva 3 participants 3 messages Sep 27, 2021 – Oct 12, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: target: tcmu: 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.

---
 drivers/target/target_core_user.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/target/target_core_user.c +1 -2
index 9f552f48084c..dc220fad06fa 100644
@@ -1255,7 +1255,6 @@ tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf,
 {
 	int i = 0, cmd_cnt = 0;
 	bool unqueued = false;
-	uint16_t *cmd_ids = NULL;
 	struct tcmu_cmd *cmd;
 	struct se_cmd *se_cmd;
 	struct tcmu_tmr *tmr;
@@ -1292,7 +1291,7 @@ tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf,
 	pr_debug("TMR event %d on dev %s, aborted cmds %d, afflicted cmd_ids %d\n",
 		 tcmu_tmr_type(tmf), udev->name, i, cmd_cnt);
 
-	tmr = kmalloc(sizeof(*tmr) + cmd_cnt * sizeof(*cmd_ids), GFP_NOIO);
+	tmr = kmalloc(struct_size(tmr, tmr_cmd_ids, cmd_cnt), GFP_NOIO);
 	if (!tmr)
 		goto unlock;
 
Signature · 1 line
2.27.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Gustavo A. R. Silva

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.

---
  drivers/target/target_core_user.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/target/target_core_user.c +0 -1
index 9f552f48084c..dc220fad06fa 100644
@@ -1255,7 +1255,6 @@ tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf,
  {
  	int i = 0, cmd_cnt = 0;
  	bool unqueued = false;
-	uint16_t *cmd_ids = NULL;
  	struct tcmu_cmd *cmd;
  	struct se_cmd *se_cmd;
  	struct tcmu_tmr *tmr;

drivers/target/target_core_user.c tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf, · line 1297

Quoted 8 lines
drivers/target/target_core_user.c +1 -1
@@ -1292,7 +1291,7 @@ tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf,
  	pr_debug("TMR event %d on dev %s, aborted cmds %d, afflicted cmd_ids %d\n",
  		 tcmu_tmr_type(tmf), udev->name, i, cmd_cnt);
 
-	tmr = kmalloc(sizeof(*tmr) + cmd_cnt * sizeof(*cmd_ids), GFP_NOIO);
+	tmr = kmalloc(struct_size(tmr, tmr_cmd_ids, cmd_cnt), GFP_NOIO);
  	if (!tmr)
  		goto unlock;
 

Looks good. Thank you.

#2 lore raw
In reply to Gustavo A. R. Silva
Quoted 5 lines · Gustavo A. R. Silva

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.

Applied to 5.16/scsi-queue, thanks!

[1/1] scsi: target: tcmu: Use struct_size() helper in kmalloc()

      https://git.kernel.org/mkp/scsi/c/c20bda341946
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering