From 1414b27d29701076411c069eb4952a3e59af57a5 Mon Sep 17 00:00:00 2001 From: Daniel Radosa <28@danielradosa.com> Date: Thu, 8 Oct 2026 20:10:49 +0200 Subject: [PATCH] feat(libkernel): implement seteuid and setegid The guest process is assumed to run as root. Under FreeBSD's sys_seteuid and sys_setegid, id 0 is then accepted without a privilege check and changes no credential, so both return 0. Any other id would change the effective credential, which is not modelled, and throws. Part of #1578. --- core/libs/prx/libkernel/CMakeLists.txt | 10 ++++++ .../libs/prx/libkernel/System/src/Process.cpp | 12 +++++++ core/libs/tests/GuestCredentials.cpp | 31 +++++++++++++++++++ docs/dev/TechnicalDebt.md | 1 + 4 files changed, 54 insertions(+) create mode 100644 core/libs/tests/GuestCredentials.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 1fe4e1e79..b904f5784 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -428,6 +428,16 @@ if(BUILD_TESTING) set_tests_properties(guest_elf_phdr_match PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_credentials_tests ${LIBS_INCLUDE_DIR}/tests/GuestCredentials.cpp) + target_include_directories(guest_credentials_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_credentials_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_credentials_tests) + add_test(NAME guest_credentials COMMAND guest_credentials_tests) + set_tests_properties(guest_credentials PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_credentials PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_address_tests ${LIBS_INCLUDE_DIR}/tests/GuestAddressConversion.cpp) target_include_directories(guest_address_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_address_tests PRIVATE libkernel libc) diff --git a/core/libs/prx/libkernel/System/src/Process.cpp b/core/libs/prx/libkernel/System/src/Process.cpp index 13e447cd1..63e4b49e8 100644 --- a/core/libs/prx/libkernel/System/src/Process.cpp +++ b/core/libs/prx/libkernel/System/src/Process.cpp @@ -290,4 +290,16 @@ int APS5_VABI sceKernelGetOperationMode(int* mode, int* submode) { return 0; } +int APS5_VABI seteuid_nid_postfix(std::uint32_t euid) { + if (euid != 0) + throw std::runtime_error("seteuid: unsupported user id " + std::to_string(euid)); + return 0; +} + +int APS5_VABI setegid_nid_postfix(std::uint32_t egid) { + if (egid != 0) + throw std::runtime_error("setegid: unsupported group id " + std::to_string(egid)); + return 0; +} + } diff --git a/core/libs/tests/GuestCredentials.cpp b/core/libs/tests/GuestCredentials.cpp new file mode 100644 index 000000000..aa2c8544f --- /dev/null +++ b/core/libs/tests/GuestCredentials.cpp @@ -0,0 +1,31 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +extern "C" { +int APS5_VABI seteuid_nid_postfix(std::uint32_t); +int APS5_VABI setegid_nid_postfix(std::uint32_t); +int* APS5_VABI __error_nid_postfix(); +} +using SetId = int (APS5_VABI *)(std::uint32_t); +static void Require(bool value) { if (!value) std::abort(); } +static bool Throws(SetId function, std::uint32_t id) { + try { + function(id); + } catch (const std::runtime_error&) { + return true; + } + return false; +} +int main() { + *__error_nid_postfix() = 13; + Require(seteuid_nid_postfix(0) == 0); + Require(setegid_nid_postfix(0) == 0); + Require(*__error_nid_postfix() == 13); + Require(Throws(seteuid_nid_postfix, 1000)); + Require(Throws(setegid_nid_postfix, 1000)); + Require(Throws(seteuid_nid_postfix, 0xFFFFFFFFu)); + Require(Throws(setegid_nid_postfix, 0xFFFFFFFFu)); + Require(seteuid_nid_postfix(0) == 0); + Require(setegid_nid_postfix(0) == 0); +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 8f2178189..d3b428f86 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -135,6 +135,7 @@ Throughout the project, every function at every stage either **does exactly what - [_sceLibcInternalForceTlsDestructor](../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) - behaviour follows `sceLibcForceTlsDestructor(int)` in a title's `libc.prx` (disassembly): the calling thread's destructors whose dso belongs to the module run newest first, only if the dso's module is loaded and the dso points to itself, in up to 4 passes. The return type is unknown (that function returns nothing), 0 is returned. The module of each dso is looked up when the destructors are forced, not when they are registered as `libc.prx` does, and the lookup is Linux only, so on Windows the call throws when the thread has destructors - [std_execute_once](../../core/libs/prx/libc/Export.cpp) (libc) - assumed to be the plain-named twin of `std::_Execute_once` (`_ZSt13_Execute_onceRSt9once_flagPFiPvS1_PS1_ES1_`, the callee of Dinkumware `call_once`): it delegates there and gets the [thread.once.callonce] semantics already verified for that symbol (one invocation per flag, concurrent callers block until it completes, and a failing or throwing invocation leaves the flag unready so the next caller runs it). The name equivalence is not confirmed on a title, and the callback convention (called with (null, arg, null), non-zero return meaning success) is the twin's, unverified for this symbol - [__progname](../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) - unknown data export +- [seteuid, setegid](../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) - the guest process is assumed to run as root, with real, effective and saved user and group ids 0. As in FreeBSD's `sys_seteuid` and `sys_setegid`, id 0 is then accepted without a privilege check and changes no credential, so 0 is returned; any other id would change the effective id, which is not modelled, and throws. The ids and privileges a title has on a console are unverified - [pthread_barrierattr_setpshared](../../core/libs/prx/libkernel/Pthread/Posix/Barrier.cpp) (libkernel) - PTHREAD_PROCESS_SHARED is accepted on the assumption that the console follows FreeBSD 11.0 libthr, which accepts it (9.0 rejects it with EINVAL); unverified on hardware. Guest code runs in one process, so a shared barrier behaves as a private one - [sceSslClose](../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - unknown signature - [sceSslGetSerialNumber](../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - unknown signature