Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion ci/test-file-size-budget.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"src/lib/onboard/preflight.test.ts": 1904,
"test/generate-openclaw-config.test.ts": 1941,
"test/install-preflight.test.ts": 3934,
"test/nemoclaw-start.test.ts": 4827,
"test/nemoclaw-start.test.ts": 4829,
"test/onboard-messaging.test.ts": 2062,
"test/onboard-selection.test.ts": 4834,
"test/onboard.test.ts": 4057,
Expand Down
127 changes: 127 additions & 0 deletions scripts/lib/normalize_mutable_config_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,109 @@ def normalize_dir(directory_fd: int, *, top_level: bool = False) -> None:
os.close(child_fd)


SEALED = 0
UNSEALED = 1
INDETERMINATE = 2


def classify_seal(root_fd: int, root_metadata: os.stat_result) -> int:
if not stat.S_ISDIR(root_metadata.st_mode):
return INDETERMINATE
dir_sealed = (
root_metadata.st_uid == 0
and root_metadata.st_gid == 0
and stat.S_IMODE(root_metadata.st_mode) == 0o755
)
if not dir_sealed:
return UNSEALED
try:
file_metadata = os.stat(CONFIG_NAME, dir_fd=root_fd, follow_symlinks=False)
except FileNotFoundError:
return SEALED
except OSError:
return INDETERMINATE
file_sealed = (
stat.S_ISREG(file_metadata.st_mode)
and file_metadata.st_uid == 0
and file_metadata.st_gid == 0
and stat.S_IMODE(file_metadata.st_mode) == 0o444
)
return SEALED if file_sealed else UNSEALED


def reclaim_dir(
directory_fd: int,
sandbox_uid: int,
sandbox_gid: int,
*,
top_level: bool,
) -> None:
with os.scandir(directory_fd) as entries:
names = sorted(entry.name for entry in entries)
for name in names:
try:
before = os.stat(name, dir_fd=directory_fd, follow_symlinks=False)
except OSError as exc:
raise UnsafeTree() from exc
if stat.S_ISLNK(before.st_mode):
raise UnsafeTree()
if stat.S_ISDIR(before.st_mode):
child_fd, opened = open_pinned(directory_fd, name, directory_flags(), before)
try:
reclaim_dir(child_fd, sandbox_uid, sandbox_gid, top_level=False)
os.fchown(child_fd, sandbox_uid, sandbox_gid)
current_mode = stat.S_IMODE(os.fstat(child_fd).st_mode)
os.fchmod(child_fd, (current_mode | 0o2070) & ~0o007)
verify_still_linked(directory_fd, name, opened)
finally:
os.close(child_fd)
continue
if not stat.S_ISREG(before.st_mode):
continue
child_fd, opened = open_pinned(directory_fd, name, file_flags(), before)
try:
os.fchown(child_fd, sandbox_uid, sandbox_gid)
target_mode = (
0o660
if top_level and name in FIXED_FILES
else (stat.S_IMODE(opened.st_mode) | 0o060) & ~0o007
)
os.fchmod(child_fd, target_mode)
verify_still_linked(directory_fd, name, opened)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
finally:
os.close(child_fd)


def reclaim_if_unsealed(config_dir: str, sandbox_uid: int, sandbox_gid: int) -> int:
"""Classify and, only if unsealed, reclaim a root-collapsed OpenClaw config.

Opens config_dir exactly once (O_NOFOLLOW) so classification and reclaim
act on the same pinned descriptor, closing the window between a
path-based seal check and a later, separately opened mutation.
"""
try:
root_fd = os.open(config_dir, directory_flags())
except OSError:
return 0
try:
root_metadata = os.fstat(root_fd)
if classify_seal(root_fd, root_metadata) != UNSEALED:
return 0
if root_metadata.st_uid != 0 or root_metadata.st_gid != 0:
raise UnsafeTree()
reclaim_dir(root_fd, sandbox_uid, sandbox_gid, top_level=True)
os.fchown(root_fd, sandbox_uid, sandbox_gid)
os.fchmod(root_fd, 0o2770)
current = os.stat(config_dir, follow_symlinks=False)
if inode_key(current) != inode_key(os.fstat(root_fd)):
raise UnsafeTree()
return 0
except (OSError, UnsafeTree):
return 1
finally:
os.close(root_fd)


