Skip to contentSkip to conversation

i386: Ensure feature names are always defined

PATCH In review Eduardo Habkost 3 participants 11 messages Feb 1, 2021 – Feb 2, 2021

lore
Share & tools
lore
#0 lore raw

cover i386: Ensure feature names are always defined

Forgetting to adding feature names to the feature array seems to be a very common mistake.

Examples:

- Missing name for MSR_VMX_EPT_WB
commit 0723cc8a5558 ("target/i386: add VMX features to named CPU models")
- Missing name for "ibrs" at
https://lore.kernel.org/qemu-devel/[email protected]

This series fixes the MSR_VMX_EPT_WB problem and adds a runtime check that should detect similar mistakes even before CPU model classes are registered.

Eduardo Habkost (3):
i386: Add missing "vmx-ept-wb" feature name
i386: Move asserts to separate x86_cpudef_validate() function
i386: Sanity check CPU model feature sets

 target/i386/cpu.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/3 i386: Add missing "vmx-ept-wb" feature name

Not having a feature name in feature_word_info breaks error reporting and query-cpu-model-expansion. Add the missing feature name to feature_word_info[FEAT_VMX_EPT_VPID_CAPS].feat_names[14].

Fixes 0723cc8a5558 ("target/i386: add VMX features to named CPU models")
---
 target/i386/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/cpu.c +1 -1
index ae89024d366..2bf3ab78056 100644
@@ -1262,7 +1262,7 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
             "vmx-ept-execonly", NULL, NULL, NULL,
             NULL, NULL, "vmx-page-walk-4", "vmx-page-walk-5",
             NULL, NULL, NULL, NULL,
-            NULL, NULL, NULL, NULL,
+            NULL, NULL, "vmx-ept-wb", NULL,
             "vmx-ept-2mb", "vmx-ept-1gb", NULL, NULL,
             "vmx-invept", "vmx-eptad", "vmx-ept-advanced-exitinfo", NULL,
             NULL, "vmx-invept-single-context", "vmx-invept-all-context", NULL,
Signature · 1 line
2.28.0
#2 lore raw

Re: [PATCH 1/3] i386: Add missing "vmx-ept-wb" feature name

This is intentional, because there's no way that any hypervisor can run if this feature is disabled.

Paolo

Il lun 1 feb 2021, 23:54 Eduardo Habkost <[email protected]> ha scritto:

Quoted 29 lines

Not having a feature name in feature_word_info breaks error reporting and query-cpu-model-expansion. Add the missing feature name to feature_word_info[FEAT_VMX_EPT_VPID_CAPS].feat_names[14].

Fixes 0723cc8a5558 ("target/i386: add VMX features to named CPU models")
---
 target/i386/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/cpu.c +1 -1
