diff --git a/include/Void/Testing/Subprocess.hpp b/include/Void/Testing/Subprocess.hpp index 72c01f8..b53dcc3 100644 --- a/include/Void/Testing/Subprocess.hpp +++ b/include/Void/Testing/Subprocess.hpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include #include @@ -9,9 +11,21 @@ namespace Void::Testing { +/// Per-child resource limits applied before exec. Unset values are left at +/// the parent's limits. +struct ProcessResourceLimits { + std::optional cpu_seconds{}; + std::optional address_space_bytes{}; + std::optional file_size_bytes{}; + std::optional open_files{}; + /// Prevent a crashing child from writing a core file. + bool disable_core_dumps{true}; +}; + struct ProcessTestOptions { std::chrono::milliseconds timeout{60000}; std::size_t max_output_bytes{1024ULL * 1024ULL}; + ProcessResourceLimits resources{}; }; enum class ProcessOutcome { diff --git a/src/Testing/Runner.cpp b/src/Testing/Runner.cpp index c96dc3b..0b5a4dc 100644 --- a/src/Testing/Runner.cpp +++ b/src/Testing/Runner.cpp @@ -944,6 +944,14 @@ namespace Void::Testing std::this_thread::sleep_for(std::chrono::seconds{5}); return 0; } + if (config.selftest_child == "alloc") { + // Touch a large buffer so an RLIMIT_AS cap is exercised. + constexpr std::size_t kAllocBytes = 256ULL * 1024ULL * 1024ULL; + std::vector big(kAllocBytes, 'x'); + volatile char sink = big[kAllocBytes - 1]; + (void)sink; + return 0; + } if (config.selftest_child == "flood") { const std::string out_chunk(131072, 'O'); const std::string err_chunk(131072, 'E'); diff --git a/src/Testing/Subprocess.cpp b/src/Testing/Subprocess.cpp index 9dc2f73..0d7e356 100644 --- a/src/Testing/Subprocess.cpp +++ b/src/Testing/Subprocess.cpp @@ -13,6 +13,7 @@ #ifndef _WIN32 #include #include +#include #include #include #include @@ -156,6 +157,35 @@ ProcessTestResult run_isolated_subprocess( (void)::dup2(err_pipe[1], STDERR_FILENO); close_fd_safe(out_pipe[1]); close_fd_safe(err_pipe[1]); + + const auto limit = [](int resource, std::uint64_t soft, + std::uint64_t hard) { + const struct rlimit value{static_cast(soft), + static_cast(hard)}; + (void)::setrlimit(resource, &value); + }; + const auto& res_limits = options.resources; + if (res_limits.disable_core_dumps) { + limit(RLIMIT_CORE, 0, 0); + } + if (res_limits.cpu_seconds) { + // Soft limit delivers SIGXCPU; the hard limit is the backstop. + limit(RLIMIT_CPU, *res_limits.cpu_seconds, + *res_limits.cpu_seconds + 1); + } + if (res_limits.address_space_bytes) { + limit(RLIMIT_AS, *res_limits.address_space_bytes, + *res_limits.address_space_bytes); + } + if (res_limits.file_size_bytes) { + limit(RLIMIT_FSIZE, *res_limits.file_size_bytes, + *res_limits.file_size_bytes); + } + if (res_limits.open_files) { + limit(RLIMIT_NOFILE, *res_limits.open_files, + *res_limits.open_files); + } + ::execv(exe_str.c_str(), argv_ptrs.data()); ::_exit(127); } diff --git a/tests/standalone/Testing/FrameworkHardening.cpp b/tests/standalone/Testing/FrameworkHardening.cpp index f346d74..3611477 100644 --- a/tests/standalone/Testing/FrameworkHardening.cpp +++ b/tests/standalone/Testing/FrameworkHardening.cpp @@ -287,4 +287,23 @@ Void::Testing::Test testing_subprocess_isolation_exit_crash_timeout_and_flood() expect_eq(res.stdout_captured.size(), std::size_t{4096}); expect_eq(res.stderr_captured.size(), std::size_t{4096}); } + + // 7. Resource limits: a large allocation fails under a tight address-space + // cap instead of succeeding. + { + const std::vector args{"--selftest-child", "alloc"}; + const auto uncapped = run_isolated_subprocess(exe, args); + expect(uncapped.outcome == ProcessOutcome::passed); + + const auto capped = run_isolated_subprocess( + exe, args, + ProcessTestOptions{ + .timeout = std::chrono::milliseconds{5000}, + .max_output_bytes = 4096, + .resources = ProcessResourceLimits{ + .address_space_bytes = 64ULL * 1024ULL * 1024ULL}, + }); + expect(capped.outcome == ProcessOutcome::crashed || + capped.outcome == ProcessOutcome::failed); + } } \ No newline at end of file