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 _begin_fstest auto quick encrypt
41 # Override the default cleanup function.
54 _require_scratch_encryption -v 2
55 _require_renameat2 noreplace
57 _scratch_mkfs_encrypted &>> $seqres.full
60 runtime=$((5 * TIME_FACTOR))
63 echo -e "\n# Creating encrypted directory containing files"
65 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
66 _set_encpolicy $dir $TEST_KEY_IDENTIFIER
71 # This is the filename which we'll try to duplicate.
72 inode=$(stat -c %i $dir/100)
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.
80 echo -e "\n# Starting duplicate filename creator process"
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
91 if mknod $dir/100 c 5 5 &> /dev/null; then
92 touch $tmp.mknod_succeeded
94 if ln -s target $dir/100 &> /dev/null; then
95 touch $tmp.symlink_succeeded
97 if ln $dir/50 $dir/100 &> /dev/null; then
98 touch $tmp.link_succeeded
100 if $here/src/renameat2 -n $dir/50 $dir/100 &> /dev/null; then
101 touch $tmp.rename_noreplace_succeeded
106 echo -e "\n# Starting add/remove enckey process"
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
116 echo -e "\n# Running for a few seconds..."
118 echo -e "\n# Stopping subprocesses"
122 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
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.
127 echo -e "\n# Checking for duplicate filenames via readdir"
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!"
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!"
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"
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"