extract: verify always runs, failures remove the extract, walk fails closed
Security review of52b1e65: 1. (HIGH) tarfile's stream could enumerate differently from GNU tar. Now extract.sh captures tar's exit code (`|| TAR_RC=$?`), ALWAYS runs the post-extract verify on whatever tar wrote, and any failure (tar, verify, either pre-extract scan) removes $DEST before exiting non-zero. The classifier also refuses (exit 3) on any stream exception and when its member count differs from GNU tar's own escape-quoted listing. Measured: a lone zero block mid-archive stops BOTH readers at the same member (no -i is used), so this is a guard, not a known differential. 2. (MEDIUM) os.walk swallowed unreadable directories. verify now passes onerror (recorded => fail) and refuses any directory that is not readable+searchable.52b1e65exited 0 on the unreadable fixture. 3. (MEDIUM) a refused tree stayed in the bind-mounted output. fail_extract opens up 0000 dirs (find -P -type d, never follows links), then rm -rf -- "$DEST". Fixtures: cpmove-bad-{unreadable,tarfail,truncated}; a fake tar that lists one extra member proves the count check. 14 checks fail on52b1e65. Docker: unreadable / tarfail runs end with status failed, failed_stage extract and no extract-work left in /host/sanitized/<id>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
52b1e656c3
commit
d4a63fba0a
4 files changed
+161
-33
No files matched your search
+20
-1
@@ -188,6 +188,10 @@ def build(fname, members):
|
||||
ti = tarfile.TarInfo(name); ti.mode = 0o644
|
||||
if kind == 'sym':
|
||||
ti.type = tarfile.SYMTYPE; ti.linkname = arg; tf.addfile(ti)
|
||||
elif kind == 'hard':
|
||||
ti.type = tarfile.LNKTYPE; ti.linkname = arg; tf.addfile(ti)
|
||||
elif kind in ('dir', 'dir0'):
|
||||
ti.type = tarfile.DIRTYPE; ti.mode = 0o755 if kind == 'dir' else 0; tf.addfile(ti)
|
||||
else:
|
||||
ti.size = len(arg); tf.addfile(ti, io.BytesIO(arg))
|
||||
mb = 'homedir/mail/example.com/alice'
|
||||
@@ -196,8 +200,23 @@ build('cpmove-bad-dslash.tar.gz', [(f'cpmove-testuser/homedir//mail/example.com
|
||||
build('cpmove-bad-dotdot.tar.gz', [(f'cpmove-testuser/homedir/../{mb}/.x', 'sym', '../../../../etc')])
|
||||
build('cpmove-bad-abs.tar.gz', [(f'/cpmove-testuser/{mb}/.x', 'sym', '../../../../etc')])
|
||||
build('cpmove-bad-newline.tar.gz', [(f'cpmove-testuser/{mb}/cur/a\ncpmove-testuser/homedir/public_html', 'file', b'x')])
|
||||
# A mail-tree directory the archive makes unreadable (mode 0000): verify
|
||||
# cannot inspect it, so the extract must fail (and be removed).
|
||||
build('cpmove-bad-unreadable.tar.gz', [(f'cpmove-testuser/{mb}/cur/m1', 'file', b'hello'),
|
||||
(f'cpmove-testuser/{mb}/.Locked', 'dir0', None),
|
||||
(f'cpmove-testuser/{mb}/.Locked/cur', 'dir0', None)])
|
||||
# Passes the classifier but GNU tar fails mid-way (hard link to a member that
|
||||
# does not exist), after it already wrote the mail files: the partial tree
|
||||
# must be removed.
|
||||
build('cpmove-bad-tarfail.tar.gz', [(f'cpmove-testuser/{mb}/cur/m1', 'file', b'hello'),
|
||||
('cpmove-testuser/homedir/public_html', 'dir', None),
|
||||
('cpmove-testuser/homedir/public_html/x', 'hard', 'cpmove-testuser/homedir/nonexistent'),
|
||||
(f'cpmove-testuser/{mb}/cur/m2', 'file', b'after the failure')])
|
||||
PY
|
||||
echo "wrote $FIXTURES_DIR/cpmove-bad-{dot,dslash,dotdot,abs,newline}.tar.gz"
|
||||
# Truncated archive: the first half of a valid one.
|
||||
head -c "$(( $(stat -c%s "$FIXTURES_DIR/cpmove-mail.tar.gz") / 2 ))" "$FIXTURES_DIR/cpmove-mail.tar.gz" \
|
||||
> "$FIXTURES_DIR/cpmove-bad-truncated.tar.gz"
|
||||
echo "wrote $FIXTURES_DIR/cpmove-bad-{dot,dslash,dotdot,abs,newline,unreadable,tarfail,truncated}.tar.gz"
|
||||
|
||||
echo ""
|
||||
echo "fixtures built:"
|
||||
|
||||
+35
-1
@@ -79,9 +79,43 @@ for bad in dot dslash dotdot abs newline; do
|
||||
IMPORT_INCLUDE_MAIL=true bash "$REPO/scripts/extract.sh" "$WORK/cpmove-bad-$bad.tar.gz" "$OUT" testuser "$WORK/bad-$bad.json" >"$WORK/bad-$bad.log" 2>&1 || rc=$?
|
||||
check "bad-$bad: extract exits non-zero (rc=$rc)" "[[ $rc -ne 0 ]]"
|
||||
check "bad-$bad: refusal logged" "grep -q 'REFUSING archive' '$WORK/bad-$bad.log'"
|
||||
check "bad-$bad: nothing extracted" "[[ -z \$(find '$OUT' -mindepth 1 2>/dev/null) ]]"
|
||||
check "bad-$bad: extract dir removed" "[[ ! -e '$OUT' ]]"
|
||||
done
|
||||
|
||||
echo "failures after extraction starts remove the extract (IMPORT_INCLUDE_MAIL=true)"
|
||||
for bad in unreadable tarfail truncated; do
|
||||
cp "$FIX/cpmove-bad-$bad.tar.gz" "$WORK/"
|
||||
OUT="$WORK/ex-bad-$bad"
|
||||
rc=0
|
||||
IMPORT_INCLUDE_MAIL=true bash "$REPO/scripts/extract.sh" "$WORK/cpmove-bad-$bad.tar.gz" "$OUT" testuser "$WORK/bad-$bad.json" >"$WORK/bad-$bad.log" 2>&1 || rc=$?
|
||||
check "bad-$bad: extract exits non-zero (rc=$rc)" "[[ $rc -ne 0 ]]"
|
||||
check "bad-$bad: extract dir removed" "[[ ! -e '$OUT' ]]"
|
||||
done
|
||||
check "bad-unreadable: failed in verify (cannot inspect the 0000 dir)" "grep -q 'not readable+searchable\|cannot inspect' '$WORK/bad-unreadable.log'"
|
||||
check "bad-tarfail: tar itself failed (rc logged), verify still ran" "grep -q 'tar failed (rc=' '$WORK/bad-tarfail.log'"
|
||||
check "bad-truncated: refused by the classifier" "grep -q 'REFUSING archive' '$WORK/bad-truncated.log'"
|
||||
|
||||
echo "classifier refuses when GNU tar lists a different member count"
|
||||
FAKEBIN="$WORK/faketar"
|
||||
mkdir -p "$FAKEBIN"
|
||||
REALTAR="$(command -v tar)"
|
||||
cat > "$FAKEBIN/tar" <<FAKE
|
||||
#!/usr/bin/env bash
|
||||
# Real tar, plus one extra member in the escape-quoted listing only.
|
||||
"$REALTAR" "\$@"; rc=\$?
|
||||
[[ " \$* " == *" --quoting-style=escape "* ]] && echo "cpmove-testuser/homedir/mail/hidden-from-the-classifier"
|
||||
exit \$rc
|
||||
FAKE
|
||||
chmod +x "$FAKEBIN/tar"
|
||||
: > "$WORK/cm.excl"; echo '{"actions":[]}' > "$WORK/cm.json"
|
||||
rc=0
|
||||
PATH="$FAKEBIN:$PATH" python3 -I "$REPO/scripts/lib/mail-tree-links.py" scan "$MAILTAR" "$WORK/cm.excl" "$WORK/cm.json" >/dev/null 2>"$WORK/cm.log" || rc=$?
|
||||
check "count mismatch: scan exits 3 (rc=$rc)" "[[ $rc -eq 3 ]]"
|
||||
check "count mismatch: reason logged" "grep -q 'member count differs' '$WORK/cm.log'"
|
||||
rc=0
|
||||
python3 -I "$REPO/scripts/lib/mail-tree-links.py" scan "$MAILTAR" "$WORK/cm.excl" "$WORK/cm.json" >/dev/null 2>&1 || rc=$?
|
||||
check "control: real tar, same archive => scan exits 0" "[[ $rc -eq 0 ]]"
|
||||
|
||||
echo "post-extract authority: mail-tree-links.py verify"
|
||||
V="$WORK/verify"
|
||||
mkvt() { rm -rf "$V"; mkdir -p "$V/$U/homedir/mail/$D/a/cur" "$V/$U/homedir/mail/$D/a/.F/cur" "$V/$U/homedir/public_html" "$V/cpmove-other/homedir/mail/$D/b/cur"; echo m > "$V/$U/homedir/mail/$D/a/cur/1"; }
|
||||
|
||||
Reference in new issue
Block a user