2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Google, Inc. All Rights Reserved.
7 # Test that no-key dentries are revalidated after adding a key. Regression test
9 # 28b4c263961c ("ext4 crypto: revalidate dentry after adding or removing the key")
11 # Furthermore, test that no-key dentries are *not* revalidated after "revoking"
12 # a key. This used to be done, but it was broken and was removed by:
13 # 1b53cf9815bb ("fscrypt: remove broken support for detecting keyring key revocation")
15 # Also test for a race condition bug in 28b4c263961c, fixed by:
16 # 03a8bb0e53d9 ("ext4/fscrypto: avoid RCU lookup in d_revalidate")
18 # Note: the following fix for another race in 28b4c263961c should be applied as
19 # well, though we don't test for it because it's very difficult to reproduce:
20 # 3d43bcfef5f0 ("ext4 crypto: use dget_parent() in ext4_d_revalidate()")
23 _begin_fstest auto encrypt
25 # Import common functions.
29 # real QA test starts here
31 _require_scratch_encryption
32 _require_command "$KEYCTL_PROG" keyctl
33 _require_test_program "t_encrypted_d_revalidate"
35 # Set up an encrypted directory
36 _scratch_mkfs_encrypted &>> $seqres.full
39 keydesc=$(_generate_key_descriptor)
40 raw_key=$(_generate_raw_encryption_key)
41 mkdir $SCRATCH_MNT/edir
42 _add_session_encryption_key $keydesc $raw_key
43 _set_encpolicy $SCRATCH_MNT/edir $keydesc
45 # Create two files in the directory: one whose name is valid in the base64
46 # format used in no-key filenames, and one whose name is not. The exact
47 # filenames *should* be irrelevant, but due to yet another bug, ->lookup() in an
48 # encrypted directory without the key returned ERR_PTR(-ENOENT) rather than NULL
49 # if the name was not a valid no-key name, causing a negative dentry to not be
50 # created. For the purpose of this test, we want at least one negative dentry
51 # to be created, so just create both types of name.
52 echo contents_@@@ > $SCRATCH_MNT/edir/@@@ # not valid base64
53 echo contents_abcd > $SCRATCH_MNT/edir/abcd # valid base64
55 filter_nokey_filenames()
57 _filter_scratch | sed 's|edir/[a-zA-Z0-9+,_]\+|edir/NOKEY_NAME|g'
62 echo "--- Contents of files using plaintext names:"
63 cat $SCRATCH_MNT/edir/@@@ |& _filter_scratch
64 cat $SCRATCH_MNT/edir/abcd |& _filter_scratch
65 echo "--- Contents of files using no-key names:"
66 cat ${nokey_names[@]} |& filter_nokey_filenames
69 show_directory_with_key()
71 echo "--- Directory listing:"
72 find $SCRATCH_MNT/edir -mindepth 1 | sort | _filter_scratch
76 # View the directory without the encryption key. The plaintext names shouldn't
77 # exist, but 'cat' each to verify this, which also should create negative
78 # dentries. The no-key names are unpredictable by design, but verify that the
79 # correct number of them are listed by readdir, and save them for later.
81 echo "***** Without encryption key *****"
82 _unlink_session_encryption_key $keydesc
84 echo "--- Directory listing:"
85 nokey_names=( $(find $SCRATCH_MNT/edir -mindepth 1 | sort) )
86 printf '%s\n' "${nokey_names[@]}" | filter_nokey_filenames
89 # Without remounting or dropping caches, add the encryption key and view the
90 # directory again. Now the plaintext names should all be there, and the no-key
91 # names should be gone. Make sure to 'cat' all the names to test for stale
94 echo "***** With encryption key *****"
95 _add_session_encryption_key $keydesc $raw_key
96 show_directory_with_key
98 # Test for ->d_revalidate() race conditions.
100 echo "***** Race conditions *****"
101 $here/src/t_encrypted_d_revalidate $SCRATCH_MNT/edir
102 rm -rf $SCRATCH_MNT/edir/dir
104 # Now open the files to pin them in the inode cache (needed to make the test
105 # reliable), then revoke the encryption key. This should no longer cause the
106 # files to be presented in no-key form immediately.
108 echo "***** After key revocation *****"
110 exec 3<$SCRATCH_MNT/edir
111 exec 4<$SCRATCH_MNT/edir/@@@
112 exec 5<$SCRATCH_MNT/edir/abcd
113 _revoke_session_encryption_key $keydesc
114 show_directory_with_key