Skip to contentSkip to conversation

dmaengine: idxd: fix double free in idxd_setup_groups() error path

PATCH In review Guangshuo Li 2 participants 3 messages Apr 1 – Apr 2

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

PATCH 1/1 dmaengine: idxd: fix double free in idxd_setup_groups() error path

When an error happens after device_initialize(), idxd_setup_groups() calls put_device(conf_dev).

The device release callback idxd_conf_group_release() frees group, but the current error paths then call kfree(group) again, causing a double free.

Keep the cleanup in idxd_conf_group_release() after put_device() and avoid freeing group again in idxd_setup_groups().

Fixes aa6f4f945b10 ("dmaengine: idxd: fix memory leak in error handling path of idxd_setup_groups")
---
 drivers/dma/idxd/init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/init.c +2 -2
index b782eb3c191d..d9a9d56dd277 100644
@@ -374,7 +374,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
 		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
 		if (rc < 0) {
 			put_device(conf_dev);
-			kfree(group);
+
 			goto err;
 		}
 
@@ -399,7 +399,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
 	while (--i >= 0) {
 		group = idxd->groups[i];
 		put_device(group_confdev(group));
-		kfree(group);
+
 	}
 	kfree(idxd->groups);
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Guangshuo Li

When an error happens after device_initialize(), idxd_setup_groups() calls put_device(conf_dev).

The device release callback idxd_conf_group_release() frees group, but the current error paths then call kfree(group) again, causing a double free.

Keep the cleanup in idxd_conf_group_release() after put_device() and avoid freeing group again in idxd_setup_groups().

Yes, I think it is a double free.
Sorry, I missed the idxd_conf_group_release path.

Quoted 22 lines
Fixes aa6f4f945b10 ("dmaengine: idxd: fix memory leak in error handling path of idxd_setup_groups")
---
  drivers/dma/idxd/init.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/init.c +1 -1
index b782eb3c191d..d9a9d56dd277 100644
@@ -374,7 +374,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
  		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
  		if (rc < 0) {
  			put_device(conf_dev);
-			kfree(group);
+
  			goto err;
  		}
 

drivers/dma/idxd/init.c static int idxd_setup_groups(struct idxd_device *idxd) · line 402

Quoted 5 lines
drivers/dma/idxd/init.c +1 -1
@@ -399,7 +399,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
  	while (--i >= 0) {
  		group = idxd->groups[i];
  		put_device(group_confdev(group));
-		kfree(group);
+

Nit: please remove the blank lines left behind by both deletions.

With those addressed:

Thanks.
Shuai

#2 lore raw
In reply to Guangshuo Li
Quoted 31 lines · Guangshuo Li

When an error happens after device_initialize(), idxd_setup_groups() calls put_device(conf_dev).

The device release callback idxd_conf_group_release() frees group, but the current error paths then call kfree(group) again, causing a double free.

Keep the cleanup in idxd_conf_group_release() after put_device() and avoid freeing group again in idxd_setup_groups().

Fixes aa6f4f945b10 ("dmaengine: idxd: fix memory leak in error handling path of idxd_setup_groups")
---
  drivers/dma/idxd/init.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/init.c +1 -1
index b782eb3c191d..d9a9d56dd277 100644
@@ -374,7 +374,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
  		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
  		if (rc < 0) {
  			put_device(conf_dev);
-			kfree(group);
+
  			goto err;
  		}
 

drivers/dma/idxd/init.c static int idxd_setup_groups(struct idxd_device *idxd) · line 405

Quoted 8 lines
drivers/dma/idxd/init.c +1 -1
@@ -399,7 +399,7 @@ static int idxd_setup_groups(struct idxd_device *idxd)
  	while (--i >= 0) {
  		group = idxd->groups[i];
  		put_device(group_confdev(group));
-		kfree(group);
+
  	}
  	kfree(idxd->groups);
 

Note that idxd_clean_groups() and idxd_clean_engines() have the same pattern. It should be fixed in the same patch as well.

Thanks.
Shuai