Skip to contentSkip to conversation

HVMlite start_info initialization fixes

PATCH Superseded Boris Ostrovsky 4 participants 10 messages Jan 5, 2016 – Jan 6, 2016

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover HVMlite start_info initialization fixes

In addition to moving certain things to a later point I also noticed a small bug with comamnd line initialization which is fixed in patch 1.

I ended up leaving code that deals with special pages and indenity page tables in alloc_magic_pages_hvm().

Boris Ostrovsky (2):
libxc: Don't write terminating NULL character to command string
libxc: Defer initialization of start_page for HVM guests

 tools/libxc/xc_dom_x86.c |  123 +++++++++++++++++++++++++++-------------------
 1 files changed, 73 insertions(+), 50 deletions(-)
#1 lore raw

PATCH v2 1/2 libxc: Don't write terminating NULL character to command string

When copying boot command string for HVMlite guests we explicitly write '\0' at MAX_GUEST_CMDLINE offset. Unless the string is close to MAX_GUEST_CMDLINE in length this write will end up in the wrong place, beyond the end of the mapped range.

Instead we should test string's length early and error out if it is too long.

---
 tools/libxc/xc_dom_x86.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
tools/libxc/xc_dom_x86.c +6 -2
index 3960875..b696149 100644
@@ -647,6 +647,11 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
         if ( dom->cmdline )
         {
             cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 8);
+            if ( cmdline_size > MAX_GUEST_CMDLINE )
+            {
+                DOMPRINTF("Boot command line is too long");
+                goto error_out;
+            }
             start_info_size += cmdline_size;
 
         }
@@ -676,8 +681,7 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
 
         if ( dom->cmdline )
         {
-            strncpy(cmdline, dom->cmdline, MAX_GUEST_CMDLINE);
-            cmdline[MAX_GUEST_CMDLINE - 1] = '\0';
+            strcpy(cmdline, dom->cmdline);
             start_info->cmdline_paddr = (seg.pfn << PAGE_SHIFT) +
                                 ((uintptr_t)cmdline - (uintptr_t)start_info);
         }
Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH v2 1/2] libxc: Don't write terminating NULL character to command string

Quoted 9 lines · Boris Ostrovsky

When copying boot command string for HVMlite guests we explicitly write '\0' at MAX_GUEST_CMDLINE offset. Unless the string is close to MAX_GUEST_CMDLINE in length this write will end up in the wrong place, beyond the end of the mapped range.

Instead we should test string's length early and error out if it is too long.

MAX_GUEST_CMDLINE is an arbitrary and incorrect restriction. It is sadly baked into the PV ABI, but I specifically want to avoid lumbering DMLite with the failings of PV.

By the looks of it, the only bug is the use of MAX_GUEST_CMDLINE. The xc_map_foreign_range() call already accounts for sufficient space to store the string when mapping guest memory.

I think you only need the 2nd hunk of this patch.

~Andrew

Quoted 30 lines
---
 tools/libxc/xc_dom_x86.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
tools/libxc/xc_dom_x86.c +6 -2
index 3960875..b696149 100644
@@ -647,6 +647,11 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
         if ( dom->cmdline )
         {
             cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 8);
+            if ( cmdline_size > MAX_GUEST_CMDLINE )
+            {
+                DOMPRINTF("Boot command line is too long");
+                goto error_out;
+            }
             start_info_size += cmdline_size;
 
         }
@@ -676,8 +681,7 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
 
         if ( dom->cmdline )
         {
-            strncpy(cmdline, dom->cmdline, MAX_GUEST_CMDLINE);
-            cmdline[MAX_GUEST_CMDLINE - 1] = '\0';
+            strcpy(cmdline, dom->cmdline);
             start_info->cmdline_paddr = (seg.pfn << PAGE_SHIFT) +
                                 ((uintptr_t)cmdline - (uintptr_t)start_info);
         }
#3 lore raw

Re: [PATCH v2 1/2] libxc: Don't write terminating NULL character to command string

Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Boris Ostrovsky

