From baaff3958114378bec2f4be6694048daeabc5d73 Mon Sep 17 00:00:00 2001 From: slopgobblercobbler <336610228+slopgobblercobbler@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:08:53 +0100 Subject: [PATCH] test(libkernel): audit sysctl security and jail nodes, bounds, and ENOENT rejection --- core/libs/prx/libkernel/CMakeLists.txt | 11 + .../tests/GuestKernelSysctlSecurityNodes.cpp | 212 ++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 core/libs/tests/GuestKernelSysctlSecurityNodes.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index d4681e024232..041029a23fbe 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -3502,3 +3502,14 @@ list(SORT libkernelTestFragments) foreach(libkernelTestFragment IN LISTS libkernelTestFragments) include(${libkernelTestFragment}) endforeach() + + add_test_executable(guest_kernel_sysctl_security_nodes_tests ${LIBS_INCLUDE_DIR}/tests/GuestKernelSysctlSecurityNodes.cpp) + target_include_directories(guest_kernel_sysctl_security_nodes_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_kernel_sysctl_security_nodes_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_kernel_sysctl_security_nodes_tests) + add_test(NAME guest_kernel_sysctl_security_nodes COMMAND guest_kernel_sysctl_security_nodes_tests) + set_tests_properties(guest_kernel_sysctl_security_nodes PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_kernel_sysctl_security_nodes PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() diff --git a/core/libs/tests/GuestKernelSysctlSecurityNodes.cpp b/core/libs/tests/GuestKernelSysctlSecurityNodes.cpp new file mode 100644 index 000000000000..cfc05a7713cd --- /dev/null +++ b/core/libs/tests/GuestKernelSysctlSecurityNodes.cpp @@ -0,0 +1,212 @@ +#include "prx/libc/include/Api.hpp" +#include "prx/libkernel/include/Api.hpp" +#include +#include +#include +#include +#include +#include + +namespace { + +constexpr int PosixEperm = 1; +constexpr int PosixEnoent = 2; +constexpr int PosixEnomem = 12; +constexpr int PosixEfault = 14; +constexpr int PosixEnotdir = 20; +constexpr int PosixEinval = 22; + +void Require(bool condition) { + if (!condition) { + std::abort(); + } +} + +bool Untouched(const unsigned char* bytes, std::size_t size) { + for (std::size_t i = 0; i < size; ++i) { + if (bytes[i] != 0xaa) { + return false; + } + } + return true; +} + +template +bool Throws(TCall call) { + try { + call(); + } catch (const std::exception&) { + return true; + } + return false; +} + +void AuditSecurityAndJailSysctlbyname() { + const char* const securityNodes[] = { + "security.jail.jailed", + "kern.securelevel", + "kern.hostuuid", + "kern.hostid", + }; + + for (const char* name : securityNodes) { + std::size_t length = 64; + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(name, nullptr, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(length == 64); + + unsigned char buf[64]; + std::memset(buf, 0xaa, sizeof(buf)); + length = sizeof(buf); + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(name, buf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(length == sizeof(buf) && Untouched(buf, sizeof(buf))); + + length = 0; + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(name, buf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(length == 0); + + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(name, buf, nullptr, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(Untouched(buf, sizeof(buf))); + + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(name, nullptr, nullptr, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + + int writeVal = 1; + length = sizeof(buf); + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(name, buf, &length, &writeVal, sizeof(writeVal)) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + + std::string dotted = std::string(name) + "."; + length = sizeof(buf); + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(dotted.c_str(), buf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + + int mib[4] = {0x5a, 0x5a, 0x5a, 0x5a}; + std::size_t mibLen = 4; + *__error_nid_postfix() = 13; + Require(sysctlnametomib_nid_postfix(name, mib, &mibLen) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(mibLen == 4 && mib[0] == 0x5a && mib[1] == 0x5a); + + mibLen = 4; + *__error_nid_postfix() = 13; + Require(sysctlnametomib_nid_postfix(dotted.c_str(), mib, &mibLen) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + + mibLen = 0; + *__error_nid_postfix() = 13; + Require(sysctlnametomib_nid_postfix(name, nullptr, &mibLen) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(mibLen == 0); + } + + const char* const invalidSubnodes[] = { + "security.jail.jailed.0", + "security.jail.jailed.child", + "kern.securelevel.val", + "kern.hostuuid.invalid", + "kern.hostid.extra", + }; + for (const char* subnode : invalidSubnodes) { + unsigned char buf[32]; + std::memset(buf, 0xaa, sizeof(buf)); + std::size_t length = sizeof(buf); + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix(subnode, buf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(length == sizeof(buf) && Untouched(buf, sizeof(buf))); + + int mib[4] = {0x5a, 0x5a, 0x5a, 0x5a}; + std::size_t mibLen = 4; + *__error_nid_postfix() = 13; + Require(sysctlnametomib_nid_postfix(subnode, mib, &mibLen) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + } +} + +void AuditSecurityTopLevelAndMibBounds() { + int mib[4] = {0x5a, 0x5a, 0x5a, 0x5a}; + std::size_t mibLen = 4; + *__error_nid_postfix() = 13; + Require(sysctlnametomib_nid_postfix("security", mib, &mibLen) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + + std::size_t length = 0; + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix("security", nullptr, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + + *__error_nid_postfix() = 13; + Require(sysctlbyname_nid_postfix("security.", nullptr, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + + const int securityMib[] = {10, 1, 1}; + unsigned char mibBuf[32]; + std::memset(mibBuf, 0xaa, sizeof(mibBuf)); + length = sizeof(mibBuf); + *__error_nid_postfix() = 13; + Require(sysctl_nid_postfix(securityMib, 3, mibBuf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEnoent); + Require(length == sizeof(mibBuf) && Untouched(mibBuf, sizeof(mibBuf))); + + Require(Throws([&] { + int writeVal = 1; + std::size_t len = sizeof(mibBuf); + sysctl_nid_postfix(securityMib, 3, mibBuf, &len, &writeVal, sizeof(writeVal)); + })); + + const int unknownKernMib[] = {1, 9999}; + Require(Throws([&] { + std::size_t len = sizeof(mibBuf); + sysctl_nid_postfix(unknownKernMib, 2, mibBuf, &len, nullptr, 0); + })); + + length = sizeof(mibBuf); + *__error_nid_postfix() = 13; + Require(sysctl_nid_postfix(securityMib, 1, mibBuf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEinval); + + int longMib[25] = {10}; + *__error_nid_postfix() = 13; + Require(sysctl_nid_postfix(longMib, 25, mibBuf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEinval); + + *__error_nid_postfix() = 13; + Require(sysctl_nid_postfix(nullptr, 2, mibBuf, &length, nullptr, 0) == -1); + Require(*__error_nid_postfix() == PosixEfault); + + Require(Throws([&] { + int outMib[4]; + std::size_t size = 4; + sysctlnametomib_nid_postfix(nullptr, outMib, &size); + })); + + Require(Throws([&] { + int outMib[4]; + sysctlnametomib_nid_postfix("security.jail.jailed", outMib, nullptr); + })); + + Require(issetugid_nid_postfix() == 0); + Require(getuid_nid_postfix() >= 0); + Require(geteuid_nid_postfix() >= 0); + Require(getgid_nid_postfix() >= 0); + Require(getegid_nid_postfix() >= 0); +} + +} // namespace + +int main() { + AuditSecurityAndJailSysctlbyname(); + AuditSecurityTopLevelAndMibBounds(); + return 0; +}