Skip to content

armgit / ARES

Public Imported from GitHub
0

ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.

fix(ci): prevent in-process preexec calls from mutating test runner process rlimits

Mafifrizi5ddaaea02c0b
diff --git a/ares/core/sandbox.py b/ares/core/sandbox.py
index 296d7ec..e7f4bee 100644
--- a/ares/core/sandbox.py
+++ b/ares/core/sandbox.py
@@ -617,10 +617,12 @@ class SandboxRunner:
drop_privileges = self.policy.drop_privileges
allow_write = self.policy.allow_write
target_uid = self.policy.sandbox_uid
+ _parent_pid = os.getpid()
def _limits():
- # 1. Best-effort resource limits
- if resource is not None:
+ # 1. Best-effort resource limits (only applied in child process after fork)
+ in_child = (os.getpid() != _parent_pid)
+ if resource is not None and in_child:
try:
resource.setrlimit(resource.RLIMIT_CPU, (cpu_limit, cpu_limit))
resource.setrlimit(resource.RLIMIT_AS, (mem_bytes, mem_bytes))
diff --git a/tests/unit/test_hardening.py b/tests/unit/test_hardening.py
index 48d6745..925f9c7 100644
--- a/tests/unit/test_hardening.py
+++ b/tests/unit/test_hardening.py
@@ -965,7 +965,7 @@ class TestSandboxAndFirewallHardening:
mock_docker.from_env.return_value = mock_client
mock_client.containers.run.return_value = b'{"success": true, "findings": [], "extra": {}}'
- monkeypatch.setattr("sys.modules", {**sys.modules, "docker": mock_docker})
+ monkeypatch.setitem(sys.modules, "docker", mock_docker)
res = asyncio.run(runner._run_docker("test.module", {}, "camp-123"))
assert res.success is True
@@ -1036,6 +1036,42 @@ class TestSecurityBoundariesAndLifecycle:
- Defense-in-depth write barrier in subprocess wrapper
"""
+ @pytest.fixture(autouse=True)
+ def _protect_test_process_limits(self, monkeypatch):
+ """Prevent in-process unit tests on Linux from mutating pytest runner's own OS rlimits."""
+ mock_resource = MagicMock()
+ monkeypatch.setattr("ares.core.sandbox.resource", mock_resource)
+
+ def test_preexec_rlimits_only_apply_in_forked_child(self, monkeypatch):
+ """
+ Verify that _limits() never mutates parent process rlimits when invoked
+ directly in-process, but calls setrlimit when running in a forked child.
+ """
+ from ares.core.sandbox import SandboxRunner, SandboxPolicy
+
+ policy = SandboxPolicy(cpu_time_s=15, memory_mb=128, allow_network=True, allow_write=False)
+ runner = SandboxRunner(policy=policy)
+ mock_res = MagicMock()
+ monkeypatch.setattr("ares.core.sandbox.resource", mock_res)
+
+ preexec = runner._make_preexec_fn()
+ assert preexec is not None
+
+ # 1. Direct in-process call in parent: setrlimit must NOT be called
+ preexec()
+ assert mock_res.setrlimit.call_count == 0
+
+ # 2. In forked child (simulated PID change): setrlimit MUST be called
+ parent_pid = os.getpid()
+ monkeypatch.setattr(os, "getpid", lambda: parent_pid + 999)
+ preexec()
+ assert mock_res.setrlimit.call_count >= 4
+ # Verify RLIMIT_CPU, RLIMIT_AS, RLIMIT_NPROC, RLIMIT_NOFILE, RLIMIT_FSIZE
+ calls = [c[0] for c in mock_res.setrlimit.call_args_list]
+ assert (mock_res.RLIMIT_CPU, (15, 15)) in calls
+ assert (mock_res.RLIMIT_AS, (128 * 1024 * 1024, 128 * 1024 * 1024)) in calls
+ assert (mock_res.RLIMIT_FSIZE, (0, 0)) in calls
+
def test_subprocess_allow_network_false_fails_closed(self, monkeypatch):
"""
FINDING A: allow_network=False must fail-closed if unshare(CLONE_NEWNET) fails.
@@ -1742,7 +1778,7 @@ class TestPostFed3098SecurityBoundaryProof:
mock_client = MagicMock()
mock_docker.from_env.return_value = mock_client
mock_client.containers.run.return_value = b'{"success": true, "findings": [], "extra": {}}'
- monkeypatch.setattr("sys.modules", {**sys.modules, "docker": mock_docker})
+ monkeypatch.setitem(sys.modules, "docker", mock_docker)
result = asyncio.run(runner.run_module("test.module", {}, "camp-123"))
assert result.success is True
@@ -2225,7 +2261,7 @@ class TestPostFed3098SecurityBoundaryProof:
mock_client = MagicMock()
mock_docker.from_env.return_value = mock_client
mock_client.containers.run.return_value = b'{"success": true, "findings": [], "extra": {}}'
- monkeypatch.setattr("sys.modules", {**sys.modules, "docker": mock_docker})
+ monkeypatch.setitem(sys.modules, "docker", mock_docker)
res = asyncio.run(runner._run_docker("test.module", {}, "camp-123"))
assert res.success is True