Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions scripts/lib/sandbox-rlimits.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# shellcheck shell=bash
# shellcheck shell=sh
#
# Shared NemoClaw sandbox RLIMIT defaults.
# Shared NemoClaw sandbox RLIMIT defaults. Sourced by bash entrypoints and by
# /bin/sh-compatible profile hooks during `openshell sandbox exec`.

NEMOCLAW_SANDBOX_NPROC_LIMIT=512
NEMOCLAW_SANDBOX_NOFILE_LIMIT=65536

_nemoclaw_ulimit() {
# `command` bypasses shell functions named `ulimit` in bash and POSIX sh,
# while still invoking the shell's special builtin. `builtin ulimit` is
# bash-only and breaks when /etc/profile.d hooks are sourced by /bin/sh.
command ulimit "$@"
}

_nemoclaw_supports_resource_limit() {
_nemoclaw_support_flag="$1"
_nemoclaw_support_status=0

_nemoclaw_ulimit "-S${_nemoclaw_support_flag}" >/dev/null 2>&1 || _nemoclaw_support_status=1

_nemoclaw_support_return="$_nemoclaw_support_status"
unset _nemoclaw_support_flag _nemoclaw_support_status
return "$_nemoclaw_support_return"
}

_nemoclaw_set_resource_limit() {
_nemoclaw_limit_flag="$1"
_nemoclaw_limit_value="$2"
_nemoclaw_limit_label="$3"
_nemoclaw_limit_quiet="${4:-}"

if ! builtin ulimit "-S${_nemoclaw_limit_flag}" "$_nemoclaw_limit_value" 2>/dev/null; then
if [ "$_nemoclaw_limit_quiet" != "--quiet" ]; then
echo "[SECURITY] Could not set soft ${_nemoclaw_limit_label} limit (container runtime may restrict ulimit)" >&2
if _nemoclaw_supports_resource_limit "$_nemoclaw_limit_flag"; then
if ! _nemoclaw_ulimit "-S${_nemoclaw_limit_flag}" "$_nemoclaw_limit_value" 2>/dev/null; then
if [ "$_nemoclaw_limit_quiet" != "--quiet" ]; then
echo "[SECURITY] Could not set soft ${_nemoclaw_limit_label} limit (container runtime may restrict ulimit)" >&2
fi
fi
fi
if ! builtin ulimit "-H${_nemoclaw_limit_flag}" "$_nemoclaw_limit_value" 2>/dev/null; then
if [ "$_nemoclaw_limit_quiet" != "--quiet" ]; then
echo "[SECURITY] Could not set hard ${_nemoclaw_limit_label} limit (container runtime may restrict ulimit)" >&2
if ! _nemoclaw_ulimit "-H${_nemoclaw_limit_flag}" "$_nemoclaw_limit_value" 2>/dev/null; then
if [ "$_nemoclaw_limit_quiet" != "--quiet" ]; then
echo "[SECURITY] Could not set hard ${_nemoclaw_limit_label} limit (container runtime may restrict ulimit)" >&2
fi
fi
fi

Expand All @@ -45,6 +66,11 @@ _nemoclaw_verify_resource_limit() {
_nemoclaw_limit_quiet="${4:-}"
_nemoclaw_limit_status=0

if ! _nemoclaw_supports_resource_limit "$_nemoclaw_limit_flag"; then
unset _nemoclaw_limit_flag _nemoclaw_limit_value _nemoclaw_limit_label _nemoclaw_limit_quiet
return 0
fi

for _nemoclaw_limit_bound in soft hard; do
case "$_nemoclaw_limit_bound" in
soft)
Expand All @@ -54,7 +80,7 @@ _nemoclaw_verify_resource_limit() {
_nemoclaw_limit_mode="H"
;;
esac
_nemoclaw_effective_limit="$(builtin ulimit "-${_nemoclaw_limit_mode}${_nemoclaw_limit_flag}" 2>/dev/null || printf '%s' unknown)"
_nemoclaw_effective_limit="$(_nemoclaw_ulimit "-${_nemoclaw_limit_mode}${_nemoclaw_limit_flag}" 2>/dev/null || printf '%s' unknown)"

if ! _nemoclaw_is_decimal_limit "$_nemoclaw_effective_limit" \
|| [ "$_nemoclaw_effective_limit" -gt "$_nemoclaw_limit_value" ]; then
Expand Down Expand Up @@ -83,13 +109,15 @@ harden_resource_limits() {
}

verify_resource_limits() {
local _nemoclaw_rlimit_quiet="${1:-}"
local _nemoclaw_rlimit_status=0
_nemoclaw_rlimit_quiet="${1:-}"
_nemoclaw_rlimit_status=0

_nemoclaw_verify_resource_limit u "$NEMOCLAW_SANDBOX_NPROC_LIMIT" nproc "$_nemoclaw_rlimit_quiet" \
|| _nemoclaw_rlimit_status=1
_nemoclaw_verify_resource_limit n "$NEMOCLAW_SANDBOX_NOFILE_LIMIT" nofile "$_nemoclaw_rlimit_quiet" \
|| _nemoclaw_rlimit_status=1

return "$_nemoclaw_rlimit_status"
_nemoclaw_rlimit_return="$_nemoclaw_rlimit_status"
unset _nemoclaw_rlimit_quiet _nemoclaw_rlimit_status
return "$_nemoclaw_rlimit_return"
}
4 changes: 2 additions & 2 deletions test/sandbox-init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ EOF
}
});

