|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
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
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |