Skip to contentSkip to conversation

Non-truncating XENVER_* subops

PATCH In review Andrew Cooper 5 participants 26 messages Aug 15, 2023 – Dec 1, 2023

lore
Share & tools
lore
#0 lore raw

v3 cover Non-truncating XENVER_* subops

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/968728976
https://cirrus-ci.com/build/6741556116324352

Posting both parts, as there has been minor rebasing in all of them.

Andrew Cooper (10):
xen/version: Calculate xen_capabilities_info once at boot xen/version: Introduce non-truncating deterministically-signed XENVER_* subops xen/version: Fold build_id handling into xenver_varbuf_op() xen/version: Misc style fixes
tools/libxc: Move xc_version() out of xc_private.c into its own file
tools: Introduce a non-truncating xc_xenver_extraversion()
tools: Introduce a non-truncating xc_xenver_capabilities()
tools: Introduce a non-truncating xc_xenver_changeset()
tools: Introduce a non-truncating xc_xenver_cmdline()
tools: Introduce a xc_xenver_buildid() wrapper

 tools/include/xenctrl.h             |  10 ++
 tools/libs/ctrl/Makefile.common     |   1 +
 tools/libs/ctrl/xc_private.c        |  66 ---------
 tools/libs/ctrl/xc_private.h        |   7 -
 tools/libs/ctrl/xc_version.c        | 206 ++++++++++++++++++++++++++++
 tools/libs/light/libxl.c            |  61 +-------
 tools/ocaml/libs/xc/xenctrl_stubs.c |  46 ++++---
 xen/arch/arm/setup.c                |  20 +--
 xen/arch/x86/setup.c                |  31 ++---
 xen/common/kernel.c                 | 119 ++++++++++------
 xen/common/version.c                |   4 +-
 xen/include/public/version.h        |  68 ++++++++-
 xen/include/xen/hypercall.h         |   2 -
 xen/include/xen/version.h           |   2 +
 xen/include/xlat.lst                |   1 +
 xen/include/xsm/dummy.h             |   3 +
 xen/xsm/flask/hooks.c               |   4 +
 17 files changed, 425 insertions(+), 226 deletions(-)
 create mode 100644 tools/libs/ctrl/xc_version.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/10 xen/version: Calculate xen_capabilities_info once at boot

The arch_get_xen_caps() infrastructure is horribly inefficient for something that is constant after features have been resolved on boot.

Every instance used snprintf() to format constants into a string (which gets shorter when %d gets resolved!), and which get double buffered on the stack.

Switch to using string literals with the "3.0" inserted - these numbers haven't changed in 18 years (The Xen 3.0 release was Dec 5th 2005).

Use initcalls to format the data into xen_cap_info, which is deliberately not of type xen_capabilities_info_t because a 1k array is a silly overhead for storing a maximum of 77 chars (the x86 version) and isn't liable to need any more space in the forseeable future.

This speeds up the the XENVER_capabilities hypercall, but the purpose of the change is to allow us to introduce a better XENVER_* API that doesn't force the use of a 1k buffer on the stack.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>
CC: Henry Wang <[email protected]>

v3:
* CC new-arch folks so they're aware
v2:
* New

Moving to __initcall() also allows new architectures to not implement this API, and I'm going to recommend strongly that they dont. Its a very dubious way of signalling about 3 bits of info to the toolstack, and inefficient to use (the toolstack has to do string parsing on the result figure out if PV64/PV32/HVM is available).

---
 xen/arch/arm/setup.c        | 20 +++++++-------------
 xen/arch/x86/setup.c        | 31 ++++++++++---------------------
 xen/common/kernel.c         |  3 ++-
 xen/include/xen/hypercall.h |  2 --
 xen/include/xen/version.h   |  2 ++
 5 files changed, 21 insertions(+), 37 deletions(-)
xen/arch/arm/setup.c +7 -13
index 44ccea03ca14..3592c9ebc1cd 100644
@@ -27,6 +27,7 @@
 #include <xen/cpu.h>
 #include <xen/pfn.h>
 #include <xen/virtual_region.h>
+#include <xen/version.h>
 #include <xen/vmap.h>
 #include <xen/trace.h>
 #include <xen/libfdt/libfdt-xen.h>
@@ -1286,24 +1287,17 @@ void __init start_xen(unsigned long boot_phys_offset,
     switch_stack_and_jump(idle_vcpu[0]->arch.cpu_info, init_done);
 }
 
-void arch_get_xen_caps(xen_capabilities_info_t *info)
+static int __init init_xen_cap_info(void)
 {
-    /* Interface name is always xen-3.0-* for Xen-3.x. */
-    int major = 3, minor = 0;
-    char s[32];
-
-    (*info)[0] = '\0';
-
 #ifdef CONFIG_ARM_64
-    snprintf(s, sizeof(s), "xen-%d.%d-aarch64 ", major, minor);
-    safe_strcat(*info, s);
+    safe_strcat(xen_cap_info, "xen-3.0-aarch64 ");
 #endif
     if ( cpu_has_aarch32 )
-    {
-        snprintf(s, sizeof(s), "xen-%d.%d-armv7l ", major, minor);
-        safe_strcat(*info, s);
-    }
+        safe_strcat(xen_cap_info, "xen-3.0-armv7l ");
+
+    return 0;
 }
+__initcall(init_xen_cap_info);
 
 /*
  * Local variables:
xen/arch/x86/setup.c +10 -21
index 2bfc1fd00f8c..206f0c50a136 100644
@@ -2049,35 +2049,24 @@ void __init noreturn __start_xen(unsigned long mbi_p)
     unreachable();
 }
 
-void arch_get_xen_caps(xen_capabilities_info_t *info)
+static int __init cf_check init_xen_cap_info(void)
 {
-    /* Interface name is always xen-3.0-* for Xen-3.x. */
-    int major = 3, minor = 0;
-    char s[32];
-
-    (*info)[0] = '\0';
-
     if ( IS_ENABLED(CONFIG_PV) )
     {
-        snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
-        safe_strcat(*info, s);
+        safe_strcat(xen_cap_info, "xen-3.0-x86_64 ");
 
         if ( opt_pv32 )
-        {
-            snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
-            safe_strcat(*info, s);
-        }
+            safe_strcat(xen_cap_info, "xen-3.0-x86_32p ");
     }
     if ( hvm_enabled )
-    {
-        snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
-        safe_strcat(*info, s);
-        snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
-        safe_strcat(*info, s);
-        snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
-        safe_strcat(*info, s);
-    }
+        safe_strcat(xen_cap_info,
+                    "hvm-3.0-x86_32 "
+                    "hvm-3.0-x86_32p "
+                    "hvm-3.0-x86_64 ");
+
+    return 0;
 }
