mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(docker): re-own the configured persistent path, not only /app/data (#4525)
* fix(docker): re-own the configured persistent path, not only /app/data The entrypoint's root phase re-owned /app/data, while the writability probe and the boot-time check both use FLOCI_STORAGE_PERSISTENT_PATH. A named volume mounted at any other path arrived root-owned, the chown never reached it, and with non-memory storage the container refused to start. The root phase now re-owns the same directory the probe checks. The path is resolved physically first and never re-owned when it resolves to /, since it now comes from an environment variable. * test(docker): make the chown helper prove the root phase completed An empty chown log also results from a root phase that stops before the privilege drop, so the 'skips a missing dir' case could pass for the wrong reason. The helper now reports when the drop was never reached. * fix(docker): resolve the persistent path without CDPATH A relative FLOCI_STORAGE_PERSISTENT_PATH was resolved with cd, which searches CDPATH and prints the directory it picks, so a set CDPATH could send the chown to another directory or corrupt its argument. The resolution now clears it. The test stub also records chown's full arguments, so the owner and the recursive flag are asserted, not only the target. * fix(docker): compare the persistent path to / by inode bash keeps a leading // through cd -P and pwd -P, so the string test let FLOCI_STORAGE_PERSISTENT_PATH=// reach a recursive chown on the native images, which run bash as sh. -ef compares device and inode and holds in every shell, and chown --preserve-root refuses / on its own as a second guard. The test harness can now run the entrypoint under a chosen shell, and a bash case pins the // spelling that dash alone cannot reach.
This commit is contained in:
2 files changed
+86
-6
No files matched your search
+17
-5
@@ -25,11 +25,23 @@ if [ "$(id -u)" = '0' ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# Re-own state dir for the case where a host bind-mount arrives with
|
||||
# ownership the floci user cannot write to. Ignore errors (read-only
|
||||
# mounts, unusual filesystems) so the container still starts.
|
||||
if [ -d /app/data ]; then
|
||||
chown -R floci:root /app/data 2>/dev/null || true
|
||||
# Re-own the state dir for the case where a volume or host bind-mount
|
||||
# arrives with ownership the floci user cannot write to. This is the same
|
||||
# directory the writability probe below checks, so a volume mounted at a
|
||||
# custom FLOCI_STORAGE_PERSISTENT_PATH is fixed up too. The path is resolved
|
||||
# physically first, then compared by inode rather than by string: bash keeps a leading //
|
||||
# (POSIX leaves it implementation-defined), so a string test would let // through.
|
||||
# --preserve-root is the second guard. Ignore errors
|
||||
# (read-only mounts, unusual filesystems) so the container still starts.
|
||||
state_dir="${FLOCI_STORAGE_PERSISTENT_PATH:-/app/data}"
|
||||
if [ -d "$state_dir" ]; then
|
||||
# CDPATH is cleared so a relative path cannot be looked up elsewhere (and echoed).
|
||||
state_dir_physical="$(CDPATH= cd -P -- "$state_dir" 2>/dev/null && pwd -P)" || state_dir_physical=''
|
||||
if [ "$state_dir_physical" -ef / ]; then
|
||||
echo "WARNING: not changing ownership of $state_dir, it resolves to /." >&2
|
||||
elif [ -n "$state_dir_physical" ]; then
|
||||
chown --preserve-root -R floci:root "$state_dir_physical" 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
|
||||
# `chroot /` changes nothing but the identity: uid 1001, primary gid 0, plus the socket's
|
||||
|
||||
@@ -52,7 +52,25 @@ cat > "${WORK}/root-bin/chroot" <<'EOF'
|
||||
#!/bin/sh
|
||||
printf 'dropped privileges\n'
|
||||
EOF
|
||||
chmod +x "${WORK}/root-bin/id" "${WORK}/root-bin/chroot"
|
||||
# Record chown's arguments instead of changing ownership (the tests run unprivileged).
|
||||
cat > "${WORK}/root-bin/chown" <<EOF
|
||||
#!/bin/sh
|
||||
printf '%s\n' "\$*" >> "${WORK}/chown.log"
|
||||
EOF
|
||||
chmod +x "${WORK}/root-bin/id" "${WORK}/root-bin/chroot" "${WORK}/root-bin/chown"
|
||||
|
||||
# Runs the root phase with the given state dir and prints every path chown was asked to re-own.
|
||||
# TEST_SH picks the shell that runs the entrypoint (default sh), so a case can pin bash.
|
||||
# A root phase that stops before the privilege drop prints a marker instead, so an empty result
|
||||
# can only mean chown was never called.
|
||||
chowned() {
|
||||
: > "${WORK}/chown.log"
|
||||
if ! PATH="${WORK}/root-bin:${PATH}" FLOCI_STORAGE_PERSISTENT_PATH="$1" LOCALSTACK_PARITY=false \
|
||||
"${TEST_SH:-sh}" "${SCRIPT}" echo preserved 2>"${WORK}/chown.err" | grep -q 'dropped privileges'; then
|
||||
printf 'root phase did not reach the privilege drop\n'
|
||||
fi
|
||||
cat "${WORK}/chown.log"
|
||||
}
|
||||
|
||||
assert_eq "root drops privileges by default" \
|
||||
"dropped privileges" \
|
||||
@@ -70,6 +88,56 @@ assert_eq "explicit root option preserves command arguments" \
|
||||
"one two|three" \
|
||||
"$(PATH="${WORK}/root-bin:${PATH}" FLOCI_RUN_AS_ROOT=true FLOCI_STORAGE_PERSISTENT_PATH="${WORK}/absent" LOCALSTACK_PARITY=false sh "${SCRIPT}" sh -c 'printf "%s|%s\n" "$1" "$2"' _ "one two" three)"
|
||||
|
||||
# --- the root phase re-owns the configured state dir, and never / ---
|
||||
CUSTOM_DIR="${WORK}/custom-data"
|
||||
mkdir -p "${CUSTOM_DIR}"
|
||||
CUSTOM_DIR_PHYSICAL="$(cd -P "${CUSTOM_DIR}" && pwd -P)"
|
||||
assert_eq "root re-owns a custom FLOCI_STORAGE_PERSISTENT_PATH" \
|
||||
"--preserve-root -R floci:root ${CUSTOM_DIR_PHYSICAL}" \
|
||||
"$(chowned "${CUSTOM_DIR}")"
|
||||
|
||||
assert_eq "root re-owns a relative path resolved against the working directory" \
|
||||
"--preserve-root -R floci:root ${CUSTOM_DIR_PHYSICAL}" \
|
||||
"$(cd "${WORK}" && chowned custom-data)"
|
||||
|
||||
mkdir -p "${WORK}/elsewhere/custom-data"
|
||||
assert_eq "CDPATH does not redirect a relative path" \
|
||||
"--preserve-root -R floci:root ${CUSTOM_DIR_PHYSICAL}" \
|
||||
"$(cd "${WORK}" && CDPATH="${WORK}/elsewhere" chowned custom-data)"
|
||||
|
||||
assert_eq "root skips a state dir that does not exist" \
|
||||
"" \
|
||||
"$(chowned "${WORK}/absent")"
|
||||
|
||||
assert_eq "root never re-owns /" \
|
||||
"" \
|
||||
"$(chowned /)"
|
||||
|
||||
assert_eq "root never re-owns //" \
|
||||
"" \
|
||||
"$(chowned //)"
|
||||
|
||||
# bash keeps a leading // through cd -P and pwd -P, which dash normalises to /, so only a bash
|
||||
# run can catch a guard that compares the path as a string. The native images run bash as sh.
|
||||
if command -v bash >/dev/null 2>&1; then
|
||||
assert_eq "root never re-owns // under bash" \
|
||||
"" \
|
||||
"$(TEST_SH=bash chowned //)"
|
||||
else
|
||||
printf '[SKIP] root never re-owns // under bash (bash not installed)\n'
|
||||
fi
|
||||
|
||||
assert_eq "root never re-owns a path that resolves to /" \
|
||||
"" \
|
||||
"$(chowned "${WORK}/../../../../../../../../../..")"
|
||||
if grep -q 'resolves to /' "${WORK}/chown.err"; then
|
||||
printf '[PASS] refusing / says why\n'
|
||||
PASS=$((PASS + 1))
|
||||
else
|
||||
printf '[FAIL] refusing / says why\n stderr was:\n%s\n' "$(cat "${WORK}/chown.err")"
|
||||
FAIL=$((FAIL + 1))
|
||||
fi
|
||||
|
||||
assert_eq "non-root process cannot elevate with root option" \
|
||||
"one two" \
|
||||
"$(FLOCI_RUN_AS_ROOT=true LOCALSTACK_PARITY=false sh "${SCRIPT}" echo one two)"
|
||||
|
||||
Reference in new issue
Block a user