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
43 _require_scratch_encryption
44 _require_command "$KEYCTL_PROG" keyctl
46 # set up an encrypted directory
49 _scratch_mkfs_encrypted &>> $seqres.full
51 mkdir $SCRATCH_MNT/edir
52 keydesc=$(_generate_session_encryption_key)
53 # -f 0x2: zero-pad to 16-byte boundary (i.e. encryption block boundary)
54 _set_encpolicy $SCRATCH_MNT/edir $keydesc -f 0x2
56 # Create files with long names (> 32 bytes, long enough to trigger the use of
57 # "digested" names) in the encrypted directory.
59 # Use 100,000 files so that we have a good chance of detecting buggy filesystems
60 # that solely use a 32-bit hash to distinguish files, which f2fs was doing.
62 # Furthermore, make the filenames differ only in the last 16-byte encryption
63 # block. This reproduces the bug where it was not accounted for that ciphertext
64 # stealing (CTS) causes the last two blocks to appear "flipped".
65 seq -f "$SCRATCH_MNT/edir/abcdefghijklmnopqrstuvwxyz012345%.0f" 100000 | xargs touch
66 find $SCRATCH_MNT/edir/ -type f | xargs stat -c %i | sort | uniq | wc -l
68 _unlink_session_encryption_key $keydesc
71 # Verify that every file has a unique inode number and can be removed without
72 # error. With the bug(s), some filenames incorrectly pointed to the same inode,
73 # and ext4 reported a "Structure needs cleaning" error when removing files.
74 find $SCRATCH_MNT/edir/ -type f | xargs stat -c %i | sort | uniq | wc -l
75 rm -rf $SCRATCH_MNT/edir |& head -n 10
76 stat $SCRATCH_MNT/edir |& _filter_stat |& _filter_scratch