[PATCH v2 06/27] conf: Require nvdimm path in validate step

Michal Privoznik posted 27 patches 5 years, 2 months ago
[PATCH v2 06/27] conf: Require nvdimm path in validate step
Posted by Michal Privoznik 5 years, 2 months ago
Our code expects that a nvdimm has a path defined always. And the
parser does check for that. Well, not fully - only when parsing
<source/> (which is an optional element). So if the element is
not in the XML then the check is not performed and the assumption
is broken. Verify in the memory def validator that a path was
set.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/conf/domain_conf.c           | 12 +++++++-----
 src/security/security_apparmor.c |  6 ------
 2 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index d8df18b542..da14760e2d 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6694,6 +6694,12 @@ virDomainMemoryDefValidate(const virDomainMemoryDef *mem,
                            const virDomainDef *def)
 {
     if (mem->model == VIR_DOMAIN_MEMORY_MODEL_NVDIMM) {
+        if (!mem->nvdimmPath) {
+            virReportError(VIR_ERR_XML_DETAIL, "%s",
+                           _("path is required for model 'nvdimm'"));
+            return -1;
+        }
+
         if (mem->discard == VIR_TRISTATE_BOOL_YES) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("discard is not supported for nvdimms"));
@@ -16690,11 +16696,7 @@ virDomainMemorySourceDefParseXML(xmlNodePtr node,
         break;
 
     case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
-        if (!(def->nvdimmPath = virXPathString("string(./path)", ctxt))) {
-            virReportError(VIR_ERR_XML_DETAIL, "%s",
-                           _("path is required for model 'nvdimm'"));
-            return -1;
-        }
+        def->nvdimmPath = virXPathString("string(./path)", ctxt);
 
         if (virDomainParseMemory("./alignsize", "./alignsize/@unit", ctxt,
                                  &def->alignsize, false, false) < 0)
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index c2d86c6940..f306af8dd3 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -686,12 +686,6 @@ AppArmorSetMemoryLabel(virSecurityManagerPtr mgr,
 
     switch ((virDomainMemoryModel) mem->model) {
     case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
-        if (mem->nvdimmPath == NULL) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("%s: nvdimm without a path"),
-                           __func__);
-            return -1;
-        }
         if (!virFileExists(mem->nvdimmPath)) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("%s: \'%s\' does not exist"),
-- 
2.26.2

Re: [PATCH v2 06/27] conf: Require nvdimm path in validate step
Posted by Peter Krempa 5 years, 2 months ago
On Thu, Dec 03, 2020 at 13:36:09 +0100, Michal Privoznik wrote:
> Our code expects that a nvdimm has a path defined always. And the
> parser does check for that. Well, not fully - only when parsing
> <source/> (which is an optional element). So if the element is
> not in the XML then the check is not performed and the assumption
> is broken. Verify in the memory def validator that a path was
> set.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/conf/domain_conf.c           | 12 +++++++-----
>  src/security/security_apparmor.c |  6 ------
>  2 files changed, 7 insertions(+), 11 deletions(-)

We don't validate existing configs, but removing the apparmor check is
okay since you can't have an running VM with broken config due to it and
the check will be done when starting the VM.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>