+__initcall(init_xen_cap_info);
 
 int __hwdom_init xen_in_range(unsigned long mfn)
 {
xen/common/kernel.c +2 -1
index 52aa28762782..f822480a8ef3 100644
@@ -30,6 +30,7 @@ enum system_state system_state = SYS_STATE_early_boot;
 
 static xen_commandline_t saved_cmdline;
 static const char __initconst opt_builtin_cmdline[] = CONFIG_CMDLINE;
+char __ro_after_init xen_cap_info[128];
 
 static int assign_integer_param(const struct kernel_param *param, uint64_t val)
 {
@@ -537,7 +538,7 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         memset(info, 0, sizeof(info));
         if ( !deny )
-            arch_get_xen_caps(&info);
+            safe_strcpy(info, xen_cap_info);
 
         if ( copy_to_guest(arg, info, ARRAY_SIZE(info)) )
             return -EFAULT;
xen/include/xen/hypercall.h +0 -2
index f307dfb59760..15b6be6ec818 100644
@@ -56,6 +56,4 @@ common_vcpu_op(int cmd,
     struct vcpu *v,
     XEN_GUEST_HANDLE_PARAM(void) arg);
 
-void arch_get_xen_caps(xen_capabilities_info_t *info);
-
 #endif /* __XEN_HYPERCALL_H__ */
xen/include/xen/version.h +2 -0
index 93c58773630c..4856ad1b446d 100644
@@ -19,6 +19,8 @@ const char *xen_deny(void);
 const char *xen_build_info(void);
 int xen_build_id(const void **p, unsigned int *len);
 
+extern char xen_cap_info[128];
+
 #ifdef BUILD_ID
 void xen_build_init(void);
 int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
Signature · 1 line
2.30.2
Reviewed-by on v3 1/10
#2 lore raw

Re: [PATCH v3 01/10] xen/version: Calculate xen_capabilities_info once at boot

Quoted 14 lines · Andrew Cooper

The arch_get_xen_caps() infrastructure is horribly inefficient for something that is constant after features have been resolved on boot.

Every instance used snprintf() to format constants into a string (which gets shorter when %d gets resolved!), and which get double buffered on the stack.

Switch to using string literals with the "3.0" inserted - these numbers haven't changed in 18 years (The Xen 3.0 release was Dec 5th 2005).

Use initcalls to format the data into xen_cap_info, which is deliberately not of type xen_capabilities_info_t because a 1k array is a silly overhead for storing a maximum of 77 chars (the x86 version) and isn't liable to need any more space in the forseeable future.

Quoted 5 lines

This speeds up the the XENVER_capabilities hypercall, but the purpose of the change is to allow us to introduce a better XENVER_* API that doesn't force the use of a 1k buffer on the stack.

Quoted 171 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>
CC: Henry Wang <[email protected]>

v3:
* CC new-arch folks so they're aware
v2:
* New

Moving to __initcall() also allows new architectures to not implement this API, and I'm going to recommend strongly that they dont. Its a very dubious way of signalling about 3 bits of info to the toolstack, and inefficient to use (the toolstack has to do string parsing on the result figure out if PV64/PV32/HVM is available).

---
 xen/arch/arm/setup.c        | 20 +++++++-------------
 xen/arch/x86/setup.c        | 31 ++++++++++---------------------
 xen/common/kernel.c         |  3 ++-
 xen/include/xen/hypercall.h |  2 --
 xen/include/xen/version.h   |  2 ++
 5 files changed, 21 insertions(+), 37 deletions(-)
xen/arch/arm/setup.c +7 -13
index 44ccea03ca14..3592c9ebc1cd 100644
@@ -27,6 +27,7 @@
 #include <xen/cpu.h>
 #include <xen/pfn.h>
 #include <xen/virtual_region.h>
+#include <xen/version.h>
 #include <xen/vmap.h>
 #include <xen/trace.h>
 #include <xen/libfdt/libfdt-xen.h>
@@ -1286,24 +1287,17 @@ void __init start_xen(unsigned long boot_phys_offset,
     switch_stack_and_jump(idle_vcpu[0]->arch.cpu_info, init_done);
 }
 
-void arch_get_xen_caps(xen_capabilities_info_t *info)
+static int __init init_xen_cap_info(void)
 {
-    /* Interface name is always xen-3.0-* for Xen-3.x. */
-    int major = 3, minor = 0;
-    char s[32];
-
-    (*info)[0] = '\0';
-
 #ifdef CONFIG_ARM_64
-    snprintf(s, sizeof(s), "xen-%d.%d-aarch64 ", major, minor);
-    safe_strcat(*info, s);
+    safe_strcat(xen_cap_info, "xen-3.0-aarch64 ");
 #endif
     if ( cpu_has_aarch32 )
-    {
-        snprintf(s, sizeof(s), "xen-%d.%d-armv7l ", major, minor);
-        safe_strcat(*info, s);
-    }
+        safe_strcat(xen_cap_info, "xen-3.0-armv7l ");
+
+    return 0;
 }
+__initcall(init_xen_cap_info);
 
 /*
  * Local variables:
xen/arch/x86/setup.c +10 -21
index 2bfc1fd00f8c..206f0c50a136 100644
@@ -2049,35 +2049,24 @@ void __init noreturn __start_xen(unsigned long mbi_p)
     unreachable();
 }
 
-void arch_get_xen_caps(xen_capabilities_info_t *info)
+static int __init cf_check init_xen_cap_info(void)
 {
-    /* Interface name is always xen-3.0-* for Xen-3.x. */
-    int major = 3, minor = 0;
-    char s[32];
-
-    (*info)[0] = '\0';
-
     if ( IS_ENABLED(CONFIG_PV) )
     {
-        snprintf(s, sizeof(s), "xen-%d.%d-x86_64 ", major, minor);
-        safe_strcat(*info, s);
+        safe_strcat(xen_cap_info, "xen-3.0-x86_64 ");
 
         if ( opt_pv32 )
-        {
-            snprintf(s, sizeof(s), "xen-%d.%d-x86_32p ", major, minor);
-            safe_strcat(*info, s);
-        }
+            safe_strcat(xen_cap_info, "xen-3.0-x86_32p ");
     }
     if ( hvm_enabled )
-    {
-        snprintf(s, sizeof(s), "hvm-%d.%d-x86_32 ", major, minor);
-        safe_strcat(*info, s);
-        snprintf(s, sizeof(s), "hvm-%d.%d-x86_32p ", major, minor);
-        safe_strcat(*info, s);
-        snprintf(s, sizeof(s), "hvm-%d.%d-x86_64 ", major, minor);
-        safe_strcat(*info, s);
-    }
+        safe_strcat(xen_cap_info,
+                    "hvm-3.0-x86_32 "
+                    "hvm-3.0-x86_32p "
+                    "hvm-3.0-x86_64 ");
+
+    return 0;
 }
+__initcall(init_xen_cap_info);
 
 int __hwdom_init xen_in_range(unsigned long mfn)
 {
xen/common/kernel.c +2 -1
index 52aa28762782..f822480a8ef3 100644
@@ -30,6 +30,7 @@ enum system_state system_state = SYS_STATE_early_boot;
 
 static xen_commandline_t saved_cmdline;
 static const char __initconst opt_builtin_cmdline[] = CONFIG_CMDLINE;
+char __ro_after_init xen_cap_info[128];
 
 static int assign_integer_param(const struct kernel_param *param, uint64_t val)
 {
@@ -537,7 +538,7 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         memset(info, 0, sizeof(info));
         if ( !deny )
-            arch_get_xen_caps(&info);
+            safe_strcpy(info, xen_cap_info);
 
         if ( copy_to_guest(arg, info, ARRAY_SIZE(info)) )
             return -EFAULT;
xen/include/xen/hypercall.h +0 -2
index f307dfb59760..15b6be6ec818 100644
@@ -56,6 +56,4 @@ common_vcpu_op(int cmd,
     struct vcpu *v,
     XEN_GUEST_HANDLE_PARAM(void) arg);
 
-void arch_get_xen_caps(xen_capabilities_info_t *info);
-
 #endif /* __XEN_HYPERCALL_H__ */
xen/include/xen/version.h +2 -0
index 93c58773630c..4856ad1b446d 100644
@@ -19,6 +19,8 @@ const char *xen_deny(void);
 const char *xen_build_info(void);
 int xen_build_id(const void **p, unsigned int *len);
 
+extern char xen_cap_info[128];
+
 #ifdef BUILD_ID
 void xen_build_init(void);
 int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
Signature · 1 line
2.30.2
Acked-by
#3 lore raw

Re: [PATCH v3 01/10] xen/version: Calculate xen_capabilities_info once at boot

Quoted 15 lines · Stefano Stabellini
Quoted 14 lines · Andrew Cooper

The arch_get_xen_caps() infrastructure is horribly inefficient for something that is constant after features have been resolved on boot.

Every instance used snprintf() to format constants into a string (which gets shorter when %d gets resolved!), and which get double buffered on the stack.

Switch to using string literals with the "3.0" inserted - these numbers haven't changed in 18 years (The Xen 3.0 release was Dec 5th 2005).

Use initcalls to format the data into xen_cap_info, which is deliberately not of type xen_capabilities_info_t because a 1k array is a silly overhead for storing a maximum of 77 chars (the x86 version) and isn't liable to need any more space in the forseeable future.

Quoted 7 lines
Quoted 5 lines

This speeds up the the XENVER_capabilities hypercall, but the purpose of the change is to allow us to introduce a better XENVER_* API that doesn't force the use of a 1k buffer on the stack.

albeit I still think your original concern regarding ...

Quoted 9 lines

xen/common/kernel.c long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 544

Quoted 9 lines
xen/common/kernel.c +1 -1
@@ -537,7 +538,7 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         memset(info, 0, sizeof(info));
         if ( !deny )
-            arch_get_xen_caps(&info);
+            safe_strcpy(info, xen_cap_info);
 
         if ( copy_to_guest(arg, info, ARRAY_SIZE(info)) )
             return -EFAULT;

... the unhelpful use of a stack variable here could do with addressing. But of course that can equally be done in a subsequent patch.

Jan

#4 lore raw

PATCH v3 2/10 xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

In reply to Andrew Cooper

Recently in XenServer, we have encountered problems caused by both XENVER_extraversion and XENVER_commandline having fixed bounds.

More than just the invariant size, the APIs/ABIs also broken by typedef-ing an array, and using an unqualified 'char' which has implementation-specific signed-ness.

Provide brand new ops, which are capable of expressing variable length strings, and mark the older ops as broken.

This fixes all issues around XENVER_extraversion being longer than 15 chars. Further work beyond just this API is needed to remove other assumptions about XENVER_commandline being 1023 chars long.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel De Graaf <[email protected]>
CC: Daniel Smith <[email protected]>
CC: Jason Andryuk <[email protected]>
CC: Henry Wang <[email protected]>

v3:
* Modify dummy.h's xsm_xen_version() in the same way as flask.
v2:
* Remove xen_capabilities_info_t from the stack now that arch_get_xen_caps() has gone.
* Use an arbitrary limit check much lower than INT_MAX.
* Use "buf" rather than "string" terminology.
* Expand the API comment.

Tested by forcing XENVER_extraversion to be 20 chars long, and confirming that an untruncated version can be obtained.

---
 xen/common/kernel.c          | 62 +++++++++++++++++++++++++++++++++++
 xen/include/public/version.h | 63 ++++++++++++++++++++++++++++++++++--
 xen/include/xlat.lst         |  1 +
 xen/include/xsm/dummy.h      |  3 ++
 xen/xsm/flask/hooks.c        |  4 +++
 5 files changed, 131 insertions(+), 2 deletions(-)
xen/common/kernel.c +62 -0
index f822480a8ef3..79c008c7ee5f 100644
@@ -24,6 +24,7 @@
 CHECK_build_id;
 CHECK_compile_info;
 CHECK_feature_info;
+CHECK_varbuf;
 #endif
 
 enum system_state system_state = SYS_STATE_early_boot;
@@ -498,6 +499,59 @@ static int __init cf_check param_init(void)
 __initcall(param_init);
 #endif
 
+static long xenver_varbuf_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
+{
+    struct xen_varbuf user_str;
+    const char *str = NULL;
+    size_t sz;
+
+    switch ( cmd )
+    {
+    case XENVER_extraversion2:
+        str = xen_extra_version();
+        break;
+
+    case XENVER_changeset2:
+        str = xen_changeset();
+        break;
+
+    case XENVER_commandline2:
+        str = saved_cmdline;
+        break;
+
+    case XENVER_capabilities2:
+        str = xen_cap_info;
+        break;
+
+    default:
+        ASSERT_UNREACHABLE();
+        return -ENODATA;
+    }
+
+    sz = strlen(str);
+
+    if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
+        return -E2BIG;
+
+    if ( guest_handle_is_null(arg) ) /* Length request */
+        return sz;
+
+    if ( copy_from_guest(&user_str, arg, 1) )
+        return -EFAULT;
+
+    if ( user_str.len == 0 )
+        return -EINVAL;
+
+    if ( sz > user_str.len )
+        return -ENOBUFS;
+
+    if ( copy_to_guest_offset(arg, offsetof(struct xen_varbuf, buf),
+                              str, sz) )
+        return -EFAULT;
+
+    return sz;
+}
+
 long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     bool_t deny = !!xsm_xen_version(XSM_OTHER, cmd);
@@ -711,6 +765,14 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         return sz;
     }
+
+    case XENVER_extraversion2:
+    case XENVER_capabilities2:
+    case XENVER_changeset2:
+    case XENVER_commandline2:
+        if ( deny )
+            return -EPERM;
+        return xenver_varbuf_op(cmd, arg);
     }
 
     return -ENOSYS;
xen/include/public/version.h +61 -2

95 lines not on this page · raw message

xen/include/xlat.lst +1 -0
index 9c41948514bf..a61ba85ed0ca 100644
@@ -173,6 +173,7 @@
 ?	build_id                        version.h
 ?	compile_info                    version.h
 ?	feature_info                    version.h
+?	varbuf                          version.h
 ?	xenoprof_init			xenoprof.h
 ?	xenoprof_passive		xenoprof.h
 ?	flask_access			xsm/flask_op.h
xen/include/xsm/dummy.h +3 -0
index 8671af1ba4d3..a4a920f74e6e 100644
@@ -828,9 +828,12 @@ static XSM_INLINE int cf_check xsm_xen_version(XSM_DEFAULT_ARG uint32_t op)
         block_speculation();
         return 0;
     case XENVER_extraversion:
+    case XENVER_extraversion2:
     case XENVER_compile_info:
     case XENVER_capabilities:
+    case XENVER_capabilities2:
     case XENVER_changeset:
+    case XENVER_changeset2:
     case XENVER_pagesize:
     case XENVER_guest_handle:
         /* These MUST always be accessible to any guest by default. */
xen/xsm/flask/hooks.c +4 -0
index 78225f68c15c..a671dcd0322e 100644
@@ -1777,15 +1777,18 @@ static int cf_check flask_xen_version(uint32_t op)
         /* These sub-ops ignore the permission checks and return data. */
         return 0;
     case XENVER_extraversion:
+    case XENVER_extraversion2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_EXTRAVERSION, NULL);
     case XENVER_compile_info:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_COMPILE_INFO, NULL);
     case XENVER_capabilities:
+    case XENVER_capabilities2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_CAPABILITIES, NULL);
     case XENVER_changeset:
+    case XENVER_changeset2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_CHANGESET, NULL);
     case XENVER_pagesize:
@@ -1795,6 +1798,7 @@ static int cf_check flask_xen_version(uint32_t op)
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_GUEST_HANDLE, NULL);
     case XENVER_commandline:
+    case XENVER_commandline2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_COMMANDLINE, NULL);
     case XENVER_build_id:
Signature · 1 line
2.30.2
Acked-by on v3 2/10
#5 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

Quoted 10 lines · Andrew Cooper

Recently in XenServer, we have encountered problems caused by both XENVER_extraversion and XENVER_commandline having fixed bounds.

More than just the invariant size, the APIs/ABIs also broken by typedef-ing an array, and using an unqualified 'char' which has implementation-specific signed-ness.

Provide brand new ops, which are capable of expressing variable length strings, and mark the older ops as broken.

Quoted 6 lines

This fixes all issues around XENVER_extraversion being longer than 15 chars. Further work beyond just this API is needed to remove other assumptions about XENVER_commandline being 1023 chars long.

#6 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

In reply to Andrew Cooper
Quoted 85 lines · Andrew Cooper

Recently in XenServer, we have encountered problems caused by both XENVER_extraversion and XENVER_commandline having fixed bounds.

More than just the invariant size, the APIs/ABIs also broken by typedef-ing an array, and using an unqualified 'char' which has implementation-specific signed-ness.

Provide brand new ops, which are capable of expressing variable length strings, and mark the older ops as broken.

This fixes all issues around XENVER_extraversion being longer than 15 chars. Further work beyond just this API is needed to remove other assumptions about XENVER_commandline being 1023 chars long.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel De Graaf <[email protected]>
CC: Daniel Smith <[email protected]>
CC: Jason Andryuk <[email protected]>
CC: Henry Wang <[email protected]>

v3:
* Modify dummy.h's xsm_xen_version() in the same way as flask.
v2:
* Remove xen_capabilities_info_t from the stack now that arch_get_xen_caps() has gone.
* Use an arbitrary limit check much lower than INT_MAX.
* Use "buf" rather than "string" terminology.
* Expand the API comment.

Tested by forcing XENVER_extraversion to be 20 chars long, and confirming that an untruncated version can be obtained.