index ae89024d366..2bf3ab78056 100644
@@ -1262,7 +1262,7 @@ static FeatureWordInfo
 feature_word_info[FEATURE_WORDS] = {
             "vmx-ept-execonly", NULL, NULL, NULL,
             NULL, NULL, "vmx-page-walk-4", "vmx-page-walk-5",
             NULL, NULL, NULL, NULL,
-            NULL, NULL, NULL, NULL,
+            NULL, NULL, "vmx-ept-wb", NULL,
             "vmx-ept-2mb", "vmx-ept-1gb", NULL, NULL,
             "vmx-invept", "vmx-eptad", "vmx-ept-advanced-exitinfo", NULL,

NULL, "vmx-invept-single-context", "vmx-invept-all-context",
NULL,
--
2.28.0

#3 lore raw

Re: [PATCH 1/3] i386: Add missing "vmx-ept-wb" feature name

Quoted 8 lines · Paolo Bonzini

Il lun 1 feb 2021, 23:54 Eduardo Habkost <[email protected]> ha scritto:

Quoted 4 lines

Not having a feature name in feature_word_info breaks error reporting and query-cpu-model-expansion. Add the missing feature name to feature_word_info[FEAT_VMX_EPT_VPID_CAPS].feat_names[14].

This is intentional, because there's no way that any hypervisor can run if this feature is disabled.

If leaving the feature without name enables some desirable behavior, that's by accident and not by design. Which part of the existing behavior is intentional?

Signature · 1 line
Eduardo
#4 lore raw

Re: [PATCH 1/3] i386: Add missing "vmx-ept-wb" feature name

Il mar 2 feb 2021, 00:05 Eduardo Habkost <[email protected]> ha scritto:

Quoted 8 lines · Paolo Bonzini
Quoted 1 line

Il lun 1 feb 2021, 23:54 Eduardo Habkost <[email protected]> ha

scritto:

Quoted 5 lines
Quoted 5 lines

Not having a feature name in feature_word_info breaks error reporting and query-cpu-model-expansion. Add the missing feature name to feature_word_info[FEAT_VMX_EPT_VPID_CAPS].feat_names[14].

Quoted 8 lines
Quoted 1 line

This is intentional, because there's no way that any hypervisor can run

if

Quoted 1 line

this feature is disabled.

If leaving the feature without name enables some desirable behavior, that's by accident and not by design. Which part of the existing behavior is intentional?

Not being able to disable it.

Paolo

Quoted 4 lines

--
Eduardo

#5 lore raw

Re: [PATCH 1/3] i386: Add missing "vmx-ept-wb" feature name

Quoted 12 lines · Paolo Bonzini

Il mar 2 feb 2021, 00:05 Eduardo Habkost <[email protected]> ha scritto:

Quoted 10 lines · Paolo Bonzini
Quoted 1 line

Il lun 1 feb 2021, 23:54 Eduardo Habkost <[email protected]> ha

scritto:

Quoted 6 lines
Quoted 5 lines

Not having a feature name in feature_word_info breaks error reporting and query-cpu-model-expansion. Add the missing feature name to feature_word_info[FEAT_VMX_EPT_VPID_CAPS].feat_names[14].

This is intentional, because there's no way that any hypervisor can run

if

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

this feature is disabled.

If leaving the feature without name enables some desirable behavior, that's by accident and not by design. Which part of the existing behavior is intentional?

Not being able to disable it.

We can make it a hard dependency of vmx, then. We shouldn't leave it without a name, though.

Signature · 1 line
Eduardo
#6 lore raw

Re: [PATCH 1/3] i386: Add missing "vmx-ept-wb" feature name

Quoted 20 lines · Eduardo Habkost
Quoted 19 lines · Paolo Bonzini

Il mar 2 feb 2021, 00:05 Eduardo Habkost <[email protected]> ha scritto:

Quoted 16 lines · Paolo Bonzini
Quoted 1 line

Il lun 1 feb 2021, 23:54 Eduardo Habkost <[email protected]> ha

scritto:

Quoted 6 lines
Quoted 5 lines

Not having a feature name in feature_word_info breaks error reporting and query-cpu-model-expansion. Add the missing feature name to feature_word_info[FEAT_VMX_EPT_VPID_CAPS].feat_names[14].

This is intentional, because there's no way that any hypervisor can run

if

Quoted 1 line

this feature is disabled.

If leaving the feature without name enables some desirable behavior, that's by accident and not by design. Which part of the existing behavior is intentional?

Quoted 4 lines
Quoted 1 line

Not being able to disable it.

We can make it a hard dependency of vmx, then. We shouldn't leave it without a name, though.

The feature is already added to the MSRs unconditionally in kvm_msr_entry_add_vmx. I think we can just remove it from the models instead.

Paolo

#7 lore raw

Re: [PATCH 1/3] i386: Add missing "vmx-ept-wb" feature name

Quoted 21 lines · Paolo Bonzini
Quoted 20 lines · Eduardo Habkost
Quoted 19 lines · Paolo Bonzini

Il mar 2 feb 2021, 00:05 Eduardo Habkost <[email protected]> ha scritto:

Quoted 16 lines · Paolo Bonzini
Quoted 1 line

Il lun 1 feb 2021, 23:54 Eduardo Habkost <[email protected]> ha

scritto:

Quoted 6 lines
Quoted 5 lines

Not having a feature name in feature_word_info breaks error reporting and query-cpu-model-expansion. Add the missing feature name to feature_word_info[FEAT_VMX_EPT_VPID_CAPS].feat_names[14].

This is intentional, because there's no way that any hypervisor can run

if

Quoted 1 line

this feature is disabled.

If leaving the feature without name enables some desirable behavior, that's by accident and not by design. Which part of the existing behavior is intentional?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Not being able to disable it.

We can make it a hard dependency of vmx, then. We shouldn't leave it without a name, though.

The feature is already added to the MSRs unconditionally in kvm_msr_entry_add_vmx. I think we can just remove it from the models instead.

Sounds even simpler, and better. I'll do it in v2.

Signature · 1 line
Eduardo
#8 lore raw

PATCH 2/3 i386: Move asserts to separate x86_cpudef_validate() function

In reply to Eduardo Habkost

Additional sanity checks will be added to the code, so move the existing asserts to a separate function.

Wrap the whole function in #ifndef NDEBUG because the checks will become more complex than trivial assert() calls.

---
 target/i386/cpu.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
target/i386/cpu.c +12 -4
index 2bf3ab78056..6285fb00eb8 100644
@@ -5431,17 +5431,25 @@ static void x86_register_cpu_model_type(const char *name, X86CPUModel *model)
     type_register(&ti);
 }
 
