From 11350dfe656bb4850e21467846111d0a2915e10d Mon Sep 17 00:00:00 2001 From: Marco Schlumpp Date: Tue, 28 Nov 2023 09:50:36 +0100 Subject: [PATCH] lib/ukvmem: Return ENOMEM for invalid addresses `mmap` which calls this function can be called with non-canonical addresses. In this case, it should indicate an ENOMEM error. mozjs uses this to detect the amount of address bits. Signed-off-by: Marco Schlumpp Signed-off-by: Sergiu Moga Approved-by: Michalis Pappas Reviewed-by: Michalis Pappas GitHub-Closes: #1638 --- lib/ukvmem/vmem.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/ukvmem/vmem.c b/lib/ukvmem/vmem.c index 692e5d98d..e483674a7 100644 --- a/lib/ukvmem/vmem.c +++ b/lib/ukvmem/vmem.c @@ -665,7 +665,15 @@ int uk_vma_map(struct uk_vas *vas, __vaddr_t *vaddr, __sz len, UK_ASSERT(PAGE_Lx_ALIGNED(va, algn_lvl)); UK_ASSERT(va <= __VADDR_MAX - len); - UK_ASSERT(ukarch_vaddr_range_isvalid(va, len)); + + /* Applications can request invalid memory ranges in mmap. In case the + * address is not valid, then ENOMEM is the specfied error code. + * This should only happen rarely in practice, for example when JS + * engines (mozjs) do weird stuff to figure out the available address + * bits. + */ + if (unlikely(!ukarch_vaddr_range_isvalid(va, len))) + return -ENOMEM; /* Create a new VMA for the requested range. */ if (ops->new) { -- 2.39.5