| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# spell-checker:ignore strace reflink FDCWD EXDEV tmpfs xdev dstdir RDONLY |
| 4 |
# |
| 5 |
# Redundant-open verification. |
| 6 |
# |
| 7 |
# These strace-based checks assert that a utility resolves an operand path |
| 8 |
# once and works off the resulting descriptor, rather than re-opening the |
| 9 |
# same path for each stage of its work. Repeated opens cost syscalls and |
| 10 |
# let two stages disagree about which file they are acting on. |
| 11 |
# |
| 12 |
# This is a correctness and efficiency check, not a security one: the |
| 13 |
# companion scripts check-toctou.sh and check-safe-traversal.sh cover the |
| 14 |
# cases where a split across two path-based syscalls is actually |
| 15 |
# attacker-exploitable. |
| 16 |
# |
| 17 |
|
| 18 |
set -e |
| 19 |
|
| 20 |
echo "=== Redundant Open Verification ===" |
| 21 |
|
| 22 |
# shellcheck disable=SC2034 # read by check-common.sh once sourced |
| 23 |
CHECK_UTILS="cp mv" |
| 24 |
. "$(dirname "${BASH_SOURCE[0]}")/check-common.sh" |
| 25 |
|
| 26 |
# check-common.sh installs `trap cleanup EXIT`; extend it so the scratch |
| 27 |
# directory on the second filesystem goes away with it. |
| 28 |
SHM_DIR="" |
| 29 |
cleanup() { |
| 30 |
rm -rf "$TEMP_DIR" |
| 31 |
if [ -n "$SHM_DIR" ]; then |
| 32 |
rm -rf "$SHM_DIR" |
| 33 |
fi |
| 34 |
} |
| 35 |
|
| 36 |
cd "$TEMP_DIR" |
| 37 |
|
| 38 |
# cp used to open the source three times per copy: once in check_for_data, |
| 39 |
# once in check_sparse_detection, then again in the copy itself. The |
| 40 |
# sparseness probe and the copy now share one descriptor, so the strategy |
| 41 |
# and the bytes come from the same file (#13185). |
| 42 |
if cp_cmd=$(util_cmd cp); then |
| 43 |
echo "" |
| 44 |
echo "Testing cp (single source open)..." |
| 45 |
# rustix issues open(2) directly on Linux, so match both spellings -- |
| 46 |
# an openat-only filter would silently match nothing and pass. |
| 47 |
for mode in "" "--reflink=never --sparse=always" "--reflink=never --sparse=never"; do |
| 48 |
rm -f cp_probe_dst |
| 49 |
printf 'source bytes for the probe\n' > cp_probe_src.txt |
| 50 |
# shellcheck disable=SC2086 # $mode is a deliberate word-split flag list |
| 51 |
strace -f -e trace=open,openat -o strace_cp_open.log \ |
| 52 |
$cp_cmd $mode cp_probe_src.txt cp_probe_dst 2>/dev/null || true |
| 53 |
|
| 54 |
if [ ! -s strace_cp_open.log ]; then |
| 55 |
fail_immediately "strace produced no output for cp" |
| 56 |
fi |
| 57 |
opens=$(grep -cE '(open|openat)\(.*"cp_probe_src\.txt"' strace_cp_open.log || true) |
| 58 |
if [ "$opens" -ne 1 ]; then |
| 59 |
cat strace_cp_open.log |
| 60 |
fail_immediately "cp ${mode:-(default)} opened the source $opens times, expected exactly 1 - the sparseness probe and the copy must share one descriptor (#13185)" |
| 61 |
fi |
| 62 |
echo "✓ cp ${mode:-(default)} opens the source exactly once" |
| 63 |
done |
| 64 |
rm -f cp_probe_src.txt cp_probe_dst |
| 65 |
fi |
| 66 |
|
| 67 |
# mv must not open an operand it is only going to rename, and its |
| 68 |
# cross-device fallback -- a copy, since rename(2) cannot cross filesystems -- |
| 69 |
# must open the source once. mv has its own fallback rather than cp's |
| 70 |
# (mv.rs, open_source + create_dest_restrictive + copy_fast), so this pins |
| 71 |
# an invariant that a future move onto shared copy machinery could break. |
| 72 |
if mv_cmd=$(util_cmd mv); then |
| 73 |
echo "" |
| 74 |
echo "Testing mv (single source open)..." |
| 75 |
|
| 76 |
printf 'moved by rename\n' > mv_probe_src.txt |
| 77 |
strace -f -e trace=open,openat -o strace_mv_rename.log \ |
| 78 |
$mv_cmd mv_probe_src.txt mv_probe_dst 2>/dev/null || true |
| 79 |
if [ ! -s strace_mv_rename.log ]; then |
| 80 |
fail_immediately "strace produced no output for mv" |
| 81 |
fi |
| 82 |
opens=$(grep -cE '(open|openat)\(.*"mv_probe_src\.txt"' strace_mv_rename.log || true) |
| 83 |
if [ "$opens" -ne 0 ]; then |
| 84 |
cat strace_mv_rename.log |
| 85 |
fail_immediately "mv opened the source $opens times for a same-filesystem move, expected 0 - it must rename, not copy" |
| 86 |
fi |
| 87 |
echo "✓ mv (same filesystem) never opens the source" |
| 88 |
rm -f mv_probe_dst |
| 89 |
|
| 90 |
# /dev/shm is tmpfs on Linux, so it is a different filesystem from |
| 91 |
# $TEMP_DIR and rename(2) across the two returns EXDEV. Skip rather than |
| 92 |
# fail where that does not hold, so the check stays honest about what it |
| 93 |
# actually exercised. |
| 94 |
if [ -d /dev/shm ] && [ "$(stat -c %d /dev/shm)" != "$(stat -c %d "$TEMP_DIR")" ]; then |
| 95 |
SHM_DIR=$(mktemp -d /dev/shm/uutils-open-once.XXXXXX) |
| 96 |
|
| 97 |
printf 'moved across filesystems\n' > mv_probe_xdev.txt |
| 98 |
strace -f -e trace=open,openat -o strace_mv_xdev.log \ |
| 99 |
$mv_cmd mv_probe_xdev.txt "$SHM_DIR/dst" 2>/dev/null || true |
| 100 |
opens=$(grep -cE '(open|openat)\(.*"mv_probe_xdev\.txt"' strace_mv_xdev.log || true) |
| 101 |
if [ "$opens" -ne 1 ]; then |
| 102 |
cat strace_mv_xdev.log |
| 103 |
fail_immediately "mv opened the source $opens times for a cross-device move, expected exactly 1" |
| 104 |
fi |
| 105 |
echo "✓ mv (cross-device file) opens the source exactly once" |
| 106 |
|
| 107 |
mkdir -p mv_probe_dir |
| 108 |
printf 'inner payload\n' > mv_probe_dir/inner.txt |
| 109 |
strace -f -e trace=open,openat -o strace_mv_xdev_dir.log \ |
| 110 |
$mv_cmd mv_probe_dir "$SHM_DIR/dstdir" 2>/dev/null || true |
| 111 |
# Match any path ending in inner.txt opened O_RDONLY: that is the |
| 112 |
# source read, whether it is spelled relative to the cwd or, once the |
| 113 |
# traversal is fd-anchored, as a bare name under a directory fd. The |
| 114 |
# O_RDONLY rules out the destination, which is opened for writing. |
| 115 |
opens=$(grep -cE '(open|openat)\([^)]*"[^"]*inner\.txt"[^)]*O_RDONLY' strace_mv_xdev_dir.log || true) |
| 116 |
if [ "$opens" -ne 1 ]; then |
| 117 |
cat strace_mv_xdev_dir.log |
| 118 |
fail_immediately "mv opened a file inside a cross-device directory move $opens times, expected exactly 1" |
| 119 |
fi |
| 120 |
echo "✓ mv (cross-device directory) opens each file exactly once" |
| 121 |
else |
| 122 |
echo "- skipped mv cross-device checks: no second filesystem available" |
| 123 |
fi |
| 124 |
rm -rf mv_probe_src.txt mv_probe_xdev.txt mv_probe_dir |
| 125 |
fi |
| 126 |
|
| 127 |
echo "" |
| 128 |
echo "✓ Redundant open verification completed" |