2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2020 Google LLC
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:
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")
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.
19 # Note, the bug wasn't actually reproducible on f2fs.
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.
35 # To test this, we try to reproduce the above race. Afterwards we check for
36 # duplicate filenames, plus a few other things.
39 seqres=$RESULT_DIR/$seq
40 echo "QA output created by $seq"
44 status=1 # failure is the default!
45 trap "_cleanup; exit \$status" 0 1 2 3 15
62 _require_scratch_encryption -v 2
63 _require_renameat2 noreplace
65 _scratch_mkfs_encrypted &>> $seqres.full
68 runtime=$((5 * TIME_FACTOR))
71 echo -e "\n# Creating encrypted directory containing files"
73 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
74 _set_encpolicy $dir $TEST_KEY_IDENTIFIER
79 # This is the filename which we'll try to duplicate.
80 inode=$(stat -c %i $dir/100)
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.
88 echo -e "\n# Starting duplicate filename creator process"
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
99 if mknod $dir/100 c 5 5 &> /dev/null; then
100 touch $tmp.mknod_succeeded
102 if ln -s target $dir/100 &> /dev/null; then
103 touch $tmp.symlink_succeeded
105 if ln $dir/50 $dir/100 &> /dev/null; then
106 touch $tmp.link_succeeded
108 if $here/src/renameat2 -n $dir/50 $dir/100 &> /dev/null; then
109 touch $tmp.rename_noreplace_succeeded
114 echo -e "\n# Starting add/remove enckey process"
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
124 echo -e "\n# Running for a few seconds..."
126 echo -e "\n# Stopping subprocesses"
130 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
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.
135 echo -e "\n# Checking for duplicate filenames via readdir"
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!"
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!"
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"
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"