Skip to contentSkip to conversation

livepatch: Pass buffer size to list sysctl

PATCH Superseded Ross Lagerwall 2 participants 2 messages May 7, 2025 – May 8, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 livepatch: Pass buffer size to list sysctl

From: Kevin Lampis <[email protected]>

The livepatch list sysctl writes metadata into a buffer provided by the caller. The caller is expected to allocate an appropriately sized buffer but this is racy and may result in Xen writing beyond the end of the buffer should the metadata size change.

The name buffer is expected to be an array of elements with size XEN_LIVEPATCH_NAME_SIZE to avoid this kind of race but the xen-livepatch tool allocates only as many bytes as needed, therefore encountering the same potential race condition.

Fix both these issues by requiring the caller to pass in the size of the name and metadata buffers and then not writing beyond the allocated size.

The sysctl interface version is bumped due to the change in semantics of the fields.

---
 tools/libs/ctrl/xc_misc.c   |  3 +++
 xen/common/livepatch.c      | 22 +++++++++++++++++-----
 xen/include/public/sysctl.h | 12 ++++++++----
 3 files changed, 28 insertions(+), 9 deletions(-)
tools/libs/ctrl/xc_misc.c +3 -0
index 6a60216bda03..33e87bac2868 100644
@@ -867,6 +867,9 @@ int xc_livepatch_list(xc_interface *xch, const unsigned int max,
         set_xen_guest_handle(sysctl.u.livepatch.u.list.metadata, metadata);
         set_xen_guest_handle(sysctl.u.livepatch.u.list.metadata_len, metadata_len);
 
+        sysctl.u.livepatch.u.list.name_total_size = name_sz;
+        sysctl.u.livepatch.u.list.metadata_total_size = metadata_sz;
+
         rc = do_sysctl(xch, &sysctl);
         /*
          * From here on we MUST call xc_hypercall_bounce. If rc < 0 we
xen/common/livepatch.c +17 -5
index be9b7e367553..72ef22bea5d2 100644
@@ -1311,11 +1311,10 @@ static int livepatch_list(struct xen_sysctl_livepatch_list *list)
         return -EINVAL;
     }
 
-    list->name_total_size = 0;
-    list->metadata_total_size = 0;
     if ( list->nr )
     {
         size_t name_offset = 0, metadata_offset = 0;
+        uint32_t name_total_copied = 0, metadata_total_copied = 0;
 
         list_for_each_entry( data, &payload_list, list )
         {
@@ -1328,10 +1327,14 @@ static int livepatch_list(struct xen_sysctl_livepatch_list *list)
             status.rc = data->rc;
 
             name_len = strlen(data->name) + 1;
-            list->name_total_size += name_len;
-
             metadata_len = data->metadata.len;
-            list->metadata_total_size += metadata_len;
+
+            if ( ((uint64_t)name_total_copied + name_len) > list->name_total_size ||
+                 ((uint64_t)metadata_total_copied + metadata_len) > list->metadata_total_size )
+            {
+                rc = -ENOMEM;
+                break;
+            }
 
             if ( !guest_handle_subrange_okay(list->name, name_offset,
                                              name_offset + name_len - 1) ||
@@ -1355,6 +1358,9 @@ static int livepatch_list(struct xen_sysctl_livepatch_list *list)
                 break;
             }
 
+            name_total_copied += name_len;
+            metadata_total_copied += metadata_len;
+
             idx++;
             name_offset += name_len;
             metadata_offset += metadata_len;
@@ -1362,9 +1368,15 @@ static int livepatch_list(struct xen_sysctl_livepatch_list *list)
             if ( (idx >= list->nr) || hypercall_preempt_check() )
                 break;
         }
+
+        list->name_total_size = name_total_copied;
+        list->metadata_total_size = metadata_total_copied;
     }
     else
     {
+        list->name_total_size = 0;
+        list->metadata_total_size = 0;
+
         list_for_each_entry( data, &payload_list, list )
         {
             list->name_total_size += strlen(data->name) + 1;
xen/include/public/sysctl.h +8 -4
index b0fec271d36f..9eca72865b87 100644
@@ -26,9 +26,9 @@
  * (e.g. adding semantics to 0-checked input fields or data to zeroed output
  * fields) don't require a change of the version.
  *
- * Last version bump: Xen 4.17
+ * Last version bump: Xen 4.21
  */
-#define XEN_SYSCTL_INTERFACE_VERSION 0x00000015
+#define XEN_SYSCTL_INTERFACE_VERSION 0x00000016
 
 /*
  * Read console content from Xen buffer ring.
@@ -1101,8 +1101,12 @@ struct xen_sysctl_livepatch_list {
                                                amount of payloads and version.
                                                OUT: How many payloads left. */
     uint32_t pad;                           /* IN: Must be zero. */
-    uint32_t name_total_size;               /* OUT: Total size of all transfer names */
-    uint32_t metadata_total_size;           /* OUT: Total size of all transfer metadata */
+    uint32_t name_total_size;               /* IN: Size of name buffer
+                                               OUT: Total size of transferred
+                                               names */
+    uint32_t metadata_total_size;           /* IN: Size of metadata buffer
+                                               OUT: Total size of transferred
+                                               metadata */
     XEN_GUEST_HANDLE_64(xen_livepatch_status_t) status;  /* OUT. Must have enough
                                                space allocate for nr of them. */
     XEN_GUEST_HANDLE_64(char) name;         /* OUT: Array of names. Each member
Signature · 1 line
2.49.0
#1 lore raw
Quoted 59 lines · Ross Lagerwall

From: Kevin Lampis <[email protected]>

The livepatch list sysctl writes metadata into a buffer provided by the caller. The caller is expected to allocate an appropriately sized buffer but this is racy and may result in Xen writing beyond the end of the buffer should the metadata size change.

The name buffer is expected to be an array of elements with size XEN_LIVEPATCH_NAME_SIZE to avoid this kind of race but the xen-livepatch tool allocates only as many bytes as needed, therefore encountering the same potential race condition.

Fix both these issues by requiring the caller to pass in the size of the name and metadata buffers and then not writing beyond the allocated size.

The sysctl interface version is bumped due to the change in semantics of the fields.

---
 tools/libs/ctrl/xc_misc.c   |  3 +++
 xen/common/livepatch.c      | 22 +++++++++++++++++-----
 xen/include/public/sysctl.h | 12 ++++++++----
 3 files changed, 28 insertions(+), 9 deletions(-)
tools/libs/ctrl/xc_misc.c +3 -0
index 6a60216bda03..33e87bac2868 100644
@@ -867,6 +867,9 @@ int xc_livepatch_list(xc_interface *xch, const unsigned int max,
         set_xen_guest_handle(sysctl.u.livepatch.u.list.metadata, metadata);
         set_xen_guest_handle(sysctl.u.livepatch.u.list.metadata_len, metadata_len);
 
+        sysctl.u.livepatch.u.list.name_total_size = name_sz;
+        sysctl.u.livepatch.u.list.metadata_total_size = metadata_sz;
+
         rc = do_sysctl(xch, &sysctl);
         /*
          * From here on we MUST call xc_hypercall_bounce. If rc < 0 we
xen/common/livepatch.c +1 -2
index be9b7e367553..72ef22bea5d2 100644
@@ -1311,11 +1311,10 @@ static int livepatch_list(struct xen_sysctl_livepatch_list *list)
         return -EINVAL;
     }
 
-    list->name_total_size = 0;
-    list->metadata_total_size = 0;
     if ( list->nr )
     {
         size_t name_offset = 0, metadata_offset = 0;
+        uint32_t name_total_copied = 0, metadata_total_copied = 0;
 
         list_for_each_entry( data, &payload_list, list )
         {

xen/common/livepatch.c static int livepatch_list(struct xen_sysctl_livepatch_list *list) · line 1333

Quoted 10 lines
xen/common/livepatch.c +3 -3
@@ -1328,10 +1327,14 @@ static int livepatch_list(struct xen_sysctl_livepatch_list *list)
             status.rc = data->rc;
 
             name_len = strlen(data->name) + 1;
-            list->name_total_size += name_len;
-
             metadata_len = data->metadata.len;
-            list->metadata_total_size += metadata_len;
+
+            if ( ((uint64_t)name_total_copied + name_len) > list->name_total_size ||
+                 ((uint64_t)metadata_total_copied + metadata_len) > list->metadata_total_size )

I would define name_total_copied and metadata_total_copied as size_t, and then avoid doing the cast to uint64_t here.

Also please adjust line length to 80 characters max.

Thanks, Roger.