2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Google, Inc. All Rights Reserved.
7 # Test that encrypted dentries are revalidated after adding a key.
9 # 28b4c263961c ("ext4 crypto: revalidate dentry after adding or removing the key")
11 # Furthermore, test that encrypted directories are *not* revalidated after
12 # "revoking" 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 seqres=$RESULT_DIR/$seq
24 echo "QA output created by $seq"
28 status=1 # failure is the default!
29 trap "_cleanup; exit \$status" 0 1 2 3 15
37 # get standard environment, filters and checks
42 # remove previous $seqres.full before test
45 # real QA test starts here
47 _require_scratch_encryption
48 _require_command "$KEYCTL_PROG" keyctl
49 _require_test_program "t_encrypted_d_revalidate"
51 # Set up an encrypted directory
52 _scratch_mkfs_encrypted &>> $seqres.full
55 keydesc=$(_generate_key_descriptor)
56 raw_key=$(_generate_raw_encryption_key)
57 mkdir $SCRATCH_MNT/edir
58 _add_session_encryption_key $keydesc $raw_key
59 _set_encpolicy $SCRATCH_MNT/edir $keydesc
61 # Create two files in the directory: one whose name is valid in the base64
62 # format used for encoding ciphertext filenames, and one whose name is not. The
63 # exact filenames *should* be irrelevant, but due to yet another bug, ->lookup()
64 # in an encrypted directory without the key returned ERR_PTR(-ENOENT) rather
65 # than NULL if the name was not valid ciphertext, causing a negative dentry to
66 # not be created. For the purpose of this test, we want at least one negative
67 # dentry to be created, so just create both types of name.
68 echo contents_@@@ > $SCRATCH_MNT/edir/@@@ # not valid base64
69 echo contents_abcd > $SCRATCH_MNT/edir/abcd # valid base64
71 filter_ciphertext_filenames()
73 _filter_scratch | sed 's|edir/[a-zA-Z0-9+,_]\+|edir/ENCRYPTED_NAME|g'
78 echo "--- Contents of files using plaintext names:"
79 cat $SCRATCH_MNT/edir/@@@ |& _filter_scratch
80 cat $SCRATCH_MNT/edir/abcd |& _filter_scratch
81 echo "--- Contents of files using ciphertext names:"
82 cat ${ciphertext_names[@]} |& filter_ciphertext_filenames
85 show_directory_with_key()
87 echo "--- Directory listing:"
88 find $SCRATCH_MNT/edir -mindepth 1 | sort | _filter_scratch
92 # View the directory without the encryption key. The plaintext names shouldn't
93 # exist, but 'cat' each to verify this, which also should create negative
94 # dentries. The ciphertext names are unpredictable by design, but verify that
95 # the correct number of them are listed by readdir, and save them for later.
97 echo "***** Without encryption key *****"
98 _unlink_session_encryption_key $keydesc
100 echo "--- Directory listing:"
101 ciphertext_names=( $(find $SCRATCH_MNT/edir -mindepth 1 | sort) )
102 printf '%s\n' "${ciphertext_names[@]}" | filter_ciphertext_filenames
105 # Without remounting or dropping caches, add the encryption key and view the
106 # directory again. Now the plaintext names should all be there, and the
107 # ciphertext names should be gone. Make sure to 'cat' all the names to test for
110 echo "***** With encryption key *****"
111 _add_session_encryption_key $keydesc $raw_key
112 show_directory_with_key
114 # Test for ->d_revalidate() race conditions.
116 echo "***** Race conditions *****"
117 $here/src/t_encrypted_d_revalidate $SCRATCH_MNT/edir
118 rm -rf $SCRATCH_MNT/edir/dir
120 # Now open the files to pin them in the inode cache (needed to make the test
121 # reliable), then revoke the encryption key. This should no longer cause the
122 # files to be presented in ciphertext form immediately.
124 echo "***** After key revocation *****"
126 exec 3<$SCRATCH_MNT/edir
127 exec 4<$SCRATCH_MNT/edir/@@@
128 exec 5<$SCRATCH_MNT/edir/abcd
129 _revoke_session_encryption_key $keydesc
130 show_directory_with_key