---
 xen/common/kernel.c          | 62 +++++++++++++++++++++++++++++++++++
 xen/include/public/version.h | 63 ++++++++++++++++++++++++++++++++++--
 xen/include/xlat.lst         |  1 +
 xen/include/xsm/dummy.h      |  3 ++
 xen/xsm/flask/hooks.c        |  4 +++
 5 files changed, 131 insertions(+), 2 deletions(-)
xen/common/kernel.c +24 -0
index f822480a8ef3..79c008c7ee5f 100644
@@ -24,6 +24,7 @@
 CHECK_build_id;
 CHECK_compile_info;
 CHECK_feature_info;
+CHECK_varbuf;
 #endif
 
 enum system_state system_state = SYS_STATE_early_boot;
@@ -498,6 +499,59 @@ static int __init cf_check param_init(void)
 __initcall(param_init);
 #endif
 
+static long xenver_varbuf_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
+{
+    struct xen_varbuf user_str;
+    const char *str = NULL;
+    size_t sz;
+
+    switch ( cmd )
+    {
+    case XENVER_extraversion2:
+        str = xen_extra_version();
+        break;
+
+    case XENVER_changeset2:
+        str = xen_changeset();
+        break;
+
+    case XENVER_commandline2:
+        str = saved_cmdline;
+        break;
+
+    case XENVER_capabilities2:
+        str = xen_cap_info;
+        break;

xen/common/kernel.c static int __init cf_check param_init(void) · line 534

Quoted 10 lines
xen/common/kernel.c +10 -0
+
+    default:
+        ASSERT_UNREACHABLE();
+        return -ENODATA;
+    }
+
+    sz = strlen(str);
+
+    if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
+        return -E2BIG;

Realistically do we want this buffer to cross page boundaries? We could use KB(4) here or even KB(4)-4 (size of len).

Quoted 42 lines
xen/common/kernel.c +27 -0
+    if ( guest_handle_is_null(arg) ) /* Length request */
+        return sz;
+
+    if ( copy_from_guest(&user_str, arg, 1) )
+        return -EFAULT;
+
+    if ( user_str.len == 0 )
+        return -EINVAL;
+
+    if ( sz > user_str.len )
+        return -ENOBUFS;
+
+    if ( copy_to_guest_offset(arg, offsetof(struct xen_varbuf, buf),
+                              str, sz) )
+        return -EFAULT;
+
+    return sz;
+}
+
 long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     bool_t deny = !!xsm_xen_version(XSM_OTHER, cmd);
@@ -711,6 +765,14 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         return sz;
     }
+
+    case XENVER_extraversion2:
+    case XENVER_capabilities2:
+    case XENVER_changeset2:
+    case XENVER_commandline2:
+        if ( deny )
+            return -EPERM;
+        return xenver_varbuf_op(cmd, arg);
     }
 
     return -ENOSYS;
xen/include/public/version.h +0 -0
index cbc4ef7a46e6..0dd6bbcb43cc 100644

xen/include/public/version.h line 25

Quoted 8 lines
xen/include/public/version.h +4 -1
@@ -19,12 +19,20 @@
 /* arg == NULL; returns major:minor (16:16). */
 #define XENVER_version      0
 
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.

Like Jan and Julien I also don't like the word "broken" especially without explanation of why it is broken next to it.

Instead, I would say:

"XENVER_extraversion is deprecated. Please use XENVER_extraversion2."

If you want to convey the message that the API has problems, then I would say:

"XENVER_extraversion might cause truncation. Please use XENVER_extraversion2."

Or even:

"XENVER_extraversion has problems. Please use XENVER_extraversion2."

xen/include/public/version.h line 34

Quoted 10 lines
xen/include/public/version.h +5 -1
+ */
 #define XENVER_extraversion 1
 typedef char xen_extraversion_t[16];
 #define XEN_EXTRAVERSION_LEN (sizeof(xen_extraversion_t))
 
