Repository navigation
test(libkernel): audit sysctl security and jail nodes, bounds, and ENOENT rejection #630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thimo188
wants to merge
3
commits into
main
Choose a base branch
from
upstream-pr/21026
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| #include "prx/libc/include/Api.hpp" | ||
| #include "prx/libkernel/include/Api.hpp" | ||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <stdexcept> | ||
|
|
||
| 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 <typename TCall> | ||
| 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; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard this test with
BUILD_TESTING.This block is outside the enclosing
if(BUILD_TESTING). The root helper creates the executable unconditionally. Thus,BUILD_TESTING=OFF, which is the default, still compiles and links this test. Wrap the complete block inif(BUILD_TESTING)so disabled-test builds exclude it and enabled-test builds retain the registration. (raw.githubusercontent.com)