def config_dir_matches(
root_fd: int,
config_dir: str,
Expand Down Expand Up @@ -1358,6 +1461,30 @@ def run_root_supervisor(


def main() -> int:
if len(sys.argv) >= 2 and sys.argv[1] == "classify-seal":
if len(sys.argv) != 3:
return 1
config_dir = sys.argv[2]
try:
root_fd = os.open(config_dir, directory_flags())
except OSError:
return INDETERMINATE
try:
return classify_seal(root_fd, os.fstat(root_fd))
finally:
os.close(root_fd)

if len(sys.argv) >= 2 and sys.argv[1] == "reclaim-if-unsealed":
if len(sys.argv) != 5:
return 1
config_dir = sys.argv[2]
try:
sandbox_uid = int(sys.argv[3])
sandbox_gid = int(sys.argv[4])
except ValueError:
return 1
return reclaim_if_unsealed(config_dir, sandbox_uid, sandbox_gid)

if len(sys.argv) not in {4, 5, 7}:
return 1
config_dir = sys.argv[1]
Expand Down
90 changes: 71 additions & 19 deletions scripts/nemoclaw-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,32 @@ export OPENCLAW_OAUTH_DIR="${_OPENCLAW_CREDENTIALS_DIR}"
# restores the setgid + group-writable contract. Host-side, `nemoclaw <name>
# doctor --fix` and the rebuild post-upgrade repair step apply the same
# normalization without requiring a restart.
resolve_mutable_config_normalizer() {
local normalizer="/usr/local/lib/nemoclaw/normalize_mutable_config_perms.py"
if [ -f "$normalizer" ]; then
printf '%s\n' "$normalizer"
return 0
fi
if [ "$(id -u)" -eq 0 ]; then
return 1
fi
if [ -n "${NEMOCLAW_MUTABLE_CONFIG_NORMALIZER:-}" ] \
&& [ -f "${NEMOCLAW_MUTABLE_CONFIG_NORMALIZER}" ]; then
printf '%s\n' "${NEMOCLAW_MUTABLE_CONFIG_NORMALIZER}"
return 0
fi
if [ -f "scripts/lib/normalize_mutable_config_perms.py" ]; then
printf '%s\n' "scripts/lib/normalize_mutable_config_perms.py"
return 0
fi
normalizer="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/normalize_mutable_config_perms.py"
if [ -f "$normalizer" ]; then
printf '%s\n' "$normalizer"
return 0
fi
return 1
}

normalize_mutable_config_perms() {
local config_dir="/sandbox/.openclaw"
local operation="${1:-normalize}"
Expand Down Expand Up @@ -551,8 +577,19 @@ PY_CLASSIFY_MUTABLE_CONFIG
return 1
fi
[ "$config_dir_uid" = "missing" ] && return 0
# Shields up: the root-owned config tree is intentionally locked.
[ "$config_dir_uid" = "0" ] && return 0
if [ "$config_dir_uid" = "0" ]; then
[ "$operation" = "normalize" ] || return 0
# Root ownership here is the same OpenClaw doctor --fix collapse tracked
# by #6047 (https://github.com/NVIDIA/NemoClaw/issues/6047), the upstream
# tightening run_oneshot_command's wrapper below also works around.
# reclaim_collapsed_mutable_config classifies and, only if unsealed,
# reclaims under one pinned descriptor, so a genuinely sealed lock
# (root:root 0755 dir, 0444 config) is never reopened for mutation; drop
# this call once the pinned OpenClaw stops collapsing the sandbox
# 2770/660 contract on its own.
reclaim_collapsed_mutable_config "$config_dir" || return 1
return 0
fi

local expected_config_dir_uid expected_config_dir_gid
if [ "$(id -u)" -eq 0 ]; then
Expand All @@ -571,23 +608,8 @@ PY_CLASSIFY_MUTABLE_CONFIG
return 1
fi

# The installed helper wins in production. Repository-relative resolution is
# only for source-tree tests and ad-hoc development runs.
local normalizer="/usr/local/lib/nemoclaw/normalize_mutable_config_perms.py"
if [ ! -f "$normalizer" ]; then
if [ "$(id -u)" -eq 0 ]; then
printf '[SECURITY] Refusing mutable config permission normalization — trusted normalizer is missing\n' >&2
return 1
elif [ -n "${NEMOCLAW_MUTABLE_CONFIG_NORMALIZER:-}" ] \
&& [ -f "${NEMOCLAW_MUTABLE_CONFIG_NORMALIZER}" ]; then
normalizer="${NEMOCLAW_MUTABLE_CONFIG_NORMALIZER}"
elif [ -f "scripts/lib/normalize_mutable_config_perms.py" ]; then
normalizer="scripts/lib/normalize_mutable_config_perms.py"
else
normalizer="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/normalize_mutable_config_perms.py"
fi
fi
if [ ! -f "$normalizer" ]; then
local normalizer
if ! normalizer="$(resolve_mutable_config_normalizer)"; then
printf '[SECURITY] Refusing mutable config permission normalization — trusted normalizer is missing\n' >&2
return 1
fi
Expand Down Expand Up @@ -622,6 +644,36 @@ PY_CLASSIFY_MUTABLE_CONFIG
fi
}

classify_openclaw_config_seal() {
local config_dir="$1"
local normalizer
normalizer="$(resolve_mutable_config_normalizer)" || return 2
python3 -I "$normalizer" classify-seal "$config_dir" >/dev/null
}

reclaim_collapsed_mutable_config() {
local config_dir="$1"

[ "$(id -u)" -eq 0 ] || return 0

local sandbox_uid sandbox_gid
if ! sandbox_uid="$(id -u sandbox)" || ! sandbox_gid="$(id -g sandbox)"; then
printf '[SECURITY] Refusing mutable config reclaim — sandbox identity lookup failed\n' >&2
return 1
fi

local normalizer
if ! normalizer="$(resolve_mutable_config_normalizer)"; then
printf '[SECURITY] Refusing mutable config reclaim — trusted normalizer is missing\n' >&2
return 1
fi

if ! python3 -I "$normalizer" reclaim-if-unsealed "$config_dir" "$sandbox_uid" "$sandbox_gid" >/dev/null; then
printf '[SECURITY] Refusing mutable config reclaim — descriptor-safe reclaim detected an unsafe link, race, owner, or metadata state\n' >&2
return 1
fi
}

# Invalid state (#4538, #6047): OpenClaw assumes a single-UID 700/600 config
# tree, while NemoClaw's separate sandbox and gateway UIDs require the mutable
# 2770/660 group contract. The tightening originates at the OpenClaw command
Expand Down
33 changes: 24 additions & 9 deletions test/e2e-gateway-isolation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,10 @@ fi
info "30. One-shot cleanup repairs 700/600 without CAP_DAC_OVERRIDE"
OUT=$(docker run --rm --cap-drop DAC_OVERRIDE --entrypoint bash "$IMAGE" -lc '
set -euo pipefail
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start >/tmp/normalize.sh
{
sed -n "/^resolve_mutable_config_normalizer() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
} >/tmp/normalize.sh
test -s /tmp/normalize.sh
source /tmp/normalize.sh
capsh --has-p=cap_setgid
Expand All @@ -643,7 +646,10 @@ fi
info "30a. One-shot cleanup rejects a mutable tree owned by another UID"
OUT=$(docker run --rm --entrypoint bash "$IMAGE" -lc '
set -euo pipefail
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start >/tmp/normalize.sh
{
sed -n "/^resolve_mutable_config_normalizer() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
} >/tmp/normalize.sh
test -s /tmp/normalize.sh
source /tmp/normalize.sh
chown -R gateway:gateway /sandbox/.openclaw
Expand All @@ -667,7 +673,10 @@ fi
info "30b. One-shot cleanup reports a missing CAP_SETGID precondition"
OUT=$(docker run --rm --user 0:0 --cap-drop DAC_OVERRIDE --cap-drop SETGID --entrypoint bash "$IMAGE" -lc '
set -euo pipefail
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start >/tmp/normalize.sh
{
sed -n "/^resolve_mutable_config_normalizer() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
} >/tmp/normalize.sh
test -s /tmp/normalize.sh
source /tmp/normalize.sh
sandbox_gid=$(id -g sandbox)
Expand Down Expand Up @@ -698,6 +707,7 @@ info "30c. Post-override capture freshens a hardlinked recovery baseline"
OUT=$(docker run --rm --entrypoint bash "$IMAGE" -lc '
set -euo pipefail
{
sed -n "/^resolve_mutable_config_normalizer() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
sed -n "/^write_openclaw_config_baseline() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
} >/tmp/normalize.sh
Expand Down Expand Up @@ -748,9 +758,11 @@ if source.count(needle) != 1:
raise SystemExit("handoff injection point changed")
Path("/tmp/normalizer-handoff-race.py").write_text(source.replace(needle, replacement))
PY_INJECT_HANDOFF_RACE
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start \
| sed "s#/usr/local/lib/nemoclaw/normalize_mutable_config_perms.py#/tmp/normalizer-handoff-race.py#" \
>/tmp/normalize.sh
{
sed -n "/^resolve_mutable_config_normalizer() {$/,/^}$/p" /usr/local/bin/nemoclaw-start \
| sed "s#/usr/local/lib/nemoclaw/normalize_mutable_config_perms.py#/tmp/normalizer-handoff-race.py#"
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
} >/tmp/normalize.sh
source /tmp/normalize.sh
find /sandbox/.openclaw -mindepth 1 -delete
gosu sandbox sh -c "printf \"{}\\n\" > /sandbox/.openclaw/openclaw.json; printf \"hash\\n\" > /sandbox/.openclaw/.config-hash"
Expand All @@ -776,6 +788,7 @@ info "30e. Empty-config recovery refuses a protected-target symlink"
OUT=$(docker run --rm --entrypoint bash "$IMAGE" -lc '
set -euo pipefail
{
sed -n "/^resolve_mutable_config_normalizer() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
sed -n "/^recover_openclaw_config_if_empty() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
} >/tmp/recover.sh
Expand Down Expand Up @@ -811,9 +824,11 @@ from pathlib import Path

Path("/tmp/untrusted-normalizer-ran").write_text("unsafe\n")
PY_UNTRUSTED_NORMALIZER
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start \
| sed "s#/usr/local/lib/nemoclaw/normalize_mutable_config_perms.py#/tmp/missing-normalizer.py#" \
>/tmp/normalize.sh
{
sed -n "/^resolve_mutable_config_normalizer() {$/,/^}$/p" /usr/local/bin/nemoclaw-start \
| sed "s#/usr/local/lib/nemoclaw/normalize_mutable_config_perms.py#/tmp/missing-normalizer.py#"
sed -n "/^normalize_mutable_config_perms() {$/,/^}$/p" /usr/local/bin/nemoclaw-start
} >/tmp/normalize.sh
source /tmp/normalize.sh
export NEMOCLAW_MUTABLE_CONFIG_NORMALIZER=/tmp/untrusted-normalizer.py
rc=0
Expand Down
Loading
Loading