diff options
author | Andreas Kling <kling@serenityos.org> | 2021-02-14 09:57:19 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-14 10:01:50 +0100 |
commit | 09b1b09c1923c5e82c5500a06c501088ab5ac4ce (patch) | |
tree | f57307d460c8856604dfa21f3ab27b7a449cd4bc /Kernel/Syscalls | |
parent | 198d64180886e6fad2997513c4c8f68b1338f4e4 (diff) | |
download | serenity-09b1b09c1923c5e82c5500a06c501088ab5ac4ce.zip |
Kernel: Assert if rounding-up-to-page-size would wrap around to 0
If we try to align a number above 0xfffff000 to the next multiple of
the page size (4 KiB), it would wrap around to 0. This is most likely
never what we want, so let's assert if that happens.
Diffstat (limited to 'Kernel/Syscalls')
-rw-r--r-- | Kernel/Syscalls/execve.cpp | 6 | ||||
-rw-r--r-- | Kernel/Syscalls/mmap.cpp | 22 |
2 files changed, 20 insertions, 8 deletions
diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 5ddc338303..033c8c1f0e 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -167,7 +167,7 @@ static KResultOr<RequiredLoadRange> get_required_load_range(FileDescription& pro size_t executable_size = inode.size(); - auto region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_ROUND_UP(executable_size), "ELF memory range calculation", Region::Access::Read); + auto region = MM.allocate_kernel_region_with_vmobject(*vmobject, page_round_up(executable_size), "ELF memory range calculation", Region::Access::Read); if (!region) { dbgln("Could not allocate memory for ELF"); return ENOMEM; @@ -203,7 +203,7 @@ static KResultOr<FlatPtr> get_interpreter_load_offset(const Elf32_Ehdr& main_pro constexpr FlatPtr minimum_interpreter_load_offset_randomization_size = 10 * MiB; auto random_load_offset_in_range([](auto start, auto size) { - return PAGE_ROUND_DOWN(start + get_good_random<FlatPtr>() % size); + return page_round_down(start + get_good_random<FlatPtr>() % size); }); if (main_program_header.e_type == ET_DYN) { @@ -263,7 +263,7 @@ static KResultOr<LoadResult> load_elf_object(NonnullOwnPtr<Space> new_space, Fil size_t executable_size = inode.size(); - auto executable_region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_ROUND_UP(executable_size), "ELF loading", Region::Access::Read); + auto executable_region = MM.allocate_kernel_region_with_vmobject(*vmobject, page_round_up(executable_size), "ELF loading", Region::Access::Read); if (!executable_region) { dbgln("Could not allocate memory for ELF loading"); return ENOMEM; diff --git a/Kernel/Syscalls/mmap.cpp b/Kernel/Syscalls/mmap.cpp index 727535ed27..b380858300 100644 --- a/Kernel/Syscalls/mmap.cpp +++ b/Kernel/Syscalls/mmap.cpp @@ -160,7 +160,10 @@ void* Process::sys$mmap(Userspace<const Syscall::SC_mmap_params*> user_params) if (alignment & ~PAGE_MASK) return (void*)-EINVAL; - if (!is_user_range(VirtualAddress(addr), PAGE_ROUND_UP(size))) + if (page_round_up_would_wrap(size)) + return (void*)-EINVAL; + + if (!is_user_range(VirtualAddress(addr), page_round_up(size))) return (void*)-EFAULT; String name; @@ -204,7 +207,7 @@ void* Process::sys$mmap(Userspace<const Syscall::SC_mmap_params*> user_params) Optional<Range> range; if (map_randomized) { - range = space().page_directory().range_allocator().allocate_randomized(PAGE_ROUND_UP(size), alignment); + range = space().page_directory().range_allocator().allocate_randomized(page_round_up(size), alignment); } else { range = space().allocate_range(VirtualAddress(addr), size, alignment); if (!range.has_value()) { @@ -272,7 +275,10 @@ int Process::sys$mprotect(void* addr, size_t size, int prot) REQUIRE_PROMISE(prot_exec); } - Range range_to_mprotect = { VirtualAddress((FlatPtr)addr & PAGE_MASK), PAGE_ROUND_UP(size) }; + if (page_round_up_would_wrap(size)) + return -EINVAL; + + Range range_to_mprotect = { VirtualAddress((FlatPtr)addr & PAGE_MASK), page_round_up(size) }; if (!range_to_mprotect.size()) return -EINVAL; @@ -343,7 +349,10 @@ int Process::sys$madvise(void* address, size_t size, int advice) { REQUIRE_PROMISE(stdio); - Range range_to_madvise { VirtualAddress((FlatPtr)address & PAGE_MASK), PAGE_ROUND_UP(size) }; + if (page_round_up_would_wrap(size)) + return -EINVAL; + + Range range_to_madvise { VirtualAddress((FlatPtr)address & PAGE_MASK), page_round_up(size) }; if (!range_to_madvise.size()) return -EINVAL; @@ -415,7 +424,10 @@ int Process::sys$munmap(void* addr, size_t size) if (!size) return -EINVAL; - Range range_to_unmap { VirtualAddress(addr), PAGE_ROUND_UP(size) }; + if (page_round_up_would_wrap(size)) + return -EINVAL; + + Range range_to_unmap { VirtualAddress(addr), page_round_up(size) }; if (!is_user_range(range_to_unmap)) return -EFAULT; |