]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/ukvmem: Return ENOMEM for invalid addresses
authorMarco Schlumpp <marco@unikraft.io>
Tue, 28 Nov 2023 08:50:36 +0000 (09:50 +0100)
committerUnikraft Bot <monkey@unikraft.io>
Wed, 30 Apr 2025 10:56:53 +0000 (10:56 +0000)
`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 <marco@unikraft.io>
Signed-off-by: Sergiu Moga <sergiu@unikraft.io>
Approved-by: Michalis Pappas <michalis@unikraft.io>
Reviewed-by: Michalis Pappas <michalis@unikraft.io>
GitHub-Closes: #1638

lib/ukvmem/vmem.c

index 692e5d98d0bec6f5accbfbc8d8b608e79614ca74..e483674a72505eb3ad0ac3aa3084a093319a5ccd 100644 (file)
@@ -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) {