Skip to contentSkip to conversation

[4710] Prevent guest reusing host memory allocations.

Qemu-devel Paul Brook 2 participants 4 messages Jun 9, 2008 – Aug 8, 2008 Kbuild

lore
Share & tools
lore

Paul Brook

author
#0 lore raw

Revision: 4710

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4710
Author:   pbrook
Date:     2008-06-09 13:47:45 +0000 (Mon, 09 Jun 2008)

Log Message:
-----------
Prevent guest reusing host memory allocations.

Modified Paths:
--------------
trunk/Makefile
trunk/Makefile.target
trunk/cutils.c
trunk/exec.c
trunk/linux-user/mmap.c

Added Paths:
-----------
trunk/qemu-malloc.c

Modified: trunk/Makefile
===================================================================

Makefile +1 -1
@@ -39,7 +39,7 @@
 #######################################################################
 # BLOCK_OBJS is code used by both qemu system emulation and qemu-img
 
-BLOCK_OBJS=cutils.o
+BLOCK_OBJS=cutils.o qemu-malloc.o
 BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
 BLOCK_OBJS+=block-qcow2.o block-parallels.o

Modified: trunk/Makefile.target
===================================================================

Makefile.target +2 -2
@@ -387,7 +387,7 @@
 endif
 endif
 
-OBJS= main.o syscall.o strace.o mmap.o signal.o path.o osdep.o thunk.o \
+OBJS= main.o syscall.o strace.o mmap.o signal.o path.o thunk.o \
       elfload.o linuxload.o uaccess.o
 LIBS+= $(AIOLIBS)
 ifdef TARGET_HAS_BFLT
@@ -444,7 +444,7 @@
 
 LIBS+=-lmx
 
-OBJS= main.o commpage.o machload.o mmap.o osdep.o signal.o syscall.o thunk.o
+OBJS= main.o commpage.o machload.o mmap.o signal.o syscall.o thunk.o
 
 OBJS+= libqemu.a
 

Modified: trunk/cutils.c
===================================================================

cutils.c +0 -35
@@ -95,38 +95,3 @@
     t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
     return t;
 }
-
-void *get_mmap_addr(unsigned long size)
-{
-    return NULL;
-}
-
-void qemu_free(void *ptr)
-{
-    free(ptr);
-}
-
-void *qemu_malloc(size_t size)
-{
-    return malloc(size);
-}
-
-void *qemu_mallocz(size_t size)
-{
-    void *ptr;
-    ptr = qemu_malloc(size);
-    if (!ptr)
-        return NULL;
-    memset(ptr, 0, size);
-    return ptr;
-}
-
-char *qemu_strdup(const char *str)
-{
-    char *ptr;
-    ptr = qemu_malloc(strlen(str) + 1);
-    if (!ptr)
-        return NULL;
-    strcpy(ptr, str);
-    return ptr;
-}

Modified: trunk/exec.c
===================================================================

