Skip to contentSkip to conversation

Correctly free nd structure

PATCH In review Glauber Costa 2 participants 4 messages Sep 17, 2009 – Sep 18, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Correctly free nd structure

When we "free" a NICInfo structure, we can leak pointers, since we don't do much more than setting used = 0.

We free() the model parameter, but we don't set it to NULL. This means that a new user of this structure will see garbage in there. It was not noticed before because reusing a NICInfo is not that common, but it can be, for users of device pci hotplug.

A user hit it, described at https://bugzilla.redhat.com/show_bug.cgi?id=524022

This patch memset's the whole structure, guaranteeing that anyone reusing it will see a fresh NICinfo. Also, we free some other strings that are currently leaking.

This codebase is quite old, so this patch should feed all stable trees.

---
 net.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
net.c +7 -2
index 340177e..a405895 100644
@@ -2804,8 +2804,13 @@ void net_client_uninit(NICInfo *nd)
 {
     nd->vlan->nb_guest_devs--;
     nb_nics--;
-    nd->used = 0;
-    free((void *)nd->model);
+
+    qemu_free((void *)nd->model);
+    qemu_free((void *)nd->name);
+    qemu_free((void *)nd->devaddr);
+    qemu_free((void *)nd->id);
+
+    memset(nd, 0, sizeof(*nd));
 }
 
 static int net_host_check_device(const char *device)
Signature · 1 line
1.6.2.2
#1 lore raw
Quoted 30 lines · Glauber Costa

When we "free" a NICInfo structure, we can leak pointers, since we don't do much more than setting used = 0.

We free() the model parameter, but we don't set it to NULL. This means that a new user of this structure will see garbage in there. It was not noticed before because reusing a NICInfo is not that common, but it can be, for users of device pci hotplug.

A user hit it, described at https://bugzilla.redhat.com/show_bug.cgi?id=524022

This patch memset's the whole structure, guaranteeing that anyone reusing it will see a fresh NICinfo. Also, we free some other strings that are currently leaking.

This codebase is quite old, so this patch should feed all stable trees.

---
 net.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
net.c +0 -1
index 340177e..a405895 100644
@@ -2804,8 +2804,13 @@ void net_client_uninit(NICInfo *nd)
 {
     nd->vlan->nb_guest_devs--;
     nb_nics--;
-    nd->used = 0;

net.c void net_client_uninit(NICInfo *nd) · line 2815

Quoted 10 lines
net.c +7 -1
-    free((void *)nd->model);
+
+    qemu_free((void *)nd->model);
+    qemu_free((void *)nd->name);
+    qemu_free((void *)nd->devaddr);
+    qemu_free((void *)nd->id);
+
+    memset(nd, 0, sizeof(*nd));
 }
 

Looks good to me; my patch to port to QemuOpts actually zeros out the struct during init()

What is the (void *) cast for, though?

Thanks,
Mark.

#2 lore raw
Quoted 45 lines · Mark McLoughlin
Quoted 30 lines · Glauber Costa

When we "free" a NICInfo structure, we can leak pointers, since we don't do much more than setting used = 0.

We free() the model parameter, but we don't set it to NULL. This means that a new user of this structure will see garbage in there. It was not noticed before because reusing a NICInfo is not that common, but it can be, for users of device pci hotplug.

A user hit it, described at https://bugzilla.redhat.com/show_bug.cgi?id=524022

This patch memset's the whole structure, guaranteeing that anyone reusing it will see a fresh NICinfo. Also, we free some other strings that are currently leaking.

This codebase is quite old, so this patch should feed all stable trees.

---
 net.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
net.c +0 -1
index 340177e..a405895 100644
@@ -2804,8 +2804,13 @@ void net_client_uninit(NICInfo *nd)
 {
     nd->vlan->nb_guest_devs--;
     nb_nics--;
-    nd->used = 0;

net.c void net_client_uninit(NICInfo *nd) · line 2815

Quoted 10 lines
net.c +7 -1
-    free((void *)nd->model);
+
+    qemu_free((void *)nd->model);
+    qemu_free((void *)nd->name);
+    qemu_free((void *)nd->devaddr);
+    qemu_free((void *)nd->id);
+
+    memset(nd, 0, sizeof(*nd));
 }
 

Looks good to me; my patch to port to QemuOpts actually zeros out the struct during init()

Quoted 1 line

What is the (void *) cast for, though?

1) it was already there.
2) those strings are marked as const, gcc whins if you remove the void cast.

#3 lore raw
Quoted 46 lines · Glauber Costa
Quoted 45 lines · Mark McLoughlin
Quoted 30 lines · Glauber Costa

When we "free" a NICInfo structure, we can leak pointers, since we don't do much more than setting used = 0.

We free() the model parameter, but we don't set it to NULL. This means that a new user of this structure will see garbage in there. It was not noticed before because reusing a NICInfo is not that common, but it can be, for users of device pci hotplug.

A user hit it, described at https://bugzilla.redhat.com/show_bug.cgi?id=524022

This patch memset's the whole structure, guaranteeing that anyone reusing it will see a fresh NICinfo. Also, we free some other strings that are currently leaking.

This codebase is quite old, so this patch should feed all stable trees.

---
 net.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
net.c +0 -1
index 340177e..a405895 100644
@@ -2804,8 +2804,13 @@ void net_client_uninit(NICInfo *nd)
 {
     nd->vlan->nb_guest_devs--;
     nb_nics--;
-    nd->used = 0;

net.c void net_client_uninit(NICInfo *nd) · line 2815

Quoted 10 lines
net.c +7 -1
-    free((void *)nd->model);
+
+    qemu_free((void *)nd->model);
+    qemu_free((void *)nd->name);
+    qemu_free((void *)nd->devaddr);
+    qemu_free((void *)nd->id);
+
+    memset(nd, 0, sizeof(*nd));
 }
 

Looks good to me; my patch to port to QemuOpts actually zeros out the struct during init()

Quoted 3 lines
Quoted 1 line

What is the (void *) cast for, though?

1) it was already there.
2) those strings are marked as const, gcc whins if you remove the void cast.

Ah, yes - I had noticed that. They shouldn't be marked as const, though

Thanks,
Mark.