-static void x86_register_cpudef_types(X86CPUDefinition *def)
+/* Sanity check CPU model definition before registering it */
+static void x86_cpudef_validate(X86CPUDefinition *def)
 {
-    X86CPUModel *m;
-    const X86CPUVersionDefinition *vdef;
-
+#ifndef NDEBUG
     /* AMD aliases are handled at runtime based on CPUID vendor, so
      * they shouldn't be set on the CPU model table.
      */
     assert(!(def->features[FEAT_8000_0001_EDX] & CPUID_EXT2_AMD_ALIASES));
     /* catch mistakes instead of silently truncating model_id when too long */
     assert(def->model_id && strlen(def->model_id) <= 48);
+#endif
+}
+
+static void x86_register_cpudef_types(X86CPUDefinition *def)
+{
+    X86CPUModel *m;
+    const X86CPUVersionDefinition *vdef;
+
+    x86_cpudef_validate(def);
 
     /* Unversioned model: */
     m = g_new0(X86CPUModel, 1);
Signature · 1 line
2.28.0
#9 lore raw

Re: [PATCH 2/3] i386: Move asserts to separate x86_cpudef_validate() function

Quoted 5 lines · Eduardo Habkost

Additional sanity checks will be added to the code, so move the existing asserts to a separate function.

Wrap the whole function in #ifndef NDEBUG because the checks will become more complex than trivial assert() calls.

How can you build with NDEBUG? See commit 262a69f4282:

osdep.h: Prohibit disabling assert() in supported builds

We already have several files that knowingly require assert() to work, sometimes because refactoring the code for proper error handling has not been tackled yet; there are probably other files that have a similar situation but with no comments documenting the same. In fact, we have places in migration that handle untrusted input with assertions, where disabling the assertions risks a worse security hole than the current behavior of losing the guest to SIGABRT when migration fails because of the assertion. Promote our current per-file safety-valve to instead be project-wide, and expand it to also cover glib's g_assert().

and "qemu/osdep.h":

/*
* We have a lot of unaudited code that may fail in strange ways, or
* even be a security risk during migration, if you disable assertions
* at compile-time. You may comment out these safety checks if you
* absolutely want to disable assertion overhead, but it is not
* supported upstream so the risk is all yours. Meanwhile, please
* submit patches to remove any side-effects inside an assertion, or
* fixing error handling that should use Error instead of assert. */
#ifdef NDEBUG
#error building with NDEBUG is not supported
#endif

IOW no need for the #idef.

Quoted 40 lines
---
 target/i386/cpu.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
target/i386/cpu.c +12 -4
index 2bf3ab78056..6285fb00eb8 100644
@@ -5431,17 +5431,25 @@ static void x86_register_cpu_model_type(const char *name, X86CPUModel *model)
     type_register(&ti);
 }
 
-static void x86_register_cpudef_types(X86CPUDefinition *def)
+/* Sanity check CPU model definition before registering it */
+static void x86_cpudef_validate(X86CPUDefinition *def)
 {
-    X86CPUModel *m;
-    const X86CPUVersionDefinition *vdef;
-
+#ifndef NDEBUG
     /* AMD aliases are handled at runtime based on CPUID vendor, so
      * they shouldn't be set on the CPU model table.
      */
     assert(!(def->features[FEAT_8000_0001_EDX] & CPUID_EXT2_AMD_ALIASES));
     /* catch mistakes instead of silently truncating model_id when too long */
     assert(def->model_id && strlen(def->model_id) <= 48);
+#endif
+}
+
+static void x86_register_cpudef_types(X86CPUDefinition *def)
+{
+    X86CPUModel *m;
+    const X86CPUVersionDefinition *vdef;
+
+    x86_cpudef_validate(def);
 
     /* Unversioned model: */
     m = g_new0(X86CPUModel, 1);
#10 lore raw

PATCH 3/3 i386: Sanity check CPU model feature sets

In reply to Eduardo Habkost

All CPU models must refer only to features that have their names defined in feature_word_info[].feat_names, otherwise error reporting and query-cpu-model-expansion will break.

Validate CPU feature flags in x86_cpudef_validate(), we can catch mistakes more easily.

---
 target/i386/cpu.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
target/i386/cpu.c +15 -0
index 6285fb00eb8..3c066738e82 100644
@@ -5435,12 +5435,27 @@ static void x86_register_cpu_model_type(const char *name, X86CPUModel *model)
 static void x86_cpudef_validate(X86CPUDefinition *def)
 {
 #ifndef NDEBUG
+    FeatureWord w;
+    int bitnr;
+
     /* AMD aliases are handled at runtime based on CPUID vendor, so
      * they shouldn't be set on the CPU model table.
      */
     assert(!(def->features[FEAT_8000_0001_EDX] & CPUID_EXT2_AMD_ALIASES));
     /* catch mistakes instead of silently truncating model_id when too long */
     assert(def->model_id && strlen(def->model_id) <= 48);
+
+    /*
+     * CPU models must enable only features with valid names, otherwise
+     * error reporting and query-cpu-model-expansion can't work correctly.
+     */
+    for (w = 0; w < FEATURE_WORDS; w++) {
+        for (bitnr = 0; bitnr < 64; bitnr++) {
+            uint64_t mask = (1ULL << bitnr);
+            assert(!(def->features[w] & mask) ||
+                   feature_word_info[w].feat_names[bitnr]);
+        }
+    }
 #endif
 }
 
Signature · 1 line
2.28.0