it("uses bash builtin ulimit for nproc and nofile enforcement and verification", () => {
const nprocLimit = process.platform === "darwin" ? 4000 : 512;
it("bypasses shadowed ulimit functions for nproc and nofile enforcement and verification", () => {
const nprocLimit = process.platform === "darwin" ? 4000 : 4096;
const { stdout } = runWithLib(
[
`NEMOCLAW_SANDBOX_NPROC_LIMIT=${nprocLimit}`,
Expand Down
39 changes: 37 additions & 2 deletions test/sandbox-rlimit-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function expectSystemRlimitHookEnforcesLimits(hookPath: string): void {
expect(Number(values.raise_nofile)).not.toBe(0);
}

function expectSystemRlimitHookUsesBuiltinUlimit(hookPath: string): void {
function expectSystemRlimitHookBypassesShadowedUlimit(hookPath: string): void {
const probe = [
"set -euo pipefail",
"ulimit() {",
Expand All @@ -168,6 +168,29 @@ function expectSystemRlimitHookUsesBuiltinUlimit(hookPath: string): void {
expect(Number(values.nofile)).toBeLessThanOrEqual(65536);
}

function expectRlimitLibIsPosixShSafe(rlimitLib: string): void {
const probe = [
`. ${JSON.stringify(rlimitLib)}`,
'current_nproc="$(command ulimit -u 2>/dev/null || printf "%s" 512)"',
'case "$current_nproc" in "" | *[!0-9]*) current_nproc=512 ;; esac',
'current_nofile="$(command ulimit -n 2>/dev/null || printf "%s" 256)"',
'case "$current_nofile" in "" | *[!0-9]*) current_nofile=256 ;; esac',
'NEMOCLAW_SANDBOX_NPROC_LIMIT="$current_nproc"',
'NEMOCLAW_SANDBOX_NOFILE_LIMIT="$current_nofile"',
"harden_resource_limits --quiet",
"verify_resource_limits",
'printf "ok\\n"',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
].join("\n");
const result = spawnSync("sh", ["-c", probe], {
encoding: "utf-8",
timeout: 5000,
});

expect(result.status, result.stderr).toBe(0);
expect(result.stderr).toBe("");
expect(result.stdout).toBe("ok\n");
}

function expectHookDeniesBoundedForkStorm(hookPath: string, safetyCap: number): void {
const probePath = path.join(path.dirname(hookPath), "fork-storm-probe.py");
fs.writeFileSync(
Expand Down Expand Up @@ -239,6 +262,18 @@ function expectHookDeniesBoundedForkStorm(hookPath: string, safetyCap: number):
describe("sandbox rlimit system hooks (#2173)", () => {
const forkStormIt = process.platform === "linux" ? it : it.skip;

it("rlimit helper is quiet and successful under POSIX sh", () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-posix-sh-rlimit-"));
const rlimitLib = path.join(tmp, "sandbox-rlimits.sh");

try {
copyRlimitFixture(rlimitLib);
expectRlimitLibIsPosixShSafe(rlimitLib);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
});

it("connect shell reports numeric nproc <=4096 and nofile <=65536 and denies raising limits after system-wide rlimit hook startup", () => {
const dockerfile = fs.readFileSync(DOCKERFILE_BASE, "utf-8");
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-base-rlimit-hooks-"));
Expand Down Expand Up @@ -268,7 +303,7 @@ describe("sandbox rlimit system hooks (#2173)", () => {
expect(fs.readFileSync(bashrc, "utf-8")).toContain(expectedRlimitShim);
expectSystemRlimitHookEnforcesLimits(rlimitHook);
expectSystemRlimitHookEnforcesLimits(bashrc);
expectSystemRlimitHookUsesBuiltinUlimit(rlimitHook);
expectSystemRlimitHookBypassesShadowedUlimit(rlimitHook);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
Expand Down
Loading