common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[xfstests-dev.git] / tests / generic / 399
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Google, Inc.  All Rights Reserved.
4 #
5 # FS QA Test generic/399
6 #
7 # Check for weaknesses in filesystem encryption involving the same ciphertext
8 # being repeated.  For file contents, we fill a small filesystem with large
9 # files of 0's and verify the filesystem is incompressible.  For filenames, we
10 # create an identical symlink in two different directories and verify the
11 # ciphertext filenames and symlink targets are different.
12 #
13 # This test can detect some basic cryptographic mistakes such as nonce reuse
14 # (across files), initialization vector reuse (across blocks), or data somehow
15 # being left in plaintext by accident.  For example, it detects the
16 # initialization vector reuse bug fixed in commit 02fc59a0d28f ("f2fs/crypto:
17 # fix xts_tweak initialization").
18 #
19 seq=`basename $0`
20 seqres=$RESULT_DIR/$seq
21 echo "QA output created by $seq"
22
23 here=`pwd`
24 tmp=/tmp/$$
25 status=1        # failure is the default!
26 trap "_cleanup; exit \$status" 0 1 2 3 15
27
28 _cleanup()
29 {
30         cd /
31         rm -f $tmp.*
32 }
33
34 # get standard environment, filters and checks
35 . ./common/rc
36 . ./common/filter
37 . ./common/encrypt
38
39 # remove previous $seqres.full before test
40 rm -f $seqres.full
41
42 # real QA test starts here
43 _supported_fs generic
44 _supported_os Linux
45 _require_scratch_encryption
46 _require_symlinks
47 _require_command "$XZ_PROG" xz
48 _require_command "$KEYCTL_PROG" keyctl
49
50 _new_session_keyring
51
52 #
53 # Set up a small filesystem containing an encrypted directory.  64 MB is enough
54 # for both ext4 and f2fs (f2fs doesn't support a 32 MB filesystem).  Before
55 # creating the filesystem, zero out the needed portion of the device so that
56 # existing data on the device doesn't contribute to the compressed size.
57 #
58 fs_size_in_mb=64
59 fs_size=$((fs_size_in_mb * 1024 * 1024))
60 dd if=/dev/zero of=$SCRATCH_DEV bs=$((1024 * 1024)) \
61         count=$fs_size_in_mb &>> $seqres.full
62 _scratch_mkfs_sized_encrypted $fs_size &>> $seqres.full
63 _scratch_mount
64
65 keydesc=$(_generate_session_encryption_key)
66 mkdir $SCRATCH_MNT/encrypted_dir
67 _set_encpolicy $SCRATCH_MNT/encrypted_dir $keydesc
68
69 # Create the "same" symlink in two different directories.
70 # Later we'll check both the name and target of the symlink.
71 mkdir $SCRATCH_MNT/encrypted_dir/subdir1
72 mkdir $SCRATCH_MNT/encrypted_dir/subdir2
73 ln -s symlink_target $SCRATCH_MNT/encrypted_dir/subdir1/symlink
74 ln -s symlink_target $SCRATCH_MNT/encrypted_dir/subdir2/symlink
75
76 #
77 # Write files of 1 MB of all the same byte until we hit ENOSPC.  Note that we
78 # must not create sparse files, since the contents of sparse files are not
79 # stored on-disk.  Also, we create multiple files rather than one big file
80 # because we want to test for reuse of per-file keys.
81 #
82 total_file_size=0
83 i=1
84 while true; do
85         file=$SCRATCH_MNT/encrypted_dir/file$i
86
87         $XFS_IO_PROG -f $file -c 'pwrite 0 1M' &> $tmp.out
88         echo "Writing $file..." >> $seqres.full
89         cat $tmp.out >> $seqres.full
90
91         file_size=0
92         if [ -e $file ]; then
93                 file_size=$(_get_filesize $file)
94         fi
95
96         # We shouldn't have been able to write more data than we had space for.
97         (( total_file_size += file_size ))
98         if (( total_file_size > fs_size )); then
99                 _fail "Wrote $total_file_size bytes but should have only" \
100                       "had space for $fs_size bytes at most!"
101         fi
102
103         # Stop if we hit ENOSPC.
104         if grep -q 'No space left on device' $tmp.out; then
105                 break
106         fi
107
108         # Otherwise the file should have been successfully created.
109         if [ ! -e $file ]; then
110                 _fail "$file failed to be created, but the fs isn't out of space yet!"
111         fi
112         if (( file_size != 1024 * 1024 )); then
113                 _fail "Size of $file is wrong (possible write error?)." \
114                       "Got $file_size, expected 1 MiB"
115         fi
116         (( i++ ))
117 done
118
119 #
120 # Unmount the filesystem and compute its compressed size.  It must be no smaller
121 # than the amount of data that was written; otherwise there was a compromise in
122 # the confidentiality of the data.  False positives should not be possible
123 # because filesystem metadata will also contribute to the compressed size.
124 #
125 # Note: it's important to use a strong compressor such as xz which can detect
126 # redundancy across most or all of the filesystem.  We run xz with a 64 MB
127 # sliding window but use some custom settings to make it faster and use less
128 # memory than the '-9' preset.  The memory needed with our settings will be
129 # 64 * 6.5 = 416 MB; see xz(1).
130 #
131 _unlink_session_encryption_key $keydesc
132 _scratch_unmount
133 fs_compressed_size=$(head -c $fs_size $SCRATCH_DEV | \
134         xz --lzma2=dict=64M,mf=hc4,mode=fast,nice=16 | \
135         wc -c)
136
137 if (( $fs_compressed_size < $total_file_size )); then
138         echo "FAIL: filesystem was compressible" \
139                 "($total_file_size bytes => $fs_compressed_size bytes)"
140 else
141         echo "PASS: ciphertexts were not repeated for contents"
142 fi
143
144 # Verify that encrypted filenames and symlink targets were not reused.  Note
145 # that since the ciphertexts should be unpredictable, we cannot simply include
146 # the expected names in the expected output file.
147 _scratch_mount
148 find $SCRATCH_MNT/encrypted_dir -type l | wc -l
149 link1=$(find $SCRATCH_MNT/encrypted_dir -type l | head -1)
150 link2=$(find $SCRATCH_MNT/encrypted_dir -type l | tail -1)
151 [ $(basename $link1) = $(basename $link2) ] && \
152         echo "Encrypted filenames were reused!"
153 [ $(readlink $link1) = $(readlink $link2) ] && \
154         echo "Encrypted symlink targets were reused!"
155
156 # success, all done
157 status=0
158 exit