1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright 2018 Google LLC
4 # Functions for setting up and testing fs-verity
6 _require_scratch_verity()
9 _require_command "$FSVERITY_PROG" fsverity
11 if ! _scratch_mkfs_verity &>>$seqres.full; then
12 # ext4: need e2fsprogs v1.44.5 or later (but actually v1.45.2+
13 # is needed for some tests to pass, due to an e2fsck bug)
14 # f2fs: need f2fs-tools v1.11.0 or later
15 _notrun "$FSTYP userspace tools don't support fs-verity"
18 # Try to mount the filesystem. If this fails then either the kernel
19 # isn't aware of fs-verity, or the mkfs options were not compatible with
20 # verity (e.g. ext4 with block size != PAGE_SIZE).
21 if ! _try_scratch_mount &>>$seqres.full; then
22 _notrun "kernel is unaware of $FSTYP verity feature," \
23 "or mkfs options are not compatible with verity"
26 # The filesystem may be aware of fs-verity but have it disabled by
27 # CONFIG_FS_VERITY=n. Detect support via sysfs.
28 if [ ! -e /sys/fs/$FSTYP/features/verity ]; then
29 _notrun "kernel $FSTYP isn't configured with verity support"
32 # The filesystem may have fs-verity enabled but not actually usable by
33 # default. E.g., ext4 only supports verity on extent-based files, so it
34 # doesn't work on ext3-style filesystems. So, try actually using it.
35 echo foo > $SCRATCH_MNT/tmpfile
36 _disable_fsverity_signatures
37 if ! _fsv_enable $SCRATCH_MNT/tmpfile; then
38 _restore_fsverity_signatures
39 _notrun "$FSTYP verity isn't usable by default with these mkfs options"
41 _restore_fsverity_signatures
42 rm -f $SCRATCH_MNT/tmpfile
46 # Merkle tree block size. Currently all filesystems only support
47 # PAGE_SIZE for this. This is also the default for 'fsverity enable'.
48 FSV_BLOCK_SIZE=$(get_page_size)
51 # Check for CONFIG_FS_VERITY_BUILTIN_SIGNATURES=y.
52 _require_fsverity_builtin_signatures()
54 if [ ! -e /proc/sys/fs/verity/require_signatures ]; then
55 _notrun "kernel doesn't support fs-verity builtin signatures"
59 # Disable mandatory signatures for fs-verity files, if they are supported.
60 _disable_fsverity_signatures()
62 if [ -e /proc/sys/fs/verity/require_signatures ]; then
63 if [ -z "$FSVERITY_SIG_CTL_ORIG" ]; then
64 FSVERITY_SIG_CTL_ORIG=$(</proc/sys/fs/verity/require_signatures)
66 echo 0 > /proc/sys/fs/verity/require_signatures
70 # Enable mandatory signatures for fs-verity files.
71 # This assumes that _require_fsverity_builtin_signatures() was called.
72 _enable_fsverity_signatures()
74 if [ -z "$FSVERITY_SIG_CTL_ORIG" ]; then
75 FSVERITY_SIG_CTL_ORIG=$(</proc/sys/fs/verity/require_signatures)
77 echo 1 > /proc/sys/fs/verity/require_signatures
80 # Restore the original signature verification setting.
81 _restore_fsverity_signatures()
83 if [ -n "$FSVERITY_SIG_CTL_ORIG" ]; then
84 echo "$FSVERITY_SIG_CTL_ORIG" > /proc/sys/fs/verity/require_signatures
88 _scratch_mkfs_verity()
92 _scratch_mkfs -O verity
95 _notrun "No verity support for $FSTYP"
100 _scratch_mkfs_encrypted_verity()
104 _scratch_mkfs -O encrypt,verity
107 # f2fs-tools as of v1.11.0 doesn't allow comma-separated
108 # features with -O. Instead -O must be supplied multiple times.
109 _scratch_mkfs -O encrypt -O verity
112 _notrun "$FSTYP not supported in _scratch_mkfs_encrypted_verity"
117 _fsv_scratch_begin_subtest()
121 rm -rf "${SCRATCH_MNT:?}"/*
127 $FSVERITY_PROG enable "$@"
132 $FSVERITY_PROG measure "$@" | awk '{print $1}'
137 $FSVERITY_PROG sign "$@"
140 # Generate a file, then enable verity on it.
141 _fsv_create_enable_file()
146 head -c $((FSV_BLOCK_SIZE * 2)) /dev/zero > "$file"
147 _fsv_enable "$file" "$@"
150 _fsv_have_hash_algorithm()
156 head -c 4096 /dev/zero > $test_file
157 if ! _fsv_enable --hash-alg=$hash_alg $test_file &>> $seqres.full; then
166 # _fsv_scratch_corrupt_bytes - Write some bytes to a file, bypassing the filesystem
168 # Write the bytes sent on stdin to the given offset in the given file, but do so
169 # by writing directly to the extents on the block device, with the filesystem
170 # unmounted. This can be used to corrupt a verity file for testing purposes,
171 # bypassing the restrictions imposed by the filesystem.
173 # The file is assumed to be located on $SCRATCH_DEV.
175 _fsv_scratch_corrupt_bytes()
179 local lstart lend pstart pend
183 sync # Sync to avoid unwritten extents
186 local end=$(( offset + $(_get_filesize $tmp.bytes ) ))
188 # For each extent that intersects the requested range in order, add a
189 # command that writes the next part of the data to that extent.
190 while read -r lstart lend pstart pend; do
191 lstart=$((lstart * 512))
192 lend=$(((lend + 1) * 512))
193 pstart=$((pstart * 512))
194 pend=$(((pend + 1) * 512))
196 if (( lend - lstart != pend - pstart )); then
197 _fail "Logical and physical extent lengths differ for file '$file'"
198 elif (( offset < lstart )); then
199 _fail "Hole in file '$file' at byte $offset. Next extent begins at byte $lstart"
200 elif (( offset < lend )); then
201 local len=$((lend - offset))
202 local seek=$((pstart + (offset - lstart)))
203 dd_cmds+=("head -c $len | dd of=$SCRATCH_DEV oflag=seek_bytes seek=$seek status=none")
206 done < <($XFS_IO_PROG -r -c "fiemap $offset $((end - offset))" "$file" \
207 | _filter_xfs_io_fiemap)
209 if (( offset < end )); then
210 _fail "Extents of file '$file' ended at byte $offset, but needed until $end"
213 # Execute the commands to write the data
215 for cmd in "${dd_cmds[@]}"; do
218 sync # Sync to flush the block device's pagecache
223 # _fsv_scratch_corrupt_merkle_tree - Corrupt a file's Merkle tree
225 # Like _fsv_scratch_corrupt_bytes(), but this corrupts the file's fs-verity
226 # Merkle tree. The offset is given as a byte offset into the Merkle tree.
228 _fsv_scratch_corrupt_merkle_tree()
235 # ext4 and f2fs store the Merkle tree after the file contents
236 # itself, starting at the next 65536-byte aligned boundary.
237 (( offset += ($(_get_filesize $file) + 65535) & ~65535 ))
238 _fsv_scratch_corrupt_bytes $file $offset
241 _fail "_fsv_scratch_corrupt_merkle_tree() unimplemented on $FSTYP"