generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / generic / 440
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Google, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 440
6 #
7 # Test that when the filesystem tries to enforce that all files in a directory
8 # tree use the same encryption policy, it doesn't get confused and incorrectly
9 # return EPERM in cases where the parent's key is cached but not the child's, or
10 # vice versa.  Such situations can arise following removal of the master key
11 # from the keyring.  Regression test for:
12 #       272f98f68462 ("fscrypt: fix context consistency check when key(s) unavailable")
13 #
14 . ./common/preamble
15 _begin_fstest auto quick encrypt
16
17 # Import common functions.
18 . ./common/filter
19 . ./common/encrypt
20
21 # real QA test starts here
22 _supported_fs generic
23 _require_scratch_encryption
24 _require_symlinks
25 _require_command "$KEYCTL_PROG" keyctl
26
27 # Set up an encryption-capable filesystem and an encryption key.
28 _init_session_keyring
29 _scratch_mkfs_encrypted &>> $seqres.full
30 _scratch_mount
31 keydesc=$(_generate_key_descriptor)
32 raw_key=$(_generate_raw_encryption_key)
33 _add_session_encryption_key $keydesc $raw_key
34
35 # Set up an encrypted directory containing a regular file, a subdirectory, and a
36 # symlink.
37 mkdir $SCRATCH_MNT/edir
38 _set_encpolicy $SCRATCH_MNT/edir $keydesc
39 mkdir $SCRATCH_MNT/edir/subdir
40 ln -s target $SCRATCH_MNT/edir/symlink
41 echo contents > $SCRATCH_MNT/edir/file
42
43 # Starting from a fresh mount (no inodes have their encryption key cached),
44 # reproduce the situation where an encrypted file *without* its key cached is
45 # looked up or opened from within a directory *with* its key cached, and no key
46 # is in the keyring.  Try with a regular file, a directory, and a symlink.
47 _scratch_cycle_mount
48 echo
49 echo "***** Parent has key, but child doesn't *****"
50 exec 3< $SCRATCH_MNT/edir # pin inode with cached key in memory
51 ls $SCRATCH_MNT/edir | sort
52 _unlink_session_encryption_key $keydesc
53 cat $SCRATCH_MNT/edir/file |& _filter_scratch
54 ls $SCRATCH_MNT/edir/subdir
55 cat $SCRATCH_MNT/edir/symlink |& _filter_scratch
56 exec 3>&-
57
58 # Now, the inverse: an encrypted file *with* its key cached is looked up or
59 # opened from within a directory *without* its key cached, and no key is in the
60 # keyring.  This is most easily reproducible using a hard link.  Note: the
61 # expected behavior (at least, until we have a real API for revoking filesystem
62 # encryption keys) is that we should still be able to open the file and read its
63 # plaintext contents, even though its filename is shown in ciphertext!
64 echo
65 echo "***** Child has key, but parent doesn't *****"
66 _add_session_encryption_key $keydesc $raw_key
67 mkdir $SCRATCH_MNT/edir2
68 _set_encpolicy $SCRATCH_MNT/edir2 $keydesc
69 ln $SCRATCH_MNT/edir/file $SCRATCH_MNT/edir2/link
70 _scratch_cycle_mount
71 cat $SCRATCH_MNT/edir2/link
72 exec 3< $SCRATCH_MNT/edir2/link # pin inode with cached key in memory
73 _unlink_session_encryption_key $keydesc
74 stat $SCRATCH_MNT/edir/file |& _filter_stat |& _filter_scratch
75 cat "$(find $SCRATCH_MNT/edir/ -type f)"
76 exec 3>&-
77
78 # success, all done
79 status=0
80 exit