Skip to contentSkip to conversation

net/vhost-vdpa: fix memory leak in vhost_vdpa_get_max_queue_pairs()

PATCH In review Stefano Garzarella 3 participants 4 messages Nov 2, 2021 – Nov 3, 2021 Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/vhost-vdpa: fix memory leak in vhost_vdpa_get_max_queue_pairs()

Use g_autofree to ensure that config is freed when vhost_vdpa_get_max_queue_pairs() returns.

Reported-by Coverity (CID 1465228: RESOURCE_LEAK)
Fixes 402378407d ("vhost-vdpa: multiqueue support")
---
 net/vhost-vdpa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/vhost-vdpa.c +1 -1
index 49ab322511..373b706b90 100644
@@ -214,7 +214,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
 static int vhost_vdpa_get_max_queue_pairs(int fd, int *has_cvq, Error **errp)
 {
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
-    struct vhost_vdpa_config *config;
+    g_autofree struct vhost_vdpa_config *config = NULL;
     __virtio16 *max_queue_pairs;
     uint64_t features;
     int ret;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Stefano Garzarella

Use g_autofree to ensure that config is freed when vhost_vdpa_get_max_queue_pairs() returns.

Reported-by Coverity (CID 1465228: RESOURCE_LEAK)
Fixes 402378407d ("vhost-vdpa: multiqueue support")
---
 net/vhost-vdpa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/vhost-vdpa.c +0 -0
index 49ab322511..373b706b90 100644

net/vhost-vdpa.c static NetClientState *net_vhost_vdpa_init(NetClientState *peer, · line 220

Quoted 8 lines
net/vhost-vdpa.c +1 -1
@@ -214,7 +214,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
 static int vhost_vdpa_get_max_queue_pairs(int fd, int *has_cvq, Error **errp)
 {
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
-    struct vhost_vdpa_config *config;
+    g_autofree struct vhost_vdpa_config *config = NULL;
     __virtio16 *max_queue_pairs;
     uint64_t features;
     int ret;

Eventually reducing the scope:

-- >8 --

net/vhost-vdpa.c +0 -0
@@ -214,7 +214,6 @@ static NetClientState

*net_vhost_vdpa_init(NetClientState *peer,
static int vhost_vdpa_get_max_queue_pairs(int fd, int *has_cvq, Error
**errp)

 {
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
-    struct vhost_vdpa_config *config;
     __virtio16 *max_queue_pairs;
     uint64_t features;
     int ret;
@@ -232,6 +231,8 @@ static int vhost_vdpa_get_max_queue_pairs(int fd,
int *has_cvq, Error **errp)
     }

     if (features & (1 << VIRTIO_NET_F_MQ)) {
+        g_autofree struct vhost_vdpa_config *config = NULL;
+
         config = g_malloc0(config_size + sizeof(*max_queue_pairs));
         config->off = offsetof(struct virtio_net_config,
max_virtqueue_pairs);
         config->len = sizeof(*max_queue_pairs);

---

Either ways:

#2 lore raw
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Stefano Garzarella

Use g_autofree to ensure that config is freed when vhost_vdpa_get_max_queue_pairs() returns.

Reported-by Coverity (CID 1465228: RESOURCE_LEAK)
Fixes 402378407d ("vhost-vdpa: multiqueue support")
---
 net/vhost-vdpa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

net/vhost-vdpa.c static NetClientState *net_vhost_vdpa_init(NetClientState *peer, · line 220

Quoted 13 lines
net/vhost-vdpa.c +1 -1
index 49ab322511..373b706b90 100644
@@ -214,7 +214,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
 static int vhost_vdpa_get_max_queue_pairs(int fd, int *has_cvq, Error **errp)
 {
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
-    struct vhost_vdpa_config *config;
+    g_autofree struct vhost_vdpa_config *config = NULL;
     __virtio16 *max_queue_pairs;
     uint64_t features;
     int ret;

Eventually reducing the scope:

Yep, I thought the same, moving also config_size, but then I switched to the simplest patch possible.

Quoted 25 lines

-- >8 --

net/vhost-vdpa.c +0 -0
@@ -214,7 +214,6 @@ static NetClientState
 *net_vhost_vdpa_init(NetClientState *peer,

static int vhost_vdpa_get_max_queue_pairs(int fd, int *has_cvq, Error **errp)

{
    unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
net/vhost-vdpa.c +2 -1
-    struct vhost_vdpa_config *config;
    __virtio16 *max_queue_pairs;
    uint64_t features;
    int ret;
@@ -232,6 +231,8 @@ static int vhost_vdpa_get_max_queue_pairs(int fd,
 int *has_cvq, Error **errp)
    }
 
    if (features & (1 << VIRTIO_NET_F_MQ)) {
+        g_autofree struct vhost_vdpa_config *config = NULL;
+
        config = g_malloc0(config_size + sizeof(*max_queue_pairs));
        config->off = offsetof(struct virtio_net_config,
max_virtqueue_pairs);
        config->len = sizeof(*max_queue_pairs);
Quoted 4 lines

---

Either ways:

Thanks, let's see what Jason and Michael prefer.

Stefano

Acked-by on v1 1/1
#3 lore raw
In reply to Stefano Garzarella
Quoted 7 lines · Stefano Garzarella

Use g_autofree to ensure that config is freed when vhost_vdpa_get_max_queue_pairs() returns.

Reported-by Coverity (CID 1465228: RESOURCE_LEAK)
Fixes 402378407d ("vhost-vdpa: multiqueue support")
Quoted 20 lines
---
 net/vhost-vdpa.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/vhost-vdpa.c +1 -2
index 49ab322511..373b706b90 100644
@@ -214,7 +214,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
 static int vhost_vdpa_get_max_queue_pairs(int fd, int *has_cvq, Error **errp)
 {
     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
-    struct vhost_vdpa_config *config;
+    g_autofree struct vhost_vdpa_config *config = NULL;
     __virtio16 *max_queue_pairs;
     uint64_t features;
     int ret;
--

2.31.1