Skip to contentSkip to conversation

vfio/iommufd: Fix memory leak

PATCH Superseded Cédric Le Goater 4 participants 5 messages Mar 13, 2024 – Mar 14, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/iommufd: Fix memory leak

Make sure variable contents is freed if scanf fails.

Cc Eric Auger <[email protected]>
Cc Yi Liu <[email protected]>
Cc Zhenzhong Duan <[email protected]>
Fixes CID 1540007
Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")
---
 hw/vfio/iommufd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/vfio/iommufd.c +3 -2
index a75a785e90c64cdcc4d10c88d217801b3f536cdb..cd549e0ee8573e75772c51cc96153762a6bc8550 100644
@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
 
     if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
         error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
-        goto out_free_dev_path;
+        goto out_free_contents;
     }
-    g_free(contents);
     vfio_devt = makedev(major, minor);
 
     vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
     trace_iommufd_cdev_getfd(vfio_path, ret);
     g_free(vfio_path);
 
+out_free_contents:
+    g_free(contents);
 out_free_dev_path:
     g_free(vfio_dev_path);
 out_close_dir:
Signature · 1 line
2.44.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Cédric Le Goater

Make sure variable contents is freed if scanf fails.

Cc Eric Auger <[email protected]>
Cc Yi Liu <[email protected]>
Cc Zhenzhong Duan <[email protected]>
Fixes CID 1540007
Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")

Thanks!

Eric

Quoted 28 lines
---
 hw/vfio/iommufd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/vfio/iommufd.c +3 -2
index a75a785e90c64cdcc4d10c88d217801b3f536cdb..cd549e0ee8573e75772c51cc96153762a6bc8550 100644
@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
 
     if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
         error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
-        goto out_free_dev_path;
+        goto out_free_contents;
     }
-    g_free(contents);
     vfio_devt = makedev(major, minor);
 
     vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
     trace_iommufd_cdev_getfd(vfio_path, ret);
     g_free(vfio_path);
 
+out_free_contents:
+    g_free(contents);
 out_free_dev_path:
     g_free(vfio_dev_path);
 out_close_dir:
Reviewed-by on v1 1/1
#2 lore raw
In reply to Cédric Le Goater
Quoted 28 lines · Cédric Le Goater

Make sure variable contents is freed if scanf fails.

Cc Eric Auger <[email protected]>
Cc Yi Liu <[email protected]>
Cc Zhenzhong Duan <[email protected]>
Fixes CID 1540007
Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")
---
  hw/vfio/iommufd.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
hw/vfio/iommufd.c +1 -2
index a75a785e90c64cdcc4d10c88d217801b3f536cdb..cd549e0ee8573e75772c51cc96153762a6bc8550 100644
@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
 
      if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
          error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
-        goto out_free_dev_path;
+        goto out_free_contents;
      }
-    g_free(contents);
      vfio_devt = makedev(major, minor);
 
      vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);

hw/vfio/iommufd.c static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp) · line 172

Quoted 8 lines
hw/vfio/iommufd.c +2 -0
@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
      trace_iommufd_cdev_getfd(vfio_path, ret);
      g_free(vfio_path);
 
+out_free_contents:
+    g_free(contents);
  out_free_dev_path:
      g_free(vfio_dev_path);
  out_close_dir:

good catch.

Signature · 2 lines
Regards,
Yi Liu
Reviewed-by on v1 1/1
#3 lore raw
In reply to Cédric Le Goater
Quoted 11 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, March 14, 2024 5:06 AM
To: [email protected]
Cc: Alex Williamson <[email protected]>; Cédric Le Goater <[email protected]>; Eric Auger <[email protected]>; Liu, Yi L <[email protected]>; Duan, Zhenzhong <[email protected]>
Subject: [PATCH] vfio/iommufd: Fix memory leak

Quoted 8 lines

Make sure variable contents is freed if scanf fails.

Cc Eric Auger <[email protected]>
Cc Yi Liu <[email protected]>
Cc Zhenzhong Duan <[email protected]>
Fixes CID 1540007
Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")

Unrelated to this patch, I see there are four g_free calls, not clear if it's deserved to cleanup with g_autofree.

Thanks
Zhenzhong

Quoted 34 lines

---
hw/vfio/iommufd.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
hw/vfio/iommufd.c +0 -0

index a75a785e90c64cdcc4d10c88d217801b3f536cdb..cd549e0ee8573e75772c5 1cc96153762a6bc8550 100644

hw/vfio/iommufd.c +1 -2
@@ -152,9 +152,8 @@ static int iommufd_cdev_getfd(const char
 *sysfs_path, Error **errp)
 
    if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
        error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
-        goto out_free_dev_path;
+        goto out_free_contents;
    }
-    g_free(contents);
    vfio_devt = makedev(major, minor);
 
    vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
hw/vfio/iommufd.c +2 -0
@@ -166,6 +165,8 @@ static int iommufd_cdev_getfd(const char
 *sysfs_path, Error **errp)
    trace_iommufd_cdev_getfd(vfio_path, ret);
    g_free(vfio_path);
 
+out_free_contents:
+    g_free(contents);

out_free_dev_path:

    g_free(vfio_dev_path);

out_close_dir:
--
2.44.0

#4 lore raw
Quoted 20 lines · Zhenzhong
Quoted 11 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, March 14, 2024 5:06 AM
To: [email protected]
Cc: Alex Williamson <[email protected]>; Cédric Le Goater <[email protected]>; Eric Auger <[email protected]>; Liu, Yi L <[email protected]>; Duan, Zhenzhong <[email protected]>
Subject: [PATCH] vfio/iommufd: Fix memory leak

Quoted 8 lines

Make sure variable contents is freed if scanf fails.

Cc Eric Auger <[email protected]>
Cc Yi Liu <[email protected]>
Cc Zhenzhong Duan <[email protected]>
Fixes CID 1540007
Fixes 5ee3dc7af785 ("vfio/iommufd: Implement the iommufd backend")
Quoted 4 lines

Unrelated to this patch, I see there are four g_free calls, not clear if it's deserved to cleanup with g_autofree.

Ah yes. This is much better indeed.

Thanks,

C.