Skip to contentSkip to conversation

tools/libxl: Only access legacy altp2m on HVM

PATCH In review Jason Andryuk 2 participants 5 messages May 12, 2025 – May 13, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libxl: Only access legacy altp2m on HVM

Only access the HVM union b_info->u.hvm on HVM guests. The union access is not guarded, so this reads and sets the default even on non-HVM guests. Usually this doesn't matter as PV and PVH unions are smaller and zero-initialized, but the zero default will be re-written as a -1 boolean. Generally, it it could incorrectly set b_info->altp2m through aliased data.

Fixes 0291089f6ea8 ("xen: enable altp2m at create domain domctl")
Change-Id Ifaca3533dcce3f409c2efa292c7e96fba6371d9d
---
 tools/libs/light/libxl_x86.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tools/libs/light/libxl_x86.c +6 -4
index 0b1c2d3a96..b8f6663829 100644
@@ -821,10 +821,12 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
      * If the legacy field info->u.hvm.altp2m is set, activate altp2m.
      * Otherwise set altp2m based on the field info->altp2m.
      */
-    libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
-    if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
-        libxl_defbool_val(b_info->u.hvm.altp2m))
-        b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
+        libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
+        if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
+            libxl_defbool_val(b_info->u.hvm.altp2m))
+            b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    }
 
     return 0;
 }
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/1 tools/libxl: Only read legacy altp2m on HVM

Only read the HVM union b_info->u.hvm on HVM guests. The union access is not guarded, so this reads even on non-HVM guests. Usually this doesn't matter as PV and PVH unions are smaller and zero-initialized. But it could incorrectly set b_info->altp2m through aliased data.

Fixes 0291089f6ea8 ("xen: enable altp2m at create domain domctl")
---
 tools/libs/light/libxl_x86.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tools/libs/light/libxl_x86.c +6 -4
index 0b1c2d3a96..b8f6663829 100644
@@ -821,10 +821,12 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
      * If the legacy field info->u.hvm.altp2m is set, activate altp2m.
      * Otherwise set altp2m based on the field info->altp2m.
      */
-    libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
-    if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
-        libxl_defbool_val(b_info->u.hvm.altp2m))
-        b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
+        libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
+        if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
+            libxl_defbool_val(b_info->u.hvm.altp2m))
+            b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    }
 
     return 0;
 }
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH] tools/libxl: Only read legacy altp2m on HVM

Quoted 5 lines · Jason Andryuk

Only read the HVM union b_info->u.hvm on HVM guests. The union access is not guarded, so this reads even on non-HVM guests. Usually this doesn't matter as PV and PVH unions are smaller and zero-initialized. But it could incorrectly set b_info->altp2m through aliased data.

Oops. I re-phrased the commit message, but I accidentally had an old version in the directory. Sorry about that. Please disregard this one and review "tools/libxl: Only access legacy altp2m on HVM".

Thanks,
Jason

#3 lore raw
In reply to Jason Andryuk
Quoted 22 lines · Jason Andryuk

Only access the HVM union b_info->u.hvm on HVM guests. The union access is not guarded, so this reads and sets the default even on non-HVM guests. Usually this doesn't matter as PV and PVH unions are smaller and zero-initialized, but the zero default will be re-written as a -1 boolean. Generally, it it could incorrectly set b_info->altp2m through aliased data.

Fixes 0291089f6ea8 ("xen: enable altp2m at create domain domctl")
Change-Id Ifaca3533dcce3f409c2efa292c7e96fba6371d9d
---
 tools/libs/light/libxl_x86.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tools/libs/light/libxl_x86.c +0 -0
index 0b1c2d3a96..b8f6663829 100644
@@ -821,10 +821,12 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
      * If the legacy field info->u.hvm.altp2m is set, activate altp2m.
      * Otherwise set altp2m based on the field info->altp2m.
      */

tools/libs/light/libxl_x86.c int libxl__arch_domain_build_info_setdefault(libxl__gc *gc, · line 829

Quoted 10 lines
tools/libs/light/libxl_x86.c +6 -4
-    libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
-    if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
-        libxl_defbool_val(b_info->u.hvm.altp2m))
-        b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
+        libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
+        if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
+            libxl_defbool_val(b_info->u.hvm.altp2m))
+            b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    }

I think at least the latter half of the comment wants to move inside the if() then.

Jan

#4 lore raw
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Jason Andryuk

Only access the HVM union b_info->u.hvm on HVM guests. The union access is not guarded, so this reads and sets the default even on non-HVM guests. Usually this doesn't matter as PV and PVH unions are smaller and zero-initialized, but the zero default will be re-written as a -1 boolean. Generally, it it could incorrectly set b_info->altp2m through aliased data.

Fixes 0291089f6ea8 ("xen: enable altp2m at create domain domctl")
Change-Id Ifaca3533dcce3f409c2efa292c7e96fba6371d9d
---
  tools/libs/light/libxl_x86.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
tools/libs/light/libxl_x86.c +1 -4
index 0b1c2d3a96..b8f6663829 100644
@@ -821,10 +821,12 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
       * If the legacy field info->u.hvm.altp2m is set, activate altp2m.
       * Otherwise set altp2m based on the field info->altp2m.
       */
-    libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
-    if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
-        libxl_defbool_val(b_info->u.hvm.altp2m))
-        b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
Quoted 8 lines

tools/libs/light/libxl_x86.c int libxl__arch_domain_build_info_setdefault(libxl__gc *gc, · line 829

Quoted 5 lines
tools/libs/light/libxl_x86.c +5 -0
+        libxl_defbool_setdefault(&b_info->u.hvm.altp2m, false);
+        if (b_info->altp2m == LIBXL_ALTP2M_MODE_DISABLED &&
+            libxl_defbool_val(b_info->u.hvm.altp2m))
+            b_info->altp2m = libxl_defbool_val(b_info->u.hvm.altp2m);
+    }

I think at least the latter half of the comment wants to move inside the if() then.

Yes. Actually, I think the whole comment should move inside the if().

Regards,
Jason