2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Google, Inc. All Rights Reserved.
5 # Functions for setting up and testing file encryption
8 # _require_scratch_encryption [-c CONTENTS_MODE] [-n FILENAMES_MODE]
11 # Require encryption support on the scratch device.
13 # This checks for support for the default type of encryption policy (v1 with
14 # AES-256-XTS and AES-256-CTS). Options can be specified to also require
15 # support for a different type of encryption policy.
17 _require_scratch_encryption()
21 _require_xfs_io_command "set_encpolicy"
23 # The 'test_dummy_encryption' mount option interferes with trying to use
24 # encryption for real, even if we are just trying to get/set policies
25 # and never put any keys in the keyring. So skip the real encryption
26 # tests if the 'test_dummy_encryption' mount option was specified.
27 _exclude_scratch_mount_option "test_dummy_encryption"
29 # Make a filesystem on the scratch device with the encryption feature
30 # enabled. If this fails then probably the userspace tools (e.g.
31 # e2fsprogs or f2fs-tools) are too old to understand encryption.
32 if ! _scratch_mkfs_encrypted &>>$seqres.full; then
33 _notrun "$FSTYP userspace tools do not support encryption"
36 # Try to mount the filesystem. If this fails then either the kernel
37 # isn't aware of encryption, or the mkfs options were not compatible
38 # with encryption (e.g. ext4 with block size != PAGE_SIZE).
39 if ! _try_scratch_mount &>>$seqres.full; then
40 _notrun "kernel is unaware of $FSTYP encryption feature," \
41 "or mkfs options are not compatible with encryption"
44 # The kernel may be aware of encryption without supporting it. For
45 # example, for ext4 this is the case with kernels configured with
46 # CONFIG_EXT4_FS_ENCRYPTION=n. Detect support for encryption by trying
47 # to set an encryption policy. (For ext4 we could instead check for the
48 # presence of /sys/fs/ext4/features/encryption, but this is broken on
49 # some older kernels and is ext4-specific anyway.)
50 mkdir $SCRATCH_MNT/tmpdir
51 if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
52 egrep -q 'Inappropriate ioctl for device|Operation not supported'
54 _notrun "kernel does not support $FSTYP encryption"
56 rmdir $SCRATCH_MNT/tmpdir
58 # If required, check for support for the specific type of encryption
59 # policy required by the test.
61 _require_encryption_policy_support $SCRATCH_MNT "$@"
67 _require_encryption_policy_support()
71 local set_encpolicy_args=""
72 local policy_version=1
76 while getopts "c:n:v:" c; do
79 set_encpolicy_args+=" -$c $OPTARG"
82 set_encpolicy_args+=" -$c $OPTARG"
83 policy_version=$OPTARG
86 _fail "Unrecognized option '$c'"
90 set_encpolicy_args=${set_encpolicy_args# }
92 echo "Checking whether kernel supports encryption policy: $set_encpolicy_args" \
96 if (( policy_version > 1 )); then
97 _require_xfs_io_command "get_encpolicy" "-t"
98 local output=$(_get_encpolicy $dir -t)
99 if [ "$output" != "supported" ]; then
100 if [ "$output" = "unsupported" ]; then
101 _notrun "kernel does not support $FSTYP encryption v2 API"
103 _fail "Unexpected output from 'get_encpolicy -t': $output"
105 # Both the kernel and xfs_io support v2 encryption policies, and
106 # therefore also filesystem-level keys -- since that's the only
107 # way to provide keys for v2 policies.
108 local raw_key=$(_generate_raw_encryption_key)
109 local keyspec=$(_add_enckey $mnt "$raw_key" | awk '{print $NF}')
111 _require_command "$KEYCTL_PROG" keyctl
113 local keyspec=$(_generate_session_encryption_key)
115 if _set_encpolicy $dir $keyspec $set_encpolicy_args \
116 2>&1 >>$seqres.full | egrep -q 'Invalid argument'; then
117 _notrun "kernel does not support encryption policy: '$set_encpolicy_args'"
119 # fscrypt allows setting policies with modes it knows about, even
120 # without kernel crypto API support. E.g. a policy using Adiantum
121 # encryption can be set on a kernel without CONFIG_CRYPTO_ADIANTUM.
122 # But actually trying to use such an encrypted directory will fail.
123 # To reliably check for availability of both the contents and filenames
124 # encryption modes, try creating a nonempty file.
125 if ! echo foo > $dir/file; then
126 _notrun "encryption policy '$set_encpolicy_args' is unusable; probably missing kernel crypto API support"
128 if (( policy_version <= 1 )); then
129 $KEYCTL_PROG clear @s
134 _scratch_mkfs_encrypted()
138 _scratch_mkfs -O encrypt
141 # erase the UBI volume; reformated automatically on next mount
142 $UBIUPDATEVOL_PROG ${SCRATCH_DEV} -t
145 _notrun "No encryption support for $FSTYP"
150 _scratch_mkfs_sized_encrypted()
154 MKFS_OPTIONS="$MKFS_OPTIONS -O encrypt" _scratch_mkfs_sized $*
157 _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized_encrypted"
162 # Give the invoking shell a new session keyring. This makes any keys we add to
163 # the session keyring scoped to the lifetime of the test script.
164 _new_session_keyring()
166 $KEYCTL_PROG new_session >>$seqres.full
169 # Generate a key descriptor (16 character hex string)
170 _generate_key_descriptor()
174 for ((i = 0; i < 8; i++)); do
175 keydesc="${keydesc}$(printf "%02x" $(( $RANDOM % 256 )))"
180 # Generate a raw encryption key, but don't add it to any keyring yet.
181 _generate_raw_encryption_key()
185 for ((i = 0; i < 64; i++)); do
186 raw="${raw}\\x$(printf "%02x" $(( $RANDOM % 256 )))"
191 # Add the specified raw encryption key to the session keyring, using the
192 # specified key descriptor.
193 _add_session_encryption_key()
199 # Add the key to the session keyring. The required structure is:
201 # #define FS_MAX_KEY_SIZE 64
202 # struct fscrypt_key {
204 # u8 raw[FS_MAX_KEY_SIZE];
208 # The kernel ignores 'mode' but requires that 'size' be 64.
210 # Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key
211 # descriptor hex string. Newer kernels (ext4 4.8 and later, f2fs 4.6
212 # and later) also allow the common key prefix "fscrypt:" in addition to
213 # their filesystem-specific key prefix ("ext4:", "f2fs:"). It would be
214 # nice to use the common key prefix, but for now use the filesystem-
215 # specific prefix to make it possible to test older kernels...
217 local big_endian=$(echo -ne '\x11' | od -tx2 | head -1 | \
218 cut -f2 -d' ' | cut -c1 )
219 if (( big_endian )); then
220 local mode='\x00\x00\x00\x00'
221 local size='\x00\x00\x00\x40'
223 local mode='\x00\x00\x00\x00'
224 local size='\x40\x00\x00\x00'
226 echo -n -e "${mode}${raw}${size}" |
227 $KEYCTL_PROG padd logon $FSTYP:$keydesc @s >>$seqres.full
231 # Generate a random encryption key, add it to the session keyring, and print out
232 # the resulting key descriptor (example: "8bf798e1a494e1ec"). Requires the
233 # keyctl program. It's assumed the caller has already set up a test-scoped
234 # session keyring using _new_session_keyring.
236 _generate_session_encryption_key()
238 local keydesc=$(_generate_key_descriptor)
239 local raw=$(_generate_raw_encryption_key)
241 _add_session_encryption_key $keydesc $raw
246 # Unlink an encryption key from the session keyring, given its key descriptor.
247 _unlink_session_encryption_key()
250 local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
251 $KEYCTL_PROG unlink $keyid >>$seqres.full
254 # Revoke an encryption key from the session keyring, given its key descriptor.
255 _revoke_session_encryption_key()
258 local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
259 $KEYCTL_PROG revoke $keyid >>$seqres.full
262 # Set an encryption policy on the specified directory.
268 $XFS_IO_PROG -c "set_encpolicy $*" "$dir"
271 _user_do_set_encpolicy()
276 _user_do "$XFS_IO_PROG -c \"set_encpolicy $*\" \"$dir\""
279 # Display the specified file or directory's encryption policy.
285 $XFS_IO_PROG -c "get_encpolicy $*" "$file"
288 _user_do_get_encpolicy()
293 _user_do "$XFS_IO_PROG -c \"get_encpolicy $*\" \"$file\""
296 # Add an encryption key to the given filesystem.
303 echo -ne "$raw_key" | $XFS_IO_PROG -c "add_enckey $*" "$mnt"
306 _user_do_add_enckey()
312 _user_do "echo -ne \"$raw_key\" | $XFS_IO_PROG -c \"add_enckey $*\" \"$mnt\""
315 # Remove the given encryption key from the given filesystem.
322 $XFS_IO_PROG -c "rm_enckey $* $keyspec" "$mnt"
331 _user_do "$XFS_IO_PROG -c \"rm_enckey $* $keyspec\" \"$mnt\""
334 # Get the status of the given encryption key on the given filesystem.
341 $XFS_IO_PROG -c "enckey_status $* $keyspec" "$mnt"
344 _user_do_enckey_status()
350 _user_do "$XFS_IO_PROG -c \"enckey_status $* $keyspec\" \"$mnt\""
353 # Retrieve the encryption nonce of the given inode as a hex string. The nonce
354 # was randomly generated by the filesystem and isn't exposed directly to
355 # userspace. But it can be read using the filesystem's debugging tools.
356 _get_encryption_nonce()
363 # Use debugfs to dump the special xattr named "c", which is the
364 # file's fscrypt_context. This produces a line like:
366 # c (28) = 01 01 04 02 00 00 00 00 00 00 00 00 ef bd 18 76 5d f6 41 4e c0 a2 cd 5f 91 29 7e 12
368 # Then filter it to get just the 16-byte 'nonce' field at the end:
370 # efbd18765df6414ec0a2cd5f91297e12
372 $DEBUGFS_PROG $device -R "ea_get <$inode> c" 2>>$seqres.full \
373 | grep '^c ' | sed 's/^.*=//' | tr -d ' \n' | tail -c 32
376 # dump.f2fs prints the fscrypt_context like:
378 # xattr: e_name_index:9 e_name:c e_name_len:1 e_value_size:28 e_value:
380 # contents_encryption_mode: 0x1
381 # filenames_encryption_mode: 0x4
383 # master_key_descriptor: 0000000000000000
384 # nonce: EFBD18765DF6414EC0A2CD5F91297E12
385 $DUMP_F2FS_PROG -i $inode $device | awk '
386 /\<e_name:c\>/ { found = 1 }
388 print substr($0, length($0) - 31, 32);
393 _fail "_get_encryption_nonce() isn't implemented on $FSTYP"
398 # Require support for _get_encryption_nonce()
399 _require_get_encryption_nonce_support()
401 echo "Checking for _get_encryption_nonce() support for $FSTYP" >> $seqres.full
404 _require_command "$DEBUGFS_PROG" debugfs
407 _require_command "$DUMP_F2FS_PROG" dump.f2fs
410 _notrun "_get_encryption_nonce() isn't implemented on $FSTYP"
415 # Retrieve the encrypted filename stored on-disk for the given file.
416 # The name is printed to stdout in binary.
417 _get_ciphertext_filename()
425 # Extract the filename from the debugfs output line like:
427 # 131075 100644 (1) 0 0 0 22-Apr-2019 16:54 \xa2\x85\xb0z\x13\xe9\x09\x86R\xed\xdc\xce\xad\x14d\x19
429 # Bytes are shown either literally or as \xHH-style escape
430 # sequences; we have to decode the escaped bytes here.
432 $DEBUGFS_PROG $device -R "ls -l -r <$dir_inode>" \
433 2>>$seqres.full | perl -ne '
434 next if not /^\s*'$inode'\s+/;
437 s/\\x([[:xdigit:]]{2})/chr hex $1/eg;
441 # Extract the filename from the dump.f2fs output line like:
443 # i_name [UpkzIPuts9by1oDmE+Ivfw]
445 # The name is shown base64-encoded; we have to decode it here.
447 $DUMP_F2FS_PROG $device -i $inode | perl -ne '
448 next if not /^i_name\s+\[([A-Za-z0-9+,]+)\]/;
450 my @chars = split //, $1;
453 my $table = join "", (A..Z, a..z, 0..9, "+", ",");
455 $ac += index($table, $_) << $bits;
458 print chr($ac & 0xff);
464 print STDERR "Invalid base64-encoded string!\n";
468 _fail "_get_ciphertext_filename() isn't implemented on $FSTYP"
473 # Require support for _get_ciphertext_filename().
474 _require_get_ciphertext_filename_support()
476 echo "Checking for _get_ciphertext_filename() support for $FSTYP" >> $seqres.full
479 # Verify that the "ls -l -r" debugfs command is supported and
480 # that it hex-encodes non-ASCII characters, rather than using an
481 # ambiguous escaping method. This requires e2fsprogs v1.45.1 or
482 # later; or more specifically, a version that has the commit
483 # "debugfs: avoid ambiguity when printing filenames".
484 _require_command "$DEBUGFS_PROG" debugfs
486 touch $SCRATCH_MNT/$'\xc1'
488 if ! $DEBUGFS_PROG $SCRATCH_DEV -R "ls -l -r /" 2>&1 \
489 | tee -a $seqres.full | grep -E -q '\s+\\xc1\s*$'; then
490 _notrun "debugfs (e2fsprogs) is too old; doesn't support showing unambiguous on-disk filenames"
494 # Verify that dump.f2fs shows encrypted filenames in full. This
495 # requires f2fs-tools v1.13.0 or later; or more specifically, a
496 # version that has the commit
497 # "f2fs-tools: improve filename printing".
499 _require_command "$DUMP_F2FS_PROG" dump.f2fs
500 _require_command "$KEYCTL_PROG" keyctl
504 local keydesc=$(_generate_session_encryption_key)
505 local dir=$SCRATCH_MNT/test.${FUNCNAME[0]}
506 local file=$dir/$(perl -e 'print "A" x 255')
508 _set_encpolicy $dir $keydesc
510 local inode=$(stat -c %i $file)
513 $KEYCTL_PROG clear @s
515 # 255-character filename should result in 340 base64 characters.
516 if ! $DUMP_F2FS_PROG -i $inode $SCRATCH_DEV \
517 | grep -E -q '^i_name[[:space:]]+\[[A-Za-z0-9+,]{340}\]'; then
518 _notrun "dump.f2fs (f2fs-tools) is too old; doesn't support showing unambiguous on-disk filenames"
522 _notrun "_get_ciphertext_filename() isn't implemented on $FSTYP"
527 # Get an encrypted file's list of on-disk blocks as a comma-separated list of
528 # block offsets from the start of the device. "Blocks" are 512 bytes each here.
529 _get_ciphertext_block_list()
534 $XFS_IO_PROG -c fiemap $file | perl -ne '
535 next if not /^\s*\d+: \[\d+\.\.\d+\]: (\d+)\.\.(\d+)/;
536 print $_ . "," foreach $1..$2;' | sed 's/,$//'
539 # Dump a block list that was previously saved by _get_ciphertext_block_list().
540 _dump_ciphertext_blocks()
546 for block in $(tr ',' ' ' <<< $blocklist); do
547 dd if=$device bs=512 count=1 skip=$block status=none
551 _do_verify_ciphertext_for_encryption_policy()
553 local contents_encryption_mode=$1
554 local filenames_encryption_mode=$2
555 local policy_flags=$3
556 local set_encpolicy_args=$4
559 local crypt_cmd="$here/src/fscrypt-crypt-util $7"
561 local blocksize=$(_get_block_size $SCRATCH_MNT)
562 local test_contents_files=()
563 local test_filenames_files=()
564 local i src dir dst inode blocklist \
565 padding_flag padding dir_inode len name f nonce decrypted_name
567 # Create files whose encrypted contents we'll verify. For each, save
568 # the information: (copy of original file, inode number of encrypted
569 # file, comma-separated block list) into test_contents_files[].
570 echo "Creating files for contents verification" >> $seqres.full
572 rm -f $tmp.testfile_*
573 for src in /dev/zero /dev/urandom; do
574 head -c $((4 * blocksize)) $src > $tmp.testfile_$i
577 dir=$SCRATCH_MNT/encdir
579 _set_encpolicy $dir $keydesc $set_encpolicy_args -f $policy_flags
580 for src in $tmp.testfile_*; do
583 inode=$(stat -c %i $dst)
584 blocklist=$(_get_ciphertext_block_list $dst)
585 test_contents_files+=("$src $inode $blocklist")
588 # Create files whose encrypted names we'll verify. For each, save the
589 # information: (original filename, inode number of encrypted file, inode
590 # of parent directory, padding amount) into test_filenames_files[]. Try
591 # each padding amount: 4, 8, 16, or 32 bytes. Also try various filename
592 # lengths, including boundary cases. Assume NAME_MAX == 255.
593 echo "Creating files for filenames verification" >> $seqres.full
594 for padding_flag in 0 1 2 3; do
595 padding=$((4 << padding_flag))
596 dir=$SCRATCH_MNT/encdir.pad$padding
598 dir_inode=$(stat -c %i $dir)
599 _set_encpolicy $dir $keydesc $set_encpolicy_args \
600 -f $((policy_flags | padding_flag))
601 for len in 1 3 15 16 17 32 100 254 255; do
602 name=$(tr -d -C a-zA-Z0-9 < /dev/urandom | head -c $len)
604 inode=$(stat -c %i $dir/$name)
605 test_filenames_files+=("$name $inode $dir_inode $padding")
609 # Now unmount the filesystem and verify the ciphertext we just wrote.
612 echo "Verifying encrypted file contents" >> $seqres.full
613 for f in "${test_contents_files[@]}"; do
614 read -r src inode blocklist <<< "$f"
615 nonce=$(_get_encryption_nonce $SCRATCH_DEV $inode)
616 _dump_ciphertext_blocks $SCRATCH_DEV $blocklist > $tmp.actual_contents
617 $crypt_cmd $contents_encryption_mode $raw_key_hex \
618 --file-nonce=$nonce --block-size=$blocksize \
619 < $src > $tmp.expected_contents
620 if ! cmp $tmp.expected_contents $tmp.actual_contents; then
621 _fail "Expected encrypted contents != actual encrypted contents. File: $f"
623 $crypt_cmd $contents_encryption_mode $raw_key_hex --decrypt \
624 --file-nonce=$nonce --block-size=$blocksize \
625 < $tmp.actual_contents > $tmp.decrypted_contents
626 if ! cmp $src $tmp.decrypted_contents; then
627 _fail "Contents decryption sanity check failed. File: $f"
631 echo "Verifying encrypted file names" >> $seqres.full
632 for f in "${test_filenames_files[@]}"; do
633 read -r name inode dir_inode padding <<< "$f"
634 nonce=$(_get_encryption_nonce $SCRATCH_DEV $dir_inode)
635 _get_ciphertext_filename $SCRATCH_DEV $inode $dir_inode \
638 $crypt_cmd $filenames_encryption_mode $raw_key_hex \
639 --file-nonce=$nonce --padding=$padding \
640 --block-size=255 > $tmp.expected_name
641 if ! cmp $tmp.expected_name $tmp.actual_name; then
642 _fail "Expected encrypted filename != actual encrypted filename. File: $f"
644 $crypt_cmd $filenames_encryption_mode $raw_key_hex --decrypt \
645 --file-nonce=$nonce --padding=$padding \
646 --block-size=255 < $tmp.actual_name \
647 > $tmp.decrypted_name
648 decrypted_name=$(tr -d '\0' < $tmp.decrypted_name)
649 if [ "$name" != "$decrypted_name" ]; then
650 _fail "Filename decryption sanity check failed ($name != $decrypted_name). File: $f"
655 _fscrypt_mode_name_to_num()
660 AES-256-XTS) echo 1 ;; # FS_ENCRYPTION_MODE_AES_256_XTS
661 AES-256-CTS-CBC) echo 4 ;; # FS_ENCRYPTION_MODE_AES_256_CTS
662 AES-128-CBC-ESSIV) echo 5 ;; # FS_ENCRYPTION_MODE_AES_128_CBC
663 AES-128-CTS-CBC) echo 6 ;; # FS_ENCRYPTION_MODE_AES_128_CTS
664 Adiantum) echo 9 ;; # FS_ENCRYPTION_MODE_ADIANTUM
665 *) _fail "Unknown fscrypt mode: $name" ;;
669 # Verify that file contents and names are encrypted correctly when an encryption
670 # policy of the specified type is used.
672 # The first two parameters are the contents and filenames encryption modes to
673 # test. Optionally, also specify 'direct' to test the DIRECT_KEY flag.
674 _verify_ciphertext_for_encryption_policy()
676 local contents_encryption_mode=$1
677 local filenames_encryption_mode=$2
680 local set_encpolicy_args=""
681 local crypt_util_args=""
687 if [ $contents_encryption_mode != \
688 $filenames_encryption_mode ]; then
689 _fail "For direct key mode, contents and filenames modes must match"
691 (( policy_flags |= 0x04 )) # FS_POLICY_FLAG_DIRECT_KEY
694 _fail "Unknown option '$opt' passed to ${FUNCNAME[0]}"
698 local contents_mode_num=$(_fscrypt_mode_name_to_num $contents_encryption_mode)
699 local filenames_mode_num=$(_fscrypt_mode_name_to_num $filenames_encryption_mode)
701 set_encpolicy_args+=" -c $contents_mode_num"
702 set_encpolicy_args+=" -n $filenames_mode_num"
704 if (( policy_flags & 0x04 )); then
705 crypt_util_args+=" --kdf=none"
707 crypt_util_args+=" --kdf=AES-128-ECB"
709 set_encpolicy_args=${set_encpolicy_args# }
711 _require_scratch_encryption $set_encpolicy_args
712 _require_test_program "fscrypt-crypt-util"
713 _require_xfs_io_command "fiemap"
714 _require_get_encryption_nonce_support
715 _require_get_ciphertext_filename_support
716 _require_command "$KEYCTL_PROG" keyctl
718 echo "Creating encryption-capable filesystem" >> $seqres.full
719 _scratch_mkfs_encrypted &>> $seqres.full
722 echo "Generating encryption key" >> $seqres.full
723 local raw_key=$(_generate_raw_encryption_key)
724 local keydesc=$(_generate_key_descriptor)
726 _add_session_encryption_key $keydesc $raw_key
727 local raw_key_hex=$(echo "$raw_key" | tr -d '\\x')
730 echo -e "Verifying ciphertext with parameters:"
731 echo -e "\tcontents_encryption_mode: $contents_encryption_mode"
732 echo -e "\tfilenames_encryption_mode: $filenames_encryption_mode"
733 [ $# -ne 0 ] && echo -e "\toptions: $*"
735 _do_verify_ciphertext_for_encryption_policy \
736 "$contents_encryption_mode" \
737 "$filenames_encryption_mode" \
739 "$set_encpolicy_args" \