[PATCH] commands: md: don't leak heap memory with unaligned accesses

Ahmad Fatoum ahmad at a3f.at
Fri Jun 18 22:42:17 PDT 2021

Doing unaligned near file end returns bogus results:

	barebox at Sandbox:/ md -s /dev/stickypage 4095
	00000fff: 959ff8ff                                           .

That 0x959ff8 is heap memory next to the buffer md had allocated.
Fix this by adding some zeroed padding after the buffer, so it instead

	00000fff: 000000ff                                           .

This is arguably better user experience.
This only applies to non-mappable fd's. For e.g. /dev/mem, unaligned
accesses will still be passed as-is.

Signed-off-by: Ahmad Fatoum <ahmad at a3f.at>
 commands/md.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commands/md.c b/commands/md.c
index ef6a1e1bc0b1..d80c7cca0ce4 100644
--- a/commands/md.c
+++ b/commands/md.c
@@ -59,7 +59,7 @@ static int do_mem_md(int argc, char *argv[])
 		goto out;
-	buf = xmalloc(RW_BUF_SIZE);
+	buf = xzalloc(RW_BUF_SIZE + 7);
 	do {
 		now = min(size, (loff_t)RW_BUF_SIZE);

More information about the barebox mailing list