-/* arg == xen_compile_info_t. */
+/*
+ * arg == xen_compile_info_t.
+ *
+ * This API/ABI is broken and truncates data.

"XENVER_compile_info is deprecated and can truncate data."

xen/include/public/version.h struct xen_compile_info { · line 48

Quoted 12 lines
xen/include/public/version.h +5 -0
+ */
 #define XENVER_compile_info 2
 struct xen_compile_info {
     char compiler[64];
@@ -34,10 +42,20 @@ struct xen_compile_info {
 };
 typedef struct xen_compile_info xen_compile_info_t;
 
+/*
+ * arg == xen_capabilities_info_t.
+ *
+ * This API/ABI is broken.  Use XENVER_capabilities2 where possible.

"XENVER_capabilities is deprecated. Please use XENVER_capabilities2."

xen/include/public/version.h struct xen_compile_info { · line 57

Quoted 9 lines
xen/include/public/version.h +5 -0
+ */
 #define XENVER_capabilities 3
 typedef char xen_capabilities_info_t[1024];
 #define XEN_CAPABILITIES_INFO_LEN (sizeof(xen_capabilities_info_t))
 
+/*
+ * arg == xen_changeset_info_t.
+ *
+ * This API/ABI is broken.  Use XENVER_changeset2 where possible.

"XENVER_changeset is deprecated. Please use XENVER_changeset2."

xen/include/public/version.h typedef struct xen_feature_info xen_feature_info_t; · line 119

Quoted 12 lines
xen/include/public/version.h +5 -0
+ */
 #define XENVER_changeset 4
 typedef char xen_changeset_info_t[64];
 #define XEN_CHANGESET_INFO_LEN (sizeof(xen_changeset_info_t))
@@ -95,6 +113,11 @@ typedef struct xen_feature_info xen_feature_info_t;
  */
 #define XENVER_guest_handle 8
 
+/*
+ * arg == xen_commandline_t.
+ *
+ * This API/ABI is broken.  Use XENVER_commandline2 where possible.

"XENVER_commandline is deprecated. Please use XENVER_commandline2."

Quoted 5 lines
xen/include/public/version.h +1 -0
+ */
 #define XENVER_commandline 9
 typedef char xen_commandline_t[1024];
 

xen/include/public/version.h struct xen_build_id { · line 141

Quoted 9 lines
xen/include/public/version.h +6 -0
@@ -110,6 +133,42 @@ struct xen_build_id {
 };
 typedef struct xen_build_id xen_build_id_t;
 
+/*
+ * Container for an arbitrary variable length buffer.
+ */
+struct xen_varbuf {
+    uint32_t len;                          /* IN:  size of buf[] in bytes. */
+    unsigned char buf[XEN_FLEX_ARRAY_DIM]; /* OUT: requested data.         */

I realize that you just copied struct xen_build_id but I recall from MISRA C training that we should use plain "char" for strings for good reasons, not "unsigned char"?

If this is meant to be generic data, I think we should use uint8_t instead.

Quoted 33 lines
xen/include/public/version.h +30 -0
+};
+typedef struct xen_varbuf xen_varbuf_t;
+
+/*
+ * arg == xen_varbuf_t
+ *
+ * Equivalent to the original ops, but with a non-truncating API/ABI.
+ *
+ * These hypercalls can fail for a number of reasons.  All callers must handle
+ * -XEN_xxx return values appropriately.
+ *
+ * Passing arg == NULL is a request for size, which will be signalled with a
+ * non-negative return value.  Note: a return size of 0 may be legitimate for
+ * the requested subop.
+ *
+ * Otherwise, the input xen_varbuf_t provides the size of the following
+ * buffer.  Xen will fill the buffer, and return the number of bytes written
+ * (e.g. if the input buffer was longer than necessary).
+ *
+ * Some subops may return binary data.  Some subops may be expected to return
+ * textural data.  These are returned without a NUL terminator, and while the
+ * contents is expected to be ASCII/UTF-8, Xen makes no guarentees to this
+ * effect.  e.g. Xen has no control over the formatting used for the command
+ * line.
+ */
+#define XENVER_extraversion2 11
+#define XENVER_capabilities2 12
+#define XENVER_changeset2    13
+#define XENVER_commandline2  14
+
 #endif /* __XEN_PUBLIC_VERSION_H__ */
 
 /*
#7 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

Quoted 8 lines · Stefano Stabellini

xen/common/kernel.c static int __init cf_check param_init(void) · line 503

Quoted 6 lines · Andrew Cooper
xen/common/kernel.c +4 -0
@@ -498,6 +499,59 @@ static int __init cf_check param_init(void)
 
+    sz = strlen(str);
+
+    if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
+        return -E2BIG;

Realistically do we want this buffer to cross page boundaries?

A 1-byte answer can cross a page boundary, even if the hypercall argument is correctly aligned (and even that is unspecified in the Xen ABI).

But importantly, this series is also prep work to fixing the cmdline limit.  1k is already causing problems, and 64k is a whole lot less bad than 4k when we enter such corner cases.

Quoted 8 lines
Quoted 8 lines
xen/include/public/version.h +0 -0
index cbc4ef7a46e6..0dd6bbcb43cc 100644
@@ -19,12 +19,20 @@
 /* arg == NULL; returns major:minor (16:16). */
 #define XENVER_version      0
 
Quoted 7 lines

xen/include/public/version.h line 25

Quoted 5 lines
xen/include/public/version.h +4 -1
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.

Like Jan and Julien I also don't like the word "broken" especially without explanation of why it is broken next to it.

The word "broken" is an accurate and appropriate word in the English language to describe the situation.  I'm sorry you don't like it, but unless any of you can articulate why you think it is inappropriate phraseology, the complaint is unactionable.

Specifically ...

Quoted 3 lines

Instead, I would say:

"XENVER_extraversion is deprecated. Please use XENVER_extraversion2."

... depreciated is misleading.

It would be acceptable for a case where we'd introduced a foo2 to add a new feature to the interface, but we're being forced to make the new interface to fix two bugs and a mis-feature in existing interface.

Quoted 8 lines

If you want to convey the message that the API has problems, then I would say:

"XENVER_extraversion might cause truncation. Please use XENVER_extraversion2."

Or even:

"XENVER_extraversion has problems. Please use XENVER_extraversion2."

If "broken" is too nondescript, then "might" is bad too and "has problems" is out of the question.

There is a partial description of the ABI problems in the comment block beside the new ops.  I could be persuaded to extend it to be a full list of the ABI breakages.

These header files are a succinct technical reference for proficient programmers to interact with Xen.  They are not a tutorial on writing C, nor are they appropriate places to sentences of no useful value.

Through this series, I have done the hard work of updating the commonly-used interfaces such that downstreams can stop working around real problems caused by real errors in these APIs.  For everyone else re-syncing the headers, it is important that the message come across as an instruction and not a suggestion ...

... People will probably ignore it irrespective, but that's then firmly on them, and not on Xen trying to downplay problems in the public interface.

Quoted 8 lines
Quoted 8 lines
xen/include/public/version.h +1 -0
+ */
 #define XENVER_commandline 9
 typedef char xen_commandline_t[1024];
 
@@ -110,6 +133,42 @@ struct xen_build_id {
 };
 typedef struct xen_build_id xen_build_id_t;
 
Quoted 7 lines

xen/include/public/version.h struct xen_build_id { · line 141

Quoted 6 lines
xen/include/public/version.h +6 -0
+/*
+ * Container for an arbitrary variable length buffer.
+ */
+struct xen_varbuf {
+    uint32_t len;                          /* IN:  size of buf[] in bytes. */
+    unsigned char buf[XEN_FLEX_ARRAY_DIM]; /* OUT: requested data.         */

I realize that you just copied struct xen_build_id

No - it was originally an ambiguously-signed char in v1.  It became unsigned through review.

But being unsigned char is relevant to the non-ABI-changingness of later patches in the series.

Quoted 3 lines

but I recall from
MISRA C training that we should use plain "char" for strings for good reasons, not "unsigned char"?

I don't recall anything to that effect, nor can I see anything obvious when scanning through the standard.

MISRA can't plausibly prohibit the use of char for arbitrary data.  It's the one and only thing the C spec states is safe for the task.

~Andrew

#8 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

Quoted 16 lines · Andrew Cooper
Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · Andrew Cooper
xen/include/public/version.h +0 -0
@@ -19,12 +19,20 @@
 /* arg == NULL; returns major:minor (16:16). */
 #define XENVER_version      0
 
Quoted 7 lines

xen/include/public/version.h line 25

Quoted 5 lines
xen/include/public/version.h +4 -1
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.

Like Jan and Julien I also don't like the word "broken" especially without explanation of why it is broken next to it.

Quoted 4 lines

The word "broken" is an accurate and appropriate word in the English language to describe the situation.  I'm sorry you don't like it, but unless any of you can articulate why you think it is inappropriate phraseology, the complaint is unactionable.

That's simply not true. A wording change is very well possible, and what you regard as "broken" may not be viewed as such by others. IOW while "broken" may be "an accurate and appropriate word in the English language to describe" your perspective of the situation, it may not be for others. Take the extraversion case: It was clear from the beginning that it means to limit what to use as XEN_EXTRAVERSION. No bug, just a limitation. Similarly for the command line: Besides the full command line (supposedly) being of use for informational purposes only anyway (and the full one can be taken from the log), no-one could ever predict at that time that we'd accumulate such a massive amount of command line options. Again a limitation (and yes, I understand that the information may disappear from the ring buffer, so "xl dmesg" after the system was up for a while may not have that data anymore, yet of course an agent in the system could make the boot messaged persistent), but not really a bug.

Quoted 11 lines

Specifically ...

Quoted 3 lines

Instead, I would say:

"XENVER_extraversion is deprecated. Please use XENVER_extraversion2."

... depreciated is misleading.

It would be acceptable for a case where we'd introduced a foo2 to add a new feature to the interface, but we're being forced to make the new interface to fix two bugs and a mis-feature in existing interface.

See above. The existing interfaces are sufficient for the common case. The extended versions therefore are an enhancement, allowing to call the original ones deprecated, but not really anything more.

Quoted 17 lines
Quoted 8 lines

If you want to convey the message that the API has problems, then I would say:

"XENVER_extraversion might cause truncation. Please use XENVER_extraversion2."

Or even:

"XENVER_extraversion has problems. Please use XENVER_extraversion2."

If "broken" is too nondescript, then "might" is bad too and "has problems" is out of the question.

There is a partial description of the ABI problems in the comment block beside the new ops.  I could be persuaded to extend it to be a full list of the ABI breakages.

Quoted 7 lines

These header files are a succinct technical reference for proficient programmers to interact with Xen.  They are not a tutorial on writing C, nor are they appropriate places to sentences of no useful value.

Through this series, I have done the hard work of updating the commonly-used interfaces such that downstreams can stop working around real problems caused by real errors in these APIs.

Or, as I view it, working around limitations of the original ABI. Hence a new feature, not so much a bug fix.

I'm afraid the main reason why this series hasn't made it in yet is your apparent unwillingness to accept that other people may look at things differently than you do, resulting in you not being willing to make a compromise on the chosen wording. While you may view this as others blocking your work, I don't think it realistically can be called this way.

Jan

#9 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

In reply to Andrew Cooper
Quoted 24 lines · Andrew Cooper
Quoted 10 lines · Stefano Stabellini
Quoted 8 lines · Andrew Cooper
xen/include/public/version.h +0 -0
index cbc4ef7a46e6..0dd6bbcb43cc 100644
@@ -19,12 +19,20 @@
 /* arg == NULL; returns major:minor (16:16). */
 #define XENVER_version      0
 
Quoted 7 lines

xen/include/public/version.h line 25

Quoted 5 lines
xen/include/public/version.h +4 -1
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.

Like Jan and Julien I also don't like the word "broken" especially without explanation of why it is broken next to it.

The word "broken" is an accurate and appropriate word in the English language to describe the situation. I'm sorry you don't like it, but unless any of you can articulate why you think it is inappropriate phraseology, the complaint is unactionable.

Quoted 8 lines

Specifically ...

Quoted 3 lines

Instead, I would say:

"XENVER_extraversion is deprecated. Please use XENVER_extraversion2."

... depreciated is misleading.

Deprecated means, "It is the official position of the developers of the project that for the moment, you *can* use it, but you *shouldn't*"; it also implies that support for it might go away at some point. The fact that we're saying "you shouldn't use it" by itself implies that it is lacking somehow. It's a factual statement that gives you useful information.

Neither "broken" nor "has problems" actually tell you anything above "deprecated", other than the opinion of the developer writing the documentation; and they are both (to different levels) emotionally charged. You don't deprecate things unless there's something wrong with them. In this particular case, I don't think anyone has an emotional attachment to the existing hypercall, so nobody is going to be insulted; but it's a good habit to avoid it. (See [1] for more about this.)

[1] http://xenbits.xenproject.org/governance/communication-practice.html , the "Avoid inflammatory and negatively charged language" section

If I have a backlog of a million things to do, how do I prioritize switching to and testing extraversion2? The situation as I understand it
is: "If it works for you now with the current value you're fine, but it may break later when you change it, in a way that's obvious". In that situation, you can safely put off fixing it until your build breaks. If, on the other hand, the situation is "It may randomly work or not work with any given build", or "It may seem to work for you now but is actually failing in a not-very-obvious way", then you probably need to prioritize fixing it.

Saying "May cause truncation" gives you some the information you need to know. "Will silently truncate past N characters" gives you even more.

We should at very least say it's deprecated. If we can summarize the issues briefly, then that would be helpful.

-George

#10 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

---- On Wed, 16 Aug 2023 05:55:07 -0400 George Dunlap wrote ---

Quoted 31 lines · On Wed, Aug 16, 2023 at 3:58 AM Andrew Cooper [email protected]> wrote:
Quoted 17 lines · Stefano Stabellini

xen/include/public/version.h line 25

Quoted 13 lines · Andrew Cooper
xen/include/public/version.h +0 -0
index cbc4ef7a46e6..0dd6bbcb43cc 100644
@@ -19,12 +19,20 @@
   /* arg == NULL; returns major:minor (16:16). */
   #define XENVER_version      0

xen/include/public/version.h +4 -1
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.

Like Jan and Julien I also don't like the word "broken" especially without explanation of why it is broken next to it.

The word "broken" is an accurate and appropriate word in the English language to describe the situation.  I'm sorry you don't like it, but unless any of you can articulate why you think it is inappropriate phraseology, the complaint is unactionable.

Specifically ...

Quoted 2 lines

Instead, I would say:

Quoted 7 lines
Quoted 1 line

"XENVER_extraversion is deprecated. Please use XENVER_extraversion2."

... depreciated is misleading.

Deprecated means, "It is the official position of the developers of the project that for the moment, you *can* use it, but you *shouldn't*"; it also implies that support for it might go away at some point.  The fact that we're saying "you shouldn't use it" by itself implies that it is lacking somehow.  It's a factual statement that gives you useful information.

Neither "broken" nor "has problems" actually tell you anything above "deprecated", other than the opinion of the developer writing the documentation; and they are both (to different levels) emotionally charged.  You don't deprecate things unless there's something wrong with them.  In this particular case, I don't think anyone has an emotional attachment to the existing hypercall, so nobody is going to be insulted; but it's a good habit to avoid it.  (See [1] for more about this.)

With all due respect George but deprecated and broken are fundamentally different. Deprecation means that an interface is being retired in the future for any number of reasons and that it can continue to be used until its retirement without risk of unintended consequences. Labeling an interface as broken, which has been acceptable to do so in other much larger communities, is a stronger sentiment that the interface should stop being used immediately because it can lead to unintended consequences, not because it will be retired in some distant future.

Quoted 7 lines

[1] http://xenbits.xenproject.org/governance/communication-practice.html , the "Avoid inflammatory and negatively charged language" section

If I have a backlog of a million things to do, how do I prioritize switching to and testing extraversion2?  The situation as I understand it is: "If it works for you now with the current value you're fine, but it may break later when you change it, in a way that's obvious".  In that situation, you can safely put off fixing it until your build breaks.  If, on the other hand, the situation is "It may randomly work or not work with any given build", or "It may seem to work for you now but is actually failing in a not-very-obvious way", then you probably need to prioritize fixing it.

Saying "May cause truncation" gives you some the information you need to know. "Will silently truncate past N characters" gives you even more.
We should at very least say it's deprecated.  If we can summarize the issues briefly, then that would be helpful.

In general I would concur here, except that these are externally consumed files for which the consuming projects just need to be told don't use this interface. The why is not so important to them as it is to the Xen community. But for the latter, any one working on Xen code can do a git blame on the comment line to find the originating commit and read the commit message for the details as to why.

v/r,
dps

#11 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

Quoted 31 lines · Daniel Smith
Quoted 31 lines · On Wed, Aug 16, 2023 at 3:58 AM Andrew Cooper [email protected]> wrote:
Quoted 17 lines · Stefano Stabellini

xen/include/public/version.h line 25

Quoted 13 lines · Andrew Cooper
xen/include/public/version.h +0 -0
index cbc4ef7a46e6..0dd6bbcb43cc 100644
@@ -19,12 +19,20 @@
   /* arg == NULL; returns major:minor (16:16). */
   #define XENVER_version      0

xen/include/public/version.h +4 -1
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.

Like Jan and Julien I also don't like the word "broken" especially without explanation of why it is broken next to it.

The word "broken" is an accurate and appropriate word in the English language to describe the situation.  I'm sorry you don't like it, but unless any of you can articulate why you think it is inappropriate phraseology, the complaint is unactionable.

Specifically ...

Quoted 3 lines

Instead, I would say:

"XENVER_extraversion is deprecated. Please use XENVER_extraversion2."

Quoted 7 lines
Quoted 5 lines

... depreciated is misleading.

Deprecated means, "It is the official position of the developers of the project that for the moment, you *can* use it, but you *shouldn't*"; it also implies that support for it might go away at some point.  The fact that we're saying "you shouldn't use it" by itself implies that it is lacking somehow.  It's a factual statement that gives you useful information.

Neither "broken" nor "has problems" actually tell you anything above "deprecated", other than the opinion of the developer writing the documentation; and they are both (to different levels) emotionally charged.  You don't deprecate things unless there's something wrong with them.  In this particular case, I don't think anyone has an emotional attachment to the existing hypercall, so nobody is going to be insulted; but it's a good habit to avoid it.  (See [1] for more about this.)

With all due respect George but deprecated and broken are fundamentally different. Deprecation means that an interface is being retired in the future for any number of reasons and that it can continue to be used until its retirement without risk of unintended consequences. Labeling an interface as broken, which has been acceptable to do so in other much larger communities, is a stronger sentiment that the interface should stop being used immediately because it can lead to unintended consequences, not because it will be retired in some distant future.

No matter what the Merriam Webster would say for the word "deprecated" and "broken", each individual "absorbs" each word and their meanings differently.

There are 6 people involved in this discussion including Andrew, and 4 out of 6 didn't react well to the usage of the word "broken" in this context. For sure in Andrew's mind "broken" triggers the right meaning and expectations straight away. But for Julien, Jan, George and me it did not.

We cannot run a representative survey of the developers population to know which exact word triggers the most appropriate reaction in this case. Even if we could, it would not be worth the effort.

Instead, I suggest to just go with the majority interpretation among the participant in this thread and drop "broken" in favor of one of the alternatives. Such as something along these lines:

"XENVER_extraversion is deprecated and causes truncation. Please use XENVER_extraversion2.

#12 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

In reply to George Dunlap
Quoted 51 lines · George Dunlap

On Wed, Aug 16, 2023 at 3:58 AM Andrew Cooper <[email protected]> wrote:

      On 16/08/2023 1:19 am, Stefano Stabellini wrote:
      > On Tue, 15 Aug 2023, Andrew Cooper wrote:

      >> diff --git a/xen/include/public/version.h b/xen/include/public/version.h
      >> index cbc4ef7a46e6..0dd6bbcb43cc 100644
      >> --- a/xen/include/public/version.h
      >> +++ b/xen/include/public/version.h
      >> @@ -19,12 +19,20 @@
      >>  /* arg == NULL; returns major:minor (16:16). */
      >>  #define XENVER_version      0
      >> 
      >> -/* arg == xen_extraversion_t. */
      >> +/*
      >> + * arg == xen_extraversion_t.
      >> + *
      >> + * This API/ABI is broken.  Use XENVER_extraversion2 where possible.
      > Like Jan and Julien I also don't like the word "broken" especially
      > without explanation of why it is broken next to it.

      The word "broken" is an accurate and appropriate word in the English
      language to describe the situation.  I'm sorry you don't like it, but
      unless any of you can articulate why you think it is inappropriate
      phraseology, the complaint is unactionable.

      Specifically ...

      > Instead, I would say:
      >
      > "XENVER_extraversion is deprecated. Please use XENVER_extraversion2."

      ... depreciated is misleading.

Deprecated means, "It is the official position of the developers of the project that for the moment, you *can* use it, but you *shouldn't*"; it also implies that support for it might go away at some point.  The fact that we're saying "you shouldn't use it" by itself implies that it is lacking somehow.  It's a factual statement that gives you useful information.

Neither "broken" nor "has problems" actually tell you anything above "deprecated", other than the opinion of the developer writing the documentation; and they are both (to different levels) emotionally charged.  You don't deprecate things unless there's something wrong with them.  In this particular case, I don't think anyone has an emotional attachment to the existing hypercall, so nobody is going to be insulted; but it's a good habit to avoid it.  (See [1] for more about this.)

[1] http://xenbits.xenproject.org/governance/communication-practice.html , the "Avoid inflammatory and negatively charged language" section

If I have a backlog of a million things to do, how do I prioritize switching to and testing extraversion2?  The situation as I understand it is: "If it works for you now with the current value you're fine, but it may break later when you change it, in a way that's obvious". In that situation, you can safely put off fixing it until your build breaks.  If, on the other hand, the situation is "It may randomly work or not work with any given build", or "It may seem to work for you now but is actually failing in a not-very-obvious way", then you probably need to prioritize fixing it.

Quoted 4 lines

Saying "May cause truncation" gives you some the information you need to know. "Will silently truncate past N characters" gives you even more.

We should at very least say it's deprecated.  If we can summarize the issues briefly, then that would be helpful.

I strongly agree with this: we should say it is deprecated and if we can summarize the issues briefly that's even better.

#13 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper
Quoted 8 lines · Stefano Stabellini

xen/common/kernel.c static int __init cf_check param_init(void) · line 503

Quoted 6 lines · Andrew Cooper
xen/common/kernel.c +4 -0
@@ -498,6 +499,59 @@ static int __init cf_check param_init(void)
 
+    sz = strlen(str);
+
+    if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
+        return -E2BIG;

Realistically do we want this buffer to cross page boundaries?

Quoted 6 lines

A 1-byte answer can cross a page boundary, even if the hypercall argument is correctly aligned (and even that is unspecified in the Xen ABI).

But importantly, this series is also prep work to fixing the cmdline limit.  1k is already causing problems, and 64k is a whole lot less bad than 4k when we enter such corner cases.

OK. It is just that 64K seems *a lot* in this context. But if you have reasons to believe that 64K is a good number for this check then OK.

Quoted 26 lines
Quoted 8 lines
Quoted 8 lines
xen/include/public/version.h +1 -0
+ */
 #define XENVER_commandline 9
 typedef char xen_commandline_t[1024];
 
@@ -110,6 +133,42 @@ struct xen_build_id {
 };
 typedef struct xen_build_id xen_build_id_t;
 
Quoted 7 lines

xen/include/public/version.h struct xen_build_id { · line 141

Quoted 6 lines
xen/include/public/version.h +6 -0
+/*
+ * Container for an arbitrary variable length buffer.
+ */
+struct xen_varbuf {
+    uint32_t len;                          /* IN:  size of buf[] in bytes. */
+    unsigned char buf[XEN_FLEX_ARRAY_DIM]; /* OUT: requested data.         */

I realize that you just copied struct xen_build_id

No - it was originally an ambiguously-signed char in v1.  It became unsigned through review.

But being unsigned char is relevant to the non-ABI-changingness of later patches in the series.

Quoted 3 lines

but I recall from
MISRA C training that we should use plain "char" for strings for good reasons, not "unsigned char"?

Quoted 5 lines

I don't recall anything to that effect, nor can I see anything obvious when scanning through the standard.

MISRA can't plausibly prohibit the use of char for arbitrary data.  It's the one and only thing the C spec states is safe for the task.

After this morning's call with Roberto, I take this comment back. unsigned char is OK from MISRA POV.

#14 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Stefano Stabellini

xen/common/kernel.c static int __init cf_check param_init(void) · line 503

Quoted 6 lines · Andrew Cooper
xen/common/kernel.c +4 -0
@@ -498,6 +499,59 @@ static int __init cf_check param_init(void)
 
+    sz = strlen(str);
+
+    if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
+        return -E2BIG;

Realistically do we want this buffer to cross page boundaries?

Quoted 8 lines
Quoted 6 lines

A 1-byte answer can cross a page boundary, even if the hypercall argument is correctly aligned (and even that is unspecified in the Xen ABI).

But importantly, this series is also prep work to fixing the cmdline limit.  1k is already causing problems, and 64k is a whole lot less bad than 4k when we enter such corner cases.

OK. It is just that 64K seems *a lot* in this context. But if you have reasons to believe that 64K is a good number for this check then OK.

Remember that this is all Xen cross-checking (or reporting to the user) the length of Xen-owned data.

If the system was only booted only 10 bytes of cmdline, this will only be 10.

If OTOH there really was 5k of data, handing E2BIG to the user when they ask for it is slightly rude but is infinitely better than just truncating it and returning success.

64k is just the upper bounds sanity check for "get a developer to look at what's going on because this probably isn't what you thought it was".  Eventually when someone does find a legitimate use for a cmdline longer than 64k, it will need upping, someone (else) can figure out the long-running-ness of this scenario.

~Andrew

#15 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

In reply to Andrew Cooper

Hi everyone following this thread,

please see:
https://marc.info/?l=xen-devel&m=170135718323946
https://cryptpad.fr/form/#/2/form/view/7ByH95Vd7KiDOvN4wjV5iUGlMuZbkVdwk7cYpZdluWo/

For a vote on the usage of the word "broken"

Quoted 299 lines · Andrew Cooper

Recently in XenServer, we have encountered problems caused by both XENVER_extraversion and XENVER_commandline having fixed bounds.

More than just the invariant size, the APIs/ABIs also broken by typedef-ing an array, and using an unqualified 'char' which has implementation-specific signed-ness.

Provide brand new ops, which are capable of expressing variable length strings, and mark the older ops as broken.

This fixes all issues around XENVER_extraversion being longer than 15 chars. Further work beyond just this API is needed to remove other assumptions about XENVER_commandline being 1023 chars long.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel De Graaf <[email protected]>
CC: Daniel Smith <[email protected]>
CC: Jason Andryuk <[email protected]>
CC: Henry Wang <[email protected]>

v3:
* Modify dummy.h's xsm_xen_version() in the same way as flask.
v2:
* Remove xen_capabilities_info_t from the stack now that arch_get_xen_caps() has gone.
* Use an arbitrary limit check much lower than INT_MAX.
* Use "buf" rather than "string" terminology.
* Expand the API comment.

Tested by forcing XENVER_extraversion to be 20 chars long, and confirming that an untruncated version can be obtained.

---
 xen/common/kernel.c          | 62 +++++++++++++++++++++++++++++++++++
 xen/include/public/version.h | 63 ++++++++++++++++++++++++++++++++++--
 xen/include/xlat.lst         |  1 +
 xen/include/xsm/dummy.h      |  3 ++
 xen/xsm/flask/hooks.c        |  4 +++
 5 files changed, 131 insertions(+), 2 deletions(-)
xen/common/kernel.c +62 -0
index f822480a8ef3..79c008c7ee5f 100644
@@ -24,6 +24,7 @@
 CHECK_build_id;
 CHECK_compile_info;
 CHECK_feature_info;
+CHECK_varbuf;
 #endif
 
 enum system_state system_state = SYS_STATE_early_boot;
@@ -498,6 +499,59 @@ static int __init cf_check param_init(void)
 __initcall(param_init);
 #endif
 
+static long xenver_varbuf_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
+{
+    struct xen_varbuf user_str;
+    const char *str = NULL;
+    size_t sz;
+
+    switch ( cmd )
+    {
+    case XENVER_extraversion2:
+        str = xen_extra_version();
+        break;
+
+    case XENVER_changeset2:
+        str = xen_changeset();
+        break;
+
+    case XENVER_commandline2:
+        str = saved_cmdline;
+        break;
+
+    case XENVER_capabilities2:
+        str = xen_cap_info;
+        break;
+
+    default:
+        ASSERT_UNREACHABLE();
+        return -ENODATA;
+    }
+
+    sz = strlen(str);
+
+    if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
+        return -E2BIG;
+
+    if ( guest_handle_is_null(arg) ) /* Length request */
+        return sz;
+
+    if ( copy_from_guest(&user_str, arg, 1) )
+        return -EFAULT;
+
+    if ( user_str.len == 0 )
+        return -EINVAL;
+
+    if ( sz > user_str.len )
+        return -ENOBUFS;
+
+    if ( copy_to_guest_offset(arg, offsetof(struct xen_varbuf, buf),
+                              str, sz) )
+        return -EFAULT;
+
+    return sz;
+}
+
 long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     bool_t deny = !!xsm_xen_version(XSM_OTHER, cmd);
@@ -711,6 +765,14 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         return sz;
     }
+
+    case XENVER_extraversion2:
+    case XENVER_capabilities2:
+    case XENVER_changeset2:
+    case XENVER_commandline2:
+        if ( deny )
+            return -EPERM;
+        return xenver_varbuf_op(cmd, arg);
     }
 
     return -ENOSYS;
xen/include/public/version.h +61 -2
index cbc4ef7a46e6..0dd6bbcb43cc 100644
@@ -19,12 +19,20 @@
 /* arg == NULL; returns major:minor (16:16). */
 #define XENVER_version      0
 
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.
+ */
 #define XENVER_extraversion 1
 typedef char xen_extraversion_t[16];
 #define XEN_EXTRAVERSION_LEN (sizeof(xen_extraversion_t))
 
-/* arg == xen_compile_info_t. */
+/*
+ * arg == xen_compile_info_t.
+ *
+ * This API/ABI is broken and truncates data.
+ */
 #define XENVER_compile_info 2
 struct xen_compile_info {
     char compiler[64];
@@ -34,10 +42,20 @@ struct xen_compile_info {
 };
 typedef struct xen_compile_info xen_compile_info_t;
 
+/*
+ * arg == xen_capabilities_info_t.
+ *
+ * This API/ABI is broken.  Use XENVER_capabilities2 where possible.
+ */
 #define XENVER_capabilities 3
 typedef char xen_capabilities_info_t[1024];
 #define XEN_CAPABILITIES_INFO_LEN (sizeof(xen_capabilities_info_t))
 
+/*
+ * arg == xen_changeset_info_t.
+ *
+ * This API/ABI is broken.  Use XENVER_changeset2 where possible.
+ */
 #define XENVER_changeset 4
 typedef char xen_changeset_info_t[64];
 #define XEN_CHANGESET_INFO_LEN (sizeof(xen_changeset_info_t))
@@ -95,6 +113,11 @@ typedef struct xen_feature_info xen_feature_info_t;
  */
 #define XENVER_guest_handle 8
 
+/*
+ * arg == xen_commandline_t.
+ *
+ * This API/ABI is broken.  Use XENVER_commandline2 where possible.
+ */
 #define XENVER_commandline 9
 typedef char xen_commandline_t[1024];
 
@@ -110,6 +133,42 @@ struct xen_build_id {
 };
 typedef struct xen_build_id xen_build_id_t;
 
+/*
+ * Container for an arbitrary variable length buffer.
+ */
+struct xen_varbuf {
+    uint32_t len;                          /* IN:  size of buf[] in bytes. */
+    unsigned char buf[XEN_FLEX_ARRAY_DIM]; /* OUT: requested data.         */
+};
+typedef struct xen_varbuf xen_varbuf_t;
+
+/*
+ * arg == xen_varbuf_t
+ *
+ * Equivalent to the original ops, but with a non-truncating API/ABI.
+ *
+ * These hypercalls can fail for a number of reasons.  All callers must handle
+ * -XEN_xxx return values appropriately.
+ *
+ * Passing arg == NULL is a request for size, which will be signalled with a
+ * non-negative return value.  Note: a return size of 0 may be legitimate for
+ * the requested subop.
+ *
+ * Otherwise, the input xen_varbuf_t provides the size of the following
+ * buffer.  Xen will fill the buffer, and return the number of bytes written
+ * (e.g. if the input buffer was longer than necessary).
+ *
+ * Some subops may return binary data.  Some subops may be expected to return
+ * textural data.  These are returned without a NUL terminator, and while the
+ * contents is expected to be ASCII/UTF-8, Xen makes no guarentees to this
+ * effect.  e.g. Xen has no control over the formatting used for the command
+ * line.
+ */
+#define XENVER_extraversion2 11
+#define XENVER_capabilities2 12
+#define XENVER_changeset2    13
+#define XENVER_commandline2  14
+
 #endif /* __XEN_PUBLIC_VERSION_H__ */
 
 /*
xen/include/xlat.lst +1 -0
index 9c41948514bf..a61ba85ed0ca 100644
@@ -173,6 +173,7 @@
 ?	build_id                        version.h
 ?	compile_info                    version.h
 ?	feature_info                    version.h
+?	varbuf                          version.h
 ?	xenoprof_init			xenoprof.h
 ?	xenoprof_passive		xenoprof.h
 ?	flask_access			xsm/flask_op.h
xen/include/xsm/dummy.h +3 -0
index 8671af1ba4d3..a4a920f74e6e 100644
@@ -828,9 +828,12 @@ static XSM_INLINE int cf_check xsm_xen_version(XSM_DEFAULT_ARG uint32_t op)
         block_speculation();
         return 0;
     case XENVER_extraversion:
+    case XENVER_extraversion2:
     case XENVER_compile_info:
     case XENVER_capabilities:
+    case XENVER_capabilities2:
     case XENVER_changeset:
+    case XENVER_changeset2:
     case XENVER_pagesize:
     case XENVER_guest_handle:
         /* These MUST always be accessible to any guest by default. */
xen/xsm/flask/hooks.c +4 -0
index 78225f68c15c..a671dcd0322e 100644
@@ -1777,15 +1777,18 @@ static int cf_check flask_xen_version(uint32_t op)
         /* These sub-ops ignore the permission checks and return data. */
         return 0;
     case XENVER_extraversion:
+    case XENVER_extraversion2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_EXTRAVERSION, NULL);
     case XENVER_compile_info:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_COMPILE_INFO, NULL);
     case XENVER_capabilities:
+    case XENVER_capabilities2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_CAPABILITIES, NULL);
     case XENVER_changeset:
+    case XENVER_changeset2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_CHANGESET, NULL);
     case XENVER_pagesize:
@@ -1795,6 +1798,7 @@ static int cf_check flask_xen_version(uint32_t op)
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_GUEST_HANDLE, NULL);
     case XENVER_commandline:
