[libvirt] [PATCH 2/6] util: storage: Sanitize parsing of disk auth XMLs

Peter Krempa pkrempa at redhat.com
Tue Mar 6 14:27:11 UTC 2018


Pass in the XPath context as we do in all other places rather than
allocating a new one.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/conf/domain_conf.c    | 21 ++++++++++++---------
 src/conf/storage_conf.c   |  2 +-
 src/util/virstoragefile.c | 32 ++++++++------------------------
 src/util/virstoragefile.h |  3 ++-
 4 files changed, 23 insertions(+), 35 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index a248d73de3..a8be0db7e4 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7218,7 +7218,8 @@ virDomainHostdevSubsysSCSIHostDefParseXML(xmlNodePtr sourcenode,

 static int
 virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
-                                           virDomainHostdevSubsysSCSIPtr def)
+                                           virDomainHostdevSubsysSCSIPtr def,
+                                           xmlXPathContextPtr ctxt)
 {
     int ret = -1;
     int auth_secret_usage = -1;
@@ -7259,7 +7260,7 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
     while (cur != NULL) {
         if (cur->type == XML_ELEMENT_NODE &&
             virXMLNodeNameEqual(cur, "auth")) {
-            if (!(authdef = virStorageAuthDefParse(sourcenode->doc, cur)))
+            if (!(authdef = virStorageAuthDefParse(cur, ctxt)))
                 goto cleanup;
             if ((auth_secret_usage =
                  virSecretUsageTypeFromString(authdef->secrettype)) < 0) {
@@ -7288,7 +7289,8 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,

 static int
 virDomainHostdevSubsysSCSIDefParseXML(xmlNodePtr sourcenode,
-                                      virDomainHostdevSubsysSCSIPtr scsisrc)
+                                      virDomainHostdevSubsysSCSIPtr scsisrc,
+                                      xmlXPathContextPtr ctxt)
 {
     char *protocol = NULL;
     int ret = -1;
@@ -7305,7 +7307,7 @@ virDomainHostdevSubsysSCSIDefParseXML(xmlNodePtr sourcenode,
     }

     if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI)
-        ret = virDomainHostdevSubsysSCSIiSCSIDefParseXML(sourcenode, scsisrc);
+        ret = virDomainHostdevSubsysSCSIiSCSIDefParseXML(sourcenode, scsisrc, ctxt);
     else
         ret = virDomainHostdevSubsysSCSIHostDefParseXML(sourcenode, scsisrc);

@@ -7550,7 +7552,7 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
         break;

     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
-        if (virDomainHostdevSubsysSCSIDefParseXML(sourcenode, scsisrc) < 0)
+        if (virDomainHostdevSubsysSCSIDefParseXML(sourcenode, scsisrc, ctxt) < 0)
             goto error;
         break;

@@ -8540,7 +8542,8 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node,

 static int
 virDomainDiskSourceAuthParse(xmlNodePtr node,
-                             virStorageAuthDefPtr *authdefsrc)
+                             virStorageAuthDefPtr *authdefsrc,
+                             xmlXPathContextPtr ctxt)
 {
     xmlNodePtr child;
     virStorageAuthDefPtr authdef;
@@ -8549,7 +8552,7 @@ virDomainDiskSourceAuthParse(xmlNodePtr node,
         if (child->type == XML_ELEMENT_NODE &&
             virXMLNodeNameEqual(child, "auth")) {

-            if (!(authdef = virStorageAuthDefParse(node->doc, child)))
+            if (!(authdef = virStorageAuthDefParse(child, ctxt)))
                 return -1;

             *authdefsrc = authdef;
@@ -8653,7 +8656,7 @@ virDomainDiskSourceParse(xmlNodePtr node,
         goto cleanup;
     }

-    if (virDomainDiskSourceAuthParse(node, &src->auth) < 0)
+    if (virDomainDiskSourceAuthParse(node, &src->auth, ctxt) < 0)
         goto cleanup;

     if (virDomainDiskSourceEncryptionParse(node, &src->encryption) < 0)
@@ -9401,7 +9404,7 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
                 goto error;
             }

-            if (!(authdef = virStorageAuthDefParse(node->doc, cur)))
+            if (!(authdef = virStorageAuthDefParse(cur, ctxt)))
                 goto error;
         } else if (virXMLNodeNameEqual(cur, "iotune")) {
             if (virDomainDiskDefIotuneParse(def, ctxt) < 0)
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index b9135722c1..f1f469d462 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -527,7 +527,7 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
     }

     if ((authnode = virXPathNode("./auth", ctxt))) {
-        if (!(authdef = virStorageAuthDefParse(node->doc, authnode)))
+        if (!(authdef = virStorageAuthDefParse(authnode, ctxt)))
             goto cleanup;

         if (authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE) {
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 3d17911297..67b9ec71ac 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -1809,16 +1809,20 @@ virStorageAuthDefCopy(const virStorageAuthDef *src)
 }


-static virStorageAuthDefPtr
-virStorageAuthDefParseXML(xmlXPathContextPtr ctxt)
+virStorageAuthDefPtr
+virStorageAuthDefParse(xmlNodePtr node,
+                       xmlXPathContextPtr ctxt)
 {
+    xmlNodePtr saveNode = ctxt->node;
     virStorageAuthDefPtr authdef = NULL;
     virStorageAuthDefPtr ret = NULL;
     xmlNodePtr secretnode = NULL;
     char *authtype = NULL;

+    ctxt->node = node;
+
     if (VIR_ALLOC(authdef) < 0)
-        return NULL;
+        goto cleanup;

     if (!(authdef->username = virXPathString("string(./@username)", ctxt))) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
@@ -1862,32 +1866,12 @@ virStorageAuthDefParseXML(xmlXPathContextPtr ctxt)
  cleanup:
     VIR_FREE(authtype);
     virStorageAuthDefFree(authdef);
+    ctxt->node = saveNode;

     return ret;
 }


-virStorageAuthDefPtr
-virStorageAuthDefParse(xmlDocPtr xml, xmlNodePtr root)
-{
-    xmlXPathContextPtr ctxt = NULL;
-    virStorageAuthDefPtr authdef = NULL;
-
-    ctxt = xmlXPathNewContext(xml);
-    if (ctxt == NULL) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
-    ctxt->node = root;
-    authdef = virStorageAuthDefParseXML(ctxt);
-
- cleanup:
-    xmlXPathFreeContext(ctxt);
-    return authdef;
-}
-
-
 void
 virStorageAuthDefFormat(virBufferPtr buf,
                         virStorageAuthDefPtr authdef)
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index 0095cd1387..596746ccb7 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -366,7 +366,8 @@ int virStorageFileGetSCSIKey(const char *path,

 void virStorageAuthDefFree(virStorageAuthDefPtr def);
 virStorageAuthDefPtr virStorageAuthDefCopy(const virStorageAuthDef *src);
-virStorageAuthDefPtr virStorageAuthDefParse(xmlDocPtr xml, xmlNodePtr root);
+virStorageAuthDefPtr virStorageAuthDefParse(xmlNodePtr node,
+                                            xmlXPathContextPtr ctxt);
 void virStorageAuthDefFormat(virBufferPtr buf, virStorageAuthDefPtr authdef);

 virSecurityDeviceLabelDefPtr
-- 
2.16.2




More information about the libvir-list mailing list