Debian Codegraph @

util/check-toctou.sh

raw
1 #!/bin/bash
2 #
3 # spell-checker:ignore mknod mknodat fchmod fchmodat mkfifoat strace newfstatat statx lstat CREAT FDCWD headtest
4 #
5 # TOCTOU (time-of-check / time-of-use) verification.
6 #
7 # These strace-based checks assert that utilities do not split a
8 # security-sensitive operation across two path-based syscalls (e.g. a
9 # stat() before open() that an attacker can race). The companion
10 # script check-safe-traversal.sh covers a different concern: that
11 # recursive walkers use the openat() family rather than re-resolving
12 # multi-component paths during traversal.
13 #
14
15 set -e
16
17 echo "=== TOCTOU Verification ==="
18
19 # shellcheck disable=SC2034 # read by check-common.sh once sourced
20 CHECK_UTILS="mkfifo touch head"
21 . "$(dirname "${BASH_SOURCE[0]}")/check-common.sh"
22
23 cd "$TEMP_DIR"
24
25 # mkfifo must not call a path-based chmod after creating the FIFO: the
26 # second syscall would re-resolve the path and could be redirected by an
27 # attacker who swaps the FIFO for a symlink in between (issue #10020).
28 # After the fix, the kernel applies the requested mode atomically via
29 # mkfifo with cleared umask.
30 if mkfifo_cmd=$(util_cmd mkfifo); then
31 rm -f test_fifo
32 # mkfifo(3)/mkfifoat(3) are libc wrappers; the underlying syscall
33 # is mknodat (or mknod on older kernels). Trace those plus any
34 # chmod variants.
35 strace -f -e trace=mknod,mknodat,chmod,fchmod,fchmodat,fchmodat2 \
36 -o strace_mkfifo.log \
37 $mkfifo_cmd -m 666 test_fifo 2>/dev/null || true
38
39 if [ ! -s strace_mkfifo.log ]; then
40 fail_immediately "strace produced no output for mkfifo"
41 fi
42 if ! grep -qE 'mknodat?\(' strace_mkfifo.log; then
43 cat strace_mkfifo.log
44 fail_immediately "mkfifo must call mknod/mknodat to create the FIFO"
45 fi
46
47 if grep -qE '\bchmod\([^,]*"test_fifo"' strace_mkfifo.log; then
48 cat strace_mkfifo.log
49 fail_immediately "mkfifo must not call path-based chmod after creation (issue #10020)"
50 fi
51 if grep -qE 'fchmodat2?\([^,]+, "test_fifo"' strace_mkfifo.log; then
52 cat strace_mkfifo.log
53 fail_immediately "mkfifo must not call fchmodat after creation (issue #10020)"
54 fi
55 echo "✓ mkfifo does not chmod after creation"
56 rm -f test_fifo
57 fi
58
59 # Test touch - creating a file must use O_CREAT but never O_TRUNC, so that a
60 # symlink planted in the metadata-check/open race window (#10019) is not
61 # truncated. This observes the flags directly, which integration tests cannot.
62 if touch_cmd=$(util_cmd touch); then
63 echo ""
64 echo "Testing touch (create_no_truncate)..."
65 strace -f -e trace=openat -o strace_touch_create.log $touch_cmd test_touch_new 2>/dev/null || true
66 cat strace_touch_create.log
67 if ! grep -q 'openat(.*test_touch_new.*O_CREAT' strace_touch_create.log; then
68 fail_immediately "touch did not create test_touch_new via openat(O_CREAT)"
69 fi
70 if grep 'test_touch_new' strace_touch_create.log | grep -q 'O_TRUNC'; then
71 fail_immediately "touch opened the target with O_TRUNC - vulnerable to truncating a symlink target (#10019)"
72 fi
73 echo "✓ touch creates with O_CREAT and without O_TRUNC"
74 rm -f test_touch_new
75 fi
76
77 # Test head - the is-a-directory check must derive from the already-open
78 # descriptor (fstat/statx on the fd), not from a separate path-based stat
79 # performed before the open. A path stat followed by an open is a TOCTOU
80 # window (#11972): the object named by the path can be swapped in between.
81 if head_cmd=$(util_cmd head); then
82 echo ""
83 echo "Testing head (fstat_after_open)..."
84 echo "headtest" > test_head_file.txt
85 strace -f -e trace=openat,fstat,newfstatat,statx,stat,lstat \
86 -o strace_head_metadata.log $head_cmd -c 4 test_head_file.txt 2>/dev/null || true
87 cat strace_head_metadata.log
88 if ! grep -q 'openat(AT_FDCWD, "test_head_file.txt"' strace_head_metadata.log; then
89 fail_immediately "head did not open test_head_file.txt via openat"
90 fi
91 # The filename should appear only in the openat; any stat-family call naming
92 # the path means head stat'd it before opening - the TOCTOU window.
93 if grep '"test_head_file.txt"' strace_head_metadata.log | grep -qv 'openat('; then
94 fail_immediately "head stat'd the path before opening it - TOCTOU window (#11972); metadata must come from the open descriptor"
95 fi
96 echo "✓ head reads metadata from the open descriptor, not a path stat"
97 rm -f test_head_file.txt
98 fi
99
100 echo ""
101 echo "✓ TOCTOU verification completed"