[PATCH 0/3] Random almost trivial fixes

*** BLURB HERE *** Michal Prívozník (3): Don't pass NULL to yajl_free() virQEMUCapsNewBinary: Avoid NULL dereference virSecurityDeviceLabelDefNew: Avoid NULL dereference src/qemu/qemu_capabilities.c | 3 ++- src/util/virjson.c | 2 +- src/util/virseclabel.c | 2 +- tools/nss/libvirt_nss_leases.c | 3 ++- tools/nss/libvirt_nss_macs.c | 3 ++- 5 files changed, 8 insertions(+), 5 deletions(-) -- 2.24.1

Unfortunately, yajl_free() is not NOP on NULL. It really does expect a valid pointer. Therefore, check whether the pointer we want to pass to it is NULL or not. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virjson.c | 2 +- tools/nss/libvirt_nss_leases.c | 3 ++- tools/nss/libvirt_nss_macs.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index be472d49e4..dc662bf8e9 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1838,7 +1838,7 @@ virJSONValueFromString(const char *jsonstring) if (!hand) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to create JSON parser")); - goto cleanup; + return NULL; } /* Yajl 2 is nice enough to default to rejecting trailing garbage. */ diff --git a/tools/nss/libvirt_nss_leases.c b/tools/nss/libvirt_nss_leases.c index 7c431e4d53..015bbc4ab6 100644 --- a/tools/nss/libvirt_nss_leases.c +++ b/tools/nss/libvirt_nss_leases.c @@ -426,7 +426,8 @@ findLeases(const char *file, *addrs = NULL; *naddrs = 0; } - yajl_free(parser); + if (parser) + yajl_free(parser); free(parserState.entry.ipaddr); free(parserState.entry.macaddr); free(parserState.entry.hostname); diff --git a/tools/nss/libvirt_nss_macs.c b/tools/nss/libvirt_nss_macs.c index 05d096a348..d4b165eef6 100644 --- a/tools/nss/libvirt_nss_macs.c +++ b/tools/nss/libvirt_nss_macs.c @@ -278,7 +278,8 @@ findMACs(const char *file, *macs = NULL; *nmacs = 0; } - yajl_free(parser); + if (parser) + yajl_free(parser); for (i = 0; i < parserState.entry.nmacs; i++) free(parserState.entry.macs[i]); free(parserState.entry.macs); -- 2.24.1

On Mon, Mar 30, 2020 at 11:24:58AM +0200, Michal Privoznik wrote:
Unfortunately, yajl_free() is not NOP on NULL. It really does expect a valid pointer. Therefore, check whether the pointer we want to pass to it is NULL or not.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virjson.c | 2 +- tools/nss/libvirt_nss_leases.c | 3 ++- tools/nss/libvirt_nss_macs.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Even with GLib it is still possible for virQEMUCapsNew() to return NULL because it calls virQEMUCapsInitialize() which is a wrapper over pthread_once() which may fail. At least, we still check for its retval. If it so happens that the virQEMUCapsNew() fails and returns NULL, we should not dereference it. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/qemu/qemu_capabilities.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index a95a60c36a..3afe8a7b2c 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -1713,7 +1713,8 @@ virQEMUCapsNewBinary(const char *binary) { virQEMUCapsPtr qemuCaps = virQEMUCapsNew(); - qemuCaps->binary = g_strdup(binary); + if (qemuCaps) + qemuCaps->binary = g_strdup(binary); return qemuCaps; } -- 2.24.1

On Mon, Mar 30, 2020 at 11:24:59AM +0200, Michal Privoznik wrote:
Even with GLib it is still possible for virQEMUCapsNew() to return NULL because it calls virQEMUCapsInitialize() which is a wrapper over pthread_once() which may fail. At least, we still check for its retval. If it so happens that the virQEMUCapsNew() fails and returns NULL, we should not dereference it.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/qemu/qemu_capabilities.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

While it is impossible for VIR_ALLOC() to return an error, we should be consistent with the rest of the code and not continue initializing the virSecurityDeviceLabelDef structure. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virseclabel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/virseclabel.c b/src/util/virseclabel.c index a2b5ebf6b7..2141d84210 100644 --- a/src/util/virseclabel.c +++ b/src/util/virseclabel.c @@ -77,7 +77,7 @@ virSecurityDeviceLabelDefNew(const char *model) if (VIR_ALLOC(seclabel) < 0) { virSecurityDeviceLabelDefFree(seclabel); - seclabel = NULL; + return NULL; } seclabel->model = g_strdup(model); -- 2.24.1

On Mon, Mar 30, 2020 at 11:25:00AM +0200, Michal Privoznik wrote:
While it is impossible for VIR_ALLOC() to return an error, we should be consistent with the rest of the code and not continue initializing the virSecurityDeviceLabelDef structure.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virseclabel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
participants (2)
-
Daniel P. Berrangé
-
Michal Privoznik