mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-12-10 05:38:49 +00:00
Core: Handle various edge cases related to executable permissions. (#3660)
* Fix flag handling on Windows Fixes a weird homebrew kalaposfos made * Fix backing protects Windows requires that protections on areas committed through MapViewOfFile functions are less than the original mapping. The best way to make sure everything works is to VirtualProtect the code area with the requested protection instead of applying prot directly. * Fix error code for sceKernelMapDirectMemory2 Real hardware returns EINVAL instead of EACCES here * Fix prot setting in ProtectBytes * Handle some extra protection-related edge cases. Real hardware treats read and write as separate perms, but appends read if you call with write-only (this is visible in VirtualQuery calls) Additionally, execute permissions are ignored when protecting dmem mappings. * Properly handle exec permission behavior for memory pools Calling sceKernelMemoryPoolCommit with executable permissions returns EINVAL, mprotect on pooled mappings ignores the exec protection. * Clang * Allow execution protection for direct memory Further hardware tests show that the dmem area is actually executable, this permission is just hidden from the end user. * Clang * More descriptive assert message * Align address and size in mmap Like most POSIX functions, mmap aligns address down to the nearest page boundary, and aligns address up to the nearest page boundary. Since mmap is the only memory mapping function that doesn't error early on misaligned length or size, handle the alignment in the libkernel code. * Clang * Fix valid flags After changing the value, games that specify just CpuWrite would hit the error return. * Fix prot conversion functions The True(bool) function returns true whenever value is greater than 0. While this rarely manifested before because of our wrongly defined CpuReadWrite prot, it's now causing trouble with the corrected values. Technically this could've also caused trouble with games mapping GpuRead permissions, but that seems to be a rare enough use case that I guess it never happened? I've also added a warning for the case where `write & !read`, since we don't properly handle write-only permissions, and I'm not entirely sure what it would take to deal with that. * Fix some lingering dmem issues ReleaseDirectMemory was always unmapping with the size parameter, which could cause it to unmap too much. Since multiple mappings can reference the same dmem area, I've calculated how much of each VMA we're supposed to unmap. Additionally, I've adjusted the logic for carving out the free dmem area to properly work if ReleaseDirectMemory is called over multiple dmem areas. Finally, I've patched a bug with my code in UnmapMemory.
This commit is contained in:
@@ -237,7 +237,7 @@ s32 PS4_SYSV_ABI sceKernelMapDirectMemory2(void** addr, u64 len, s32 type, s32 p
|
||||
const auto mem_prot = static_cast<Core::MemoryProt>(prot);
|
||||
if (True(mem_prot & Core::MemoryProt::CpuExec)) {
|
||||
LOG_ERROR(Kernel_Vmm, "Executable permissions are not allowed.");
|
||||
return ORBIS_KERNEL_ERROR_EACCES;
|
||||
return ORBIS_KERNEL_ERROR_EINVAL;
|
||||
}
|
||||
|
||||
const auto map_flags = static_cast<Core::MemoryMapFlags>(flags);
|
||||
@@ -537,11 +537,16 @@ s32 PS4_SYSV_ABI sceKernelMemoryPoolCommit(void* addr, u64 len, s32 type, s32 pr
|
||||
return ORBIS_KERNEL_ERROR_EINVAL;
|
||||
}
|
||||
|
||||
const auto mem_prot = static_cast<Core::MemoryProt>(prot);
|
||||
if (True(mem_prot & Core::MemoryProt::CpuExec)) {
|
||||
LOG_ERROR(Kernel_Vmm, "Executable permissions are not allowed.");
|
||||
return ORBIS_KERNEL_ERROR_EINVAL;
|
||||
}
|
||||
|
||||
LOG_INFO(Kernel_Vmm, "addr = {}, len = {:#x}, type = {:#x}, prot = {:#x}, flags = {:#x}",
|
||||
fmt::ptr(addr), len, type, prot, flags);
|
||||
|
||||
const VAddr in_addr = reinterpret_cast<VAddr>(addr);
|
||||
const auto mem_prot = static_cast<Core::MemoryProt>(prot);
|
||||
auto* memory = Core::Memory::Instance();
|
||||
return memory->PoolCommit(in_addr, len, mem_prot, type);
|
||||
}
|
||||
@@ -651,13 +656,18 @@ void* PS4_SYSV_ABI posix_mmap(void* addr, u64 len, s32 prot, s32 flags, s32 fd,
|
||||
const auto mem_prot = static_cast<Core::MemoryProt>(prot);
|
||||
const auto mem_flags = static_cast<Core::MemoryMapFlags>(flags);
|
||||
|
||||
// mmap is less restrictive than other functions in regards to alignment
|
||||
// To avoid potential issues, align address and size here.
|
||||
const VAddr aligned_addr = Common::AlignDown(std::bit_cast<VAddr>(addr), 16_KB);
|
||||
const u64 aligned_size = Common::AlignUp(len, 16_KB);
|
||||
|
||||
s32 result = ORBIS_OK;
|
||||
if (fd == -1) {
|
||||
result = memory->MapMemory(&addr_out, std::bit_cast<VAddr>(addr), len, mem_prot, mem_flags,
|
||||
result = memory->MapMemory(&addr_out, aligned_addr, aligned_size, mem_prot, mem_flags,
|
||||
Core::VMAType::Flexible, "anon", false);
|
||||
} else {
|
||||
result = memory->MapFile(&addr_out, std::bit_cast<VAddr>(addr), len, mem_prot, mem_flags,
|
||||
fd, phys_addr);
|
||||
result = memory->MapFile(&addr_out, aligned_addr, aligned_size, mem_prot, mem_flags, fd,
|
||||
phys_addr);
|
||||
}
|
||||
|
||||
if (result != ORBIS_OK) {
|
||||
|
||||
Reference in New Issue
Block a user