2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Google, Inc. All Rights Reserved.
7 # Test that without the encryption key for a directory, long filenames are
8 # presented in a way which avoids collisions, even though they are abbreviated
9 # in order to support names up to NAME_MAX bytes.
11 # Regression test for:
12 # 6332cd32c829 ("f2fs: check entire encrypted bigname when finding a dentry")
13 # 6b06cdee81d6 ("fscrypt: avoid collisions when presenting long encrypted filenames")
15 # Even with these two fixes it's still possible to create intentional
16 # collisions. For now this test covers "accidental" collisions only.
19 seqres=$RESULT_DIR/$seq
20 echo "QA output created by $seq"
24 status=1 # failure is the default!
25 trap "_cleanup; exit \$status" 0 1 2 3 15
33 # get standard environment, filters and checks
38 # remove previous $seqres.full before test
41 # real QA test starts here
44 _require_scratch_encryption
45 _require_command "$KEYCTL_PROG" keyctl
47 # set up an encrypted directory
50 _scratch_mkfs_encrypted &>> $seqres.full
52 mkdir $SCRATCH_MNT/edir
53 keydesc=$(_generate_session_encryption_key)
54 # -f 0x2: zero-pad to 16-byte boundary (i.e. encryption block boundary)
55 _set_encpolicy $SCRATCH_MNT/edir $keydesc -f 0x2
57 # Create files with long names (> 32 bytes, long enough to trigger the use of
58 # "digested" names) in the encrypted directory.
60 # Use 100,000 files so that we have a good chance of detecting buggy filesystems
61 # that solely use a 32-bit hash to distinguish files, which f2fs was doing.
63 # Furthermore, make the filenames differ only in the last 16-byte encryption
64 # block. This reproduces the bug where it was not accounted for that ciphertext
65 # stealing (CTS) causes the last two blocks to appear "flipped".
66 seq -f "$SCRATCH_MNT/edir/abcdefghijklmnopqrstuvwxyz012345%.0f" 100000 | xargs touch
67 find $SCRATCH_MNT/edir/ -type f | xargs stat -c %i | sort | uniq | wc -l
69 _unlink_session_encryption_key $keydesc
72 # Verify that every file has a unique inode number and can be removed without
73 # error. With the bug(s), some filenames incorrectly pointed to the same inode,
74 # and ext4 reported a "Structure needs cleaning" error when removing files.
75 find $SCRATCH_MNT/edir/ -type f | xargs stat -c %i | sort | uniq | wc -l
76 rm -rf $SCRATCH_MNT/edir |& head -n 10
77 stat $SCRATCH_MNT/edir |& _filter_scratch