From caa3fe8c457cae8046d3d8e87311895eedbcd541 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 5 Jan 2023 22:39:09 +0100 Subject: [PATCH] MemoryManager: Add validate_userspace_string() A bit crude, should be replaced by a strdup_from_user() helper to avoid touching userspace memory directly. But it'll do for now. --- kernel/src/memory/MemoryManager.cpp | 17 +++++++++++++++++ kernel/src/memory/MemoryManager.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 75014bae..9a834af2 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -347,6 +347,23 @@ namespace MemoryManager return false; } + // FIXME: Replace this with some kind of strdup_from_user() function. + bool validate_userspace_string(u64 address) + { + if (!validate_readable_page(address)) return false; + + while (*(char*)address != 0) + { + address++; + if (address % ARCH_PAGE_SIZE) + { + if (!validate_readable_page(address)) return false; + } + } + + return true; + } + usize free() { return free_mem; diff --git a/kernel/src/memory/MemoryManager.h b/kernel/src/memory/MemoryManager.h index d22ed45c..b2f72a84 100644 --- a/kernel/src/memory/MemoryManager.h +++ b/kernel/src/memory/MemoryManager.h @@ -20,6 +20,8 @@ namespace MemoryManager bool validate_readable_page(u64 address); bool validate_writable_page(u64 address); + bool validate_userspace_string(u64 address); + Result map_frames_at(u64 virt, u64 phys, usize count, int flags); Result alloc_at(u64 virt, usize count, int flags);