Blame SOURCES/libvirt-cim-0.6.3-0a742856.patch

5cef56
From 0a742856490bfdcb02c2af48a2a849593cccf1c7 Mon Sep 17 00:00:00 2001
5cef56
From: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
5cef56
Date: Thu, 29 Aug 2013 17:18:49 +0200
5cef56
Subject: [PATCH 08/48] libxkutil: Improve domain.os_info cleanup
5cef56
5cef56
The union fields in os_info were set by means of XML parsing which
5cef56
doesn't take into account that certain fields are depending on the
5cef56
virtualization type.
5cef56
This could lead both to memory overwrites and memory leaks.
5cef56
Fixed by using temporary variables and type-based setting of fields
5cef56
5cef56
Signed-off-by: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
5cef56
Signed-off-by: John Ferlan <jferlan@redhat.com>
5cef56
---
5cef56
 libxkutil/device_parsing.c | 73 +++++++++++++++++++++++++++++++++-------------
5cef56
 1 file changed, 52 insertions(+), 21 deletions(-)
5cef56
5cef56
diff --git a/libxkutil/device_parsing.c b/libxkutil/device_parsing.c
5cef56
index 542e4e9..ad0f19c 100644
5cef56
--- a/libxkutil/device_parsing.c
5cef56
+++ b/libxkutil/device_parsing.c
5cef56
@@ -1103,23 +1103,37 @@ int parse_fq_devid(const char *devid, char **host, char **device)
5cef56
         return 1;
5cef56
 }
5cef56
 
5cef56
+static void cleanup_bootlist(char **blist, unsigned blist_ct)
5cef56
+{
5cef56
+        while (blist_ct > 0) {
5cef56
+                free(blist[--blist_ct]);
5cef56
+        }
5cef56
+        free(blist);
5cef56
+}
5cef56
+
5cef56
 static int parse_os(struct domain *dominfo, xmlNode *os)
5cef56
 {
5cef56
         xmlNode *child;
5cef56
         char **blist = NULL;
5cef56
         unsigned bl_size = 0;
5cef56
+        char *kernel = NULL;
5cef56
+        char *initrd = NULL;
5cef56
+        char *cmdline = NULL;
5cef56
+        char *loader = NULL;
5cef56
+        char *boot = NULL;
5cef56
+        char *init = NULL;
5cef56
 
5cef56
         for (child = os->children; child != NULL; child = child->next) {
5cef56
-                if (XSTREQ(child->name, "type"))
5cef56
+                if (XSTREQ(child->name, "type")) {
5cef56
                         STRPROP(dominfo, os_info.pv.type, child);
5cef56
-                else if (XSTREQ(child->name, "kernel"))
5cef56
-                        STRPROP(dominfo, os_info.pv.kernel, child);
5cef56
+                } else if (XSTREQ(child->name, "kernel"))
5cef56
+                        kernel = get_node_content(child);
5cef56
                 else if (XSTREQ(child->name, "initrd"))
5cef56
-                        STRPROP(dominfo, os_info.pv.initrd, child);
5cef56
+                        initrd = get_node_content(child);
5cef56
                 else if (XSTREQ(child->name, "cmdline"))
5cef56
-                        STRPROP(dominfo, os_info.pv.cmdline, child);
5cef56
+                        cmdline = get_node_content(child);
5cef56
                 else if (XSTREQ(child->name, "loader"))
5cef56
-                        STRPROP(dominfo, os_info.fv.loader, child);
5cef56
+                        loader = get_node_content(child);
5cef56
                 else if (XSTREQ(child->name, "boot")) {
5cef56
                         char **tmp_list = NULL;
5cef56
 
5cef56
@@ -1137,7 +1151,7 @@ static int parse_os(struct domain *dominfo, xmlNode *os)
5cef56
                         blist[bl_size] = get_attr_value(child, "dev");
5cef56
                         bl_size++;
5cef56
                 } else if (XSTREQ(child->name, "init"))
5cef56
-                        STRPROP(dominfo, os_info.lxc.init, child);
5cef56
+                        init = get_node_content(child);
5cef56
         }
5cef56
 
5cef56
         if ((STREQC(dominfo->os_info.fv.type, "hvm")) &&
5cef56
@@ -1154,17 +1168,39 @@ static int parse_os(struct domain *dominfo, xmlNode *os)
5cef56
         else
5cef56
                 dominfo->type = -1;
5cef56
 
5cef56
-        if (STREQC(dominfo->os_info.fv.type, "hvm")) {
5cef56
+        switch (dominfo->type) {
5cef56
+        case DOMAIN_XENFV:
5cef56
+        case DOMAIN_KVM:
5cef56
+        case DOMAIN_QEMU:
5cef56
+                dominfo->os_info.fv.loader = loader;
5cef56
                 dominfo->os_info.fv.bootlist_ct = bl_size;
5cef56
                 dominfo->os_info.fv.bootlist = blist;
5cef56
-        } else {
5cef56
-            int i;
5cef56
-
5cef56
-            for (i = 0; i < bl_size; i++)
5cef56
-                free(blist[i]);
5cef56
-            free(blist);
5cef56
+                loader = NULL;
5cef56
+                blist = NULL;
5cef56
+                bl_size = 0;
5cef56
+                break;
5cef56
+        case DOMAIN_XENPV:
5cef56
+                dominfo->os_info.pv.kernel = kernel;
5cef56
+                dominfo->os_info.pv.initrd = initrd;
5cef56
+                dominfo->os_info.pv.cmdline = cmdline;
5cef56
+                kernel = NULL;
5cef56
+                initrd = NULL;
5cef56
+                cmdline = NULL;
5cef56
+                break;
5cef56
+        case DOMAIN_LXC:
5cef56
+                dominfo->os_info.lxc.init = init;
5cef56
+                init = NULL;
5cef56
+                break;
5cef56
+        default:
5cef56
+                break;
5cef56
         }
5cef56
 
5cef56
+        free(kernel);
5cef56
+        free(initrd);
5cef56
+        free(cmdline);
5cef56
+        free(boot);
5cef56
+        free(init);
5cef56
+        cleanup_bootlist(blist, bl_size);
5cef56
         return 1;
5cef56
 }
5cef56
 
5cef56
@@ -1360,15 +1396,10 @@ void cleanup_dominfo(struct domain **dominfo)
5cef56
                 free(dom->os_info.pv.cmdline);
5cef56
         } else if ((dom->type == DOMAIN_XENFV) ||
5cef56
                    (dom->type == DOMAIN_KVM) || (dom->type == DOMAIN_QEMU)) {
5cef56
-                int i;
5cef56
-
5cef56
                 free(dom->os_info.fv.type);
5cef56
                 free(dom->os_info.fv.loader);
5cef56
-                
5cef56
-                for (i = 0; i < dom->os_info.fv.bootlist_ct; i++) {
5cef56
-                        free(dom->os_info.fv.bootlist[i]);
5cef56
-                } 
5cef56
-                free(dom->os_info.fv.bootlist);
5cef56
+                cleanup_bootlist(dom->os_info.fv.bootlist,
5cef56
+                                 dom->os_info.fv.bootlist_ct);
5cef56
         } else if (dom->type == DOMAIN_LXC) {
5cef56
                 free(dom->os_info.lxc.type);
5cef56
                 free(dom->os_info.lxc.init);
5cef56
-- 
5cef56
1.8.5.3
5cef56