+    case XENVER_commandline2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_COMMANDLINE, NULL);
     case XENVER_build_id:
Signature · 1 line
2.30.2
#16 lore raw

Re: [PATCH v3 02/10] xen/version: Introduce non-truncating deterministically-signed XENVER_* subops

Quoted 7 lines · Stefano Stabellini

Hi everyone following this thread,

please see:
https://marc.info/?l=xen-devel&m=170135718323946
https://cryptpad.fr/form/#/2/form/view/7ByH95Vd7KiDOvN4wjV5iUGlMuZbkVdwk7cYpZdluWo/

For a vote on the usage of the word "broken"

So I did vote before becoming aware of this context. I would have voted differently if I had known that this _alone_ is the context. Yet then I'm also not going to change my vote, because as written _there_ it is intended to be more general. If the wording of the text describing what to vote on changed, things would be different.

Jan

Quoted 300 lines · Andrew Cooper
Quoted 299 lines

Recently in XenServer, we have encountered problems caused by both XENVER_extraversion and XENVER_commandline having fixed bounds.

More than just the invariant size, the APIs/ABIs also broken by typedef-ing an array, and using an unqualified 'char' which has implementation-specific signed-ness.

Provide brand new ops, which are capable of expressing variable length strings, and mark the older ops as broken.

