* [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
@ 2026-12-03 13:17 Jan Beulich
2026-12-04 2:55 ` Stephen Cheng
0 siblings, 1 reply; 5+ messages in thread
From: Jan Beulich @ 2026-12-03 13:17 UTC (permalink / raw)
To: xen-devel@lists.xenproject.org
Cc: Andrew Cooper, Julien Grall, Stefano Stabellini, Anthony PERARD,
Michal Orzel, Roger Pau Monné
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.
Fixes: 50879e805edb ("xen/common: Introduce _xrealloc function")
Reported-by: Roger Pau Monné <roger.pau@amd.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
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.
--- a/xen/common/xmalloc_tlsf.c
+++ b/xen/common/xmalloc_tlsf.c
@@ -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, unsig
return 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 long
if ( 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 */
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
2026-12-03 13:17 [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment Jan Beulich
@ 2026-12-04 2:55 ` Stephen Cheng
2026-12-04 6:42 ` Jan Beulich
2026-12-04 8:11 ` Jan Beulich
0 siblings, 2 replies; 5+ messages in thread
From: Stephen Cheng @ 2026-12-04 2:55 UTC (permalink / raw)
To: jbeulich
Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger,
sstabellini, xen-devel, Stephen Cheng
On 03/12/2026 13:18, Jan Beulich wrote:
> + 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:
Reviewed-by: Stephen Cheng <stephen.cheng@citrix.com>
Tested-by: Stephen Cheng <stephen.cheng@citrix.com>
Stephen
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
2026-12-04 2:55 ` Stephen Cheng
@ 2026-12-04 6:42 ` Jan Beulich
2026-12-04 8:11 ` Jan Beulich
1 sibling, 0 replies; 5+ messages in thread
From: Jan Beulich @ 2026-12-04 6:42 UTC (permalink / raw)
To: Stephen Cheng
Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger,
sstabellini, xen-devel
On 04.12.2026 03:55, Stephen Cheng wrote:
> On 03/12/2026 13:18, Jan Beulich wrote:
>> + 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.
> 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.
> With or without that change:
> Reviewed-by: Stephen Cheng <stephen.cheng@citrix.com>
> Tested-by: Stephen Cheng <stephen.cheng@citrix.com>
Thanks.
Jan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
2026-12-04 2:55 ` Stephen Cheng
2026-12-04 6:42 ` Jan Beulich
@ 2026-12-04 8:11 ` Jan Beulich
2026-12-04 9:03 ` Stephen Cheng
1 sibling, 1 reply; 5+ messages in thread
From: Jan Beulich @ 2026-12-04 8:11 UTC (permalink / raw)
To: Stephen Cheng
Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger,
sstabellini, xen-devel
On 04.12.2026 03:55, Stephen Cheng wrote:
> On 03/12/2026 13:18, Jan Beulich wrote:
>> + 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 be
min(curr_size - min(p - p0, ptr - p0)). Yet that extra complexity
doesn't look warranted.
Jan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
2026-12-04 8:11 ` Jan Beulich
@ 2026-12-04 9:03 ` Stephen Cheng
0 siblings, 0 replies; 5+ messages in thread
From: Stephen Cheng @ 2026-12-04 9:03 UTC (permalink / raw)
To: jbeulich
Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger,
sstabellini, stephen.cheng, xen-devel
On 04/12/2026 08:11, Jan Beulich wrote:
> 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 be
> min(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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 9:04 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 13:17 [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment Jan Beulich
2026-12-04 2:55 ` Stephen Cheng
2026-12-04 6:42 ` Jan Beulich
2026-12-04 8:11 ` Jan Beulich
2026-12-04 9:03 ` Stephen Cheng
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.