feat(kernel): reject invalid sceKernelBatchMap2 entries with EINVAL

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Adrià Franch
2026-10-06 14:27:48 +02:00
parent 5895ea721a
commit ca6557aed1
2 changed files with 38 additions and 2 deletions
@@ -403,6 +403,10 @@ int APS5_VABI sceKernelBatchMap2(KernelBatchMapEntry* entries, int num_entries,
int result = 0;
for (; processed < num_entries; ++processed) {
auto& entry = entries[processed];
if (entry.length == 0 || entry.operation < OpMapDirect || entry.operation > OpTypeProtect) {
result = SCE_KERNEL_ERROR_EINVAL;
break;
}
switch (entry.operation) {
case OpMapDirect:
result = DoMapDirect(&entry.start, entry.length, static_cast<uint8_t>(entry.protection), flags, static_cast<int64_t>(entry.offset), 0);
@@ -417,8 +421,6 @@ int APS5_VABI sceKernelBatchMap2(KernelBatchMapEntry* entries, int num_entries,
case OpMapFlexible:
result = _mapFlexible(&entry.start, entry.length, static_cast<uint8_t>(entry.protection), flags);
break;
default:
throw std::invalid_argument("sceKernelBatchMap2: unsupported operation " + std::to_string(entry.operation));
}
if (result != 0) break;
}
+34
View File
@@ -61,6 +61,7 @@ int APS5_VABI sceKernelAioWaitRequest(std::int32_t, std::int32_t*, std::uint32_t
int APS5_VABI sceKernelAioDeleteRequest(std::int32_t, std::int32_t*);
int APS5_VABI sceKernelMlock_nid_postfix(void*, std::uint64_t);
int APS5_VABI sceKernelGetDirectMemoryType(std::int64_t, int*, std::int64_t*, std::int64_t*);
int APS5_VABI sceKernelBatchMap2(KernelBatchMapEntry*, int, int*, int);
}
static void Require(bool condition, std::source_location location = std::source_location::current()) {
@@ -123,6 +124,38 @@ static void CheckInternalNamedFlexibleMapping() {
Require(sceKernelAvailableFlexibleMemorySize(&available) == 0 && available == before);
}
static void CheckBatchMapStopsAtInvalidEntry() {
constexpr std::size_t page = 0x4000;
constexpr int mapFlexible = 3;
constexpr int unmap = 1;
constexpr int protect = 2;
const auto flexible = [&] { return KernelBatchMapEntry{nullptr, 0, page, 3, 0, 0, mapFlexible}; };
for (const std::int32_t operation : {5, 6, -1, std::numeric_limits<std::int32_t>::max()}) {
KernelBatchMapEntry entries[3] = {flexible(), flexible(), flexible()};
entries[1].operation = operation;
int processed = -1;
Require(sceKernelBatchMap2(entries, 3, &processed, 0) == SCE_KERNEL_ERROR_EINVAL);
Require(processed == 1);
Require(entries[0].start != nullptr && entries[1].start == nullptr && entries[2].start == nullptr);
Require(sceKernelMunmap(entries[0].start, page) == 0);
}
KernelBatchMapEntry entries[3] = {flexible(), flexible(), flexible()};
entries[1].operation = protect;
entries[1].length = 0;
int processed = -1;
Require(sceKernelBatchMap2(entries, 3, &processed, 0) == SCE_KERNEL_ERROR_EINVAL);
Require(processed == 1 && entries[0].start != nullptr);
Require(entries[1].start == nullptr && entries[2].start == nullptr);
KernelBatchMapEntry unmaps[2] = {entries[0], entries[0]};
unmaps[0].operation = unmap;
unmaps[1].operation = unmap;
unmaps[1].length = 0;
processed = -1;
Require(sceKernelBatchMap2(unmaps, 2, &processed, 0) == SCE_KERNEL_ERROR_EINVAL && processed == 1);
Require(sceKernelBatchMap2(entries, 1, &processed, 0) == 0 && processed == 1);
Require(sceKernelMunmap(entries[0].start, page) == 0);
}
static void CheckCheckedReleaseDirectMemory() {
constexpr std::size_t page = 0x4000;
std::int64_t phys = 0;
@@ -799,6 +832,7 @@ static void CheckDirectMemoryWriteWatch() {
int main() {
CheckNamedAndHintedMappings();
CheckInternalNamedFlexibleMapping();
CheckBatchMapStopsAtInvalidEntry();
CheckCheckedReleaseDirectMemory();
CheckDirectMemoryFollowsPhysicalPages();
CheckReleaseDirectMemoryClearsMappings();