Merge pull request #274 from RomainROCH/codex/fix-directory-buffer-errors

fix(libkernel): report undersized Windows directory buffers
This commit is contained in:
boykopovar
2026-10-02 22:39:37 +03:00
committed by GitHub
4 changed files with 78 additions and 4 deletions
+9
View File
@@ -222,6 +222,15 @@ if(BUILD_TESTING)
set_tests_properties(guest_memory PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
if(WIN32)
add_test_executable(guest_directory_buffer_tests ${LIBS_INCLUDE_DIR}/tests/GuestDirectoryBuffer.cpp)
target_include_directories(guest_directory_buffer_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_directory_buffer_tests PRIVATE libkernel libc)
configure_windows_unwind(guest_directory_buffer_tests)
add_test(NAME guest_directory_buffer COMMAND guest_directory_buffer_tests)
set_tests_properties(guest_directory_buffer PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
if(NOT WIN32)
add_test_executable(guest_directory_entry_tests ${LIBS_INCLUDE_DIR}/tests/GuestDirectoryEntries.cpp)
target_include_directories(guest_directory_entry_tests PRIVATE ${LIBS_INCLUDE_DIR})
@@ -1,4 +1,5 @@
#include "prx/libkernel/File/include/DirectoryDescriptor.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#ifdef _WIN32
@@ -54,7 +55,7 @@ void ForgetDirectoryDescriptor(int fd) {
int ReadDirectoryDescriptor(int fd, char* buf, int nbytes) {
std::lock_guard lock(g_mutex);
const auto found = g_directories.find(fd);
if (found == g_directories.end()) return -1;
if (found == g_directories.end()) return SCE_KERNEL_ERROR_ENOTDIR;
auto& state = found->second;
if (!state.loaded) {
state.loaded = true;
@@ -70,7 +71,10 @@ int ReadDirectoryDescriptor(int fd, char* buf, int nbytes) {
while (state.cursor < state.entries.size()) {
const auto& [name, type] = state.entries[state.cursor];
const std::size_t record = (8 + name.size() + 1 + 3) & ~std::size_t{3};
if (used + record > static_cast<std::size_t>(nbytes)) break;
if (used + record > static_cast<std::size_t>(nbytes)) {
if (used == 0) return SCE_KERNEL_ERROR_EINVAL;
break;
}
char* out = buf + used;
std::memset(out, 0, record);
const auto fileNumber = static_cast<std::uint32_t>(state.cursor + 1);
+1 -2
View File
@@ -320,8 +320,7 @@ int APS5_VABI sceKernelGetdirentries(int fd, char* buf, int nbytes, int64_t* bas
if (buf == nullptr) return SceErrorFromErrno(GUEST_EFAULT);
if (nbytes <= 0) return SceErrorFromErrno(GUEST_EINVAL);
if (basep != nullptr) *basep = 0;
const int written = File::ReadDirectoryDescriptor(fd, buf, nbytes);
return written < 0 ? SceErrorFromErrno(GUEST_ENOTDIR) : written;
return File::ReadDirectoryDescriptor(fd, buf, nbytes);
}
int APS5_VABI sceKernelGetdents(int fd, char* buf, int nbytes) {
+62
View File
@@ -0,0 +1,62 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/File/include/FileFlags.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <string>
extern "C" {
int APS5_VABI sceKernelOpen(const char*, int, std::uint16_t);
int APS5_VABI sceKernelClose(int);
int APS5_VABI sceKernelGetdents(int, char*, int);
int APS5_VABI sceKernelGetdirentries(int, char*, int, std::int64_t*);
}
static void Check(bool value, int line) {
if (!value) {
std::fprintf(stderr, "Directory buffer check failed at line %d\n", line);
std::abort();
}
}
#define Require(value) Check((value), __LINE__)
int main() {
const auto root = std::filesystem::temp_directory_path() /
("anyps5-directory-buffer-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
Require(std::filesystem::create_directory(root));
const std::string name = "a-long-directory-entry-name.bin";
{ std::ofstream file(root / name); Require(static_cast<bool>(file)); }
const int directory = sceKernelOpen(root.string().c_str(), SCE_KERNEL_O_RDONLY | SCE_KERNEL_O_DIRECTORY, 0);
Require(directory >= 0);
std::array<char, 256> buffer;
buffer.fill('x');
std::int64_t base = -1;
Require(sceKernelGetdirentries(directory, buffer.data(), 8, &base) == SCE_KERNEL_ERROR_EINVAL);
Require(std::all_of(buffer.begin(), buffer.end(), [](char byte) { return byte == 'x'; }));
Require(sceKernelGetdents(directory, buffer.data(), 8) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelGetdents(directory, nullptr, 256) == SCE_KERNEL_ERROR_EFAULT);
Require(sceKernelGetdents(directory, buffer.data(), 0) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelGetdents(directory, buffer.data(), -1) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelGetdents(directory, buffer.data(), 20) == 12);
Require(std::strcmp(buffer.data() + 8, ".") == 0);
Require(sceKernelGetdents(directory, buffer.data(), 12) == 12);
Require(std::strcmp(buffer.data() + 8, "..") == 0);
buffer.fill('x');
Require(sceKernelGetdents(directory, buffer.data(), 12) == SCE_KERNEL_ERROR_EINVAL);
Require(std::all_of(buffer.begin(), buffer.end(), [](char byte) { return byte == 'x'; }));
const int recordSize = static_cast<int>((8 + name.size() + 1 + 3) & ~std::size_t{3});
Require(sceKernelGetdirentries(directory, buffer.data(), recordSize, &base) == recordSize);
Require(std::strcmp(buffer.data() + 8, name.c_str()) == 0);
Require(sceKernelGetdents(directory, buffer.data(), 8) == 0);
Require(sceKernelGetdents(directory, buffer.data(), 256) == 0);
Require(sceKernelClose(directory) == 0);
Require(sceKernelGetdents(-1, buffer.data(), 256) == SCE_KERNEL_ERROR_ENOTDIR);
std::filesystem::remove_all(root);
}