xmalloc: correct _xrealloc() for large or changing alignment
PATCH v2 1/1 xmalloc: correct _xrealloc() for large or changing alignment
The curr_size calculation on the non-page-aligned path may include leading padding; therefore that size cannot be passed unadjusted to memcpy() (or the min() used in its invocation).
Further, for the "can be re-used" case on the non-page-aligned path, if alignment changes from what was used for the original calculation, the new pointer could differ from the original one. Hence memory contents may need moving.
No in-tree users presently exercise either of the problem cases. Add some self-testing to cover them.
---
v2: Also include xen/errno.h in selftest section. Fix block moving up in-
place due to higher alignment. Add respective case to self-test.
xen/common/xmalloc_tlsf.c +102 -6
@@ -578,17 +578,23 @@ static void *strip_padding(void *p)return p; } -static void *add_padding (void *p,unsigned long align) +static void *add_padding (void *p,unsigned long align,bool dry) {unsigned int pad;if ( (pad = -(long )p & (align -1 )) !=0 ) {void *q = p + pad; -struct bhdr *b = q -BHDR_OVERHEAD ;ASSERT (q > p); - b->size = pad |FREE_BLOCK ; + +if ( !dry ) + { +struct bhdr *b = q -BHDR_OVERHEAD ; + + b->size = pad |FREE_BLOCK ; + } + p = q; }
@@ -622,7 +628,7 @@ void *_xmalloc(unsigned long size, unsigreturn xmalloc_whole_pages (size - align +MEM_ALIGN , align);/* Add alignment padding. */ - p =add_padding (p, align); + p =add_padding (p, align, false);ASSERT (((unsigned long )p & (align -1 )) ==0 );return p;
@@ -681,13 +687,23 @@ void *_xrealloc(void *ptr, unsigned longif ( tmp_size <= curr_size ) { -/* Add alignment padding. */ - p =add_padding (p, align); +void *p0 = p; +/* Determine alignment padding. */ + p =add_padding (p, align, true);ASSERT (!((unsigned long )p & (align -1 ))); +if ( p != ptr ) + ptr =memmove (p, ptr,min (curr_size - (p - p0), size)); + +/* Add alignment padding. */ + p =add_padding (p0, align, false); +ASSERT (p == ptr); +return p; } + + curr_size -= ptr - p; } p =_xmalloc (size, align);
@@ -730,3 +746,83 @@ void xfree(void *p)xmem_pool_free (p, xenpool); } + +#ifdef CONFIG_SELF_TESTS + +#include <xen/errno.h> +#include <xen/init.h> + +static int cf_check __init selftest(void) +{ +unsigned char *ptr =_xmalloc (1 ,1 ), *new; +unsigned long align; + +if ( !ptr ) +return -ENOMEM ; + +BUG_ON (!IS_ALIGNED ((unsigned long )ptr,MEM_ALIGN )); + ptr[0 ] ='A' ; + + new =_xrealloc (ptr,MEM_ALIGN ,MEM_ALIGN ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN )); +BUG_ON (new[0 ] !='A' ); +memset (new +1 ,'B' ,MEM_ALIGN -1 ); + ptr = new; + +for ( align =MEM_ALIGN <<2 ; align <PAGE_SIZE ; align <<=1 ) +if ( !IS_ALIGNED ((unsigned long )ptr, align) ) +break ; + new =_xrealloc (ptr,MEM_ALIGN , align); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new, align)); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); + ptr = new; + + new =_xrealloc (ptr, align >>=1 ,MEM_ALIGN ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN )); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); +memset (new +MEM_ALIGN ,'C' , align -MEM_ALIGN ); + ptr = new; + + new =_xrealloc (ptr, align <<1 ,MEM_ALIGN <<1 ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN <<1 )); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); +BUG_ON (memchr_inv (new +MEM_ALIGN ,'C' , align -MEM_ALIGN )); + ptr = new; + + new =_xrealloc (ptr, align,MEM_ALIGN <<2 ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN <<2 )); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); +BUG_ON (memchr_inv (new +MEM_ALIGN ,'C' , align -MEM_ALIGN )); + ptr = new; + + new =_xrealloc (ptr, align,MEM_ALIGN ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN )); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); +BUG_ON (memchr_inv (new +MEM_ALIGN ,'C' , align -MEM_ALIGN )); + ptr = new; + + out: +xfree (ptr); + +return new ?0 : -ENOMEM ; +} +presmp_initcall (selftest); + +#endif /* CONFIG_SELF_TESTS */
xen/common/xmalloc_tlsf.c void *_xrealloc(void *ptr, unsigned long · line 697
Quoted 2 lines · Jan Beulich
xen/common/xmalloc_tlsf.c +2
-0+if ( p != ptr ) + ptr =memmove (p, ptr,min (curr_size - (p - p0), size));
Should this be min(curr_size - (ptr - p0), size), i.e. use the old padding, like the "curr_size -= ptr - p" below? With the new padding, when the pointer moves down and size is larger than the old data, memmove() reads past the end of the block. That is harmless in practice.
With or without that change:
Stephen
Quoted 6 lines · Stephen Cheng
xen/common/xmalloc_tlsf.c void *_xrealloc(void *ptr, unsigned long · line 697
Quoted 2 lines · Jan Beulich
xen/common/xmalloc_tlsf.c +2
-0+if ( p != ptr ) + ptr =memmove (p, ptr,min (curr_size - (p - p0), size));Should this be
min(curr_size - (ptr - p0), size), i.e. use the old padding, like the "curr_size-= ptr - p" below?
Oh, yes, it should. I failed to make the necessary adjustment when moving.
Quoted 3 lines
With the new padding,
when the pointer moves down and size is larger than the old data,memmove()reads past the end of the block. That is harmless in practice.
Reading past the end of a block is rarely entirely harmless.
Quoted 3 lines
With or without that change:
Reviewed-by Stephen Cheng <[email protected]>Tested-by Stephen Cheng <[email protected]>
Thanks.
Jan
Quoted 8 lines · Stephen Cheng
xen/common/xmalloc_tlsf.c void *_xrealloc(void *ptr, unsigned long · line 697
Quoted 2 lines · Jan Beulich
xen/common/xmalloc_tlsf.c +2
-0+if ( p != ptr ) + ptr =memmove (p, ptr,min (curr_size - (p - p0), size));Should this be
min(curr_size - (ptr - p0), size), i.e. use the old padding, like the "curr_size-= ptr - p" below? With the new padding, when the pointer moves down and size is larger than the old data,memmove()reads past the end of the block. That is harmless in practice.
Upon looking more closely, I think the code is correct as-is. curr_size describes the full block (from p0), and what we want to subtract is the padding size that add_padding() inserts. The (possible) amount of (original) tail padding doesn't matter. Whether we read into tail padding isn't overly important, I think - maybe that you you meant with "harmless". If we wanted to be more precise no matter whether size grows or shrinks, I think it would need to bemin(curr_size - min(p - p0, ptr - p0)). Yet that extra complexity doesn't look warranted.
Jan
Quoted 9 lines · Jan Beulich
Upon looking more closely, I think the code is correct as-is.
curr_sizedescribes the full block (from p0), and what we want to subtract is the padding size thatadd_padding()inserts. The (possible) amount of (original) tail padding doesn't matter. Whether we read into tail padding isn't overly important, I think - maybe that you you meant with "harmless". If we wanted to be more precise no matter whether size grows or shrinks, I think it would need to bemin(curr_size - min(p - p0, ptr - p0)). Yet that extra complexity doesn't look warranted.
Thanks for looking at it again. I also looked more closely after your reply, and I should not have said "harmless", sorry. As you said in your earlier reply, reading past the end of a block is rarely harmless.
The read is not only into tail padding. When the pointer moves down, it can go up to (ptr - p) bytes past the end of the block. If the block is the last one in its pool page, that is past the end of the page. On x86 the directmap only maps RAM, so the next page may not be mapped.
For example, _xmalloc(32, 64) gets an 80-byte block at p0 = 0x10, so the block ends at 0x60 and ptr = 0x40 (48 bytes of padding). Then _xrealloc(ptr, 80, MEM_ALIGN) re-uses the block with p = 0x10, and memmove() reads 80 bytes from 0x40, up to 0x90. That is 48 bytes past the end of the block:
0x10 0x40 0x60 0x90
+-------------+-------------+ - - - - - - -
| padding | old data | next block
+-------------+-------------+ - - - - - - -
p0, new p ptr block end
|<---- memmove() reads ---->|This needs the pointer to move down, the block to be the last one in its page, and the next page to be unmapped, so it is unlikely. But the fix is small.
Unless I am missing something, min(curr_size - (ptr - p0), size) is enough in both directions: the re-use check already makes sure size fits after the new pointer, so only the old pointer needs the limit. But of course it is your call.
Stephen