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
40 changes: 40 additions & 0 deletions scripts/lib/sandbox-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,46 @@ validate_tmp_permissions() {
return $failed
}

# ── Config file permission helpers ────────────────────────────────
# After drop_capabilities() strips CAP_DAC_OVERRIDE, root can no longer
# write to files with mode 444. These helpers temporarily relax config
# files to 644 for writing, then re-lock to 444 afterward.
#
# CAP_FOWNER is retained (by design in PR #917), so root can still chmod
# files it doesn't own. The helpers include symlink guards to prevent
# symlink-following attacks on the config path.
#
# Usage:
# relax_config_for_write /sandbox/.openclaw/openclaw.json /sandbox/.openclaw/.config-hash
# # ... perform writes ...
# lock_config_after_write /sandbox/.openclaw/openclaw.json /sandbox/.openclaw/.config-hash
#
# Ref: https://github.com/NVIDIA/NemoClaw/issues/2653

relax_config_for_write() {
local f
for f in "$@"; do
if [ -L "$f" ]; then
printf '[SECURITY] Refusing to relax permissions — %s is a symlink\n' "$f" >&2
return 1
fi
[ -f "$f" ] || continue
chmod 644 "$f"
done
}

lock_config_after_write() {
local f
for f in "$@"; do
if [ -L "$f" ]; then
printf '[SECURITY] Refusing to lock permissions — %s is a symlink\n' "$f" >&2
return 1
fi
[ -f "$f" ] || continue
chmod 444 "$f"
done
}

# ── Capability dropping ──────────────────────────────────────────
# CIS Docker Benchmark 5.3: containers should not run with default caps.
# OpenShell manages the container runtime so we cannot pass --cap-drop=ALL
Expand Down
27 changes: 23 additions & 4 deletions scripts/nemoclaw-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ _SANDBOX_HOME="/sandbox" # Home dir for the sandbox user (useradd -d /s
# Ref: https://github.com/NVIDIA/NemoClaw/issues/759

