[libvirt] [PATCH] nwfilter: Fix possible segfault on sometimes consumed variable

John Ferlan posted 1 patch 6 years, 6 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20170927142217.30717-1-jferlan@redhat.com
src/conf/nwfilter_ipaddrmap.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
[libvirt] [PATCH] nwfilter: Fix possible segfault on sometimes consumed variable
Posted by John Ferlan 6 years, 6 months ago
The virNWFilterIPAddrMapAddIPAddr code can consume the @addr parameter
on success when the @ifname is found in the ipAddressMap->hashTable
hash table in the call to virNWFilterVarValueAddValue; however, if
not found in the hash table, then @addr is formatted into a @val
which is stored in the table and on return the caller would be
expected to free @addr.

Thus, the caller has no way to determine on success whether @addr was
consumed, so in order to fix this create a @tmp variable which will
be stored/consumed when virNWFilterVarValueAddValue succeeds. That way
the caller can free @addr whether the function returns success or failure.

Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/conf/nwfilter_ipaddrmap.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c
index 9c8584ce2..5668f366d 100644
--- a/src/conf/nwfilter_ipaddrmap.c
+++ b/src/conf/nwfilter_ipaddrmap.c
@@ -26,7 +26,9 @@
 
 #include "internal.h"
 
+#include "viralloc.h"
 #include "virerror.h"
+#include "virstring.h"
 #include "datatypes.h"
 #include "nwfilter_params.h"
 #include "nwfilter_ipaddrmap.h"
@@ -52,6 +54,7 @@ virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr)
 {
     int ret = -1;
     virNWFilterVarValuePtr val;
+    char *tmp = NULL;
 
     virMutexLock(&ipAddressMapLock);
 
@@ -65,14 +68,18 @@ virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr)
             virNWFilterVarValueFree(val);
         goto cleanup;
     } else {
-        if (virNWFilterVarValueAddValue(val, addr) < 0)
+        if (VIR_STRDUP(tmp, addr) < 0)
             goto cleanup;
+        if (virNWFilterVarValueAddValue(val, tmp) < 0)
+            goto cleanup;
+        tmp = NULL;
     }
 
     ret = 0;
 
  cleanup:
     virMutexUnlock(&ipAddressMapLock);
+    VIR_FREE(tmp);
 
     return ret;
 }
-- 
2.13.5

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] nwfilter: Fix possible segfault on sometimes consumed variable
Posted by Erik Skultety 6 years, 6 months ago
On Wed, Sep 27, 2017 at 10:22:17AM -0400, John Ferlan wrote:
> The virNWFilterIPAddrMapAddIPAddr code can consume the @addr parameter
> on success when the @ifname is found in the ipAddressMap->hashTable
> hash table in the call to virNWFilterVarValueAddValue; however, if
> not found in the hash table, then @addr is formatted into a @val
> which is stored in the table and on return the caller would be
> expected to free @addr.
>
> Thus, the caller has no way to determine on success whether @addr was
> consumed, so in order to fix this create a @tmp variable which will
> be stored/consumed when virNWFilterVarValueAddValue succeeds. That way
> the caller can free @addr whether the function returns success or failure.
>
> Signed-off-by: John Ferlan <jferlan@redhat.com>

Reviewed-by: Erik Skultety <eskultet@redhat.com>

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list