open_by_handle: make -h (help) a valid option
[xfstests-dev.git] / common / encrypt
1 #-----------------------------------------------------------------------
2 #
3 # Common functions for testing filesystem-level encryption
4 #
5 #-----------------------------------------------------------------------
6 # Copyright (c) 2016 Google, Inc.  All Rights Reserved.
7 #
8 # Author: Eric Biggers <ebiggers@google.com>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation.
13 #
14 # This program is distributed in the hope that it would be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write the Free Software Foundation,
21 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22 #-----------------------------------------------------------------------
23
24 _require_scratch_encryption()
25 {
26         _require_scratch
27
28         _require_xfs_io_command "set_encpolicy"
29
30         # The 'test_dummy_encryption' mount option interferes with trying to use
31         # encryption for real, even if we are just trying to get/set policies
32         # and never put any keys in the keyring.  So skip the real encryption
33         # tests if the 'test_dummy_encryption' mount option was specified.
34         _exclude_scratch_mount_option "test_dummy_encryption"
35
36         # Make a filesystem on the scratch device with the encryption feature
37         # enabled.  If this fails then probably the userspace tools (e.g.
38         # e2fsprogs or f2fs-tools) are too old to understand encryption.
39         if ! _scratch_mkfs_encrypted &>>$seqres.full; then
40                 _notrun "$FSTYP userspace tools do not support encryption"
41         fi
42
43         # Try to mount the filesystem.  If this fails then either the kernel
44         # isn't aware of encryption, or the mkfs options were not compatible
45         # with encryption (e.g. ext4 with block size != PAGE_SIZE).
46         if ! _try_scratch_mount &>>$seqres.full; then
47                 _notrun "kernel is unaware of $FSTYP encryption feature," \
48                         "or mkfs options are not compatible with encryption"
49         fi
50
51         # The kernel may be aware of encryption without supporting it.  For
52         # example, for ext4 this is the case with kernels configured with
53         # CONFIG_EXT4_FS_ENCRYPTION=n.  Detect support for encryption by trying
54         # to set an encryption policy.  (For ext4 we could instead check for the
55         # presence of /sys/fs/ext4/features/encryption, but this is broken on
56         # some older kernels and is ext4-specific anyway.)
57         mkdir $SCRATCH_MNT/tmpdir
58         if $XFS_IO_PROG -c set_encpolicy $SCRATCH_MNT/tmpdir \
59                 2>&1 >>$seqres.full | \
60                 egrep -q 'Inappropriate ioctl for device|Operation not supported'
61         then
62                 _notrun "kernel does not support $FSTYP encryption"
63         fi
64         rmdir $SCRATCH_MNT/tmpdir
65         _scratch_unmount
66 }
67
68 _scratch_mkfs_encrypted()
69 {
70         case $FSTYP in
71         ext4|f2fs)
72                 _scratch_mkfs -O encrypt
73                 ;;
74         ubifs)
75                 # erase the UBI volume; reformated automatically on next mount
76                 $UBIUPDATEVOL_PROG ${SCRATCH_DEV} -t
77                 ;;
78         *)
79                 _notrun "No encryption support for $FSTYP"
80                 ;;
81         esac
82 }
83
84 _scratch_mkfs_sized_encrypted()
85 {
86         case $FSTYP in
87         ext4|f2fs)
88                 MKFS_OPTIONS="$MKFS_OPTIONS -O encrypt" _scratch_mkfs_sized $*
89                 ;;
90         *)
91                 _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized_encrypted"
92                 ;;
93         esac
94 }
95
96 # Give the invoking shell a new session keyring.  This makes any keys we add to
97 # the session keyring scoped to the lifetime of the test script.
98 _new_session_keyring()
99 {
100         $KEYCTL_PROG new_session >>$seqres.full
101 }
102
103 # Generate a key descriptor (16 character hex string)
104 _generate_key_descriptor()
105 {
106         local keydesc=""
107         local i
108         for ((i = 0; i < 8; i++)); do
109                 keydesc="${keydesc}$(printf "%02x" $(( $RANDOM % 256 )))"
110         done
111         echo $keydesc
112 }
113
114 # Generate a raw encryption key, but don't add it to the keyring yet.
115 _generate_raw_encryption_key()
116 {
117         local raw=""
118         local i
119         for ((i = 0; i < 64; i++)); do
120                 raw="${raw}\\x$(printf "%02x" $(( $RANDOM % 256 )))"
121         done
122         echo $raw
123 }
124
125 # Add the specified raw encryption key to the session keyring, using the
126 # specified key descriptor.
127 _add_encryption_key()
128 {
129         local keydesc=$1
130         local raw=$2
131
132         #
133         # Add the key to the session keyring.  The required structure is:
134         #
135         #       #define FS_MAX_KEY_SIZE 64
136         #       struct fscrypt_key {
137         #               u32 mode;
138         #               u8 raw[FS_MAX_KEY_SIZE];
139         #               u32 size;
140         #       } __packed;
141         #
142         # The kernel ignores 'mode' but requires that 'size' be 64.
143         #
144         # Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key
145         # descriptor hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6
146         # and later) also allow the common key prefix "fscrypt:" in addition to
147         # their filesystem-specific key prefix ("ext4:", "f2fs:").  It would be
148         # nice to use the common key prefix, but for now use the filesystem-
149         # specific prefix to make it possible to test older kernels...
150         #
151         local big_endian=$(echo -ne '\x11' | od -tx2 | head -1 | \
152                            cut -f2 -d' ' | cut -c1 )
153         if (( big_endian )); then
154                 local mode='\x00\x00\x00\x00'
155                 local size='\x00\x00\x00\x40'
156         else
157                 local mode='\x00\x00\x00\x00'
158                 local size='\x40\x00\x00\x00'
159         fi
160         echo -n -e "${mode}${raw}${size}" |
161                 $KEYCTL_PROG padd logon $FSTYP:$keydesc @s >>$seqres.full
162 }
163
164 #
165 # Generate a random encryption key, add it to the session keyring, and print out
166 # the resulting key descriptor (example: "8bf798e1a494e1ec").  Requires the
167 # keyctl program.  It's assumed the caller has already set up a test-scoped
168 # session keyring using _new_session_keyring.
169 #
170 _generate_encryption_key()
171 {
172         local keydesc=$(_generate_key_descriptor)
173         local raw=$(_generate_raw_encryption_key)
174
175         _add_encryption_key $keydesc $raw
176
177         echo $keydesc
178 }
179
180 # Unlink an encryption key from the session keyring, given its key descriptor.
181 _unlink_encryption_key()
182 {
183         local keydesc=$1
184         local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
185         $KEYCTL_PROG unlink $keyid >>$seqres.full
186 }
187
188 # Revoke an encryption key from the keyring, given its key descriptor.
189 _revoke_encryption_key()
190 {
191         local keydesc=$1
192         local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
193         $KEYCTL_PROG revoke $keyid >>$seqres.full
194 }