This fixes all issues around XENVER_extraversion being longer than 15 chars. Further work beyond just this API is needed to remove other assumptions about XENVER_commandline being 1023 chars long.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel De Graaf <[email protected]>
CC: Daniel Smith <[email protected]>
CC: Jason Andryuk <[email protected]>
CC: Henry Wang <[email protected]>

v3:
* Modify dummy.h's xsm_xen_version() in the same way as flask.
v2:
* Remove xen_capabilities_info_t from the stack now that arch_get_xen_caps() has gone.
* Use an arbitrary limit check much lower than INT_MAX.
* Use "buf" rather than "string" terminology.
* Expand the API comment.

Tested by forcing XENVER_extraversion to be 20 chars long, and confirming that an untruncated version can be obtained.

---
 xen/common/kernel.c          | 62 +++++++++++++++++++++++++++++++++++
 xen/include/public/version.h | 63 ++++++++++++++++++++++++++++++++++--
 xen/include/xlat.lst         |  1 +
 xen/include/xsm/dummy.h      |  3 ++
 xen/xsm/flask/hooks.c        |  4 +++
 5 files changed, 131 insertions(+), 2 deletions(-)
xen/common/kernel.c +62 -0
index f822480a8ef3..79c008c7ee5f 100644
@@ -24,6 +24,7 @@
 CHECK_build_id;
 CHECK_compile_info;
 CHECK_feature_info;
+CHECK_varbuf;
 #endif
 
 enum system_state system_state = SYS_STATE_early_boot;
@@ -498,6 +499,59 @@ static int __init cf_check param_init(void)
 __initcall(param_init);
 #endif
 
+static long xenver_varbuf_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
+{
+    struct xen_varbuf user_str;
+    const char *str = NULL;
+    size_t sz;
+
+    switch ( cmd )
+    {
+    case XENVER_extraversion2:
+        str = xen_extra_version();
+        break;
+
+    case XENVER_changeset2:
+        str = xen_changeset();
+        break;
+
+    case XENVER_commandline2:
+        str = saved_cmdline;
+        break;
+
+    case XENVER_capabilities2:
+        str = xen_cap_info;
+        break;
+
+    default:
+        ASSERT_UNREACHABLE();
+        return -ENODATA;
+    }
+
+    sz = strlen(str);
+
+    if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
+        return -E2BIG;
+
+    if ( guest_handle_is_null(arg) ) /* Length request */
+        return sz;
+
+    if ( copy_from_guest(&user_str, arg, 1) )
+        return -EFAULT;
+
+    if ( user_str.len == 0 )
+        return -EINVAL;
+
+    if ( sz > user_str.len )
+        return -ENOBUFS;
+
+    if ( copy_to_guest_offset(arg, offsetof(struct xen_varbuf, buf),
+                              str, sz) )
+        return -EFAULT;
+
+    return sz;
+}
+
 long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     bool_t deny = !!xsm_xen_version(XSM_OTHER, cmd);
@@ -711,6 +765,14 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         return sz;
     }
+
+    case XENVER_extraversion2:
+    case XENVER_capabilities2:
+    case XENVER_changeset2:
+    case XENVER_commandline2:
+        if ( deny )
+            return -EPERM;
+        return xenver_varbuf_op(cmd, arg);
     }
 
     return -ENOSYS;
xen/include/public/version.h +61 -2
index cbc4ef7a46e6..0dd6bbcb43cc 100644
@@ -19,12 +19,20 @@
 /* arg == NULL; returns major:minor (16:16). */
 #define XENVER_version      0
 
-/* arg == xen_extraversion_t. */
+/*
+ * arg == xen_extraversion_t.
+ *
+ * This API/ABI is broken.  Use XENVER_extraversion2 where possible.
+ */
 #define XENVER_extraversion 1
 typedef char xen_extraversion_t[16];
 #define XEN_EXTRAVERSION_LEN (sizeof(xen_extraversion_t))
 
-/* arg == xen_compile_info_t. */
+/*
+ * arg == xen_compile_info_t.
+ *
+ * This API/ABI is broken and truncates data.
+ */
 #define XENVER_compile_info 2
 struct xen_compile_info {
     char compiler[64];
@@ -34,10 +42,20 @@ struct xen_compile_info {
 };
 typedef struct xen_compile_info xen_compile_info_t;
 
+/*
+ * arg == xen_capabilities_info_t.
+ *
+ * This API/ABI is broken.  Use XENVER_capabilities2 where possible.
+ */
 #define XENVER_capabilities 3
 typedef char xen_capabilities_info_t[1024];
 #define XEN_CAPABILITIES_INFO_LEN (sizeof(xen_capabilities_info_t))
 
+/*
+ * arg == xen_changeset_info_t.
+ *
+ * This API/ABI is broken.  Use XENVER_changeset2 where possible.
+ */
 #define XENVER_changeset 4
 typedef char xen_changeset_info_t[64];
 #define XEN_CHANGESET_INFO_LEN (sizeof(xen_changeset_info_t))
@@ -95,6 +113,11 @@ typedef struct xen_feature_info xen_feature_info_t;
  */
 #define XENVER_guest_handle 8
 
+/*
+ * arg == xen_commandline_t.
+ *
+ * This API/ABI is broken.  Use XENVER_commandline2 where possible.
+ */
 #define XENVER_commandline 9
 typedef char xen_commandline_t[1024];
 
@@ -110,6 +133,42 @@ struct xen_build_id {
 };
 typedef struct xen_build_id xen_build_id_t;
 
+/*
+ * Container for an arbitrary variable length buffer.
+ */
+struct xen_varbuf {
+    uint32_t len;                          /* IN:  size of buf[] in bytes. */
+    unsigned char buf[XEN_FLEX_ARRAY_DIM]; /* OUT: requested data.         */
+};
+typedef struct xen_varbuf xen_varbuf_t;
+
+/*
+ * arg == xen_varbuf_t
+ *
+ * Equivalent to the original ops, but with a non-truncating API/ABI.
+ *
+ * These hypercalls can fail for a number of reasons.  All callers must handle
+ * -XEN_xxx return values appropriately.
+ *
+ * Passing arg == NULL is a request for size, which will be signalled with a
+ * non-negative return value.  Note: a return size of 0 may be legitimate for
+ * the requested subop.
+ *
+ * Otherwise, the input xen_varbuf_t provides the size of the following
+ * buffer.  Xen will fill the buffer, and return the number of bytes written
+ * (e.g. if the input buffer was longer than necessary).
+ *
+ * Some subops may return binary data.  Some subops may be expected to return
+ * textural data.  These are returned without a NUL terminator, and while the
+ * contents is expected to be ASCII/UTF-8, Xen makes no guarentees to this
+ * effect.  e.g. Xen has no control over the formatting used for the command
+ * line.
+ */
+#define XENVER_extraversion2 11
+#define XENVER_capabilities2 12
+#define XENVER_changeset2    13
+#define XENVER_commandline2  14
+
 #endif /* __XEN_PUBLIC_VERSION_H__ */
 
 /*
xen/include/xlat.lst +1 -0
index 9c41948514bf..a61ba85ed0ca 100644
@@ -173,6 +173,7 @@
 ?	build_id                        version.h
 ?	compile_info                    version.h
 ?	feature_info                    version.h
+?	varbuf                          version.h
 ?	xenoprof_init			xenoprof.h
 ?	xenoprof_passive		xenoprof.h
 ?	flask_access			xsm/flask_op.h
xen/include/xsm/dummy.h +3 -0
index 8671af1ba4d3..a4a920f74e6e 100644
@@ -828,9 +828,12 @@ static XSM_INLINE int cf_check xsm_xen_version(XSM_DEFAULT_ARG uint32_t op)
         block_speculation();
         return 0;
     case XENVER_extraversion:
+    case XENVER_extraversion2:
     case XENVER_compile_info:
     case XENVER_capabilities:
+    case XENVER_capabilities2:
     case XENVER_changeset:
+    case XENVER_changeset2:
     case XENVER_pagesize:
     case XENVER_guest_handle:
         /* These MUST always be accessible to any guest by default. */
xen/xsm/flask/hooks.c +4 -0
index 78225f68c15c..a671dcd0322e 100644
@@ -1777,15 +1777,18 @@ static int cf_check flask_xen_version(uint32_t op)
         /* These sub-ops ignore the permission checks and return data. */
         return 0;
     case XENVER_extraversion:
+    case XENVER_extraversion2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_EXTRAVERSION, NULL);
     case XENVER_compile_info:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_COMPILE_INFO, NULL);
     case XENVER_capabilities:
