common: kill _supported_os
[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 _require_scratch_encryption -v 2
42 _require_command "$KEYCTL_PROG" keyctl
43
44 _scratch_mkfs_encrypted &>> $seqres.full
45 _scratch_mount
46
47 dir=$SCRATCH_MNT/dir
48 runtime=$((4 * TIME_FACTOR))
49
50 # Create an encrypted directory.
51 mkdir $dir
52 _set_encpolicy $dir $TEST_KEY_IDENTIFIER
53 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
54
55 # Start with a single-threaded reproducer:
56 echo -e "\n# Single-threaded reproducer"
57 # Keep a fd open to a file past its fscrypt master key being removed.
58 exec 3>$dir/file
59 _rm_enckey $SCRATCH_MNT $TEST_KEY_IDENTIFIER
60 # Write to and close the open fd.
61 echo contents >&3
62 exec 3>&-
63 # Drop any dentries which might be pinning the inode for "file".
64 echo 2 > /proc/sys/vm/drop_caches
65 # In buggy kernels, the inode for "file" was evicted despite the dirty data,
66 # causing the dirty data to be lost.  Check whether the write made it through.
67 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
68 cat $dir/file
69 rm -f $dir/file
70
71 # Also run a multi-threaded reproducer.  This is included for good measure, as
72 # this type of thing tends to be good for finding other bugs too.
73 echo -e "\n# Multi-threaded reproducer"
74 touch $dir/file
75
76 # One process add/removes the encryption key repeatedly.
77 (
78         while [ ! -e $tmp.done ]; do
79                 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
80                 _rm_enckey $SCRATCH_MNT $TEST_KEY_IDENTIFIER &> /dev/null
81         done
82 ) &
83
84 # Another process repeatedly tries to append to the encrypted file.  The file is
85 # re-opened each time, so that there are chances for the inode to be evicted.
86 # Failures to open the file due to the key being removed are ignored.
87 (
88         touch $tmp.expected
89         while [ ! -e $tmp.done ]; do
90                 if sh -c "echo -n X >> $dir/file" 2>/dev/null; then
91                         # Keep track of the expected file contents.
92                         echo -n X >> $tmp.expected
93                 fi
94         done
95 ) &
96
97 # Run for a while.
98 sleep $runtime
99
100 # Stop all subprocesses.
101 touch $tmp.done
102 wait
103
104 # Make sure no writes were lost.
105 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" > /dev/null
106 stat $tmp.expected >> $seqres.full
107 stat $dir/file >> $seqres.full
108 cmp $tmp.expected $dir/file
109
110 echo "Multi-threaded reproducer done"
111
112 # success, all done
113 status=0
114 exit