Skip to content

Commit

Permalink
Kernel: Recognize a stack pointer pointing to the top of stack as valid
Browse files Browse the repository at this point in the history
This also removes the explicit decrementing of the stack pointer in
`sys$create_thread` before passing it to `validate_user_stack`,
as it's unnecessary now.
  • Loading branch information
spholz authored and nico committed Dec 18, 2024
1 parent a6be79d commit 1d3a255
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Kernel/Memory/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,15 @@ bool MemoryManager::validate_user_stack(AddressSpace& space, VirtualAddress vadd
return false;

auto* region = find_user_region_from_vaddr(space, vaddr);
return region && region->is_user() && region->is_stack();
bool is_valid_user_stack = region && region->is_user() && region->is_stack();

// The stack pointer initially points to the exclusive end of the stack region.
if (!is_valid_user_stack) {
region = find_user_region_from_vaddr(space, vaddr.offset(-1));
is_valid_user_stack = region && region->range().end() == vaddr && region->is_user() && region->is_stack();
}

return is_valid_user_stack;
}

void MemoryManager::unregister_kernel_region(Region& region)
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Syscalls/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ErrorOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<Sys
return EOVERFLOW;

TRY(address_space().with([&](auto& space) -> ErrorOr<void> {
if (!MM.validate_user_stack(*space, VirtualAddress(user_sp.value() - 4)))
if (!MM.validate_user_stack(*space, VirtualAddress(user_sp.value())))
return EFAULT;
return {};
}));
Expand Down

0 comments on commit 1d3a255

Please sign in to comment.