+    case XENVER_capabilities2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_CAPABILITIES, NULL);
     case XENVER_changeset:
+    case XENVER_changeset2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_CHANGESET, NULL);
     case XENVER_pagesize:
@@ -1795,6 +1798,7 @@ static int cf_check flask_xen_version(uint32_t op)
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_GUEST_HANDLE, NULL);
     case XENVER_commandline:
+    case XENVER_commandline2:
         return avc_has_perm(dsid, SECINITSID_XEN, SECCLASS_VERSION,
                             VERSION__XEN_COMMANDLINE, NULL);
     case XENVER_build_id:
Signature · 1 line
2.30.2
#17 lore raw

PATCH v3 3/10 xen/version: Fold build_id handling into xenver_varbuf_op()

In reply to Andrew Cooper

struct xen_build_id and struct xen_varbuf are identical from an ABI point of view, so XENVER_build_id can reuse xenver_varbuf_op() rather than having it's own almost identical copy of the logic.

No functional change.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Henry Wang <[email protected]>

v2:
* New

---
 xen/common/kernel.c          | 49 +++++++++++-------------------------
 xen/include/public/version.h |  5 +++-
 2 files changed, 18 insertions(+), 36 deletions(-)
xen/common/kernel.c +14 -35
index 79c008c7ee5f..4a051be1cbe0 100644
@@ -504,9 +504,22 @@ static long xenver_varbuf_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     struct xen_varbuf user_str;
     const char *str = NULL;
     size_t sz;
+    int rc;
 
     switch ( cmd )
     {
+    case XENVER_build_id:
+    {
+        unsigned int local_sz;
+
+        rc = xen_build_id((const void **)&str, &local_sz);
+        if ( rc )
+            return rc;
+
+        sz = local_sz;
+        goto have_len;
+    }
+
     case XENVER_extraversion2:
         str = xen_extra_version();
         break;
@@ -530,6 +543,7 @@ static long xenver_varbuf_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
     sz = strlen(str);
 
+ have_len:
     if ( sz > KB(64) ) /* Arbitrary limit.  Avoid long-running operations. */
         return -E2BIG;
 
@@ -731,41 +745,6 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     }
 
     case XENVER_build_id:
-    {
-        xen_build_id_t build_id;
-        unsigned int sz;
-        int rc;
-        const void *p;
-
-        if ( deny )
-            return -EPERM;
-
-        /* Only return size. */
-        if ( !guest_handle_is_null(arg) )
-        {
-            if ( copy_from_guest(&build_id, arg, 1) )
-                return -EFAULT;
-
-            if ( build_id.len == 0 )
-                return -EINVAL;
-        }
-
-        rc = xen_build_id(&p, &sz);
-        if ( rc )
-            return rc;
-
-        if ( guest_handle_is_null(arg) )
-            return sz;
-
-        if ( sz > build_id.len )
-            return -ENOBUFS;
-
-        if ( copy_to_guest_offset(arg, offsetof(xen_build_id_t, buf), p, sz) )
-            return -EFAULT;
-
-        return sz;
-    }
-
     case XENVER_extraversion2:
     case XENVER_capabilities2:
     case XENVER_changeset2:
xen/include/public/version.h +4 -1
index 0dd6bbcb43cc..1022604daa41 100644
@@ -124,8 +124,10 @@ typedef char xen_commandline_t[1024];
 /*
  * Return value is the number of bytes written, or XEN_Exx on error.
  * Calling with empty parameter returns the size of build_id.
+ *
+ * Note: structure only kept for backwards compatibility.  Xen operates in
+ * terms of xen_varbuf_t.
  */
-#define XENVER_build_id 10
 struct xen_build_id {
         uint32_t        len; /* IN: size of buf[]. */
         unsigned char   buf[XEN_FLEX_ARRAY_DIM];
@@ -164,6 +166,7 @@ typedef struct xen_varbuf xen_varbuf_t;
  * effect.  e.g. Xen has no control over the formatting used for the command
  * line.
  */
+#define XENVER_build_id      10
 #define XENVER_extraversion2 11
 #define XENVER_capabilities2 12
 #define XENVER_changeset2    13
Signature · 1 line
2.30.2
#18 lore raw

PATCH v3 4/10 xen/version: Misc style fixes

In reply to Andrew Cooper

No functional change.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel De Graaf <[email protected]>
CC: Daniel Smith <[email protected]>
CC: Jason Andryuk <[email protected]>
CC: Henry Wang <[email protected]>

---
 xen/common/kernel.c  | 11 +++++------
 xen/common/version.c |  4 ++--
 2 files changed, 7 insertions(+), 8 deletions(-)
xen/common/kernel.c +5 -6
index 4a051be1cbe0..24e778a44f7b 100644
@@ -1,6 +1,6 @@
 /******************************************************************************
  * kernel.c
- * 
+ *
  * Copyright (c) 2002-2005 K A Fraser
  */
 
@@ -568,7 +568,7 @@ static long xenver_varbuf_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
 long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
-    bool_t deny = !!xsm_xen_version(XSM_OTHER, cmd);
+    bool deny = xsm_xen_version(XSM_OTHER, cmd);
 
     switch ( cmd )
     {
@@ -612,7 +612,7 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
             return -EFAULT;
         return 0;
     }
-    
+
     case XENVER_platform_parameters:
     {
         const struct vcpu *curr = current;
@@ -651,9 +651,8 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         }
 
         return 0;
-        
     }
-    
+
     case XENVER_changeset:
     {
         xen_changeset_info_t chgset;
@@ -680,7 +679,7 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
             if ( VM_ASSIST(d, pae_extended_cr3) )
                 fi.submap |= (1U << XENFEAT_pae_pgdir_above_4gb);
             if ( paging_mode_translate(d) )
-                fi.submap |= 
+                fi.submap |=
                     (1U << XENFEAT_writable_page_tables) |
                     (1U << XENFEAT_auto_translated_physmap);
             if ( is_hardware_domain(d) )
xen/common/version.c +2 -2
index d32013520863..8e738672debe 100644
@@ -209,11 +209,11 @@ void __init xen_build_init(void)
             }
         }
     }
-#endif
+#endif /* CONFIG_X86 */
     if ( !rc )
         printk(XENLOG_INFO "build-id: %*phN\n", build_id_len, build_id_p);
 }
-#endif
+#endif /* BUILD_ID */
 /*
  * Local variables:
  * mode: C
Signature · 1 line
2.30.2
#20 lore raw

PATCH v3 5/10 tools/libxc: Move xc_version() out of xc_private.c into its own file

In reply to Andrew Cooper

kexec-tools uses xc_version(), meaning that it is not a private API. As we're going to extend the functionality substantially, move it to its own file.

---
CC: Wei Liu <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/libs/ctrl/Makefile.common |  1 +
 tools/libs/ctrl/xc_private.c    | 66 --------------------------
 tools/libs/ctrl/xc_private.h    |  7 ---
 tools/libs/ctrl/xc_version.c    | 83 +++++++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+), 73 deletions(-)
 create mode 100644 tools/libs/ctrl/xc_version.c
tools/libs/ctrl/Makefile.common +1 -0
index 0a09c28fd369..4e3680c123f6 100644
@@ -16,6 +16,7 @@ OBJS-y       += xc_pm.o
 OBJS-y       += xc_cpu_hotplug.o
 OBJS-y       += xc_vm_event.o
 OBJS-y       += xc_vmtrace.o
+OBJS-y       += xc_version.o
 OBJS-y       += xc_monitor.o
 OBJS-y       += xc_mem_paging.o
 OBJS-y       += xc_mem_access.o
tools/libs/ctrl/xc_private.c +0 -66

72 lines not on this page · raw message

tools/libs/ctrl/xc_private.h +0 -7
index 8faabaea67f3..754da8840979 100644
@@ -216,13 +216,6 @@ void xc__hypercall_buffer_cache_release(xc_interface *xch);
  * Hypercall interfaces.
  */
 
