fsx/fsstress: round blocksize properly
[xfstests-dev.git] / tests / generic / 621
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2020 Google LLC
4 #
5 # FS QA Test No. 621
6 #
7 # Test for a race condition where a duplicate filename could be created in an
8 # encrypted directory while the directory's encryption key was being added
9 # concurrently.  This is a regression test for the following kernel commits:
10 #
11 #       968dd6d0c6d6 ("fscrypt: fix race allowing rename() and link() of ciphertext dentries")
12 #       75d18cd1868c ("ext4: prevent creating duplicate encrypted filenames")
13 #       bfc2b7e85189 ("f2fs: prevent creating duplicate encrypted filenames")
14 #       76786a0f0834 ("ubifs: prevent creating duplicate encrypted filenames")
15 #
16 # The first commit fixed the bug for the rename() and link() syscalls.
17 # The others fixed the bug for the other syscalls that create new filenames.
18 #
19 # Note, the bug wasn't actually reproducible on f2fs.
20 #
21 # The race condition worked as follows:
22 #    1. Initial state: an encrypted directory "dir" contains a file "foo",
23 #       but the directory's key hasn't been added yet so 'ls dir' shows an
24 #       encoded no-key name rather than "foo".
25 #    2. The key is added concurrently with mkdir("dir/foo") or another syscall
26 #       that creates a new filename and should fail if it already exists.
27 #        a. The syscall looks up "dir/foo", creating a negative no-key dentry
28 #           for "foo" since the directory's key hasn't been added yet.
29 #        b. The directory's key is added.
30 #        c. The syscall does the actual fs-level operation to create the
31 #           filename.  With the bug, the filesystem failed to detect that the
32 #           dentry was created without the key, potentially causing the
33 #           operation to unexpectedly succeed and add a duplicate filename.
34 #
35 # To test this, we try to reproduce the above race.  Afterwards we check for
36 # duplicate filenames, plus a few other things.
37 #
38 seq=`basename $0`
39 seqres=$RESULT_DIR/$seq
40 echo "QA output created by $seq"
41
42 here=`pwd`
43 tmp=/tmp/$$
44 status=1        # failure is the default!
45 trap "_cleanup; exit \$status" 0 1 2 3 15
46
47 _cleanup()
48 {
49         touch $tmp.done
50         wait
51         rm -f $tmp.*
52 }
53
54 . ./common/rc
55 . ./common/filter
56 . ./common/encrypt
57 . ./common/renameat2
58
59 rm -f $seqres.full
60
61 _supported_fs generic
62 _require_scratch_encryption -v 2
63 _require_renameat2 noreplace
64
65 _scratch_mkfs_encrypted &>> $seqres.full
66 _scratch_mount
67
68 runtime=$((5 * TIME_FACTOR))
69 dir=$SCRATCH_MNT/dir
70
71 echo -e "\n# Creating encrypted directory containing files"
72 mkdir $dir
73 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
74 _set_encpolicy $dir $TEST_KEY_IDENTIFIER
75 for i in {1..100}; do
76         touch $dir/$i
77 done
78
79 # This is the filename which we'll try to duplicate.
80 inode=$(stat -c %i $dir/100)
81
82 # ext4 checks for duplicate dentries when inserting one, which can hide the bug
83 # this test is testing for.  However, ext4 stops checking for duplicates once it
84 # finds space for the new dentry.  Therefore, we can circumvent ext4's duplicate
85 # checking by creating space at the beginning of the directory block.
86 rm $dir/1
87
88 echo -e "\n# Starting duplicate filename creator process"
89 (
90         # Repeatedly try to create the filename $dir/100 (which already exists)
91         # using syscalls that should fail if the file already exists: mkdir(),
92         # mknod(), symlink(), link(), and renameat2(RENAME_NOREPLACE).  This
93         # hopefully detects any one of them having the bug.  TODO: we should
94         # also try open(O_EXCL|O_CREAT), but it needs a command-line tool.
95         while [ ! -e $tmp.done ]; do
96                 if mkdir $dir/100 &> /dev/null; then
97                         touch $tmp.mkdir_succeeded
98                 fi
99                 if mknod $dir/100 c 5 5 &> /dev/null; then
100                         touch $tmp.mknod_succeeded
101                 fi
102                 if ln -s target $dir/100 &> /dev/null; then
103                         touch $tmp.symlink_succeeded
104                 fi
105                 if ln $dir/50 $dir/100 &> /dev/null; then
106                         touch $tmp.link_succeeded
107                 fi
108                 if $here/src/renameat2 -n $dir/50 $dir/100 &> /dev/null; then
109                         touch $tmp.rename_noreplace_succeeded
110                 fi
111         done
112 ) &
113
114 echo -e "\n# Starting add/remove enckey process"
115 (
116         # Repeatedly add and remove the encryption key for $dir.  The actual
117         # race this test is trying to reproduce occurs when adding the key.
118         while [ ! -e $tmp.done ]; do
119                 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
120                 _rm_enckey $SCRATCH_MNT $TEST_KEY_IDENTIFIER > /dev/null
121         done
122 ) &
123
124 echo -e "\n# Running for a few seconds..."
125 sleep $runtime
126 echo -e "\n# Stopping subprocesses"
127 touch $tmp.done
128 wait
129
130 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
131
132 # Check for failure in several different ways, since different ways work on
133 # different filesystems.  E.g. ext4 shows duplicate filenames but ubifs doesn't.
134
135 echo -e "\n# Checking for duplicate filenames via readdir"
136 ls $dir | grep 100
137
138 echo -e "\n# Checking for unexpected change in inode number"
139 new_inode=$(stat -c %i $dir/100)
140 if [ $new_inode != $inode ]; then
141         echo "Dentry changed inode number $inode => $new_inode!"
142 fi
143
144 echo -e "\n# Checking for operations that unexpectedly succeeded on an existing filename"
145 for op in "mkdir" "mknod" "symlink" "link" "rename_noreplace"; do
146         if [ -e $tmp.${op}_succeeded ]; then
147                 echo "$op operation(s) on existing filename unexpectedly succeeded!"
148         fi
149 done
150
151 # Also check that the fsck program can't find any duplicate filenames.
152 # For ext4, override _check_scratch_fs() so that we can specify -D (optimize
153 # directories); otherwise e2fsck doesn't check for duplicate filenames.
154 echo -e "\n# Checking for duplicate filenames via fsck"
155 _scratch_unmount
156 if [ "$FSTYP" = ext4 ]; then
157         if ! e2fsck -f -y -D $SCRATCH_DEV &>> $seqres.full; then
158                 _log_err "filesystem on $SCRATCH_DEV is inconsistent"
159         fi
160 else
161         _check_scratch_fs
162 fi
163
164 # success, all done
165 status=0
166 exit