Blame SOURCES/libvirt-qemu_namespace-Don-t-leak-memory-in-qemuDomainGetPreservedMounts.patch

3a9410
From 77b0485ba92fe5f0520321385af8a7581c286df1 Mon Sep 17 00:00:00 2001
3a9410
Message-Id: <77b0485ba92fe5f0520321385af8a7581c286df1@dist-git>
3a9410
From: Michal Privoznik <mprivozn@redhat.com>
3a9410
Date: Mon, 31 Oct 2022 15:38:13 +0100
3a9410
Subject: [PATCH] qemu_namespace: Don't leak memory in
3a9410
 qemuDomainGetPreservedMounts()
3a9410
MIME-Version: 1.0
3a9410
Content-Type: text/plain; charset=UTF-8
3a9410
Content-Transfer-Encoding: 8bit
3a9410
3a9410
The aim of qemuDomainGetPreservedMounts() is to get a list of
3a9410
filesystems mounted under /dev and optionally generate a path for
3a9410
each one where they are moved temporarily when building the
3a9410
namespace. And the function tries to be a bit clever about it.
3a9410
For instance, if /dev/shm mount point exists, there's no need to
3a9410
consider /dev/shm/a nor /dev/shm/b as preserving just 'top level'
3a9410
/dev/shm gives the same result. To achieve this, the function
3a9410
iterates over the list of filesystem as returned by
3a9410
virFileGetMountSubtree() and removes the nested ones. However, it
3a9410
does so in a bit clumsy way: plain VIR_DELETE_ELEMENT() is used
3a9410
without freeing the string itself. Therefore, if all three
3a9410
aforementioned example paths appeared on the list, /dev/shm/a and
3a9410
/dev/shm/b strings would be leaked.
3a9410
3a9410
And when I think about it more, there's no real need to shrink
3a9410
the array down (realloc()). It's going to be free()-d when
3a9410
returning from the function. Switch to
3a9410
VIR_DELETE_ELEMENT_INPLACE() then.
3a9410
3a9410
Fixes: cdd9205dfffa3aaed935446a41f0d2dd1357c268
3a9410
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
3a9410
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
3a9410
Reviewed-by: Ján Tomko <jtomko@redhat.com>
3a9410
(cherry picked from commit bca7a53333ead7c1afd178728de74c2977cd4b5e)
3a9410
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2166573
3a9410
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
3a9410
---
3a9410
 src/qemu/qemu_namespace.c | 3 ++-
3a9410
 1 file changed, 2 insertions(+), 1 deletion(-)
3a9410
3a9410
diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c
3a9410
index 74ffd6fb90..2f50087c1d 100644
3a9410
--- a/src/qemu/qemu_namespace.c
3a9410
+++ b/src/qemu/qemu_namespace.c
3a9410
@@ -160,7 +160,8 @@ qemuDomainGetPreservedMounts(virQEMUDriverConfig *cfg,
3a9410
 
3a9410
             if (c && (*c == '/' || *c == '\0')) {
3a9410
                 VIR_DEBUG("Dropping path %s because of %s", mounts[j], mounts[i]);
3a9410
-                VIR_DELETE_ELEMENT(mounts, j, nmounts);
3a9410
+                VIR_FREE(mounts[j]);
3a9410
+                VIR_DELETE_ELEMENT_INPLACE(mounts, j, nmounts);
3a9410
             } else {
3a9410
                 j++;
3a9410
             }
3a9410
-- 
3a9410
2.39.1
3a9410