When copying boot command string for HVMlite guests we explicitly write '\0' at MAX_GUEST_CMDLINE offset. Unless the string is close to MAX_GUEST_CMDLINE in length this write will end up in the wrong place, beyond the end of the mapped range.

Instead we should test string's length early and error out if it is too long.

Quoted 8 lines
Quoted 1 line

MAX_GUEST_CMDLINE is an arbitrary and incorrect restriction. It is sadly baked into the PV ABI, but I specifically want to avoid lumbering DMLite with the failings of PV.

By the looks of it, the only bug is the use of MAX_GUEST_CMDLINE. The xc_map_foreign_range() call already accounts for sufficient space to store the string when mapping guest memory.

Yes, I was also thinking about dropping it but ended up keeping it mostly because it didn't feel right to blindly use strcpy().

-boris

Quoted 35 lines

I think you only need the 2nd hunk of this patch.

~Andrew

Quoted 30 lines
---
  tools/libxc/xc_dom_x86.c |    8 ++++++--
  1 files changed, 6 insertions(+), 2 deletions(-)
tools/libxc/xc_dom_x86.c +6 -2
index 3960875..b696149 100644
@@ -647,6 +647,11 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
          if ( dom->cmdline )
          {
              cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 8);
+            if ( cmdline_size > MAX_GUEST_CMDLINE )
+            {
+                DOMPRINTF("Boot command line is too long");
+                goto error_out;
+            }
              start_info_size += cmdline_size;
 
          }
@@ -676,8 +681,7 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
 
          if ( dom->cmdline )
          {
-            strncpy(cmdline, dom->cmdline, MAX_GUEST_CMDLINE);
-            cmdline[MAX_GUEST_CMDLINE - 1] = '\0';
+            strcpy(cmdline, dom->cmdline);
              start_info->cmdline_paddr = (seg.pfn << PAGE_SHIFT) +
                                  ((uintptr_t)cmdline - (uintptr_t)start_info);
          }
#4 lore raw

Re: [PATCH v2 1/2] libxc: Don't write terminating NULL character to command string

Quoted 15 lines · Boris Ostrovsky
Quoted 14 lines · Andrew Cooper
Quoted 9 lines · Boris Ostrovsky

When copying boot command string for HVMlite guests we explicitly write '\0' at MAX_GUEST_CMDLINE offset. Unless the string is close to MAX_GUEST_CMDLINE in length this write will end up in the wrong place, beyond the end of the mapped range.

Instead we should test string's length early and error out if it is too long.

MAX_GUEST_CMDLINE is an arbitrary and incorrect restriction. It is sadly baked into the PV ABI, but I specifically want to avoid lumbering DMLite with the failings of PV.

Quoted 6 lines
Quoted 3 lines

By the looks of it, the only bug is the use of MAX_GUEST_CMDLINE. The xc_map_foreign_range() call already accounts for sufficient space to store the string when mapping guest memory.

Yes, I was also thinking about dropping it but ended up keeping it mostly because it didn't feel right to blindly use strcpy().

Possibly add a comment explaining that the length has already been checked, and that sufficient space has been allocated, if that helps? One way or another, the use of strcpy() here is correct.

~Andrew

#5 lore raw

Re: [PATCH v2 1/2] libxc: Don't write terminating NULL character to command string

Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Boris Ostrovsky
Quoted 22 lines · Andrew Cooper
Quoted 7 lines · Boris Ostrovsky

When copying boot command string for HVMlite guests we explicitly write
'\0' at MAX_GUEST_CMDLINE offset. Unless the string is close to MAX_GUEST_CMDLINE in length this write will end up in the wrong place,
beyond the end of the mapped range.

Quoted 5 lines

Instead we should test string's length early and error out if it is too
long.

MAX_GUEST_CMDLINE is an arbitrary and incorrect restriction.  It is sadly baked into the PV ABI, but I specifically want to avoid lumbering
DMLite with the failings of PV.

By the looks of it, the only bug is the use of MAX_GUEST_CMDLINE.  The
xc_map_foreign_range() call already accounts for sufficient space to store the string when mapping guest memory.

