fstests: move test group info to test files
[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 . ./common/preamble
39 _begin_fstest auto quick encrypt
40
41 # Override the default cleanup function.
42 _cleanup()
43 {
44         touch $tmp.done
45         wait
46         rm -f $tmp.*
47 }
48
49 . ./common/filter
50 . ./common/encrypt
51 . ./common/renameat2
52
53 _supported_fs generic
54 _require_scratch_encryption -v 2
55 _require_renameat2 noreplace
56
57 _scratch_mkfs_encrypted &>> $seqres.full
58 _scratch_mount
59
60 runtime=$((5 * TIME_FACTOR))
61 dir=$SCRATCH_MNT/dir
62
63 echo -e "\n# Creating encrypted directory containing files"
64 mkdir $dir
65 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
66 _set_encpolicy $dir $TEST_KEY_IDENTIFIER
67 for i in {1..100}; do
68         touch $dir/$i
69 done
70
71 # This is the filename which we'll try to duplicate.
72 inode=$(stat -c %i $dir/100)
73
74 # ext4 checks for duplicate dentries when inserting one, which can hide the bug
75 # this test is testing for.  However, ext4 stops checking for duplicates once it
76 # finds space for the new dentry.  Therefore, we can circumvent ext4's duplicate
77 # checking by creating space at the beginning of the directory block.
78 rm $dir/1
79
80 echo -e "\n# Starting duplicate filename creator process"
81 (
82         # Repeatedly try to create the filename $dir/100 (which already exists)
83         # using syscalls that should fail if the file already exists: mkdir(),
84         # mknod(), symlink(), link(), and renameat2(RENAME_NOREPLACE).  This
85         # hopefully detects any one of them having the bug.  TODO: we should
86         # also try open(O_EXCL|O_CREAT), but it needs a command-line tool.
87         while [ ! -e $tmp.done ]; do
88                 if mkdir $dir/100 &> /dev/null; then
89                         touch $tmp.mkdir_succeeded
90                 fi
91                 if mknod $dir/100 c 5 5 &> /dev/null; then
92                         touch $tmp.mknod_succeeded
93                 fi
94                 if ln -s target $dir/100 &> /dev/null; then
95                         touch $tmp.symlink_succeeded
96                 fi
97                 if ln $dir/50 $dir/100 &> /dev/null; then
98                         touch $tmp.link_succeeded
99                 fi
100                 if $here/src/renameat2 -n $dir/50 $dir/100 &> /dev/null; then
101                         touch $tmp.rename_noreplace_succeeded
102                 fi
103         done
104 ) &
105
106 echo -e "\n# Starting add/remove enckey process"
107 (
108         # Repeatedly add and remove the encryption key for $dir.  The actual
109         # race this test is trying to reproduce occurs when adding the key.
110         while [ ! -e $tmp.done ]; do
111                 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
112                 _rm_enckey $SCRATCH_MNT $TEST_KEY_IDENTIFIER > /dev/null
113         done
114 ) &
115
116 echo -e "\n# Running for a few seconds..."
117 sleep $runtime
118 echo -e "\n# Stopping subprocesses"
119 touch $tmp.done
120 wait
121
122 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
123
124 # Check for failure in several different ways, since different ways work on
125 # different filesystems.  E.g. ext4 shows duplicate filenames but ubifs doesn't.
126
127 echo -e "\n# Checking for duplicate filenames via readdir"
128 ls $dir | grep 100
129
130 echo -e "\n# Checking for unexpected change in inode number"
131 new_inode=$(stat -c %i $dir/100)
132 if [ $new_inode != $inode ]; then
133         echo "Dentry changed inode number $inode => $new_inode!"
134 fi
135
136 echo -e "\n# Checking for operations that unexpectedly succeeded on an existing filename"
137 for op in "mkdir" "mknod" "symlink" "link" "rename_noreplace"; do
138         if [ -e $tmp.${op}_succeeded ]; then
139                 echo "$op operation(s) on existing filename unexpectedly succeeded!"
140         fi
141 done
142
143 # Also check that the fsck program can't find any duplicate filenames.
144 # For ext4, override _check_scratch_fs() so that we can specify -D (optimize
145 # directories); otherwise e2fsck doesn't check for duplicate filenames.
146 echo -e "\n# Checking for duplicate filenames via fsck"
147 _scratch_unmount
148 if [ "$FSTYP" = ext4 ]; then
149         if ! e2fsck -f -y -D $SCRATCH_DEV &>> $seqres.full; then
150                 _log_err "filesystem on $SCRATCH_DEV is inconsistent"
151         fi
152 else
153         _check_scratch_fs
154 fi
155
156 # success, all done
157 status=0
158 exit