Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): refuse a database or key file that resolves to /

Yudhi Armyndharis0b4ee0b26d8b
diff --git a/scripts/restore.sh b/scripts/restore.sh
index 93112f333..3df8eb663 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -133,6 +133,16 @@ refuse_unwritable() {
exit 1
}
+# refuse_root_target <path> <label>: a database or key file that resolves to / (//, /., a link to /) is
+# snapshotted with cp -pRH, which copies the whole filesystem into a directory inside it, and a root user can
+# write there. Checked first in the snapshot phase, ahead of the writability test a non-root user stops at.
+refuse_root_target() {
+ if [ "$(resolve_path "$1")" = / ]; then
+ log "ERROR: refusing to replace broad $2 target: $1"
+ exit 1
+ fi
+}
+
replace_tree() {
source_dir="$1"
target_dir="$2"
@@ -222,6 +232,7 @@ snapshot_external() {
restore_db() {
resolved_db="$(resolve_path "$2")"
if [ "$PHASE" = snapshot ]; then
+ refuse_root_target "$2" "$3"
openwa_writable "$2" || refuse_unwritable "$2" "$3"
for db in "$2" "$resolved_db"; do
for sfx in -wal -shm -journal; do
@@ -450,6 +461,7 @@ restore_targets() {
# with the other targets: it may be the only plaintext copy of a key the replaced main.sqlite accepts.
restore_admin_key() {
if [ "$PHASE" = snapshot ]; then
+ refuse_root_target "$ADMIN_KEY_FILE" "admin key (BOOTSTRAP_KEY_FILE)"
openwa_writable "$ADMIN_KEY_FILE" || refuse_unwritable "$ADMIN_KEY_FILE" "admin key (BOOTSTRAP_KEY_FILE)"
snapshot_external "$ADMIN_KEY_FILE"
return
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index 8ed90df36..4c24c53ac 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -81,6 +81,9 @@
# (aq) a restore removes the -wal, -shm and -journal of every database it replaces, beside the file a
# link points at and beside the link's own name, and an external database's are kept in its
# snapshot
+# (ar) restore refuses a MAIN_DATABASE_NAME, DATABASE_NAME or BOOTSTRAP_KEY_FILE that is /, //, /. or a
+# link to /, ahead of the writability check a non-root user would stop at (skipped where a user can
+# write /)
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -2582,5 +2585,29 @@ for sfx in -wal -shm -journal; do
done
pass "(aq) -wal, -shm and -journal are cleared from both databases and kept in their snapshots"
+echo ""
+echo "==> (ar) restore refuses a database or key file that resolves to /"
+# restore_db and restore_admin_key snapshot their target with cp -pRH, so a root user whose target is / would
+# copy the filesystem into a directory inside it. A non-root user is stopped by the writability check, which
+# is why the refusal has to come before it, and why these runs, like the root spellings in (am), have the cp
+# that stops at / ahead on PATH (created there, so they run only where (am) does).
+if [ "$ROOT_SAFE" -eq 1 ]; then
+ AR="$WORK/ar"
+ mkdir -p "$AR/work/data" "$AR/home"
+ printf 'ar-keep\n' >"$AR/work/data/keep.txt"
+ ln -s / "$AR/rootlink"
+ for target in 'MAIN_DATABASE_NAME auth/audit DB' 'DATABASE_NAME data store' 'BOOTSTRAP_KEY_FILE admin key (BOOTSTRAP_KEY_FILE)'; do
+ ar_var="${target%% *}"
+ ar_label="${target#* }"
+ for ar_spelling in / // /. "$AR/rootlink"; do
+ refused_restore "$AR" "ar: $ar_var=$ar_spelling" "refusing to replace broad $ar_label target" "$AR/work" "$ARCHIVE_AP" \
+ PATH="$AM/cpguard:$PATH" OPENWA_DATA_DIR="$AR/work/data" "$ar_var=$ar_spelling"
+ done
+ done
+ pass "(ar) MAIN_DATABASE_NAME, DATABASE_NAME and BOOTSTRAP_KEY_FILE as /, //, /. or a link to / are refused"
+else
+ echo "SKIP: (ar) the / cases, which need a user who cannot write /"
+fi
+
echo ""
echo "All smoke tests passed!"