exec.c +26 -2
@@ -263,13 +263,33 @@
 {
     PageDesc **lp, *p;
 
+#if TARGET_LONG_BITS > 32
+    /* Host memory outside guest VM.  For 32-bit targets we have already
+       excluded high addresses.  */
+    if (index > ((target_ulong)L2_SIZE * L1_SIZE * TARGET_PAGE_SIZE))
+        return NULL;
+#endif
     lp = &l1_map[index >> L2_BITS];
     p = *lp;
     if (!p) {
         /* allocate if not found */
-        p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
-        memset(p, 0, sizeof(PageDesc) * L2_SIZE);
+#if defined(CONFIG_USER_ONLY)
+        unsigned long addr;
+        size_t len = sizeof(PageDesc) * L2_SIZE;
+        /* Don't use qemu_malloc because it may recurse.  */
+        p = mmap(0, len, PROT_READ | PROT_WRITE,
+                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
         *lp = p;
+        addr = h2g(p);
+        if (addr == (target_ulong)addr) {
+            page_set_flags(addr & TARGET_PAGE_MASK,
+                           TARGET_PAGE_ALIGN(addr + len),
+                           PAGE_RESERVED); 
+        }
+#else
+        p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
+        *lp = p;
+#endif
     }
     return p + (index & (L2_SIZE - 1));
 }
@@ -1912,6 +1932,10 @@
         flags |= PAGE_WRITE_ORG;
     for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
         p = page_find_alloc(addr >> TARGET_PAGE_BITS);
+        /* We may be called for host regions that are outside guest
+           address space.  */
+        if (!p)
+            return;
         /* if the write protection is set, then we invalidate the code
            inside */
         if (!(p->flags & PAGE_WRITE) &&

Modified: trunk/linux-user/mmap.c
===================================================================

linux-user/mmap.c +46 -0

52 lines not on this page · raw message

Added: trunk/qemu-malloc.c
===================================================================

qemu-malloc.c +59 -0

59 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Paul Brook

Modified: trunk/exec.c
===================================================================

exec.c 2008-06-09 13:47:45 UTC (rev 4710) renamed from exec.c 2008-06-09 12:10:22 UTC (rev 4709) +0 -0
@@ -263,13 +263,33 @@

[...]

exec.c 2008-06-09 13:47:45 UTC (rev 4710)

Quoted 13 lines
exec.c 2008-06-09 13:47:45 UTC (rev 4710) renamed from exec.c 2008-06-09 12:10:22 UTC (rev 4709) +12 -0
+#if defined(CONFIG_USER_ONLY)
+        unsigned long addr;
+        size_t len = sizeof(PageDesc) * L2_SIZE;
+        /* Don't use qemu_malloc because it may recurse.  */
+        p = mmap(0, len, PROT_READ | PROT_WRITE,
+                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        *lp = p;
+        addr = h2g(p);
+        if (addr == (target_ulong)addr) {
+            page_set_flags(addr & TARGET_PAGE_MASK,
+                           TARGET_PAGE_ALIGN(addr + len),
+                           PAGE_RESERVED);
+        }

I don't understand what the test is supposed to do, to me it looks like it can't fail since we have:

#define h2g(x) ((target_ulong)(x - GUEST_BASE))

Did I miss something?

Laurent

#2 lore raw
Quoted 21 lines · Laurent Desnogues
Quoted 5 lines · Paul Brook

Modified: trunk/exec.c
===================================================================

exec.c 2008-06-09 13:47:45 UTC (rev 4710) renamed from exec.c 2008-06-09 12:10:22 UTC (rev 4709) +0 -0
@@ -263,13 +263,33 @@

[...]

exec.c 2008-06-09 13:47:45 UTC (rev 4710)

Quoted 13 lines
exec.c 2008-06-09 13:47:45 UTC (rev 4710) renamed from exec.c 2008-06-09 12:10:22 UTC (rev 4709) +12 -0
+#if defined(CONFIG_USER_ONLY)
+        unsigned long addr;
+        size_t len = sizeof(PageDesc) * L2_SIZE;
+        /* Don't use qemu_malloc because it may recurse.  */
+        p = mmap(0, len, PROT_READ | PROT_WRITE,
+                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        *lp = p;
+        addr = h2g(p);
+        if (addr == (target_ulong)addr) {
+            page_set_flags(addr & TARGET_PAGE_MASK,
+                           TARGET_PAGE_ALIGN(addr + len),
+                           PAGE_RESERVED);
+        }
Quoted 6 lines

I don't understand what the test is supposed to do, to me it looks like it can't fail since we have:

#define h2g(x) ((target_ulong)(x - GUEST_BASE))

Did I miss something?

I guess addr = h2g(p); should be replaced with

    addr = (unsigned long)p;

BTW this makes me think that perhaps GUEST_BASE should be used here and if that's true in some other places as well (qemu_vmalloc for instance).

These are all *wild guesses* from someone who doesn't fully understand qemu...

Laurent

#3 lore raw
In reply to Paul Brook
Quoted 17 lines · Paul Brook

Revision: 4710

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4710
Author:   pbrook
Date:     2008-06-09 13:47:45 +0000 (Mon, 09 Jun 2008)

Log Message:
-----------
Prevent guest reusing host memory allocations.

Modified Paths:
--------------
trunk/Makefile
trunk/Makefile.target
trunk/cutils.c
trunk/exec.c
trunk/linux-user/mmap.c

Quoted 3 lines

Added Paths:
-----------
trunk/qemu-malloc.c

(The above mentioned commit has already been commented. Look in the mailing list archive.)

I have found that it breaks alpha user-mode emulation on 64 bit hosts. Has anyone observed that for some other 64 bit target? Also has someone tried to correct that problem? If not I will give this a try.

Laurent