From 77694f9225e669df054dfdc0810d0119dcbece61 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Fri, 3 Jun 2022 15:35:17 -0400 Subject: [PATCH] kernel: Move validate_user_memory_range to kernel.h and rename it. It has more general use than just in the VM code; basically anything which receives buffers from userland should be invoking this if it does anything besides user_memcpy (which alreay does it.) --- headers/private/kernel/kernel.h | 17 +++++++++++++++++ src/system/kernel/vm/vm.cpp | 23 ++++------------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/headers/private/kernel/kernel.h b/headers/private/kernel/kernel.h index 36ace801fb..981f37a892 100644 --- a/headers/private/kernel/kernel.h +++ b/headers/private/kernel/kernel.h @@ -40,6 +40,23 @@ ((addr_t)(x) >= USER_BASE && (addr_t)(x) <= USER_TOP) #endif +#ifdef __cplusplus +// Validate that an address range is fully in userspace. +static inline bool +is_user_address_range(const void* addr, size_t size) +{ + addr_t address = (addr_t)addr; + + // Check for overflows on all addresses. + if ((address + size) < address) + return false; + + // Validate that both the start and end address are in userspace + return IS_USER_ADDRESS(address) && IS_USER_ADDRESS(address + size - 1); +} +#endif + + #define DEBUG_KERNEL_STACKS // Note, debugging kernel stacks doesn't really work yet. Since the // interrupt will also try to use the stack on a page fault, all diff --git a/src/system/kernel/vm/vm.cpp b/src/system/kernel/vm/vm.cpp index 75f058852e..941232c851 100644 --- a/src/system/kernel/vm/vm.cpp +++ b/src/system/kernel/vm/vm.cpp @@ -5433,21 +5433,6 @@ validate_memory_range(const void* addr, size_t size) } -/** Validate that a memory range is fully in userspace. */ -static inline bool -validate_user_memory_range(const void* addr, size_t size) -{ - addr_t address = (addr_t)addr; - - // Check for overflows on all addresses. - if ((address + size) < address) - return false; - - // Validate that both the start and end address are in userspace - return IS_USER_ADDRESS(address) && IS_USER_ADDRESS(address + size - 1); -} - - // #pragma mark - kernel public API @@ -6626,7 +6611,7 @@ _user_set_memory_protection(void* _address, size_t size, uint32 protection) if ((address % B_PAGE_SIZE) != 0) return B_BAD_VALUE; - if (!validate_user_memory_range(_address, size)) { + if (!is_user_address_range(_address, size)) { // weird error code required by POSIX return ENOMEM; } @@ -6777,7 +6762,7 @@ _user_sync_memory(void* _address, size_t size, uint32 flags) // check params if ((address % B_PAGE_SIZE) != 0) return B_BAD_VALUE; - if (!validate_user_memory_range(_address, size)) { + if (!is_user_address_range(_address, size)) { // weird error code required by POSIX return ENOMEM; } @@ -6855,7 +6840,7 @@ _user_memory_advice(void* _address, size_t size, uint32 advice) return B_BAD_VALUE; size = PAGE_ALIGN(size); - if (!validate_user_memory_range(_address, size)) { + if (!is_user_address_range(_address, size)) { // weird error code required by POSIX return B_NO_MEMORY; } @@ -6932,7 +6917,7 @@ user_set_memory_swappable(const void* _address, size_t size, bool swappable) if ((address % B_PAGE_SIZE) != 0) return EINVAL; - if (!validate_user_memory_range(_address, size)) + if (!is_user_address_range(_address, size)) return EINVAL; const addr_t endAddress = address + size;