From bc353f454b02d45a403051677fcd6213bc2a094d Mon Sep 17 00:00:00 2001 From: tkchia Date: Fri, 7 Oct 2022 07:27:52 +0800 Subject: [PATCH] Fix sys_mmap_metal() for non-MAP_FIXED case (#651) - correctly check that virtual region we want to use is unmapped, rather than accidentally clobbering existing pages - avoid placing mmap'd memory at null virtual address --- libc/intrin/directmap-metal.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libc/intrin/directmap-metal.c b/libc/intrin/directmap-metal.c index d7d0550e4..4616340ff 100644 --- a/libc/intrin/directmap-metal.c +++ b/libc/intrin/directmap-metal.c @@ -39,8 +39,10 @@ noasan struct DirectMap sys_mmap_metal(void *paddr, size_t size, int prot, size = ROUNDUP(size, 4096); addr = (uint64_t)paddr; if (!(flags & MAP_FIXED)) { + if (!addr) + addr = 4096; for (i = 0; i < size; i += 4096) { - pte = __get_virtual(mm, pml4t, addr, false); + pte = __get_virtual(mm, pml4t, addr + i, false); if (pte && (*pte & PAGE_V)) { addr = MAX(addr, sys_mmap_metal_break) + i + 4096; i = 0;