#!/bin/bash # # Proof test for patches/v1.11.0/0005-posix-no-symlinks.patch. # # sudo bash proof.sh /path/to/versitygw # # Starts two throwaway gateways from the given binary, A on 127.0.0.1:${PROOF_PORT:-7393} # and B on the next port, each on the posix backend rooted at its own namespace # directory under ${PROOF_DIR:-/var/tmp/s3gw-no-symlinks} (ns-a, ns-b), with its own # internal IAM, --chuid --chgid and --dir-perms 0700 as leil-rig's gateways run. # It then replays Norn's M5 stage 13 escape layout: as root, owned by the user's # UID, it plants in A's home and in B's bucket links to the other namespace's # marker (absolute and relative), a link to a directory there, a link to another # user's home in the same namespace, a FIFO, a socket and a device, and drives # every S3 operation at them. Nothing installed is touched: no package, unit or # config. Prints one line per check and exits non-zero if any fails. KEEP=1 # leaves the directory (and both gateways' logs) behind. # # The accounts, made by root through each gateway's admin API, on # leil-cluster's reserved test band: # # ns-user uid 74990 user owns "home" on A and "data" on B # ns-other uid 74991 user owns "sibling" on A (another user's home) # # It needs root: it chowns, plants entries as root and makes a device node. set -uo pipefail VGW=${1:?usage: proof.sh /path/to/versitygw} HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) S3REQ=${S3REQ:-$HERE/../owner-scope/s3req.py} PORT_A=${PROOF_PORT:-7393} PORT_B=$((PORT_A + 1)) W=${PROOF_DIR:-/var/tmp/s3gw-no-symlinks} export S3_REGION=us-east-1 UID_USER=74990 UID_OTHER=74991 [ -x "$VGW" ] || { echo "FATAL: $VGW is not an executable" >&2; exit 2; } [ -f "$S3REQ" ] || { echo "FATAL: $S3REQ is missing (tests/owner-scope/s3req.py)" >&2; exit 2; } [ "$(id -u)" = 0 ] || { echo "FATAL: run as root" >&2; exit 2; } command -v python3 >/dev/null || { echo "FATAL: python3 is required" >&2; exit 2; } for port in "$PORT_A" "$PORT_B"; do if (exec 3<>"/dev/tcp/127.0.0.1/$port") 2>/dev/null; then echo "FATAL: something already listens on 127.0.0.1:$port (set PROOF_PORT)" >&2 exit 2 fi done rm -rf "$W" && mkdir -p "$W/ns-a" "$W/ns-b" "$W/iam-a" "$W/iam-b" "$W/tmp" || exit 2 chmod 0711 "$W" "$W/ns-a" "$W/ns-b" secret() { python3 -c 'import secrets; print(secrets.token_hex(16))'; } declare -A KEY SECRET for who in root ns-user ns-other; do KEY[$who]=$who SECRET[$who]=$(secret); done KEY[root]=proofroot start() { # start "$VGW" --access "${KEY[root]}" --secret "${SECRET[root]}" --region "$S3_REGION" \ --port "127.0.0.1:$2" --iam-dir "$4" \ posix --chuid --chgid --dir-perms 0700 "$3" >"$W/gateway-$1.log" 2>&1 & echo $! } GW_A=$(start a "$PORT_A" "$W/ns-a" "$W/iam-a") GW_B=$(start b "$PORT_B" "$W/ns-b" "$W/iam-b") cleanup() { kill "$GW_A" "$GW_B" 2>/dev/null wait "$GW_A" "$GW_B" 2>/dev/null [ "${KEEP:-0}" = 1 ] && echo "kept $W" || rm -rf "$W" } trap cleanup EXIT for port in "$PORT_A" "$PORT_B"; do for _ in $(seq 1 50); do (exec 3<>"/dev/tcp/127.0.0.1/$port") 2>/dev/null && break kill -0 "$GW_A" 2>/dev/null && kill -0 "$GW_B" 2>/dev/null || { cat "$W"/gateway-*.log; echo "FATAL: a gateway exited" >&2; exit 2; } sleep 0.2 done done echo "gateways $("$VGW" --version 2>&1 | head -1) on 127.0.0.1:$PORT_A (A) and :$PORT_B (B), sha256 $(sha256sum "$VGW" | cut -c1-16)" port() { [ "$1" = A ] && echo "$PORT_A" || echo "$PORT_B"; } admin() { # admin local gw=$1; shift ADMIN_ACCESS_KEY_ID=${KEY[root]} ADMIN_SECRET_ACCESS_KEY=${SECRET[root]} \ "$VGW" admin --region "$S3_REGION" --endpoint-url "http://127.0.0.1:$(port "$gw")" "$@" } # req -- one request; prints the status req() { local gw=$1 who=$2; shift 2 S3_ENDPOINT=http://127.0.0.1:$(port "$gw") S3_KEY=${KEY[$who]} S3_SECRET=${SECRET[$who]} \ timeout 20 python3 "$S3REQ" "$@" 2>"$W/tmp/err" || [ -s "$W/tmp/err" ] || echo "no answer in 20 s" >"$W/tmp/err" } PASS=0 FAIL=0 ok() { PASS=$((PASS + 1)); printf 'PASS %-4s %s %s\n' "$1" "$2" "$3"; } bad() { FAIL=$((FAIL + 1)); printf 'FAIL %-4s %s (want %s) %s [%s]\n' "$1" "${2:-none}" "$3" "$4" "$5"; } # check check() { local id=$1 want=$2 what=$3; shift 3 local got got=$(req "$@") if [ "$got" = "$want" ]; then ok "$id" "$got" "$what"; else bad "$id" "$got" "$want" "$what" "$(tr '\n' ' ' <"$W/tmp/err")"; fi } # check_cmd -- whether the command succeeds check_cmd() { local id=$1 want=$2 what=$3; shift 3 local got=yes "$@" >"$W/tmp/err" 2>&1 || got=no if [ "$got" = "$want" ]; then ok "$id" "$got" "$what"; else bad "$id" "$got" "$want" "$what" "$(tr '\n' ' ' <"$W/tmp/err")"; fi } # The namespaces' data, as each gateway stores it A=$W/ns-a B=$W/ns-b MARK_A="marker of home namespace A" MARK_B="marker of bucket namespace B" MARK_SIB="marker of another home in A" is_file() { [ -f "$1" ] && [ ! -L "$1" ]; } is_link() { [ -L "$1" ]; } holds() { [ "$(cat "$1" 2>/dev/null)" = "$2" ]; } absent() { [ ! -e "$1" ] && [ ! -L "$1" ]; } no_attrs() { [ -z "$(getfattr --absolute-names -d -m '^user\.X-Amz' "$1" 2>/dev/null)" ]; } markers_intact() { holds "$A/home/escape/marker.txt" "$MARK_A" && holds "$B/data/escape/marker.txt" "$MARK_B" && holds "$A/sibling/secret.txt" "$MARK_SIB" && no_attrs "$A/home/escape/marker.txt" && no_attrs "$B/data/escape/marker.txt" && no_attrs "$A/sibling/secret.txt" } echo "--- setup (root, admin API; then the planted entries, as root)" for gw in A B; do check_cmd S1$gw yes "$gw create-user ns-user (uid $UID_USER)" \ admin "$gw" create-user --access ns-user --secret "${SECRET[ns-user]}" --role user --user-id $UID_USER --group-id $UID_USER done check_cmd S2 yes "A create-user ns-other (uid $UID_OTHER)" \ admin A create-user --access ns-other --secret "${SECRET[ns-other]}" --role user --user-id $UID_OTHER --group-id $UID_OTHER check_cmd S3 yes "A create-bucket home --owner ns-user" admin A create-bucket --owner ns-user --bucket home check_cmd S4 yes "A create-bucket sibling --owner ns-other" admin A create-bucket --owner ns-other --bucket sibling check_cmd S5 yes "B create-bucket data --owner ns-user" admin B create-bucket --owner ns-user --bucket data printf '%s' "$MARK_A" >"$W/tmp/mark-a"; printf '%s' "$MARK_B" >"$W/tmp/mark-b"; printf '%s' "$MARK_SIB" >"$W/tmp/mark-sib" check S6 200 "A ns-user PutObject home/escape/marker.txt" A ns-user PUT /home/escape/marker.txt --body "$W/tmp/mark-a" check S7 200 "B ns-user PutObject data/escape/marker.txt" B ns-user PUT /data/escape/marker.txt --body "$W/tmp/mark-b" check S8 200 "A ns-other PutObject sibling/secret.txt" A ns-other PUT /sibling/secret.txt --body "$W/tmp/mark-sib" plant() { # plant local d=$1 other=$2 rel=$3 ln -s "$other/marker.txt" "$d/link-abs" && ln -s "$rel/marker.txt" "$d/link-rel" && ln -s "$other" "$d/link-dir" && ln -s "$other/marker.txt" "$d/link-del" && mkfifo "$d/fifo" && python3 -c 'import socket,sys; s=socket.socket(socket.AF_UNIX); s.bind(sys.argv[1])' "$d/sock" && mknod "$d/dev" c 1 3 && chown -h $UID_USER:$UID_USER "$d"/link-* "$d/fifo" "$d/sock" "$d/dev" } check_cmd S9 yes "plant in A home/escape: links to B's marker (abs, ../../../ns-b/...), to B's dir, FIFO, socket, device" \ plant "$A/home/escape" "$B/data/escape" ../../../ns-b/data/escape check_cmd S10 yes "plant in B data/escape: links to A's marker (abs, ../../../ns-a/...), to A's dir, FIFO, socket, device" \ plant "$B/data/escape" "$A/home/escape" ../../../ns-a/home/escape check_cmd S11 yes "plant in A home/escape: link-sib -> ../../sibling/secret.txt (another home, same namespace)" \ bash -c "ln -s ../../sibling/secret.txt '$A/home/escape/link-sib' && chown -h $UID_USER:$UID_USER '$A/home/escape/link-sib'" check_cmd S12 yes "the links resolve on this host (as they did on leil-rig)" \ bash -c "holds() { [ \"\$(cat \"\$1\")\" = \"\$2\" ]; }; holds '$A/home/escape/link-abs' '$MARK_B' && holds '$A/home/escape/link-rel' '$MARK_B' && holds '$B/data/escape/link-abs' '$MARK_A' && holds '$B/data/escape/link-rel' '$MARK_A' && holds '$A/home/escape/link-sib' '$MARK_SIB'" echo "--- reads: a link, FIFO, socket or device is no object (404), at the end of the key or on its way" n=0 for gw in A B; do bk=$([ "$gw" = A ] && echo home || echo data) for key in link-abs link-rel link-dir/marker.txt link-dir/ fifo sock dev $([ "$gw" = A ] && echo link-sib); do n=$((n + 1)) check R$n 404 "$gw GetObject $bk/escape/$key" "$gw" ns-user GET "/$bk/escape/$key" -o "$W/tmp/body" n=$((n + 1)) check R$n 404 "$gw HeadObject $bk/escape/$key" "$gw" ns-user HEAD "/$bk/escape/$key" done done check R50 404 "A GetObjectTagging home/escape/link-abs" A ns-user GET /home/escape/link-abs tagging check R51 404 "A GetObjectAttributes home/escape/link-rel" A ns-user GET /home/escape/link-rel attributes -H "x-amz-object-attributes: ETag,ObjectSize" check R52 404 "A CopyObject from home/escape/link-abs" A ns-user PUT /home/copied.txt -H "x-amz-copy-source: /home/escape/link-abs" check R53 404 "A CopyObject from home/escape/link-dir/marker.txt" A ns-user PUT /home/copied.txt -H "x-amz-copy-source: /home/escape/link-dir/marker.txt" check R54 404 "A CopyObject from home/escape/fifo" A ns-user PUT /home/copied.txt -H "x-amz-copy-source: /home/escape/fifo" check R55 200 "A GetObject home/escape/marker.txt (its own object still reads)" A ns-user GET /home/escape/marker.txt -o "$W/tmp/body" check_cmd R56 yes " ...and holds A's marker" holds "$W/tmp/body" "$MARK_A" echo "--- listings skip them and never enter them" listed() { # listed -- the keys and common prefixes, one per line req "$1" ns-user GET "/$2" "$3" -o "$W/tmp/list.xml" >/dev/null python3 -c 'import re,sys; d=open(sys.argv[1]).read(); print("\n".join(re.findall(r"<(?:Key|Prefix)>([^<]*)", d)))' "$W/tmp/list.xml" } expect_listed() { # expect_listed local got got=$(listed "$4" "$5" "$6" | grep -v '^$' | grep -vx "$(sed -n 's/.*prefix=\([^&]*\).*/\1/p' <<<"$6")" | sort | tr '\n' ' ') if [ "$got" = "$3" ]; then ok "$1" "[$got]" "$2"; else bad "$1" "[$got]" "[$3]" "$2" "$(head -c 300 "$W/tmp/list.xml")"; fi } expect_listed L1 "A ListObjectsV2 home prefix escape/: its marker alone" "escape/marker.txt " A home "list-type=2&prefix=escape/" expect_listed L2 "B ListObjectsV2 data prefix escape/: its marker alone" "escape/marker.txt " B data "list-type=2&prefix=escape/" expect_listed L3 "A ListObjectsV2 home prefix escape/link-dir/: nothing" "" A home "list-type=2&prefix=escape/link-dir/" expect_listed L4 "B ListObjects (v1) data prefix escape/link-dir/: nothing" "" B data "prefix=escape/link-dir/" expect_listed L5 "A ListObjectsV2 home delimiter /, prefix escape/: no link-dir/ prefix" "escape/marker.txt " A home "list-type=2&prefix=escape/&delimiter=/" expect_listed L6 "A ListObjectVersions home prefix escape/" "escape/marker.txt " A home "versions&prefix=escape/" echo "--- writes act on the entry's own name, never through it" printf 'written by ns-user\n' >"$W/tmp/new" check W1 200 "A PutObject home/escape/link-abs (over the link)" A ns-user PUT /home/escape/link-abs --body "$W/tmp/new" check_cmd W2 yes " ...the link is now that object, a file of uid $UID_USER" \ bash -c "[ -f '$A/home/escape/link-abs' ] && [ ! -L '$A/home/escape/link-abs' ] && [ \$(stat -c %u '$A/home/escape/link-abs') = $UID_USER ]" check W3 200 "B PutObject data/escape/fifo (over the FIFO)" B ns-user PUT /data/escape/fifo --body "$W/tmp/new" check_cmd W4 yes " ...now a regular file" is_file "$B/data/escape/fifo" check W5 200 "A CopyObject home/escape/marker.txt to home/escape/link-rel" A ns-user PUT /home/escape/link-rel -H "x-amz-copy-source: /home/escape/marker.txt" check_cmd W6 yes " ...link-rel is now a copy of A's marker" bash -c "[ ! -L '$A/home/escape/link-rel' ] && [ \"\$(cat '$A/home/escape/link-rel')\" = '$MARK_A' ]" check W7 409 "A PutObject home/escape/link-dir/new.txt (under the link)" A ns-user PUT /home/escape/link-dir/new.txt --body "$W/tmp/new" check W8 409 "B PutObject data/escape/link-dir/new.txt (under the link)" B ns-user PUT /data/escape/link-dir/new.txt --body "$W/tmp/new" check W9 409 "A CopyObject to home/escape/link-dir/copy.txt" A ns-user PUT /home/escape/link-dir/copy.txt -H "x-amz-copy-source: /home/escape/marker.txt" check W10 409 "A PutObject home/escape/sock/x (under the socket)" A ns-user PUT /home/escape/sock/x --body "$W/tmp/new" check_cmd W11 yes " ...nothing was written in B, or in A through B's link" \ bash -c "[ ! -e '$B/data/escape/new.txt' ] && [ ! -e '$B/data/escape/copy.txt' ] && [ ! -e '$A/home/escape/new.txt' ]" TAGS=$W/tmp/tags.xml printf 'kv' >"$TAGS" check W12 404 "B PutObjectTagging data/escape/link-abs" B ns-user PUT /data/escape/link-abs tagging --body "$TAGS" check W13 404 "A PutObjectTagging home/escape/link-dir/marker.txt" A ns-user PUT /home/escape/link-dir/marker.txt tagging --body "$TAGS" check W14 404 "A PutObjectTagging home/escape/link-sib" A ns-user PUT /home/escape/link-sib tagging --body "$TAGS" # a multipart upload completed over a link (B's link-dir/ has its own marker under it, so complete onto link-del's twin) ln -s "$A/home/escape/marker.txt" "$B/data/escape/link-mp" && chown -h $UID_USER:$UID_USER "$B/data/escape/link-mp" req B ns-user POST /data/escape/link-mp uploads -o "$W/tmp/mp.xml" >/dev/null UPLOAD=$(sed -n 's:.*\([^<]*\).*:\1:p' "$W/tmp/mp.xml") check W15 200 "B UploadPart data/escape/link-mp" B ns-user PUT /data/escape/link-mp "partNumber=1&uploadId=$UPLOAD" --body "$W/tmp/new" printf '1"%s"' \ "$(md5sum <"$W/tmp/new" | cut -d' ' -f1)" >"$W/tmp/complete.xml" check W16 200 "B CompleteMultipartUpload data/escape/link-mp (over the link)" B ns-user POST /data/escape/link-mp "uploadId=$UPLOAD" --body "$W/tmp/complete.xml" check_cmd W17 yes " ...link-mp is now that object" bash -c "[ ! -L '$B/data/escape/link-mp' ] && [ \"\$(cat '$B/data/escape/link-mp')\" = 'written by ns-user' ]" echo "--- deletes remove the entry itself, never what a link points to" check D1 204 "A DeleteObject home/escape/link-del" A ns-user DELETE /home/escape/link-del check_cmd D2 yes " ...the link is gone, B's marker is not" bash -c "[ ! -L '$A/home/escape/link-del' ] && [ -f '$B/data/escape/marker.txt' ]" check D3 204 "B DeleteObject data/escape/link-dir/marker.txt (under the link)" B ns-user DELETE /data/escape/link-dir/marker.txt check_cmd D4 yes " ...A's marker is still there, and the link" bash -c "[ -f '$A/home/escape/marker.txt' ] && [ -L '$B/data/escape/link-dir' ]" check D5 204 "A DeleteObject home/escape/sock" A ns-user DELETE /home/escape/sock check D6 204 "A DeleteObject home/escape/dev" A ns-user DELETE /home/escape/dev check_cmd D7 yes " ...both gone" bash -c "[ ! -e '$A/home/escape/sock' ] && [ ! -e '$A/home/escape/dev' ]" DEL=$W/tmp/delete.xml printf 'escape/link-delescape/link-dir/marker.txt' >"$DEL" check D8 200 "B DeleteObjects data: escape/link-del, escape/link-dir/marker.txt" B ns-user POST /data delete --body "$DEL" check_cmd D9 yes " ...B's link-del is gone, A's marker is not" bash -c "[ ! -L '$B/data/escape/link-del' ] && [ -f '$A/home/escape/marker.txt' ]" echo "--- a directory swapped for a link while requests run" check X0 200 "A ns-user PutObject home/race/obj.txt" A ns-user PUT /home/race/obj.txt --body "$W/tmp/mark-a" mkdir -p "$B/data/racedir" && printf '%s' "$MARK_B" >"$B/data/racedir/obj.txt" && printf '%s' "$MARK_B" >"$B/data/racedir/victim.txt" && ln -s "$B/data/racedir" "$A/home/race-alt" check_cmd X1 yes "3 s of GET, PUT and DELETE under home/race/ while it swaps with a link to B's directory" \ env S3_ENDPOINT=http://127.0.0.1:$PORT_A S3_KEY=ns-user S3_SECRET="${SECRET[ns-user]}" S3REQ="$S3REQ" \ python3 "$HERE/race.py" "$A/home/race" "$A/home/race-alt" home race "$MARK_A" "$MARK_B" 3 sed 's/^/ /' "$W/tmp/err" check_cmd X2 yes " ...B's directory holds only what it held" \ bash -c "[ \"\$(ls -A '$B/data/racedir' | sort | tr '\n' ' ')\" = 'obj.txt victim.txt ' ] && [ -z \"\$(getfattr --absolute-names -d -m '^user\.' '$B/data/racedir/obj.txt' 2>/dev/null)\" ]" echo "--- the other namespace is as it was" check_cmd Z1 yes "A's marker, B's marker and the sibling's marker unchanged, and no attributes set on them" markers_intact check_cmd Z2 yes "the gateways logged no panic" bash -c "! grep -qi panic '$W'/gateway-*.log" echo echo "$PASS passed, $FAIL failed" [ "$FAIL" = 0 ]