virStorageSourceParseBackingJSON: Prevent arbitrary nesting with format drivers

Since we parse attributes for 'raw' which is a format driver and thus
has nested 'file' structure we must prevent that this isn't nested
arbitrarily.

Add a flag for the function which allows parsing of 'format' type
drivers only on the first pass.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa
2020-02-03 16:25:48 +01:00
parent f8e097570e
commit fd70f1b4d3

View File

@@ -3052,7 +3052,8 @@ virStorageSourceParseBackingColon(virStorageSourcePtr src,
static int static int
virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src, virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
virJSONValuePtr json, virJSONValuePtr json,
const char *jsonstr); const char *jsonstr,
bool allowformat);
static int static int
@@ -3531,7 +3532,7 @@ virStorageSourceParseBackingJSONRaw(virStorageSourcePtr src,
return -1; return -1;
} }
return virStorageSourceParseBackingJSONInternal(src, file, jsonstr); return virStorageSourceParseBackingJSONInternal(src, file, jsonstr, false);
} }
@@ -3606,7 +3607,8 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
static int static int
virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src, virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
virJSONValuePtr json, virJSONValuePtr json,
const char *jsonstr) const char *jsonstr,
bool allowformat)
{ {
const char *drvname; const char *drvname;
size_t i; size_t i;
@@ -3619,7 +3621,16 @@ virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
} }
for (i = 0; i < G_N_ELEMENTS(jsonParsers); i++) { for (i = 0; i < G_N_ELEMENTS(jsonParsers); i++) {
if (STREQ(drvname, jsonParsers[i].drvname)) if (STRNEQ(drvname, jsonParsers[i].drvname))
continue;
if (jsonParsers[i].formatdriver && !allowformat) {
virReportError(VIR_ERR_INVALID_ARG,
_("JSON backing volume definition '%s' must not have nested format drivers"),
jsonstr);
return -1;
}
return jsonParsers[i].func(src, json, jsonstr, jsonParsers[i].opaque); return jsonParsers[i].func(src, json, jsonstr, jsonParsers[i].opaque);
} }
@@ -3655,7 +3666,7 @@ virStorageSourceParseBackingJSON(virStorageSourcePtr src,
if (!file) if (!file)
file = deflattened; file = deflattened;
return virStorageSourceParseBackingJSONInternal(src, file, json); return virStorageSourceParseBackingJSONInternal(src, file, json, true);
} }