kernel/svc: Sanitize heap sizes within svcSetHeapSize()

The kernel checks if the given size is a multiple of 2MB and <= to 4GB
before going ahead and attempting to allocate that much memory.
This commit is contained in:
Lioncash 2018-09-13 19:09:04 -04:00
parent 8e7497d5bb
commit 7bd2faad9a
2 changed files with 8 additions and 0 deletions

View File

@ -17,6 +17,7 @@ enum {
// Confirmed Switch OS error codes // Confirmed Switch OS error codes
MaxConnectionsReached = 7, MaxConnectionsReached = 7,
InvalidSize = 101,
InvalidAddress = 102, InvalidAddress = 102,
HandleTableFull = 105, HandleTableFull = 105,
InvalidMemoryState = 106, InvalidMemoryState = 106,
@ -55,6 +56,7 @@ constexpr ResultCode ERR_INVALID_MEMORY_PERMISSIONS(ErrorModule::Kernel,
ErrCodes::InvalidMemoryPermissions); ErrCodes::InvalidMemoryPermissions);
constexpr ResultCode ERR_INVALID_HANDLE(ErrorModule::Kernel, ErrCodes::InvalidHandle); constexpr ResultCode ERR_INVALID_HANDLE(ErrorModule::Kernel, ErrCodes::InvalidHandle);
constexpr ResultCode ERR_INVALID_PROCESSOR_ID(ErrorModule::Kernel, ErrCodes::InvalidProcessorId); constexpr ResultCode ERR_INVALID_PROCESSOR_ID(ErrorModule::Kernel, ErrCodes::InvalidProcessorId);
constexpr ResultCode ERR_INVALID_SIZE(ErrorModule::Kernel, ErrCodes::InvalidSize);
constexpr ResultCode ERR_INVALID_STATE(ErrorModule::Kernel, ErrCodes::InvalidState); constexpr ResultCode ERR_INVALID_STATE(ErrorModule::Kernel, ErrCodes::InvalidState);
constexpr ResultCode ERR_INVALID_THREAD_PRIORITY(ErrorModule::Kernel, constexpr ResultCode ERR_INVALID_THREAD_PRIORITY(ErrorModule::Kernel,
ErrCodes::InvalidThreadPriority); ErrCodes::InvalidThreadPriority);

View File

@ -39,6 +39,12 @@ namespace Kernel {
/// Set the process heap to a given Size. It can both extend and shrink the heap. /// Set the process heap to a given Size. It can both extend and shrink the heap.
static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size); LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size);
// Size must be a multiple of 0x200000 (2MB) and be equal to or less than 4GB.
if ((heap_size & 0xFFFFFFFE001FFFFF) != 0) {
return ERR_INVALID_SIZE;
}
auto& process = *Core::CurrentProcess(); auto& process = *Core::CurrentProcess();
CASCADE_RESULT(*heap_addr, CASCADE_RESULT(*heap_addr,
process.HeapAllocate(Memory::HEAP_VADDR, heap_size, VMAPermission::ReadWrite)); process.HeapAllocate(Memory::HEAP_VADDR, heap_size, VMAPermission::ReadWrite));