common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[xfstests-dev.git] / tests / generic / 595
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2020 Google LLC
4 #
5 # FS QA Test No. 595
6 #
7 # Regression test for a bug in the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl fixed by
8 # commit 2b4eae95c736 ("fscrypt: don't evict dirty inodes after removing key").
9 # This bug could cause writes to encrypted files to be lost if they raced with
10 # the corresponding fscrypt master key being removed.  With f2fs, this bug could
11 # also crash the kernel.
12 #
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 here=`pwd`
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         # Stop all subprocesses.
25         touch $tmp.done
26         wait
27
28         rm -f $tmp.*
29 }
30
31 # get standard environment, filters and checks
32 . ./common/rc
33 . ./common/filter
34 . ./common/encrypt
35
36 # remove previous $seqres.full before test
37 rm -f $seqres.full
38
39 # real QA test starts here
40 _supported_fs generic
41 _supported_os Linux
42 _require_scratch_encryption -v 2
43 _require_command "$KEYCTL_PROG" keyctl
44
45 _scratch_mkfs_encrypted &>> $seqres.full
46 _scratch_mount
47
48 dir=$SCRATCH_MNT/dir
49 runtime=$((4 * TIME_FACTOR))
50
51 # Create an encrypted directory.
52 mkdir $dir
53 _set_encpolicy $dir $TEST_KEY_IDENTIFIER
54 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
55
56 # Start with a single-threaded reproducer:
57 echo -e "\n# Single-threaded reproducer"
58 # Keep a fd open to a file past its fscrypt master key being removed.
59 exec 3>$dir/file
60 _rm_enckey $SCRATCH_MNT $TEST_KEY_IDENTIFIER
61 # Write to and close the open fd.
62 echo contents >&3
63 exec 3>&-
64 # Drop any dentries which might be pinning the inode for "file".
65 echo 2 > /proc/sys/vm/drop_caches
66 # In buggy kernels, the inode for "file" was evicted despite the dirty data,
67 # causing the dirty data to be lost.  Check whether the write made it through.
68 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
69 cat $dir/file
70 rm -f $dir/file
71
72 # Also run a multi-threaded reproducer.  This is included for good measure, as
73 # this type of thing tends to be good for finding other bugs too.
74 echo -e "\n# Multi-threaded reproducer"
75 touch $dir/file
76
77 # One process add/removes the encryption key repeatedly.
78 (
79         while [ ! -e $tmp.done ]; do
80                 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
81                 _rm_enckey $SCRATCH_MNT $TEST_KEY_IDENTIFIER &> /dev/null
82         done
83 ) &
84
85 # Another process repeatedly tries to append to the encrypted file.  The file is
86 # re-opened each time, so that there are chances for the inode to be evicted.
87 # Failures to open the file due to the key being removed are ignored.
88 (
89         touch $tmp.expected
90         while [ ! -e $tmp.done ]; do
91                 if sh -c "echo -n X >> $dir/file" 2>/dev/null; then
92                         # Keep track of the expected file contents.
93                         echo -n X >> $tmp.expected
94                 fi
95         done
96 ) &
97
98 # Run for a while.
99 sleep $runtime
100
101 # Stop all subprocesses.
102 touch $tmp.done
103 wait
104
105 # Make sure no writes were lost.
106 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
107 stat $tmp.expected >> $seqres.full
108 stat $dir/file >> $seqres.full
109 cmp $tmp.expected $dir/file
110
111 echo "Multi-threaded reproducer done"
112
113 # success, all done
114 status=0
115 exit