util: storage: Sanitize parsing of disk encryption XMLs

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

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Peter Krempa 2018-03-06 14:17:59 +01:00
parent 15948e6266
commit 1e6e34b144
4 changed files with 16 additions and 37 deletions

View File

@ -8542,7 +8542,8 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node,
static int static int
virDomainDiskSourceEncryptionParse(xmlNodePtr node, virDomainDiskSourceEncryptionParse(xmlNodePtr node,
virStorageEncryptionPtr *encryptionsrc) virStorageEncryptionPtr *encryptionsrc,
xmlXPathContextPtr ctxt)
{ {
xmlNodePtr child; xmlNodePtr child;
virStorageEncryptionPtr encryption = NULL; virStorageEncryptionPtr encryption = NULL;
@ -8551,7 +8552,7 @@ virDomainDiskSourceEncryptionParse(xmlNodePtr node,
if (child->type == XML_ELEMENT_NODE && if (child->type == XML_ELEMENT_NODE &&
virXMLNodeNameEqual(child, "encryption")) { virXMLNodeNameEqual(child, "encryption")) {
if (!(encryption = virStorageEncryptionParseNode(node->doc, child))) if (!(encryption = virStorageEncryptionParseNode(child, ctxt)))
return -1; return -1;
*encryptionsrc = encryption; *encryptionsrc = encryption;
@ -8637,7 +8638,7 @@ virDomainDiskSourceParse(xmlNodePtr node,
!(src->auth = virStorageAuthDefParse(tmp, ctxt))) !(src->auth = virStorageAuthDefParse(tmp, ctxt)))
goto cleanup; goto cleanup;
if (virDomainDiskSourceEncryptionParse(node, &src->encryption) < 0) if (virDomainDiskSourceEncryptionParse(node, &src->encryption, ctxt) < 0)
goto cleanup; goto cleanup;
if (virDomainDiskSourcePrivateDataParse(ctxt, src, flags, xmlopt) < 0) if (virDomainDiskSourcePrivateDataParse(ctxt, src, flags, xmlopt) < 0)
@ -9408,7 +9409,7 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
goto error; goto error;
} }
if (!(encryption = virStorageEncryptionParseNode(node->doc, cur))) if (!(encryption = virStorageEncryptionParseNode(cur, ctxt)))
goto error; goto error;
} else if (!serial && } else if (!serial &&
virXMLNodeNameEqual(cur, "serial")) { virXMLNodeNameEqual(cur, "serial")) {

View File

@ -1209,8 +1209,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
node = virXPathNode("./target/encryption", ctxt); node = virXPathNode("./target/encryption", ctxt);
if (node != NULL) { if (node != NULL) {
ret->target.encryption = virStorageEncryptionParseNode(ctxt->doc, ret->target.encryption = virStorageEncryptionParseNode(node, ctxt);
node);
if (ret->target.encryption == NULL) if (ret->target.encryption == NULL)
goto error; goto error;
} }

View File

@ -242,9 +242,11 @@ virStorageEncryptionInfoParseIvgen(xmlNodePtr info_node,
} }
static virStorageEncryptionPtr virStorageEncryptionPtr
virStorageEncryptionParseXML(xmlXPathContextPtr ctxt) virStorageEncryptionParseNode(xmlNodePtr node,
xmlXPathContextPtr ctxt)
{ {
xmlNodePtr saveNode = ctxt->node;
xmlNodePtr *nodes = NULL; xmlNodePtr *nodes = NULL;
virStorageEncryptionPtr encdef = NULL; virStorageEncryptionPtr encdef = NULL;
virStorageEncryptionPtr ret = NULL; virStorageEncryptionPtr ret = NULL;
@ -252,6 +254,8 @@ virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
int n; int n;
size_t i; size_t i;
ctxt->node = node;
if (VIR_ALLOC(encdef) < 0) if (VIR_ALLOC(encdef) < 0)
goto cleanup; goto cleanup;
@ -311,36 +315,11 @@ virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
VIR_FREE(format_str); VIR_FREE(format_str);
VIR_FREE(nodes); VIR_FREE(nodes);
virStorageEncryptionFree(encdef); virStorageEncryptionFree(encdef);
ctxt->node = saveNode;
return ret; return ret;
} }
virStorageEncryptionPtr
virStorageEncryptionParseNode(xmlDocPtr xml, xmlNodePtr root)
{
xmlXPathContextPtr ctxt = NULL;
virStorageEncryptionPtr enc = NULL;
if (STRNEQ((const char *) root->name, "encryption")) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("unknown root element for volume "
"encryption information"));
goto cleanup;
}
ctxt = xmlXPathNewContext(xml);
if (ctxt == NULL) {
virReportOOMError();
goto cleanup;
}
ctxt->node = root;
enc = virStorageEncryptionParseXML(ctxt);
cleanup:
xmlXPathFreeContext(ctxt);
return enc;
}
static int static int
virStorageEncryptionSecretFormat(virBufferPtr buf, virStorageEncryptionSecretFormat(virBufferPtr buf,

View File

@ -83,8 +83,8 @@ virStorageEncryptionPtr virStorageEncryptionCopy(const virStorageEncryption *src
void virStorageEncryptionFree(virStorageEncryptionPtr enc); void virStorageEncryptionFree(virStorageEncryptionPtr enc);
virStorageEncryptionPtr virStorageEncryptionParseNode(xmlDocPtr xml, virStorageEncryptionPtr virStorageEncryptionParseNode(xmlNodePtr node,
xmlNodePtr root); xmlXPathContextPtr ctxt);
int virStorageEncryptionFormat(virBufferPtr buf, int virStorageEncryptionFormat(virBufferPtr buf,
virStorageEncryptionPtr enc); virStorageEncryptionPtr enc);