-static inline int do_xen_version(xc_interface *xch, int cmd, xc_hypercall_buffer_t *dest)
-{
-    DECLARE_HYPERCALL_BUFFER_ARGUMENT(dest);
-    return xencall2(xch->xcall, __HYPERVISOR_xen_version,
-                    cmd, HYPERCALL_BUFFER_AS_ARG(dest));
-}
-
 static inline int do_physdev_op(xc_interface *xch, int cmd, void *op, size_t len)
 {
     int ret = -1;
tools/libs/ctrl/xc_version.c new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.30.2
#21 lore raw

PATCH v3 6/10 tools: Introduce a non-truncating xc_xenver_extraversion()

In reply to Andrew Cooper

... which uses XENVER_extraversion2.

In order to do sensibly, use manual hypercall buffer handling. Not only does this avoid an extra bounce buffer (we need to strip the xen_varbuf_t header anyway), it's also shorter and easlier to follow.

Update libxl and the ocaml stubs to match. No API/ABI change in either.

With this change made, xl info can now correctly access a >15 char extraversion:

  # xl info xen_version
  4.18-unstable+REALLY LONG EXTRAVERSION

---
CC: Wei Liu <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Christian Lindig <[email protected]>
CC: David Scott <[email protected]>
CC: Edwin Torok <[email protected]>
CC: Rob Hoes <[email protected]>

Note: There is a marginal risk for a memory leak in the ocaml bindings, but it turns out we have the same bug elsewhere and fixing that is going to be rather complicated.

---
 tools/include/xenctrl.h             |  6 +++
 tools/libs/ctrl/xc_version.c        | 75 +++++++++++++++++++++++++++++
 tools/libs/light/libxl.c            |  4 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c |  9 ++--
 4 files changed, 87 insertions(+), 7 deletions(-)
tools/include/xenctrl.h +6 -0
index faec1dd82453..b839196bf4c4 100644
@@ -1593,6 +1593,12 @@ long xc_memory_op(xc_interface *xch, unsigned int cmd, void *arg, size_t len);
 
 int xc_version(xc_interface *xch, int cmd, void *arg);
 
+/*
+ * Wrappers around XENVER_* subops.  Callers must pass the returned pointer to
+ * free().
+ */
+char *xc_xenver_extraversion(xc_interface *xch);
+
 int xc_flask_op(xc_interface *xch, xen_flask_op_t *op);
 
 /*
tools/libs/ctrl/xc_version.c +75 -0

78 lines not on this page · raw message

tools/libs/light/libxl.c +1 -3
index 175d6dde0b80..f91f7271d5bf 100644
@@ -582,7 +582,6 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
 {
     GC_INIT(ctx);
     union {
-        xen_extraversion_t xen_extra;
         xen_compile_info_t xen_cc;
         xen_changeset_info_t xen_chgset;
         xen_capabilities_info_t xen_caps;
@@ -601,8 +600,7 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
     info->xen_version_major = xen_version >> 16;
     info->xen_version_minor = xen_version & 0xFF;
 
-    xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
-    info->xen_version_extra = libxl__strdup(NOGC, u.xen_extra);
+    info->xen_version_extra = xc_xenver_extraversion(ctx->xch);
 
     xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
     info->compiler = libxl__strdup(NOGC, u.xen_cc.compiler);
tools/ocaml/libs/xc/xenctrl_stubs.c +5 -4
index 3703f48c74d1..379a7935a145 100644
@@ -980,9 +980,8 @@ CAMLprim value stub_xc_version_version(value xch_val)
 	CAMLparam1(xch_val);
 	CAMLlocal1(result);
 	xc_interface *xch = xch_of_val(xch_val);
-	xen_extraversion_t extra;
+	char *extra;
 	long packed;
-	int retval;
 
 	caml_enter_blocking_section();
 	packed = xc_version(xch, XENVER_version, NULL);
@@ -992,10 +991,10 @@ CAMLprim value stub_xc_version_version(value xch_val)
 		failwith_xc(xch);
 
 	caml_enter_blocking_section();
-	retval = xc_version(xch, XENVER_extraversion, &extra);
+	extra = xc_xenver_extraversion(xch);
 	caml_leave_blocking_section();
 
-	if (retval)
+	if (!extra)
 		failwith_xc(xch);
 
 	result = caml_alloc_tuple(3);
@@ -1004,6 +1003,8 @@ CAMLprim value stub_xc_version_version(value xch_val)
 	Store_field(result, 1, Val_int(packed & 0xffff));
 	Store_field(result, 2, caml_copy_string(extra));
 
+	free(extra);
+
 	CAMLreturn(result);
 }
 
Signature · 1 line
2.30.2
#22 lore raw

PATCH v3 7/10 tools: Introduce a non-truncating xc_xenver_capabilities()

In reply to Andrew Cooper

Update libxl and the ocaml stubs to match. No API/ABI change in either.

---
CC: Wei Liu <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Christian Lindig <[email protected]>
CC: David Scott <[email protected]>
CC: Edwin Torok <[email protected]>
CC: Rob Hoes <[email protected]>

---
 tools/include/xenctrl.h             |  1 +
 tools/libs/ctrl/xc_version.c        |  5 +++++
 tools/libs/light/libxl.c            |  4 +---
 tools/ocaml/libs/xc/xenctrl_stubs.c | 18 ++++++++++++++++--
 4 files changed, 23 insertions(+), 5 deletions(-)
tools/include/xenctrl.h +1 -0
index b839196bf4c4..9bd5eed7397a 100644
@@ -1598,6 +1598,7 @@ int xc_version(xc_interface *xch, int cmd, void *arg);
  * free().
  */
 char *xc_xenver_extraversion(xc_interface *xch);
+char *xc_xenver_capabilities(xc_interface *xch);
 
 int xc_flask_op(xc_interface *xch, xen_flask_op_t *op);
 
tools/libs/ctrl/xc_version.c +5 -0
index 2c14474feec5..512302a393ea 100644
@@ -156,3 +156,8 @@ char *xc_xenver_extraversion(xc_interface *xch)
 {
     return varbuf_simple_string(xch, XENVER_extraversion2);
 }
+
+char *xc_xenver_capabilities(xc_interface *xch)
+{
+    return varbuf_simple_string(xch, XENVER_capabilities2);
+}
tools/libs/light/libxl.c +1 -3
index f91f7271d5bf..ae6c5e04cd59 100644
@@ -584,7 +584,6 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
     union {
         xen_compile_info_t xen_cc;
         xen_changeset_info_t xen_chgset;
-        xen_capabilities_info_t xen_caps;
         xen_platform_parameters_t p_parms;
         xen_commandline_t xen_commandline;
         xen_build_id_t build_id;
@@ -608,8 +607,7 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
     info->compile_domain = libxl__strdup(NOGC, u.xen_cc.compile_domain);
     info->compile_date = libxl__strdup(NOGC, u.xen_cc.compile_date);
 
-    xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
-    info->capabilities = libxl__strdup(NOGC, u.xen_caps);
+    info->capabilities = xc_xenver_capabilities(ctx->xch);
 
     xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
     info->changeset = libxl__strdup(NOGC, u.xen_chgset);
tools/ocaml/libs/xc/xenctrl_stubs.c +16 -2
index 379a7935a145..e8da7d18d29d 100644
@@ -1062,9 +1062,23 @@ CAMLprim value stub_xc_version_changeset(value xch_val)
 
 CAMLprim value stub_xc_version_capabilities(value xch_val)
 {
-	xen_capabilities_info_t ci;
+	CAMLparam1(xch_val);
+	CAMLlocal1(result);
+	xc_interface *xch = xch_of_val(xch_val);
+	char *capabilities;
+
+	caml_enter_blocking_section();
+	capabilities = xc_xenver_capabilities(xch);
+	caml_leave_blocking_section();
 
-	return xc_version_single_string(xch_val, XENVER_capabilities, &ci);
+	if (!capabilities)
+		failwith_xc(xch);
+
+	result = caml_copy_string(capabilities);
+
+	free(capabilities);
+
+	CAMLreturn(result);
 }
 
 
Signature · 1 line
2.30.2
#23 lore raw

PATCH v3 8/10 tools: Introduce a non-truncating xc_xenver_changeset()

In reply to Andrew Cooper

Update libxl and the ocaml stubs to match. No API/ABI change in either.

---
CC: Wei Liu <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Christian Lindig <[email protected]>
CC: David Scott <[email protected]>
CC: Edwin Torok <[email protected]>
CC: Rob Hoes <[email protected]>

---
 tools/include/xenctrl.h             |  1 +
 tools/libs/ctrl/xc_version.c        |  5 +++++
 tools/libs/light/libxl.c            |  5 +----
 tools/ocaml/libs/xc/xenctrl_stubs.c | 19 ++++++++-----------
 4 files changed, 15 insertions(+), 15 deletions(-)
tools/include/xenctrl.h +1 -0
index 9bd5eed7397a..0400d65acaed 100644
@@ -1599,6 +1599,7 @@ int xc_version(xc_interface *xch, int cmd, void *arg);
  */
 char *xc_xenver_extraversion(xc_interface *xch);
 char *xc_xenver_capabilities(xc_interface *xch);
+char *xc_xenver_changeset(xc_interface *xch);
 
 int xc_flask_op(xc_interface *xch, xen_flask_op_t *op);
 
tools/libs/ctrl/xc_version.c +5 -0
index 512302a393ea..9f2cae03dba8 100644
@@ -161,3 +161,8 @@ char *xc_xenver_capabilities(xc_interface *xch)
 {
     return varbuf_simple_string(xch, XENVER_capabilities2);
 }
+
+char *xc_xenver_changeset(xc_interface *xch)
+{
+    return varbuf_simple_string(xch, XENVER_changeset2);
+}
tools/libs/light/libxl.c +1 -4
index ae6c5e04cd59..9a3532ce139a 100644
@@ -583,7 +583,6 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
     GC_INIT(ctx);
     union {
         xen_compile_info_t xen_cc;
-        xen_changeset_info_t xen_chgset;
         xen_platform_parameters_t p_parms;
         xen_commandline_t xen_commandline;
         xen_build_id_t build_id;
@@ -608,9 +607,7 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
     info->compile_date = libxl__strdup(NOGC, u.xen_cc.compile_date);
 
     info->capabilities = xc_xenver_capabilities(ctx->xch);
-
-    xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
-    info->changeset = libxl__strdup(NOGC, u.xen_chgset);
+    info->changeset = xc_xenver_changeset(ctx->xch);
 
     xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
     info->virt_start = u.p_parms.virt_start;
tools/ocaml/libs/xc/xenctrl_stubs.c +8 -11
index e8da7d18d29d..8495d5bd9892 100644
@@ -1035,28 +1035,25 @@ CAMLprim value stub_xc_version_compile_info(value xch_val)
 }
 
 
-static value xc_version_single_string(value xch_val, int code, void *info)
+CAMLprim value stub_xc_version_changeset(value xch_val)
 {
 	CAMLparam1(xch_val);
+	CAMLlocal1(result);
 	xc_interface *xch = xch_of_val(xch_val);
-	int retval;
+	char *changeset;
 
 	caml_enter_blocking_section();
-	retval = xc_version(xch, code, info);
+	changeset = xc_xenver_changeset(xch);
 	caml_leave_blocking_section();
 
-	if (retval)
+	if (!changeset)
 		failwith_xc(xch);
 
-	CAMLreturn(caml_copy_string((char *)info));
-}
+	result = caml_copy_string(changeset);
 
+	free(changeset);
 
-CAMLprim value stub_xc_version_changeset(value xch_val)
-{
-	xen_changeset_info_t ci;
-
-	return xc_version_single_string(xch_val, XENVER_changeset, &ci);
+	CAMLreturn(result);
 }
 
 
Signature · 1 line
2.30.2
#24 lore raw

PATCH v3 9/10 tools: Introduce a non-truncating xc_xenver_cmdline()

In reply to Andrew Cooper

Update libxl to match. No API/ABI change.

---
CC: Wei Liu <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/include/xenctrl.h      | 1 +
 tools/libs/ctrl/xc_version.c | 5 +++++
 tools/libs/light/libxl.c     | 4 +---
 3 files changed, 7 insertions(+), 3 deletions(-)
tools/include/xenctrl.h +1 -0
index 0400d65acaed..1b6f2ac508e0 100644
@@ -1600,6 +1600,7 @@ int xc_version(xc_interface *xch, int cmd, void *arg);
 char *xc_xenver_extraversion(xc_interface *xch);
 char *xc_xenver_capabilities(xc_interface *xch);
 char *xc_xenver_changeset(xc_interface *xch);
+char *xc_xenver_commandline(xc_interface *xch);
 
 int xc_flask_op(xc_interface *xch, xen_flask_op_t *op);
 
tools/libs/ctrl/xc_version.c +5 -0
index 9f2cae03dba8..02f6e9551b57 100644
@@ -166,3 +166,8 @@ char *xc_xenver_changeset(xc_interface *xch)
 {
     return varbuf_simple_string(xch, XENVER_changeset2);
 }
+
+char *xc_xenver_commandline(xc_interface *xch)
+{
+    return varbuf_simple_string(xch, XENVER_commandline2);
+}
tools/libs/light/libxl.c +1 -3
index 9a3532ce139a..04f037f3c199 100644
@@ -584,7 +584,6 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
     union {
         xen_compile_info_t xen_cc;
         xen_platform_parameters_t p_parms;
-        xen_commandline_t xen_commandline;
         xen_build_id_t build_id;
     } u;
     long xen_version;
@@ -614,8 +613,7 @@ const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
 
     info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
 
-    xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
-    info->commandline = libxl__strdup(NOGC, u.xen_commandline);
+    info->commandline = xc_xenver_commandline(ctx->xch);
 
     u.build_id.len = sizeof(u) - sizeof(u.build_id);
     r = libxl__xc_version_wrap(gc, info, &u.build_id);
Signature · 1 line
2.30.2
#25 lore raw

PATCH v3 10/10 tools: Introduce a xc_xenver_buildid() wrapper

In reply to Andrew Cooper

... which converts binary content to hex automatically.

Update libxl to match. No API/ABI change.

This removes a latent libxl bug for cases when the buildid is longer than 4092 bytes.

---
CC: Wei Liu <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Juergen Gross <[email protected]>

---
 tools/include/xenctrl.h      |  1 +
 tools/libs/ctrl/xc_version.c | 33 +++++++++++++++++++++++++++
 tools/libs/light/libxl.c     | 44 +-----------------------------------
 3 files changed, 35 insertions(+), 43 deletions(-)
tools/include/xenctrl.h +1 -0
index 1b6f2ac508e0..7f51cb43b931 100644
@@ -1601,6 +1601,7 @@ char *xc_xenver_extraversion(xc_interface *xch);
 char *xc_xenver_capabilities(xc_interface *xch);
 char *xc_xenver_changeset(xc_interface *xch);
 char *xc_xenver_commandline(xc_interface *xch);
+char *xc_xenver_buildid(xc_interface *xch);
 
 int xc_flask_op(xc_interface *xch, xen_flask_op_t *op);
 
tools/libs/ctrl/xc_version.c +33 -0
index 02f6e9551b57..54d1b9296696 100644
@@ -171,3 +171,36 @@ char *xc_xenver_commandline(xc_interface *xch)
 {
     return varbuf_simple_string(xch, XENVER_commandline2);
 }
+
+static void str2hex(char *dst, const unsigned char *src, size_t n)
+{
+    static const unsigned char hex[] = "0123456789abcdef";
+
+    for ( ; n; n-- )
+    {
+        unsigned char c = *src++;
+
+        *dst++ = hex[c >> 4];
+        *dst++ = hex[c & 0xf];
+    }
+}
+
+char *xc_xenver_buildid(xc_interface *xch)
+{
+    xen_varbuf_t *hbuf = varbuf_op(xch, XENVER_build_id);
+    char *res;
+
+    if ( !hbuf )
+        return NULL;
+
+    res = malloc((hbuf->len * 2) + 1);
+    if ( res )
+    {
+        str2hex(res, hbuf->buf, hbuf->len);
+        res[hbuf->len * 2] = '\0';
+    }
+
+    xencall_free_buffer(xch->xcall, hbuf);
+
+    return res;
+}
tools/libs/light/libxl.c +1 -43

63 lines not on this page · raw message

Signature · 1 line
2.30.2