Quoted 6 lines
Quoted 2 lines

Yes, I was also thinking about dropping it but ended up keeping it mostly because it didn't feel right to blindly use strcpy().

Possibly add a comment explaining that the length has already been checked, and that sufficient space has been allocated, if that helps? One way or another, the use of strcpy() here is correct.

The code has cmdline_size in hand still, so using strncpy with that might make people feel better and also serve as commentary regarding the sizing.

This code wants a check that the whole start_info + cmdline + modlist doesn't run off the end of whatever guest mapping has been made.

In fact it is only mapping sizeof(*start_info) and relying on that being rounded up to a page, which seems very wrong (e.g. guest admin controlled cmdline, this would be a security issue if dmlite weren't still experimental), it should do the foreign mapping after figuring out where modlist ends and make sure it maps enough.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw

Re: [PATCH v2 1/2] libxc: Don't write terminating NULL character to command string

Quoted 25 lines · Ian Campbell
Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Boris Ostrovsky
Quoted 22 lines · Andrew Cooper
Quoted 7 lines · Boris Ostrovsky

When copying boot command string for HVMlite guests we explicitly write
'\0' at MAX_GUEST_CMDLINE offset. Unless the string is close to MAX_GUEST_CMDLINE in length this write will end up in the wrong place,
beyond the end of the mapped range.

Quoted 5 lines

Instead we should test string's length early and error out if it is too
long.

MAX_GUEST_CMDLINE is an arbitrary and incorrect restriction. It is sadly baked into the PV ABI, but I specifically want to avoid lumbering
DMLite with the failings of PV.

By the looks of it, the only bug is the use of MAX_GUEST_CMDLINE. The
xc_map_foreign_range() call already accounts for sufficient space to store the string when mapping guest memory.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Yes, I was also thinking about dropping it but ended up keeping it mostly because it didn't feel right to blindly use strcpy().

Possibly add a comment explaining that the length has already been checked, and that sufficient space has been allocated, if that helps? One way or another, the use of strcpy() here is correct.

The code has cmdline_size in hand still, so using strncpy with that might make people feel better and also serve as commentary regarding the sizing.

Can do.

Quoted 9 lines

This code wants a check that the whole start_info + cmdline + modlist doesn't run off the end of whatever guest mapping has been made.

In fact it is only mapping sizeof(*start_info) and relying on that being rounded up to a page, which seems very wrong (e.g. guest admin controlled cmdline, this would be a security issue if dmlite weren't still experimental), it should do the foreign mapping after figuring out where modlist ends and make sure it maps enough.

The mapping is start_info_size which includes cmdline_size and a single modlist entry.

There is no risk (that I can see) of the command line wandering over the mapping boundary.

~Andrew

#7 lore raw

Re: [PATCH v2 1/2] libxc: Don't write terminating NULL character to command string

Quoted 55 lines · Andrew Cooper
Quoted 31 lines · Ian Campbell
Quoted 30 lines · Andrew Cooper
Quoted 29 lines · Boris Ostrovsky
Quoted 21 lines · Andrew Cooper
Quoted 8 lines · Boris Ostrovsky

When copying boot command string for HVMlite guests we explicitly
write
'\0' at MAX_GUEST_CMDLINE offset. Unless the string is close to MAX_GUEST_CMDLINE in length this write will end up in the wrong place,
beyond the end of the mapped range.

Quoted 6 lines

Instead we should test string's length early and error out if it is
too
long.

MAX_GUEST_CMDLINE is an arbitrary and incorrect restriction.  It is
sadly baked into the PV ABI, but I specifically want to avoid lumbering
DMLite with the failings of PV.

Quoted 5 lines

By the looks of it, the only bug is the use of MAX_GUEST_CMDLINE.  The
xc_map_foreign_range() call already accounts for sufficient space to
store the string when mapping guest memory.

Yes, I was also thinking about dropping it but ended up keeping it mostly because it didn't feel right to blindly use strcpy().

Quoted 7 lines
Quoted 3 lines

Possibly add a comment explaining that the length has already been checked, and that sufficient space has been allocated, if that helps? One way or another, the use of strcpy() here is correct.

The code has cmdline_size in hand still, so using strncpy with that might
make people feel better and also serve as commentary regarding the sizing.

Can do.

Quoted 4 lines

This code wants a check that the whole start_info + cmdline + modlist doesn't run off the end of whatever guest mapping has been made.

Quoted 8 lines

In fact it is only mapping sizeof(*start_info) and relying on that being
rounded up to a page, which seems very wrong (e.g. guest admin controlled
cmdline, this would be a security issue if dmlite weren't still experimental), it should do the foreign mapping after figuring out where
modlist ends and make sure it maps enough.

Quoted 5 lines

The mapping is start_info_size which includes cmdline_size and a single modlist entry.

There is no risk (that I can see) of the command line wandering over the mapping boundary.

I confused the bit which calculates the actual pointers with the bit which calculated the total size, sorry for the noise.

Ian

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw

PATCH v2 2/2 libxc: Defer initialization of start_page for HVM guests

In reply to Boris Ostrovsky

With commit 8c45adec18e0 ("libxc: create unmapped initrd in domain builder if supported") location of ramdisk may not be available to HVMlite guests by the time alloc_magic_pages_hvm() is invoked if the guest supports unmapped initrd.

So let's move ramdisk info initialization (along with a few other operations that are not directly related to allocating magic/special pages) from alloc_magic_pages_hvm() to bootlate_hvm().

---
 tools/libxc/xc_dom_x86.c |  117 ++++++++++++++++++++++++++-------------------
 1 files changed, 68 insertions(+), 49 deletions(-)
tools/libxc/xc_dom_x86.c +68 -49
index b696149..e8c947f 100644
@@ -586,23 +586,12 @@ static void build_hvm_info(void *hvm_info_page, struct xc_dom_image *dom)
 static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
 {
     unsigned long i;
-    void *hvm_info_page;
     uint32_t *ident_pt, domid = dom->guest_domid;
     int rc;
     xen_pfn_t special_array[X86_HVM_NR_SPECIAL_PAGES];
     xen_pfn_t ioreq_server_array[NR_IOREQ_SERVER_PAGES];
     xc_interface *xch = dom->xch;
 
-    if ( dom->device_model )
-    {
-        if ( (hvm_info_page = xc_map_foreign_range(
-                  xch, domid, PAGE_SIZE, PROT_READ | PROT_WRITE,
-                  HVM_INFO_PFN)) == NULL )
-            goto error_out;
-        build_hvm_info(hvm_info_page, dom);
-        munmap(hvm_info_page, PAGE_SIZE);
-    }
-
     /* Allocate and clear special pages. */
     for ( i = 0; i < X86_HVM_NR_SPECIAL_PAGES; i++ )
         special_array[i] = special_pfn(i);
@@ -637,12 +626,9 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
     if ( !dom->device_model )
     {
         struct xc_dom_seg seg;
-        struct hvm_start_info *start_info;
-        char *cmdline;
         struct hvm_modlist_entry *modlist;
-        void *start_page;
         size_t cmdline_size = 0;
-        size_t start_info_size = sizeof(*start_info);
+        size_t start_info_size = sizeof(struct hvm_start_info);
 
         if ( dom->cmdline )
         {
@@ -666,39 +652,6 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
             goto out;
         }
 
-        start_page = xc_map_foreign_range(xch, domid, start_info_size,
-                                          PROT_READ | PROT_WRITE,
-                                          seg.pfn);
-        if ( start_page == NULL )
-        {
-            DOMPRINTF("Unable to map HVM start info page");
-            goto error_out;
-        }
-
-        start_info = start_page;
-        cmdline = start_page + sizeof(*start_info);
-        modlist = start_page + sizeof(*start_info) + cmdline_size;
-
-        if ( dom->cmdline )
-        {
-            strcpy(cmdline, dom->cmdline);
-            start_info->cmdline_paddr = (seg.pfn << PAGE_SHIFT) +
-                                ((uintptr_t)cmdline - (uintptr_t)start_info);
-        }
-
-        if ( dom->ramdisk_blob )
-        {
-            modlist[0].paddr = dom->ramdisk_seg.vstart - dom->parms.virt_base;
-            modlist[0].size = dom->ramdisk_seg.vend - dom->ramdisk_seg.vstart;
-            start_info->modlist_paddr = (seg.pfn << PAGE_SHIFT) +
-                                ((uintptr_t)modlist - (uintptr_t)start_info);
-            start_info->nr_modules = 1;
-        }
-
-        start_info->magic = HVM_START_MAGIC_VALUE;
-
-        munmap(start_page, start_info_size);
-
         dom->start_info_pfn = seg.pfn;
     }
     else
@@ -1788,7 +1741,73 @@ static int alloc_pgtables_hvm(struct xc_dom_image *dom)
 
 static int bootlate_hvm(struct xc_dom_image *dom)
 {
-    DOMPRINTF("%s: doing nothing", __func__);
+    uint32_t domid = dom->guest_domid;
+    xc_interface *xch = dom->xch;
+
+    if ( !dom->device_model )
+    {
+        struct hvm_start_info *start_info;
+        size_t start_info_size = sizeof(*start_info);
+        void *start_page;
+        struct hvm_modlist_entry *modlist;
+        size_t cmdline_size = 0;
+
+        if ( dom->cmdline )
+        {
+            cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 8);
+            start_info_size += cmdline_size;
+        }
+        if ( dom->ramdisk_blob )
+            start_info_size += sizeof(*modlist); /* Limited to one module. */
+
+        start_page = xc_map_foreign_range(xch, domid, start_info_size,
+                                          PROT_READ | PROT_WRITE,
+                                          dom->start_info_pfn);
+        if ( start_page == NULL )
+        {
+            DOMPRINTF("Unable to map HVM start info page");
+            return -1;
+        }
+
+        start_info = start_page;
+
+        if ( dom->cmdline )
+        {
+            char *cmdline = start_page + sizeof(*start_info);
+
+            /* We've already tested cmdline's size in alloc_magic_pages_hvm() */
+            strcpy(cmdline, dom->cmdline);
+            start_info->cmdline_paddr = (dom->start_info_pfn << PAGE_SHIFT) +
+                                ((uintptr_t)cmdline - (uintptr_t)start_info);
+        }
+
+        if ( dom->ramdisk_blob )
+        {
+            modlist = start_page + sizeof(*start_info) + cmdline_size;
+
+            modlist[0].paddr = dom->ramdisk_seg.vstart - dom->parms.virt_base;
+            modlist[0].size = dom->ramdisk_seg.vend - dom->ramdisk_seg.vstart;
+            start_info->modlist_paddr = (dom->start_info_pfn << PAGE_SHIFT) +
+                                ((uintptr_t)modlist - (uintptr_t)start_info);
+            start_info->nr_modules = 1;
+        }
+
+        start_info->magic = HVM_START_MAGIC_VALUE;
+
+        munmap(start_page, start_info_size);
+    }
+    else
+    {
+        void *hvm_info_page;
+
+        if ( (hvm_info_page = xc_map_foreign_range(
+                  xch, domid, PAGE_SIZE, PROT_READ | PROT_WRITE,
+                  HVM_INFO_PFN)) == NULL )
+            return -1;
+        build_hvm_info(hvm_info_page, dom);
+        munmap(hvm_info_page, PAGE_SIZE);
+    }
+
     return 0;
 }
 
Signature · 1 line
1.7.1
Acked-by on v2 2/2
#9 lore raw

Re: [PATCH v2 2/2] libxc: Defer initialization of start_page for HVM guests

Quoted 10 lines · Boris Ostrovsky

With commit 8c45adec18e0 ("libxc: create unmapped initrd in domain builder if supported") location of ramdisk may not be available to HVMlite guests by the time alloc_magic_pages_hvm() is invoked if the guest supports unmapped initrd.

So let's move ramdisk info initialization (along with a few other operations that are not directly related to allocating magic/special pages) from alloc_magic_pages_hvm() to bootlate_hvm().