Skip to content

Commit 4fc4bdc

Browse files
packaging: harden installer file handling (#245)
Keep installer file and directory handling aligned with the packaged tmpfiles policy.
1 parent 402c082 commit 4fc4bdc

4 files changed

Lines changed: 248 additions & 12 deletions

File tree

‎packaging/lxc/install.sh‎

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@ fail() {
2020
exit 1
2121
}
2222

23+
# Remove any generated-content temp file left behind by a failed run.
24+
cleanup_tmp_files() {
25+
rm -f "${tokens_tmp:-}" "${audit_key_tmp:-}"
26+
}
27+
trap cleanup_tmp_files EXIT
28+
29+
# Refuse a path that is not one plain file.
30+
require_regular_file() {
31+
local path="$1"
32+
local links=""
33+
if [[ -L "$path" ]] || { [[ -e "$path" ]] && [[ ! -f "$path" ]]; }; then
34+
fail "$path is not a regular file; refusing"
35+
fi
36+
if [[ -e "$path" ]]; then
37+
links="$(stat -c '%h' "$path")"
38+
if [[ "$links" != "1" ]]; then
39+
fail "$path is not a regular file; refusing"
40+
fi
41+
fi
42+
}
43+
44+
# chmod as the parent directory's owner.
45+
secure_chmod() {
46+
local mode="$1" path="$2"
47+
local parent owner=""
48+
parent="$(dirname -- "$path")"
49+
owner="$(stat -c '%U' "$parent" 2>/dev/null || true)"
50+
if [[ "$SKIP_USER_SETUP" != "1" && "$owner" == "$SERVICE_USER" ]]; then
51+
runuser -u "$SERVICE_USER" -- chmod "$mode" "$path"
52+
else
53+
chmod "$mode" "$path"
54+
fi
55+
}
56+
2357
target_path() {
2458
local relative="${1#/}"
2559
if [[ "$INSTALL_ROOT" == "/" ]]; then
@@ -191,6 +225,13 @@ fi
191225

192226
# Install config example (not to the live filename).
193227
install -d -m 0750 "$CONFIG_DIR"
228+
229+
# Keep the config dir root-owned per tmpfiles before writing into it.
230+
if [[ "$SKIP_USER_SETUP" != "1" ]]; then
231+
chown -h "root:$SERVICE_GROUP" "$CONFIG_DIR"
232+
chmod 0750 "$CONFIG_DIR"
233+
fi
234+
194235
if [[ -e "$PACKAGE_ROOT/config/devices.example.json" ]]; then
195236
install -m 0644 "$PACKAGE_ROOT/config/devices.example.json" \
196237
"$CONFIG_DIR/devices.json.example"
@@ -212,21 +253,36 @@ fi
212253
# the state directory exists before writing to it.
213254
install -d -m 0700 "$STATE_DIR"
214255

256+
require_regular_file "$STATE_DIR/tokens.json"
215257
if [[ ! -e "$STATE_DIR/tokens.json" ]]; then
216258
if [[ -e "$CONFIG_DIR/tokens.json" ]]; then
217259
printf '%s\n' ">> Not creating $STATE_DIR/tokens.json: a token store already exists at"
218260
printf '%s\n' ">> $CONFIG_DIR/tokens.json. The server reads it via the legacy fallback and warns."
219261
printf '%s\n' ">> Migrate it deliberately, then remove the old copy:"
220262
printf '%s\n' ">> install -m 0600 -o $SERVICE_USER -g $SERVICE_GROUP $CONFIG_DIR/tokens.json $STATE_DIR/tokens.json"
221263
printf '%s\n' ">> rm $CONFIG_DIR/tokens.json"
264+
elif [[ "$SKIP_USER_SETUP" != "1" ]]; then
265+
# Create the token store as the directory's owner.
266+
runuser -u "$SERVICE_USER" -- sh -c '
267+
umask 077
268+
dir=$1
269+
tmp=$(mktemp "$dir/.tokens.json.XXXXXX") || exit 1
270+
trap "rm -f \"\$tmp\"" EXIT
271+
printf "%s\n" "{\"version\":1,\"tokens\":[]}" >"$tmp"
272+
mv -fT "$tmp" "$dir/tokens.json"
273+
' sh "$STATE_DIR"
222274
else
223-
printf '%s\n' '{"version":1,"tokens":[]}' >"$STATE_DIR/tokens.json"
224-
chmod 0600 "$STATE_DIR/tokens.json"
275+
tokens_tmp="$(mktemp "$STATE_DIR/.tokens.json.XXXXXX")"
276+
printf '%s\n' '{"version":1,"tokens":[]}' >"$tokens_tmp"
277+
mv -fT "$tokens_tmp" "$STATE_DIR/tokens.json"
225278
fi
226279
fi
227280

228281
# Ensure tokens.json has 0600 even on upgrade.
229-
chmod 0600 "$STATE_DIR/tokens.json"
282+
if [[ -e "$STATE_DIR/tokens.json" ]]; then
283+
require_regular_file "$STATE_DIR/tokens.json"
284+
secure_chmod 0600 "$STATE_DIR/tokens.json"
285+
fi
230286

231287
# Warn if the old /etc location still exists — it may be a live file from
232288
# before the /var/lib migration, or it may be a leftover decoy. Do not delete:
@@ -239,32 +295,37 @@ fi
239295

240296
# Create audit HMAC key if absent. Never regenerate on upgrade — a new key
241297
# breaks verification of every prior record.
298+
require_regular_file "$CONFIG_DIR/audit-hmac.key"
242299
if [[ ! -e "$CONFIG_DIR/audit-hmac.key" ]]; then
243-
head -c 32 /dev/urandom | base64 >"$CONFIG_DIR/audit-hmac.key"
244-
chmod 0600 "$CONFIG_DIR/audit-hmac.key"
300+
audit_key_tmp="$(mktemp "$CONFIG_DIR/.audit-hmac.key.XXXXXX")"
301+
head -c 32 /dev/urandom | base64 >"$audit_key_tmp"
302+
mv -fT "$audit_key_tmp" "$CONFIG_DIR/audit-hmac.key"
245303
fi
246304

247305
# If devices.json exists, ensure it has 0600.
248306
if [[ -e "$CONFIG_DIR/devices.json" ]]; then
249-
chmod 0600 "$CONFIG_DIR/devices.json"
307+
require_regular_file "$CONFIG_DIR/devices.json"
308+
secure_chmod 0600 "$CONFIG_DIR/devices.json"
250309
fi
251310

252311
# Never clobber mutation-state.json — it holds change-set audit trail.
253312
# Leave it exactly alone if it exists.
254313

255314
if [[ "$SKIP_USER_SETUP" != "1" ]]; then
256-
chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR"
315+
# Own the files inside the config dir. The directory stays root-owned.
257316
if [[ -e "$CONFIG_DIR/devices.json" ]]; then
258-
chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/devices.json"
317+
require_regular_file "$CONFIG_DIR/devices.json"
318+
chown -h "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/devices.json"
259319
fi
260320
if [[ -e "$CONFIG_DIR/devices.json.example" ]]; then
261-
chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/devices.json.example"
321+
require_regular_file "$CONFIG_DIR/devices.json.example"
322+
chown -h "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/devices.json.example"
262323
fi
263324
if [[ -e "$CONFIG_DIR/audit-hmac.key" ]]; then
264-
chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/audit-hmac.key"
325+
require_regular_file "$CONFIG_DIR/audit-hmac.key"
326+
chown -h "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/audit-hmac.key"
265327
fi
266-
# The state dir holds tokens.json, mutation-state.json, evidence files.
267-
# Recursive ownership for everything under /var/lib.
328+
# Own everything under the state dir.
268329
chown -R "$SERVICE_USER:$SERVICE_GROUP" "$STATE_DIR" 2>/dev/null || true
269330
fi
270331

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
# The config directory stays root-owned, and that correction precedes any
3+
# write into the directory.
4+
5+
set -euo pipefail
6+
7+
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
8+
PACKAGE_ROOT="$(cd -- "$SCRIPT_DIR/../../.." && pwd -P)"
9+
INSTALL_SH="$PACKAGE_ROOT/packaging/lxc/install.sh"
10+
11+
line_of() {
12+
local pattern="$1"
13+
local found=""
14+
found="$(grep -nF -- "$pattern" "$INSTALL_SH" | head -n 1 | cut -d: -f1 || true)"
15+
printf '%s\n' "$found"
16+
}
17+
18+
# shellcheck disable=SC2016 # Patterns match installer source text, not this script.
19+
own="$(line_of 'chown -h "root:$SERVICE_GROUP" "$CONFIG_DIR"')"
20+
# shellcheck disable=SC2016
21+
example="$(line_of '"$CONFIG_DIR/devices.json.example"')"
22+
# shellcheck disable=SC2016
23+
key="$(line_of 'mktemp "$CONFIG_DIR/.audit-hmac.key.XXXXXX"')"
24+
25+
if [[ -z "$own" || -z "$example" || -z "$key" ]]; then
26+
echo "FAIL: expected config-dir ownership correction or write site is missing" >&2
27+
exit 1
28+
fi
29+
30+
if (( own > example || own > key )); then
31+
echo "FAIL: config dir ownership correction is after a write into it (own=$own example=$example key=$key)" >&2
32+
exit 1
33+
fi
34+
35+
# shellcheck disable=SC2016 # Pattern matches installer source text, not this script.
36+
if grep -Eq 'chown[[:space:]].*"\$SERVICE_USER:\$SERVICE_GROUP"[[:space:]]+"\$CONFIG_DIR"[[:space:]]*$' "$INSTALL_SH"; then
37+
echo "FAIL: config dir is assigned to the service account" >&2
38+
exit 1
39+
fi
40+
41+
echo "PASS: config dir stays root-owned and is corrected before writes"
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env bash
2+
# Regression test: the installer must refuse to operate on tokens.json,
3+
# audit-hmac.key, or devices.json when the existing path is not a regular
4+
# file, and must leave whatever that path points to untouched.
5+
6+
set -euo pipefail
7+
8+
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
9+
PACKAGE_ROOT="$(cd -- "$SCRIPT_DIR/../../.." && pwd -P)"
10+
11+
STAGING="$(mktemp -d)"
12+
trap 'rm -rf "$STAGING"' EXIT
13+
14+
FAKE_PACKAGE="$STAGING/rust-panosmcp-test"
15+
install -d "$FAKE_PACKAGE/bin" "$FAKE_PACKAGE/packaging/systemd" "$FAKE_PACKAGE/packaging/lxc" \
16+
"$FAKE_PACKAGE/config"
17+
18+
cp "$PACKAGE_ROOT/packaging/lxc/install.sh" "$FAKE_PACKAGE/packaging/lxc/install.sh"
19+
cp "$PACKAGE_ROOT/packaging/systemd/rust-panosmcp.service" "$FAKE_PACKAGE/packaging/systemd/"
20+
cp "$PACKAGE_ROOT/packaging/systemd/rust-panosmcp.sysusers" "$FAKE_PACKAGE/packaging/systemd/"
21+
cp "$PACKAGE_ROOT/packaging/systemd/rust-panosmcp.tmpfiles" "$FAKE_PACKAGE/packaging/systemd/"
22+
cp "$PACKAGE_ROOT/config/devices.example.json" "$FAKE_PACKAGE/config/"
23+
24+
echo '#!/bin/sh' > "$FAKE_PACKAGE/bin/rust-panosmcp"
25+
echo 'echo "fake binary"' >> "$FAKE_PACKAGE/bin/rust-panosmcp"
26+
chmod +x "$FAKE_PACKAGE/bin/rust-panosmcp"
27+
chmod +x "$FAKE_PACKAGE/packaging/lxc/install.sh"
28+
29+
INSTALL_ROOT="$STAGING/staged"
30+
export PANOSMCP_INSTALL_ROOT="$INSTALL_ROOT"
31+
export PANOSMCP_INSTALL_SKIP_USER=1
32+
export PANOSMCP_INSTALL_SKIP_SYSTEMD_RELOAD=1
33+
export PANOSMCP_INSTALL_SKIP_RUNTIME_DEPS=1
34+
35+
cd "$FAKE_PACKAGE"
36+
37+
STATE_DIR="$INSTALL_ROOT/var/lib/rust-panosmcp"
38+
CONFIG_DIR="$INSTALL_ROOT/etc/rust-panosmcp"
39+
CANARY="$STAGING/canary"
40+
printf 'canary-untouched\n' >"$CANARY"
41+
chmod 0644 "$CANARY"
42+
43+
assert_refused_and_canary_untouched() {
44+
local label="$1"
45+
local canary_perms_before="$2"
46+
local canary_perms_after
47+
canary_perms_after="$(stat -c '%a' "$CANARY")"
48+
if [[ "$canary_perms_after" != "$canary_perms_before" ]]; then
49+
echo "FAIL: $label: canary perms changed ($canary_perms_before -> $canary_perms_after)" >&2
50+
exit 1
51+
fi
52+
if [[ "$(cat "$CANARY")" != "canary-untouched" ]]; then
53+
echo "FAIL: $label: canary content was overwritten" >&2
54+
exit 1
55+
fi
56+
}
57+
58+
# --- First install: establishes the baseline state dir/tokens.json. ---
59+
./packaging/lxc/install.sh >/dev/null
60+
61+
if [[ ! -f "$STATE_DIR/tokens.json" ]]; then
62+
echo "FAIL: baseline install did not create $STATE_DIR/tokens.json" >&2
63+
exit 1
64+
fi
65+
66+
# --- tokens.json: replace the real file with a non-regular entry. ---
67+
rm -f "$STATE_DIR/tokens.json"
68+
ln -s "$CANARY" "$STATE_DIR/tokens.json"
69+
canary_before="$(stat -c '%a' "$CANARY")"
70+
71+
if ./packaging/lxc/install.sh >/dev/null 2>"$STAGING/tokens-refusal.log"; then
72+
echo "FAIL: installer did not refuse a symlinked tokens.json" >&2
73+
exit 1
74+
fi
75+
grep -q "not a regular file; refusing" "$STAGING/tokens-refusal.log" \
76+
|| { echo "FAIL: unexpected refusal message for tokens.json" >&2; cat "$STAGING/tokens-refusal.log" >&2; exit 1; }
77+
assert_refused_and_canary_untouched "tokens.json" "$canary_before"
78+
rm -f "$STATE_DIR/tokens.json"
79+
80+
# --- audit-hmac.key: replace the file left by the baseline install. ---
81+
rm -f "$CONFIG_DIR/audit-hmac.key"
82+
ln -s "$CANARY" "$CONFIG_DIR/audit-hmac.key"
83+
canary_before="$(stat -c '%a' "$CANARY")"
84+
85+
if ./packaging/lxc/install.sh >/dev/null 2>"$STAGING/audit-key-refusal.log"; then
86+
echo "FAIL: installer did not refuse a symlinked audit-hmac.key" >&2
87+
exit 1
88+
fi
89+
grep -q "not a regular file; refusing" "$STAGING/audit-key-refusal.log" \
90+
|| { echo "FAIL: unexpected refusal message for audit-hmac.key" >&2; cat "$STAGING/audit-key-refusal.log" >&2; exit 1; }
91+
assert_refused_and_canary_untouched "audit-hmac.key" "$canary_before"
92+
rm -f "$CONFIG_DIR/audit-hmac.key" "$STATE_DIR/tokens.json"
93+
94+
# --- devices.json: operator-managed file, present as a non-regular entry. ---
95+
ln -s "$CANARY" "$CONFIG_DIR/devices.json"
96+
canary_before="$(stat -c '%a' "$CANARY")"
97+
98+
if ./packaging/lxc/install.sh >/dev/null 2>"$STAGING/devices-refusal.log"; then
99+
echo "FAIL: installer did not refuse a symlinked devices.json" >&2
100+
exit 1
101+
fi
102+
grep -q "not a regular file; refusing" "$STAGING/devices-refusal.log" \
103+
|| { echo "FAIL: unexpected refusal message for devices.json" >&2; cat "$STAGING/devices-refusal.log" >&2; exit 1; }
104+
assert_refused_and_canary_untouched "devices.json" "$canary_before"
105+
rm -f "$CONFIG_DIR/devices.json"
106+
107+
# --- devices.json: same destination, present as a hard link. ---
108+
ln "$CANARY" "$CONFIG_DIR/devices.json"
109+
canary_before="$(stat -c '%a' "$CANARY")"
110+
111+
if ./packaging/lxc/install.sh >/dev/null 2>"$STAGING/devices-hardlink-refusal.log"; then
112+
echo "FAIL: installer did not refuse a hardlinked devices.json" >&2
113+
exit 1
114+
fi
115+
grep -q "not a regular file; refusing" "$STAGING/devices-hardlink-refusal.log" \
116+
|| { echo "FAIL: unexpected refusal message for hardlinked devices.json" >&2; cat "$STAGING/devices-hardlink-refusal.log" >&2; exit 1; }
117+
assert_refused_and_canary_untouched "hardlinked devices.json" "$canary_before"
118+
rm -f "$CONFIG_DIR/devices.json"
119+
120+
echo "PASS: installer refuses symlinked tokens.json, audit-hmac.key, and devices.json, and a hardlinked devices.json"

‎scripts/verify-packaging.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ else
137137
echo "WARN: install/unit consistency test not found or not executable" >&2
138138
fi
139139

140+
# Run the installer path-guard regression test.
141+
if [[ -x "$ROOT/packaging/lxc/tests/test_installer_path_guard.sh" ]]; then
142+
"$ROOT/packaging/lxc/tests/test_installer_path_guard.sh"
143+
else
144+
echo "WARN: installer path-guard test not found or not executable" >&2
145+
fi
146+
147+
# Config directory ownership matches tmpfiles and precedes writes into it.
148+
if [[ -x "$ROOT/packaging/lxc/tests/test_config_dir_ownership.sh" ]]; then
149+
"$ROOT/packaging/lxc/tests/test_config_dir_ownership.sh"
150+
else
151+
echo "WARN: config dir ownership test not found or not executable" >&2
152+
fi
153+
140154
if (( failures > 0 )); then
141155
printf 'packaging policy: FAIL (%d violation(s))\n' "$failures" >&2
142156
exit 1

0 commit comments

Comments
 (0)