Skip to contentSkip to conversation

qemu-pr-helper: do not free reply when multipath_process_cmd() fails

PATCH Superseded Stefan Hajnoczi 3 participants 4 messages Oct 19 – Oct 23

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-pr-helper: do not free reply when multipath_process_cmd() fails

Kevin Wolf pointed out that the multipath_process_cmd() documentation only defines the value of reply on success. Don't touch it when the function returns -1.

The libmpathcmd implementation as of c78cf64a9e8e does not set *reply to NULL when its internal mpath_send_cmd() call fails, so I think Kevin's interpretation is correct.

As it stands, there is no observable bug in the QEMU code though because reply is initialized to NULL before calling multipath_process_cmd(). This patch is just a cleanup.

---
Eric pointed out that g_autofree could eliminate the manual free(3) calls. In this case we cannot use it since g_autofree calls glib's g_free() rather than the standard library's free(3).

---
 scsi/qemu-pr-helper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scsi/qemu-pr-helper.c +5 -1
index 54d196a503f..d52109cab25 100644
@@ -571,13 +571,17 @@ static bool multipathd_cmd(int fd, const char *cmd, Error **errp)
         free(reply);
         return false;
     }
-    free(reply);
     if (ret == -1) {
+        /*
+         * reply is not freed because the documentation only defines it when
+         * the function returns success.
+         */
         error_setg_errno(errp, last_errno,
                          "mpath_process_cmd(%d, \"%s\") failed",
                          fd, cmd);
         return false;
     }
+    free(reply);
     return true;
 }
 
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Stefan Hajnoczi

Kevin Wolf pointed out that the multipath_process_cmd() documentation only defines the value of reply on success. Don't touch it when the function returns -1.

The libmpathcmd implementation as of c78cf64a9e8e does not set *reply to NULL when its internal mpath_send_cmd() call fails, so I think Kevin's interpretation is correct.

As it stands, there is no observable bug in the QEMU code though because reply is initialized to NULL before calling multipath_process_cmd(). This patch is just a cleanup.

Quoted 8 lines

---
Eric pointed out that g_autofree could eliminate the manual free(3) calls. In this case we cannot use it since g_autofree calls glib's g_free() rather than the standard library's free(3).

I know glib encourages g_free() in case malloc was interposed, but aren't we guaranteed that we are not exploiting glib's malloc overrides, and therefore g_free() calls into free(3)? But I'm fine with your decision to not use g_autofree here.

Quoted 14 lines
---
 scsi/qemu-pr-helper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scsi/qemu-pr-helper.c +0 -1
index 54d196a503f..d52109cab25 100644
@@ -571,13 +571,17 @@ static bool multipathd_cmd(int fd, const char *cmd, Error **errp)
         free(reply);
         return false;
     }
-    free(reply);
     if (ret == -1) {

scsi/qemu-pr-helper.c static bool multipathd_cmd(int fd, const char *cmd, Error **errp) · line 584

Quoted 10 lines
scsi/qemu-pr-helper.c +5 -0
+        /*
+         * reply is not freed because the documentation only defines it when
+         * the function returns success.
+         */
         error_setg_errno(errp, last_errno,
                          "mpath_process_cmd(%d, \"%s\") failed",
                          fd, cmd);
         return false;
     }
+    free(reply);
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#2 lore raw
Quoted 22 lines · Eric Blake
Quoted 12 lines · Stefan Hajnoczi

Kevin Wolf pointed out that the multipath_process_cmd() documentation only defines the value of reply on success. Don't touch it when the function returns -1.

The libmpathcmd implementation as of c78cf64a9e8e does not set *reply to NULL when its internal mpath_send_cmd() call fails, so I think Kevin's interpretation is correct.

As it stands, there is no observable bug in the QEMU code though because reply is initialized to NULL before calling multipath_process_cmd(). This patch is just a cleanup.

Quoted 8 lines

---
Eric pointed out that g_autofree could eliminate the manual free(3) calls. In this case we cannot use it since g_autofree calls glib's g_free() rather than the standard library's free(3).

Quoted 4 lines

I know glib encourages g_free() in case malloc was interposed, but aren't we guaranteed that we are not exploiting glib's malloc overrides, and therefore g_free() calls into free(3)? But I'm fine with your decision to not use g_autofree here.

g_mem_set_vtable() is deprecated and a nop. Interposing no longer works. g_mem_is_system_malloc() always returns TRUE.

If we take advantage of glib internals, then you can call g_free() on malloc() memory AFAIK. But I don't think the API documentation guarantees this will always be the case and I'm not sure it's worth baking in this assumption.

Maybe there is a clean way to do this with the glib API: define a type called SystemMallocPtr and then use g_autoptr(SystemMallocPtr)?

If someone with more glib knowledge has a clean solution, we could start using it. IMO if you need to think too much about it, it's probably best to do the simple thing instead.

Quoted 33 lines
Quoted 15 lines
---
 scsi/qemu-pr-helper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scsi/qemu-pr-helper.c +0 -1
index 54d196a503f..d52109cab25 100644
@@ -571,13 +571,17 @@ static bool multipathd_cmd(int fd, const char *cmd, Error **errp)
         free(reply);
         return false;
     }
-    free(reply);
     if (ret == -1) {

scsi/qemu-pr-helper.c static bool multipathd_cmd(int fd, const char *cmd, Error **errp) · line 584

Quoted 10 lines
scsi/qemu-pr-helper.c +5 -0
+        /*
+         * reply is not freed because the documentation only defines it when
+         * the function returns success.
+         */
         error_setg_errno(errp, last_errno,
                          "mpath_process_cmd(%d, \"%s\") failed",
                          fd, cmd);
         return false;
     }
+    free(reply);
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#3 lore raw
Quoted 28 lines · Stefan Hajnoczi
Quoted 22 lines · Eric Blake
Quoted 12 lines · Stefan Hajnoczi

Kevin Wolf pointed out that the multipath_process_cmd() documentation only defines the value of reply on success. Don't touch it when the function returns -1.

The libmpathcmd implementation as of c78cf64a9e8e does not set *reply to NULL when its internal mpath_send_cmd() call fails, so I think Kevin's interpretation is correct.

As it stands, there is no observable bug in the QEMU code though because reply is initialized to NULL before calling multipath_process_cmd(). This patch is just a cleanup.

Quoted 8 lines

---
Eric pointed out that g_autofree could eliminate the manual free(3) calls. In this case we cannot use it since g_autofree calls glib's g_free() rather than the standard library's free(3).

Quoted 4 lines

I know glib encourages g_free() in case malloc was interposed, but aren't we guaranteed that we are not exploiting glib's malloc overrides, and therefore g_free() calls into free(3)? But I'm fine with your decision to not use g_autofree here.

Quoted 7 lines

g_mem_set_vtable() is deprecated and a nop. Interposing no longer works. g_mem_is_system_malloc() always returns TRUE.

If we take advantage of glib internals, then you can call g_free() on malloc() memory AFAIK. But I don't think the API documentation guarantees this will always be the case and I'm not sure it's worth baking in this assumption.

Glib has public guaranteed this for years now, since I personally added this guarantee to their API docs :-)

"Since GLib 2.46, g_malloc() is hardcoded to always use the system
malloc implementation."

https://docs.gtk.org/glib/memory.html

QEMU has equally relied on this guarantee for years.

The only things we have to be wary of are other non-glib based 3rd party libraries QEMU uses that may allow malloc overrides.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|