Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(docker): re-own the configured persistent path, not only /app/data (#4525)

TiM7e3ff3c5ee4b
diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh
index b9d205e1b6..86a17a103b 100755
--- a/docker/entrypoint.sh
+++ b/docker/entrypoint.sh
@@ -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
diff --git a/docker/test-entrypoint.sh b/docker/test-entrypoint.sh
index a2eed75fd3..f5f4242a7c 100644
--- a/docker/test-entrypoint.sh
+++ b/docker/test-entrypoint.sh
@@ -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)"