apply_model_override() {
# Any of these env vars trigger a config patch
# Only explicit override env vars trigger a config patch. NEMOCLAW_CONTEXT_WINDOW,
# NEMOCLAW_MAX_TOKENS, and NEMOCLAW_REASONING are promoted from Dockerfile build
# ARGs to ENV and are always set — they should only take effect when accompanied
# by an explicit model or API override. Without this guard the function runs on
# every container start even with no override requested. Ref: #2653
[ -n "${NEMOCLAW_MODEL_OVERRIDE:-}" ] \
|| [ -n "${NEMOCLAW_INFERENCE_API_OVERRIDE:-}" ] \
|| [ -n "${NEMOCLAW_CONTEXT_WINDOW:-}" ] \
|| [ -n "${NEMOCLAW_MAX_TOKENS:-}" ] \
|| [ -n "${NEMOCLAW_REASONING:-}" ] \
|| return 0
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

# SECURITY: Only root can write to /sandbox/.openclaw (root:root 444).
Expand Down Expand Up @@ -272,6 +273,9 @@ apply_model_override() {
[ -n "$max_tokens" ] && printf '[config] Applying max tokens override: %s\n' "$max_tokens" >&2
[ -n "$reasoning" ] && printf '[config] Applying reasoning override: %s\n' "$reasoning" >&2

# Relax 444 → 644 so writes succeed after CAP_DAC_OVERRIDE is dropped (#2653)
relax_config_for_write "$config_file" "$hash_file"

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
NEMOCLAW_CONTEXT_WINDOW="$context_window" \
NEMOCLAW_MAX_TOKENS="$max_tokens" \
NEMOCLAW_REASONING="$reasoning" \
Expand Down Expand Up @@ -314,6 +318,9 @@ PYOVERRIDE
# Recompute config hash so integrity check passes on next startup
(cd /sandbox/.openclaw && sha256sum openclaw.json >"$hash_file")
printf '[SECURITY] Config hash recomputed after model override\n' >&2

# Re-lock 644 → 444 after writes complete (#2653)
lock_config_after_write "$config_file" "$hash_file"
}

# ── Runtime CORS origin override ──────────────────────────────────
Expand Down Expand Up @@ -356,6 +363,9 @@ apply_cors_override() {

printf '[config] Adding CORS origin: %s\n' "$cors_origin" >&2

# Relax 444 → 644 so writes succeed after CAP_DAC_OVERRIDE is dropped (#2653)
relax_config_for_write "$config_file" "$hash_file"

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
python3 - "$config_file" "$cors_origin" <<'PYCORS'
import json, sys

Expand All @@ -375,6 +385,9 @@ PYCORS

(cd /sandbox/.openclaw && sha256sum openclaw.json >"$hash_file")
printf '[config] Config hash recomputed after CORS override\n' >&2

# Re-lock 644 → 444 after writes complete (#2653)
lock_config_after_write "$config_file" "$hash_file"
}

# ── Slack token placeholder resolution ────────────────────────────
Expand Down Expand Up @@ -431,6 +444,9 @@ apply_slack_token_override() {

printf '[channels] Resolving Slack token placeholders in openclaw.json\n' >&2

# Relax 444 → 644 so writes succeed after CAP_DAC_OVERRIDE is dropped (#2653)
relax_config_for_write "$config_file" "$hash_file"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Same concern: Slack token override lacks error-safe re-locking.

The pattern repeats here. For defense-in-depth, consider the same trap-based approach to ensure permissions are re-locked even if the Python script or hash recomputation fails.

Also applies to: 484-486

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/nemoclaw-start.sh` around lines 447 - 449, The relax_config_for_write
call that temporarily loosens permissions for the Slack token override should be
protected with a trap-based cleanup so permissions are always re-locked even if
the subsequent Python script or hash recomputation fails: before calling
relax_config_for_write (used for the slack token override) register a trap that
will call the inverse/relock routine (undoing relax_config_for_write’s changes)
on EXIT/ERR, then run the Python script and hash recompute, and finally remove
or replace the trap on successful completion; apply the exact same trap-wrapping
pattern to the other occurrence of relax_config_for_write mentioned in the
review.

SLACK_BOT_TOKEN="$SLACK_BOT_TOKEN" \
SLACK_APP_TOKEN="${SLACK_APP_TOKEN:-}" \
python3 - "$config_file" <<'PYSLACK'
Expand Down Expand Up @@ -465,6 +481,9 @@ PYSLACK

(cd /sandbox/.openclaw && sha256sum openclaw.json >"$hash_file")
printf '[channels] Config hash recomputed after Slack token override\n' >&2

# Re-lock 644 → 444 after writes complete (#2653)
lock_config_after_write "$config_file" "$hash_file"
}

# ── Slack channel guard (unhandled-rejection safety net) ─────────
Expand Down
39 changes: 26 additions & 13 deletions test/e2e/test-runtime-overrides.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ info() { echo -e "${YELLOW}TEST${NC}: $1"; }
PASSED=0
FAILED=0

# ── Log file for CI artifact collection ──────────────────────────
# Create a timestamped log file whose name matches the CI artifact glob
# test-runtime-overrides-*.log so Docker stderr is captured automatically.
LOG_DIR="${SCRIPT_DIR}"
LOG_FILE="${LOG_DIR}/test-runtime-overrides-$(date +%Y%m%dT%H%M%S).log"
: >"$LOG_FILE"
info "Logging Docker stderr to: $LOG_FILE"

# Helper: run entrypoint with env vars, then read a config field via jq.
# The entrypoint patches config and starts the gateway — we only need the
# config patch, so we override CMD to just cat the config and exit.
# Docker stderr is captured to the log file for CI artifact visibility.
run_override() {
local env_args=("$@")
docker run --rm "${env_args[@]}" "$IMAGE" \
bash -c 'cat /sandbox/.openclaw/openclaw.json' 2>/dev/null
bash -c 'cat /sandbox/.openclaw/openclaw.json' 2>>"$LOG_FILE"
}

# Helper: run entrypoint with env vars and capture stderr for validation messages.
Expand All @@ -53,6 +62,8 @@ run_override_stderr() {
docker run --rm "${env_args[@]}" "$IMAGE" \
bash -c 'true' >/dev/null 2>"$tmpfile" || true
cat "$tmpfile"
# Also append to the main log file for CI artifact capture
cat "$tmpfile" >>"$LOG_FILE"
rm -f "$tmpfile"
}

Expand Down Expand Up @@ -83,7 +94,7 @@ info "Baseline: model=$BASELINE_MODEL ctx=$BASELINE_CTX max=$BASELINE_MAX reason
# ── Test 1: No-op baseline ───────────────────────────────────────

info "1. No overrides — config matches build-time defaults"
HASH_CHECK=$(docker run --rm "$IMAGE" bash -c 'cd /sandbox/.openclaw && sha256sum -c .config-hash --status && echo OK || echo FAIL' 2>/dev/null)
HASH_CHECK=$(docker run --rm "$IMAGE" bash -c 'cd /sandbox/.openclaw && sha256sum -c .config-hash --status && echo OK || echo FAIL' 2>>"$LOG_FILE")
if [ "$HASH_CHECK" = "OK" ]; then
pass "baseline config hash valid"
else
Expand All @@ -104,17 +115,19 @@ fi

# Verify hash was recomputed
HASH_CHECK=$(docker run --rm -e "NEMOCLAW_MODEL_OVERRIDE=$OVERRIDE_MODEL" "$IMAGE" \
bash -c 'cd /sandbox/.openclaw && sha256sum -c .config-hash --status && echo OK || echo FAIL' 2>/dev/null)
bash -c 'cd /sandbox/.openclaw && sha256sum -c .config-hash --status && echo OK || echo FAIL' 2>>"$LOG_FILE")
if [ "$HASH_CHECK" = "OK" ]; then
pass "config hash valid after model override"
else
fail "config hash invalid after model override"
fi

# ── Test 3: Context window override ──────────────────────────────
# NEMOCLAW_CONTEXT_WINDOW only takes effect alongside a model override
# (standalone values are baked at build time). Ref: #2653 Phase 2.

info "3. NEMOCLAW_CONTEXT_WINDOW patches contextWindow"
CFG=$(run_override -e "NEMOCLAW_CONTEXT_WINDOW=32768")
info "3. NEMOCLAW_CONTEXT_WINDOW patches contextWindow (with model override)"
CFG=$(run_override -e "NEMOCLAW_MODEL_OVERRIDE=$OVERRIDE_MODEL" -e "NEMOCLAW_CONTEXT_WINDOW=32768")
ACTUAL=$(echo "$CFG" | jq -r '.models.providers | to_entries[0].value.models[0].contextWindow')
if [ "$ACTUAL" = "32768" ]; then
pass "contextWindow overridden to 32768"
Expand All @@ -124,8 +137,8 @@ fi

# ── Test 4: Max tokens override ──────────────────────────────────

info "4. NEMOCLAW_MAX_TOKENS patches maxTokens"
CFG=$(run_override -e "NEMOCLAW_MAX_TOKENS=16384")
info "4. NEMOCLAW_MAX_TOKENS patches maxTokens (with model override)"
CFG=$(run_override -e "NEMOCLAW_MODEL_OVERRIDE=$OVERRIDE_MODEL" -e "NEMOCLAW_MAX_TOKENS=16384")
ACTUAL=$(echo "$CFG" | jq -r '.models.providers | to_entries[0].value.models[0].maxTokens')
if [ "$ACTUAL" = "16384" ]; then
pass "maxTokens overridden to 16384"
Expand All @@ -135,8 +148,8 @@ fi

# ── Test 5: Reasoning override ───────────────────────────────────

info "5. NEMOCLAW_REASONING=true patches reasoning"
CFG=$(run_override -e "NEMOCLAW_REASONING=true")
info "5. NEMOCLAW_REASONING=true patches reasoning (with model override)"
CFG=$(run_override -e "NEMOCLAW_MODEL_OVERRIDE=$OVERRIDE_MODEL" -e "NEMOCLAW_REASONING=true")
ACTUAL=$(echo "$CFG" | jq -r '.models.providers | to_entries[0].value.models[0].reasoning')
if [ "$ACTUAL" = "true" ]; then
pass "reasoning overridden to true"
Expand Down Expand Up @@ -190,23 +203,23 @@ else
fi

info "9. NEMOCLAW_CONTEXT_WINDOW with non-integer is rejected"
STDERR=$(run_override_stderr -e "NEMOCLAW_CONTEXT_WINDOW=notanumber")
STDERR=$(run_override_stderr -e "NEMOCLAW_MODEL_OVERRIDE=test" -e "NEMOCLAW_CONTEXT_WINDOW=notanumber")
if echo "$STDERR" | grep -q "must be a positive integer"; then
pass "non-integer context window rejected"
else
fail "non-integer context window was not rejected"
fi

info "10. NEMOCLAW_MAX_TOKENS with non-integer is rejected"
STDERR=$(run_override_stderr -e "NEMOCLAW_MAX_TOKENS=abc")
STDERR=$(run_override_stderr -e "NEMOCLAW_MODEL_OVERRIDE=test" -e "NEMOCLAW_MAX_TOKENS=abc")
if echo "$STDERR" | grep -q "must be a positive integer"; then
pass "non-integer max tokens rejected"
else
fail "non-integer max tokens was not rejected"
fi

info "11. NEMOCLAW_REASONING with invalid value is rejected"
STDERR=$(run_override_stderr -e "NEMOCLAW_REASONING=maybe")
STDERR=$(run_override_stderr -e "NEMOCLAW_MODEL_OVERRIDE=test" -e "NEMOCLAW_REASONING=maybe")
if echo "$STDERR" | grep -q 'must be "true" or "false"'; then
pass "invalid reasoning value rejected"
else
Expand All @@ -232,7 +245,7 @@ fi
# ── Test 14: Original config unchanged after rejected override ───

info "14. Config unchanged after rejected override"
CFG=$(run_override -e "NEMOCLAW_CONTEXT_WINDOW=notanumber")
CFG=$(run_override -e "NEMOCLAW_MODEL_OVERRIDE=test" -e "NEMOCLAW_CONTEXT_WINDOW=notanumber")
ACTUAL_CTX=$(echo "$CFG" | jq -r '.models.providers | to_entries[0].value.models[0].contextWindow')
if [ "$ACTUAL_CTX" = "$BASELINE_CTX" ]; then
pass "config unchanged after rejected override"
Expand Down
Loading