@@ -42,14 +42,38 @@ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
4242# ── Early stderr/stdout capture ──────────────────────────────────
4343# Capture all entrypoint output to /tmp/nemoclaw-start.log so startup
4444# failures before /tmp/gateway.log exists are still diagnosable.
45+ prepare_restricted_log () {
46+ local path=" $1 "
47+ local owner=" ${2:- } "
48+ local mode=" ${3:- 600} "
49+ local dir base tmp
50+
51+ dir=" $( dirname " $path " ) "
52+ base=" $( basename " $path " ) "
53+ tmp=" $( mktemp " ${dir} /.${base} .tmp.XXXXXX" ) " || return 1
54+ : > " $tmp " || {
55+ rm -f " $tmp "
56+ return 1
57+ }
58+ if [ " $( id -u) " -eq 0 ] && [ -n " $owner " ] && ! chown " $owner " " $tmp " ; then
59+ rm -f " $tmp "
60+ return 1
61+ fi
62+ if ! chmod " $mode " " $tmp " ; then
63+ rm -f " $tmp "
64+ return 1
65+ fi
66+ if ! mv -f " $tmp " " $path " ; then
67+ rm -f " $tmp "
68+ return 1
69+ fi
70+ }
71+
4572_START_LOG=" /tmp/nemoclaw-start.log"
4673if [ " $( id -u) " -eq 0 ]; then
47- : > " $_START_LOG "
48- chown root:root " $_START_LOG "
49- chmod 600 " $_START_LOG "
74+ prepare_restricted_log " $_START_LOG " root:root 600
5075else
51- : > " $_START_LOG "
52- chmod 600 " $_START_LOG " 2> /dev/null || true
76+ prepare_restricted_log " $_START_LOG " " " 600
5377fi
5478exec > >( tee -a " $_START_LOG " ) 2> >( tee -a " $_START_LOG " >&2 )
5579
@@ -91,10 +115,9 @@ PUBLIC_PORT=8642
91115INTERNAL_PORT=18642
92116HERMES=" $( command -v hermes) " # Resolve once, use absolute path everywhere
93117
94- # Hermes writes state files (PID, state.db, .channel_directory) directly into
95- # HERMES_HOME alongside config. Config is mutable by default for the sandbox user
96- # and group-readable by the gateway user. Runtime state dirs are group-writable
97- # because the Hermes gateway writes logs, PID files, and channel directory state.
118+ # Hermes resolves config and runtime state relative to HERMES_HOME. The config
119+ # root is mutable by the sandbox owner and readable by the gateway group, while
120+ # gateway-created top-level state is redirected to a scoped runtime directory.
98121# Immutability is opt-in via `shields up`.
99122HERMES_DIR=" /sandbox/.hermes"
100123HERMES_HASH_FILE=" /etc/nemoclaw/hermes.config-hash"
@@ -474,9 +497,7 @@ if [ "$(id -u)" -ne 0 ]; then
474497 exec " ${NEMOCLAW_CMD[@]} "
475498 fi
476499
477- # TODO(#2277-P2): migrate to shared emit_restricted_log() helper
478- touch /tmp/gateway.log
479- chmod 600 /tmp/gateway.log
500+ prepare_restricted_log /tmp/gateway.log " " 600
480501
481502 # Defence-in-depth: verify /tmp file permissions before launching services.
482503 # shellcheck disable=SC2119
@@ -521,10 +542,7 @@ if [ ${#NEMOCLAW_CMD[@]} -gt 0 ]; then
521542fi
522543
523544# SECURITY: Protect gateway log from sandbox user tampering
524- # TODO(#2277-P2): migrate to shared emit_restricted_log() helper
525- touch /tmp/gateway.log
526- chown gateway:gateway /tmp/gateway.log
527- chmod 600 /tmp/gateway.log
545+ prepare_restricted_log /tmp/gateway.log gateway:gateway 600
528546
529547# Defence-in-depth: verify /tmp file permissions before launching services.
530548# shellcheck disable=SC2119
0 commit comments