mmap, munmap: Add more checks

This commit is contained in:
apio 2022-10-13 18:50:12 +02:00
parent 83e6b8cd21
commit 522aa2f812

View File

@ -14,7 +14,13 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
{ {
if (size < PAGE_SIZE) if (size < PAGE_SIZE)
{ {
kdbgln("sys_mmap: size too small"); kwarnln("mmap(): size too small");
context->rax = MAP_FAIL(EINVAL);
return;
}
if (size % PAGE_SIZE)
{
kwarnln("mmap(): size not a multiple of PAGE_SIZE");
context->rax = MAP_FAIL(EINVAL); context->rax = MAP_FAIL(EINVAL);
return; return;
} }
@ -22,11 +28,11 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
if (flags & MAP_READ_WRITE) real_flags |= MAP_READ_WRITE; if (flags & MAP_READ_WRITE) real_flags |= MAP_READ_WRITE;
if (address) if (address)
{ {
kdbgln("sys_mmap: %ld pages at address %p, %s", size / PAGE_SIZE, address, kdbgln("mmap(): %ld pages at address %p, %s", size / PAGE_SIZE, address,
real_flags & MAP_READ_WRITE ? "rw" : "ro"); real_flags & MAP_READ_WRITE ? "rw" : "ro");
if (VMM::get_physical((uint64_t)address) != (uint64_t)-1) // Address is already used. if (VMM::get_physical((uint64_t)address) != (uint64_t)-1) // Address is already used.
{ {
kdbgln("attempt to mmap an already mapped address"); kwarnln("attempt to map an already mapped address");
context->rax = MAP_FAIL(ENOMEM); context->rax = MAP_FAIL(ENOMEM);
return; return;
} }
@ -35,29 +41,29 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags); Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
if (result) if (result)
{ {
kdbgln("mmap succeeded: %p", result); kdbgln("mmap() succeeded: %p", result);
context->rax = (uint64_t)result; context->rax = (uint64_t)result;
return; return;
} }
else else
{ {
kdbgln("mmap failed"); kwarnln("mmap() failed");
context->rax = MAP_FAIL(ENOMEM); context->rax = MAP_FAIL(ENOMEM);
return; return;
} }
} }
kdbgln("sys_mmap: %ld pages at any address, %s", Utilities::get_blocks_from_size(PAGE_SIZE, size), kdbgln("mmap(): %ld pages at any address, %s", Utilities::get_blocks_from_size(PAGE_SIZE, size),
real_flags & MAP_READ_WRITE ? "rw" : "ro"); real_flags & MAP_READ_WRITE ? "rw" : "ro");
void* result = MemoryManager::get_pages(Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags); void* result = MemoryManager::get_pages(Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
if (result) if (result)
{ {
kdbgln("mmap succeeded: %p", result); kdbgln("mmap() succeeded: %p", result);
context->rax = (uint64_t)result; context->rax = (uint64_t)result;
return; return;
} }
else else
{ {
kdbgln("mmap failed"); kwarnln("mmap() failed");
context->rax = MAP_FAIL(ENOMEM); context->rax = MAP_FAIL(ENOMEM);
return; return;
} }
@ -65,29 +71,35 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
void sys_munmap(Context* context, void* address, size_t size) void sys_munmap(Context* context, void* address, size_t size)
{ {
kdbgln("sys_munmap: attempting to unmap %p", address); kdbgln("munmap(): attempting to unmap %p", address);
if (size < PAGE_SIZE) if (size < PAGE_SIZE)
{ {
kdbgln("munmap failed: size is too small"); kwarnln("munmap() failed: size is too small");
context->rax = -EINVAL;
return;
}
if (size % PAGE_SIZE)
{
kwarnln("munmap() failed: size is not a multiple of PAGE_SIZE");
context->rax = -EINVAL; context->rax = -EINVAL;
return; return;
} }
if (!address) if (!address)
{ {
kdbgln("munmap failed: attempted to unmap page 0"); kwarnln("munmap() failed: attempted to unmap page 0");
context->rax = -EINVAL; context->rax = -EINVAL;
return; return;
} }
uint64_t flags = VMM::get_flags((uint64_t)address); uint64_t flags = VMM::get_flags((uint64_t)address);
if (!(flags & MAP_USER)) if (!(flags & MAP_USER))
{ {
kdbgln("munmap failed: attempted to unmap a kernel page"); kwarnln("munmap() failed: attempted to unmap a non-existent or kernel page");
context->rax = -EINVAL; context->rax = -EINVAL;
return; return;
} }
uint64_t offset = (uint64_t)address % PAGE_SIZE; uint64_t offset = (uint64_t)address % PAGE_SIZE;
MemoryManager::release_pages((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size)); MemoryManager::release_pages((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size));
kdbgln("munmap succeeded"); kdbgln("munmap() succeeded");
context->rax = 0; context->rax = 0;
return; return;
} }