Debian Codegraph @

util/check-safe-traversal.sh

raw
1 #!/bin/bash
2 #
3 # Check that utilities are using safe traversal (openat family syscalls)
4 # to prevent TOCTOU race conditions
5 #
6
7 set -e
8
9 echo "=== Safe Traversal Verification ==="
10
11 # shellcheck disable=SC2034 # read by check-common.sh once sourced
12 CHECK_UTILS="rm chmod chown chgrp du mv cp chcon split"
13 . "$(dirname "${BASH_SOURCE[0]}")/check-common.sh"
14
15 require_command strace
16
17 cd "$TEMP_DIR"
18
19 # Create test directory structure
20 mkdir -p test_dir/sub1/sub2/sub3
21 echo "test1" > test_dir/file1.txt
22 echo "test2" > test_dir/sub1/file2.txt
23 echo "test3" > test_dir/sub1/sub2/file3.txt
24 echo "test4" > test_dir/sub1/sub2/sub3/file4.txt
25
26 check_utility() {
27 local util="$1"
28 local trace_syscalls="$2"
29 local expected_syscalls="$3"
30 local test_args="$4"
31 local test_name="$5"
32
33 echo ""
34 echo "Testing $util ($test_name)..."
35
36 local strace_log="strace_${util}_${test_name}.log"
37
38 local cmd
39 cmd=$(util_cmd "$util") || fail_immediately "$util binary not found in $BIN_DIR"
40
41 # Run utility under strace
42 strace -f -e trace="$trace_syscalls" -o "$strace_log" \
43 $cmd $test_args 2>/dev/null || true
44 cat $strace_log
45 # Check for expected safe syscalls
46 local found_safe=0
47 for syscall in $expected_syscalls; do
48 # fchmodat2 is the modern replacement for fchmodat on Linux 6.6+
49 # Accept either as a valid safe traversal syscall
50 case "$syscall" in
51 fchmodat)
52 if grep -qE "fchmodat2?\(" "$strace_log"; then
53 echo "✓ Found fchmodat/fchmodat2() (safe traversal)"
54 found_safe=$((found_safe + 1))
55 else
56 fail_immediately "Missing fchmodat() or fchmodat2() (safe traversal not active for $util)"
57 fi
58 ;;
59 *)
60 if grep -q "$syscall" "$strace_log"; then
61 echo "✓ Found $syscall() (safe traversal)"
62 found_safe=$((found_safe + 1))
63 else
64 fail_immediately "Missing $syscall() (safe traversal not active for $util)"
65 fi
66 ;;
67 esac
68 done
69
70 # Count detailed syscall statistics
71 local openat_count unlinkat_count fchmodat_count fchownat_count newfstatat_count renameat_count
72 local unlink_count rmdir_count chmod_count chown_count safe_ops unsafe_ops
73
74 openat_count=$(grep -c "openat(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
75 unlinkat_count=$(grep -c "unlinkat(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
76 fchmodat_count=$(grep -cE "fchmodat2?\(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
77 fchownat_count=$(grep -c "fchownat(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
78 newfstatat_count=$(grep -c "newfstatat(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
79 renameat_count=$(grep -c "renameat(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
80
81 # Count old unsafe syscalls (exclude the trace line prefix)
82 unlink_count=$(grep -cE "\bunlink\(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
83 rmdir_count=$(grep -cE "\brmdir\(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
84 chmod_count=$(grep -cE "\bchmod\(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
85 chown_count=$(grep -cE "\b(chown|lchown)\(" "$strace_log" 2>/dev/null | tr -d '\n' || echo "0")
86
87 # Ensure all variables are integers
88 [ -z "$openat_count" ] && openat_count=0
89 [ -z "$unlinkat_count" ] && unlinkat_count=0
90 [ -z "$fchmodat_count" ] && fchmodat_count=0
91 [ -z "$fchownat_count" ] && fchownat_count=0
92 [ -z "$newfstatat_count" ] && newfstatat_count=0
93 [ -z "$renameat_count" ] && renameat_count=0
94 [ -z "$unlink_count" ] && unlink_count=0
95 [ -z "$rmdir_count" ] && rmdir_count=0
96 [ -z "$chmod_count" ] && chmod_count=0
97 [ -z "$chown_count" ] && chown_count=0
98
99 # Calculate totals
100 safe_ops=$((openat_count + unlinkat_count + fchmodat_count + fchownat_count + newfstatat_count + renameat_count))
101 unsafe_ops=$((unlink_count + rmdir_count + chmod_count + chown_count))
102
103 echo " Strace statistics:"
104 echo " Safe syscalls: openat=$openat_count unlinkat=$unlinkat_count fchmodat=$fchmodat_count fchownat=$fchownat_count newfstatat=$newfstatat_count renameat=$renameat_count"
105 echo " Unsafe syscalls: unlink=$unlink_count rmdir=$rmdir_count chmod=$chmod_count chown/lchown=$chown_count"
106 echo " Total: safe=$safe_ops unsafe=$unsafe_ops"
107
108 # For rm specifically, we expect unlinkat instead of unlink/rmdir for file operations
109 # Note: A single rmdir() for the root directory is acceptable because:
110 # 1. The root directory path is provided by the user (not discovered during traversal)
111 # 2. There's no TOCTOU race - we're not resolving paths during recursive operations
112 # 3. After safe traversal removes all contents via unlinkat(), rmdir() is safe for the empty root
113 if [ "$util" = "rm" ]; then
114 if [ "$unlinkat_count" -gt 0 ] && [ "$unlink_count" -eq 0 ] && [ "$rmdir_count" -le 1 ]; then
115 echo "✓ Using safe syscalls (unlinkat for traversal)"
116 if [ "$rmdir_count" -eq 1 ]; then
117 echo " Note: Single rmdir() for root directory is acceptable"
118 fi
119 elif [ "$unlink_count" -gt 0 ] || [ "$rmdir_count" -gt 1 ]; then
120 fail_immediately "$util is UNSAFE: Using unlink/rmdir for file operations (unlink=$unlink_count rmdir=$rmdir_count unlinkat=$unlinkat_count) - vulnerable to TOCTOU attacks"
121 else
122 echo "⚠ No file removal operations detected"
123 fi
124 elif [ "$safe_ops" -gt 0 ] && [ "$unsafe_ops" -eq 0 ]; then
125 echo "✓ Using only safe syscalls"
126 elif [ "$safe_ops" -gt 0 ] && [ "$safe_ops" -ge "$unsafe_ops" ]; then
127 echo "✓ Using primarily safe syscalls"
128 elif [ "$found_safe" -gt 0 ]; then
129 echo "⚠ Some safe syscalls found but mixed with unsafe ops"
130 else
131 fail_immediately "$util is not using safe traversal"
132 fi
133 }
134
135 # Recursive descent must open every subdirectory relative to the parent dirfd
136 # WITH O_NOFOLLOW. Otherwise an attacker who swaps the just-stat'd directory for
137 # a symlink between the stat and the open redirects the descent off-tree (the
138 # chown/chgrp/chmod/rm recursive-descent TOCTOU). The top-level command-line
139 # argument is opened via AT_FDCWD and is allowed to follow, so it is excluded by
140 # matching only numeric-dirfd openat calls.
141 assert_descent_nofollow() {
142 local util="$1"
143 local log="$2"
144 local offenders
145 offenders=$(grep -E 'openat\([0-9]+, "[^"]*",[^)]*O_DIRECTORY' "$log" | grep -v 'O_NOFOLLOW' || true)
146 if [ -n "$offenders" ]; then
147 echo "$offenders"
148 fail_immediately "$util descends into subdirectories without O_NOFOLLOW (TOCTOU symlink-swap risk)"
149 fi
150 echo "✓ $util descent opens use O_NOFOLLOW"
151 }
152
153 # Test rm - should use openat, unlinkat, newfstatat
154 if have_util rm; then
155 cp -r test_dir test_rm
156 check_utility "rm" "openat,unlinkat,newfstatat,unlink,rmdir" "openat" "-rf test_rm" "recursive_remove"
157
158 # Regression guard: rm must not issue path-based statx calls (should rely on dirfd-relative newfstatat)
159 if grep -qE 'statx\(AT_FDCWD, "/' strace_rm_recursive_remove.log; then
160 fail_immediately "rm is using path-based statx (absolute path); expected dirfd-relative newfstatat"
161 fi
162 if grep -qE 'statx\(AT_FDCWD, "[^"]*/' strace_rm_recursive_remove.log; then
163 fail_immediately "rm is using path-based statx (multi-component relative path); expected dirfd-relative newfstatat"
164 fi
165 assert_descent_nofollow "rm" strace_rm_recursive_remove.log
166 fi
167
168 # Test chmod - should use openat, fchmodat, newfstatat
169 if have_util chmod; then
170 cp -r test_dir test_chmod
171 check_utility "chmod" "openat,fchmodat,fchmodat2,newfstatat,chmod" "openat fchmodat" "-R 755 test_chmod" "recursive_chmod"
172
173 # Additional regression guard: ensure recursion uses dirfd-relative openat, not AT_FDCWD with a multi-component path
174 if grep -q 'openat(AT_FDCWD, "test_chmod/' strace_chmod_recursive_chmod.log; then
175 fail_immediately "chmod recursed using AT_FDCWD with a multi-component path; expected dirfd-relative openat"
176 fi
177 assert_descent_nofollow "chmod" strace_chmod_recursive_chmod.log
178 fi
179
180 # Test chown - should use openat, fchownat, newfstatat
181 if have_util chown; then
182 cp -r test_dir test_chown
183 USER_ID=$(id -u)
184 GROUP_ID=$(id -g)
185 check_utility "chown" "openat,fchownat,newfstatat,chown,lchown" "openat fchownat" "-R $USER_ID:$GROUP_ID test_chown" "recursive_chown"
186 assert_descent_nofollow "chown" strace_chown_recursive_chown.log
187 fi
188
189 # Test chgrp - should use openat, fchownat, newfstatat
190 if have_util chgrp; then
191 cp -r test_dir test_chgrp
192 check_utility "chgrp" "openat,fchownat,newfstatat,chown,lchown" "openat fchownat" "-R $GROUP_ID test_chgrp" "recursive_chgrp"
193 assert_descent_nofollow "chgrp" strace_chgrp_recursive_chgrp.log
194 fi
195
196 # chcon recursive relabel must resolve each target relative to the traversal
197 # directory fd with O_NOFOLLOW and operate on the resulting fd (fd-based xattr,
198 # or /proc/self/fd), never re-resolving the entry by path. Otherwise a
199 # rename/symlink race could redirect a privileged recursive relabel off-tree
200 # (issue #11402). This holds even without SELinux: the fd-anchored open happens
201 # before the SELinux get/set, so the syscalls are observable regardless.
202 if have_util chcon; then
203 chcon_cmd=$(util_cmd chcon)
204
205 mkdir -p chcon_tree/sub
206 echo a > chcon_tree/file
207 echo b > chcon_tree/sub/nested
208 strace -f -e trace=openat,getxattr,lgetxattr,fgetxattr,setxattr,lsetxattr,fsetxattr \
209 -o strace_chcon_recursive.log \
210 $chcon_cmd -R -t etc_t chcon_tree 2>/dev/null || true
211
212 # Each relabel target is opened relative to a numeric dirfd with O_NOFOLLOW.
213 if ! grep -qE 'openat\([0-9]+, "(file|sub|nested)", [^)]*O_NOFOLLOW' strace_chcon_recursive.log; then
214 cat strace_chcon_recursive.log
215 fail_immediately "chcon -R must open relabel targets relative to the traversal dirfd with O_NOFOLLOW (issue #11402)"
216 fi
217 # SELinux xattr ops must be fd-anchored: fgetxattr/fsetxattr on a numeric fd,
218 # or *xattr on /proc/self/fd. A path-based xattr on the traversal entry is the
219 # TOCTOU pattern the fix removes.
220 path_based_xattr=$(grep -E '\bl?(get|set)xattr\("' strace_chcon_recursive.log | grep -v '"/proc/self/fd/' || true)
221 if [ -n "$path_based_xattr" ]; then
222 echo "$path_based_xattr"
223 fail_immediately "chcon -R is using path-based SELinux xattr (TOCTOU; expected fd-anchored access, issue #11402)"
224 fi
225 # The relabel must actually reach SELinux through the anchored fd. Without
226 # this positive check, a chcon that aborts before the get/set (e.g. EBADF
227 # from f*filecon on an O_PATH fd) would still pass the assertions above.
228 if ! grep -qE '(get|set)xattr\("/proc/self/fd/|f(get|set)xattr\([0-9]+,' strace_chcon_recursive.log; then
229 cat strace_chcon_recursive.log
230 fail_immediately "chcon -R never reached an fd-anchored SELinux xattr op (relabel aborted before get/set?, issue #11402)"
231 fi
232 echo "✓ chcon -R anchors relabel to the traversal dirfd (O_NOFOLLOW, fd-based access)"
233 rm -rf chcon_tree
234 fi
235
236 # Test du - should use openat, newfstatat
237 if have_util du; then
238 cp -r test_dir test_du
239 check_utility "du" "openat,newfstatat,stat,lstat" "openat" "-a test_du" "directory_usage"
240 fi
241
242 # Test mv - should use openat, renameat for directory moves
243 if have_util mv; then
244 mkdir -p test_mv_src/sub
245 echo "test" > test_mv_src/file.txt
246 echo "test" > test_mv_src/sub/file2.txt
247 check_utility "mv" "openat,renameat,newfstatat,rename" "openat" "test_mv_src test_mv_dst" "move_directory"
248 fi
249
250 # cp invariant checks. Both #10011 (restrictive 0600 destination mode) and
251 # #10017 (O_NOFOLLOW on the -P source) need to hold; verify each on its own
252 # strace.
253 if have_util cp; then
254 cp_cmd=$(util_cmd cp)
255
256 # #10011: destination created with mode 0600 so other users cannot open
257 # the file through its umask-derived initial mode before cp narrows it.
258 # rustix may emit either open(2) or openat(2); accept both.
259 echo "cp_perm_test" > test_cp_src_perm
260 rm -f test_cp_dst_perm
261 strace -f -e trace=open,openat,openat2 -o strace_cp_dest_perm.log \
262 $cp_cmd test_cp_src_perm test_cp_dst_perm 2>/dev/null || true
263 if ! grep -qE 'open(at)?2?\([^)]*"test_cp_dst_perm".*O_CREAT.*, 0600\)' strace_cp_dest_perm.log; then
264 cat strace_cp_dest_perm.log
265 fail_immediately "cp must create the destination with mode 0600 (issue #10011)"
266 fi
267 echo "✓ cp creates destination with restrictive 0600 mode"
268 rm -f test_cp_src_perm test_cp_dst_perm
269
270 # #10017: -P opens source with O_NOFOLLOW so a path swap to a symlink
271 # between the lstat check and the open cannot redirect the copy.
272 echo "cp_nofollow_test" > test_cp_src
273 strace -f -e trace=open,openat,openat2 -o strace_cp_nofollow.log \
274 $cp_cmd -P test_cp_src test_cp_dst 2>/dev/null || true
275 if ! grep -qE 'open(at)?2?\([^)]*"test_cp_src".*O_NOFOLLOW' strace_cp_nofollow.log; then
276 cat strace_cp_nofollow.log
277 fail_immediately "cp -P must open the source with O_NOFOLLOW (issue #10017)"
278 fi
279 echo "✓ cp -P opens source with O_NOFOLLOW"
280 rm -f test_cp_src test_cp_dst
281 fi
282
283 # split must harden its output open against TOCTOU target swaps (issue #11401 /
284 # CVE-2026-35374). The old code opened the output by path with O_TRUNC, so an
285 # attacker could swap the just-validated output for a symlink and have split
286 # truncate a different file. The fix creates outputs atomically with
287 # O_CREAT|O_EXCL and only ever truncates via ftruncate after an fd-based check
288 # that the opened output is not the input -- so a split that would overwrite its
289 # own input is refused.
290 if have_util split; then
291 split_cmd=$(util_cmd split)
292
293 printf '0123456789abcdef' > split_input
294 strace -f -e trace=openat -o strace_split_output_open.log \
295 $split_cmd -b 4 split_input split_out_ 2>/dev/null || true
296
297 if ! grep -qE 'openat\(AT_FDCWD, "split_out_[a-z]+", [^)]*O_CREAT[^)]*O_EXCL' strace_split_output_open.log; then
298 cat strace_split_output_open.log
299 fail_immediately "split must create output files with O_CREAT|O_EXCL (issue #11401)"
300 fi
301 if grep -qE 'openat\(AT_FDCWD, "split_out_[a-z]+", [^)]*O_TRUNC' strace_split_output_open.log; then
302 cat strace_split_output_open.log
303 fail_immediately "split must not open output files with a path-based O_TRUNC (TOCTOU truncation risk, issue #11401)"
304 fi
305 echo "✓ split creates outputs with O_CREAT|O_EXCL and no path-based O_TRUNC"
306 rm -f split_input split_out_*
307
308 # A split whose output already resolves (via a symlink) to the input must be
309 # refused, leaving the input untouched.
310 printf 'split_victim_payload' > split_victim
311 ln -s split_victim split_swap_aa
312 if $split_cmd -b 4 split_victim split_swap_ 2>/dev/null; then
313 fail_immediately "split must refuse when the output would overwrite the input (issue #11401)"
314 fi
315 if [ "$(cat split_victim)" != "split_victim_payload" ]; then
316 fail_immediately "split truncated its own input through a swapped output symlink (issue #11401)"
317 fi
318 echo "✓ split refuses to overwrite its input via a swapped output symlink"
319 rm -f split_victim split_swap_*
320 fi
321
322 # mv cross-device (EXDEV) must use fd-based *xattr ops (issue #10014).
323 if have_util mv && [ -d /dev/shm ]; then
324 # Need different filesystems for the EXDEV fallback to fire.
325 temp_fs_id=$(stat -f -c %i "$TEMP_DIR" 2>/dev/null || echo "")
326 shm_fs_id=$(stat -f -c %i /dev/shm 2>/dev/null || echo "")
327 if [ -z "$temp_fs_id" ] || [ -z "$shm_fs_id" ] || [ "$temp_fs_id" = "$shm_fs_id" ]; then
328 echo "WARN: mv cross-device xattr check: TMPDIR and /dev/shm are on the same filesystem; skipped"
329 else
330 shm_probe=$(mktemp -p /dev/shm mv_xattr_probe.XXXXXX)
331 if setfattr -n user.probe -v ok "$shm_probe" 2>/dev/null; then
332 rm -f "$shm_probe"
333 cross_src=$(mktemp -p "$TEMP_DIR" cross_src.XXXXXX)
334 cross_dst=$(mktemp -u -p /dev/shm cross_dst.XXXXXX)
335 echo "cross-device payload" > "$cross_src"
336 if setfattr -n user.tag -v pinned "$cross_src" 2>/dev/null; then
337 mv_cmd=$(util_cmd mv)
338 strace -f -e trace='%file,fgetxattr,fsetxattr,flistxattr,getxattr,setxattr,listxattr' \
339 -o strace_mv_xattr.log \
340 $mv_cmd "$cross_src" "$cross_dst" 2>/dev/null || true
341
342 # Path-based xattr calls on src/dst basenames = the TOCTOU pattern.
343 cross_src_base=$(basename "$cross_src")
344 cross_dst_base=$(basename "$cross_dst")
345 if grep -qE "(listxattr|getxattr|setxattr)\([^,]*($cross_src_base|$cross_dst_base)" strace_mv_xattr.log; then
346 cat strace_mv_xattr.log
347 fail_immediately "mv cross-device must use fd-based xattr ops (issue #10014)"
348 fi
349 if grep -qE 'flistxattr|fgetxattr|fsetxattr' strace_mv_xattr.log; then
350 echo "OK: mv cross-device uses fd-based xattr syscalls"
351 else
352 echo "WARN: mv cross-device xattr check: no xattr syscalls observed (xattr may have been filtered - check filesystem support)"
353 fi
354 rm -f "$cross_dst"
355 else
356 echo "WARN: mv cross-device xattr check: TMPDIR does not support user xattrs; skipped"
357 fi
358 else
359 rm -f "$shm_probe"
360 echo "WARN: mv cross-device xattr check: /dev/shm does not support user xattrs; skipped"
361 fi
362 fi
363 fi
364
365 # mv cross-device must open the destination with O_NOFOLLOW (issue #10015).
366 # The EXDEV fallback unlinks the dest then opens with O_CREAT|O_TRUNC; an
367 # attacker racing in a planted symlink would otherwise let the copy write
368 # through to the symlink's target. rustix may emit either open(2) or
369 # openat(2) depending on the path, so the check accepts both.
370 if have_util mv && [ -d /dev/shm ]; then
371 temp_fs_id=$(stat -f -c %i "$TEMP_DIR" 2>/dev/null || echo "")
372 shm_fs_id=$(stat -f -c %i /dev/shm 2>/dev/null || echo "")
373 if [ -z "$temp_fs_id" ] || [ -z "$shm_fs_id" ] || [ "$temp_fs_id" = "$shm_fs_id" ]; then
374 echo "WARN: mv cross-device O_NOFOLLOW check: TMPDIR and /dev/shm are on the same filesystem; skipped"
375 else
376 nofollow_src=$(mktemp -p "$TEMP_DIR" mv_nofollow_src.XXXXXX)
377 nofollow_dst=$(mktemp -u -p /dev/shm mv_nofollow_dst.XXXXXX)
378 echo "payload" > "$nofollow_src"
379 echo "existing" > "$nofollow_dst"
380
381 mv_cmd=$(util_cmd mv)
382 strace -f -e trace=open,openat,openat2 -o strace_mv_nofollow.log \
383 $mv_cmd -f "$nofollow_src" "$nofollow_dst" 2>/dev/null || true
384
385 nofollow_dst_base=$(basename "$nofollow_dst")
386 # Lines that open the dest with O_CREAT — the dest may appear as a
387 # full path (open) or basename (openat). Every such open must carry
388 # O_NOFOLLOW or the planted-symlink writethrough is possible.
389 dest_creates=$(grep -E "open(at)?2?\(.*\"[^\"]*${nofollow_dst_base}\".*O_CREAT" strace_mv_nofollow.log || true)
390 if [ -z "$dest_creates" ]; then
391 cat strace_mv_nofollow.log
392 fail_immediately "mv cross-device did not open dest with O_CREAT — strace check broken (issue #10015)"
393 fi
394 if echo "$dest_creates" | grep -vq "O_NOFOLLOW"; then
395 cat strace_mv_nofollow.log
396 fail_immediately "mv cross-device must open dest with O_NOFOLLOW (issue #10015)"
397 fi
398 echo "OK: mv cross-device opens dest with O_NOFOLLOW"
399 rm -f "$nofollow_dst"
400 fi
401 fi
402
403 # mv cross-device symlink replacement must use *at syscalls against a
404 # pinned parent fd (matches GNU's force_symlinkat) so a concurrent rename
405 # of the parent directory cannot redirect the temp-and-rename dance, and
406 # the temp name must come from /dev/urandom rather than a guessable
407 # pid+nanos pattern.
408 if have_util mv && [ -d /dev/shm ]; then
409 temp_fs_id=$(stat -f -c %i "$TEMP_DIR" 2>/dev/null || echo "")
410 shm_fs_id=$(stat -f -c %i /dev/shm 2>/dev/null || echo "")
411 if [ -z "$temp_fs_id" ] || [ -z "$shm_fs_id" ] || [ "$temp_fs_id" = "$shm_fs_id" ]; then
412 echo "WARN: mv symlink-replace check: TMPDIR and /dev/shm are on the same filesystem; skipped"
413 else
414 sym_src=$(mktemp -u -p "$TEMP_DIR" sym_src.XXXXXX)
415 sym_dst=$(mktemp -u -p /dev/shm sym_dst.XXXXXX)
416 ln -s /nowhere "$sym_src"
417 # Pre-existing dest forces the EEXIST branch into create_symlink_replace.
418 ln -s /elsewhere "$sym_dst"
419
420 mv_cmd=$(util_cmd mv)
421 strace -f -e trace=openat,symlink,symlinkat,rename,renameat,renameat2,unlink,unlinkat,read \
422 -o strace_mv_symlink_replace.log \
423 $mv_cmd "$sym_src" "$sym_dst" 2>/dev/null || true
424
425 sym_dst_base=$(basename "$sym_dst")
426
427 # Temp-and-rename must happen against a real parent dirfd, not AT_FDCWD.
428 if ! grep -qE 'symlinkat\("[^"]*", [0-9]+,' strace_mv_symlink_replace.log; then
429 cat strace_mv_symlink_replace.log
430 fail_immediately "mv symlink replace must use symlinkat with a parent dirfd (issue #10010 follow-up)"
431 fi
432 if ! grep -qE "renameat2?\([0-9]+, \"[^\"]+\", [0-9]+, \"$sym_dst_base\"" strace_mv_symlink_replace.log; then
433 cat strace_mv_symlink_replace.log
434 fail_immediately "mv symlink replace must use renameat with a parent dirfd to commit the dest (issue #10010 follow-up)"
435 fi
436
437 # Random temp source must be /dev/urandom (rejecting the prior pid+nanos scheme).
438 if ! grep -q 'openat(AT_FDCWD, "/dev/urandom"' strace_mv_symlink_replace.log; then
439 cat strace_mv_symlink_replace.log
440 fail_immediately "mv symlink replace must seed the temp name from /dev/urandom"
441 fi
442
443 echo "OK: mv symlink replace uses symlinkat+renameat with parent dirfd and unguessable temp name"
444 rm -f "$sym_dst"
445 fi
446 fi
447
448 echo ""
449 echo "✓ Basic safe traversal verification completed"
450 echo ""
451 echo "=== Additional Safety Checks ==="
452
453 # Check for dangerous patterns across all logs
454 echo "Checking for dangerous path resolution patterns..."
455
456 # Check that we're not doing excessive path resolutions (sign of TOCTOU vulnerability)
457 echo "Checking path resolution frequency..."
458 for log in strace_*.log; do
459 if [ -f "$log" ]; then
460 # grep -c already prints 0 when nothing matches, so a `|| echo 0`
461 # here would make the variable "0\n0" and break the comparison.
462 path_resolutions=$(grep -c "test_" "$log" 2>/dev/null || true)
463 if [ "${path_resolutions:-0}" -gt 20 ]; then
464 echo "⚠ $log: High path resolution count ($path_resolutions) - potential TOCTOU risk"
465 fi
466 fi
467 done
468
469 echo ""
470 echo "=== Summary ==="
471 echo "All utilities are using safe traversal correctly!"