Skip to contentSkip to conversation

xen/common: Do not tolerate xmalloc(0, ...)

RFC PATCH In review Andrew Cooper 3 participants 3 messages Mar 31, 2015 – Apr 13, 2015

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 xen/common: Do not tolerate xmalloc(0, ...)

Currently, _xmalloc() supports zero-sized allocations by returning a sentinel poisoned pointer.

I posit that there are no legitimate situation for any code in the hypervisor to make a zero sized allocation.

Furthermore, the sentinel value will pass a NULL pointer check, and introduces an unnecessary security risk if it is accidentally used.

Instead, turn a zero-sized allocation into a clean fatal error.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Ian Campbell <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Tim Deegan <[email protected]>

---

This is RFC to gauge opinion, and because the amount of testing it has had is "works on my box", which I admit is not much for a change of this nature.

---
 xen/common/xmalloc_tlsf.c    |   12 ++----------
 xen/include/asm-x86/config.h |    3 ---
 2 files changed, 2 insertions(+), 13 deletions(-)
xen/common/xmalloc_tlsf.c +2 -10
index b13317e..519dfbb 100644
@@ -563,21 +563,13 @@ static void tlsf_init(void)
 /*
  * xmalloc()
  */
-
-#ifndef ZERO_BLOCK_PTR
-/* Return value for zero-size allocation, distinguished from NULL. */
-#define ZERO_BLOCK_PTR ((void *)-1L)
-#endif
-
 void *_xmalloc(unsigned long size, unsigned long align)
 {
     void *p = NULL;
     u32 pad;
 
     ASSERT(!in_irq());
-
-    if ( !size )
-        return ZERO_BLOCK_PTR;
+    BUG_ON(size == 0);
 
     ASSERT((align & (align - 1)) == 0);
     if ( align < MEM_ALIGN )
@@ -617,7 +609,7 @@ void xfree(void *p)
 {
     struct bhdr *b;
 
-    if ( p == NULL || p == ZERO_BLOCK_PTR )
+    if ( p == NULL )
         return;
 
     ASSERT(!in_irq());
xen/include/asm-x86/config.h +0 -3
index 3569753..591f20b 100644
@@ -94,9 +94,6 @@
 /* Primary stack is restricted to 8kB by guard pages. */
 #define PRIMARY_STACK_SIZE 8192
 
-/* Return value for zero-size _xmalloc(), distinguished from NULL. */
-#define ZERO_BLOCK_PTR ((void *)0xBAD0BAD0BAD0BAD0UL)
-
 /* Override include/xen/list.h to make these non-canonical addresses. */
 #define LIST_POISON1  ((void *)0x0100100100100100UL)
 #define LIST_POISON2  ((void *)0x0200200200200200UL)
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 10 lines · Andrew Cooper

Currently, _xmalloc() supports zero-sized allocations by returning a sentinel poisoned pointer.

I posit that there are no legitimate situation for any code in the hypervisor to make a zero sized allocation.

Furthermore, the sentinel value will pass a NULL pointer check, and introduces an unnecessary security risk if it is accidentally used.

Instead, turn a zero-sized allocation into a clean fatal error.

I think this code is better as it is now. malloc-style allocators typically allow zero-sized allocations so code that expects it to work isn't unreasonable. And even if we audited all callers and remembered to catch this in new code, we might inherit some next time we import code from linux, since AFAICT the linux allocators allow it.

The poisoned pointer we have right now will DTRT in a pretty obvious way if it's actually dereferenced, so I think we're OK there -- that is, I don't see a security risk other than DoS, and this patch wouldn't fix the DoS.

Cheers,

Tim.

#2 lore raw
In reply to Andrew Cooper
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 31.03.15 at 17:57, <[email protected]> wrote:

Currently, _xmalloc() supports zero-sized allocations by returning a sentinel poisoned pointer.

I posit that there are no legitimate situation for any code in the hypervisor
to make a zero sized allocation.

I'm afraid there are, and we ran into them (I vaguely recall with an earlier version of this patch, which tried to do something similar to what you suggest now). I don't recall where exactly they were, but it should have been some array allocation with zero array elements (perhaps because some particular feature was unsupported).

And I second Tim's reply independent from the above.

Jan