blob: 045c86011bca70a050912af29c3a88730725dd3c [file] [log] [blame]
Andrew Geissler1fe918a2020-05-15 14:16:47 -05001# SPDX-License-Identifier: MIT
2#
3# Copyright (C) 2020 BayLibre SAS
4# Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
5#
6# This bbclass allows creating of dm-verity protected partition images. It
7# generates a device image file with dm-verity hash data appended at the end
8# plus the corresponding .env file containing additional information needed
9# to mount the image such as the root hash in the form of ell variables. To
10# assure data integrity, the root hash must be stored in a trusted location
11# or cryptographically signed and verified.
12#
Patrick Williams520786c2023-06-25 16:20:36 -050013# Optionally, we can store the hash data on a separate device or partition
14# for improved compartmentalization and ease of use/deployment.
15#
Andrew Geissler1fe918a2020-05-15 14:16:47 -050016# Usage:
17# DM_VERITY_IMAGE = "core-image-full-cmdline" # or other image
18# DM_VERITY_IMAGE_TYPE = "ext4" # or ext2, ext3 & btrfs
Patrick Williams520786c2023-06-25 16:20:36 -050019# DM_VERITY_SEPARATE_HASH = "1" # optional; store hash on separate dev
Andrew Geissler1fe918a2020-05-15 14:16:47 -050020# IMAGE_CLASSES += "dm-verity-img"
21#
Patrick Williams520786c2023-06-25 16:20:36 -050022# Using the GPT UUIDs specified in the standard can also be useful in that
23# they are displayed and translated in cfdisk output.
24#
25# DM_VERITY_ROOT_GUID = <UUID for your architecture and root-fs>
26# DM_VERITY_RHASH_GUID = <UUID for your architecture and verity-hash>
27# https://uapi-group.org/specifications/specs/discoverable_partitions_specification/
28
Andrew Geissler1fe918a2020-05-15 14:16:47 -050029# The resulting image can then be used to implement the device mapper block
30# integrity checking on the target device.
31
Andrew Geisslercc589282020-09-18 13:34:40 -050032# Define the location where the DM_VERITY_IMAGE specific dm-verity root hash
33# is stored where it can be installed into associated initramfs rootfs.
34STAGING_VERITY_DIR ?= "${TMPDIR}/work-shared/${MACHINE}/dm-verity"
35
Andrew Geissler5199d832021-09-24 16:47:35 -050036# Define the data block size to use in veritysetup.
37DM_VERITY_IMAGE_DATA_BLOCK_SIZE ?= "1024"
38
Andrew Geissler2daf84b2023-03-31 09:57:23 -050039# Define the hash block size to use in veritysetup.
40DM_VERITY_IMAGE_HASH_BLOCK_SIZE ?= "4096"
41
Patrick Williams520786c2023-06-25 16:20:36 -050042# Should we store the hash data on a separate device/partition?
43DM_VERITY_SEPARATE_HASH ?= "0"
44
45# These are arch specific. We could probably intelligently auto-assign these?
46# Take x86-64 values as defaults. No impact on functionality currently.
47# See SD_GPT_ROOT_X86_64 and SD_GPT_ROOT_X86_64_VERITY in the spec.
48# Note - these are passed directly to sgdisk so hyphens needed.
49DM_VERITY_ROOT_GUID ?= "4f68bce3-e8cd-4db1-96e7-fbcaf984b709"
50DM_VERITY_RHASH_GUID ?= "2c7357ed-ebd2-46d9-aec1-23d437ec2bf5"
51
Andrew Geissler1fe918a2020-05-15 14:16:47 -050052# Process the output from veritysetup and generate the corresponding .env
53# file. The output from veritysetup is not very machine-friendly so we need to
54# convert it to some better format. Let's drop the first line (doesn't contain
55# any useful info) and feed the rest to a script.
56process_verity() {
Andrew Geisslercc589282020-09-18 13:34:40 -050057 local ENV="${STAGING_VERITY_DIR}/${IMAGE_BASENAME}.$TYPE.verity.env"
Patrick Williams520786c2023-06-25 16:20:36 -050058 local WKS_INC="${STAGING_VERITY_DIR}/${IMAGE_BASENAME}.$TYPE.wks.in"
Andrew Geisslercc589282020-09-18 13:34:40 -050059 rm -f $ENV
Andrew Geissler1fe918a2020-05-15 14:16:47 -050060
61 # Each line contains a key and a value string delimited by ':'. Read the
62 # two parts into separate variables and process them separately. For the
63 # key part: convert the names to upper case and replace spaces with
64 # underscores to create correct shell variable names. For the value part:
65 # just trim all white-spaces.
66 IFS=":"
67 while read KEY VAL; do
Andrew Geisslercc589282020-09-18 13:34:40 -050068 printf '%s=%s\n' \
69 "$(echo "$KEY" | tr '[:lower:]' '[:upper:]' | sed 's/ /_/g')" \
70 "$(echo "$VAL" | tr -d ' \t')" >> $ENV
Andrew Geissler1fe918a2020-05-15 14:16:47 -050071 done
72
73 # Add partition size
74 echo "DATA_SIZE=$SIZE" >> $ENV
Patrick Williams520786c2023-06-25 16:20:36 -050075
76 # Add whether we are storing the hash data separately
77 echo "SEPARATE_HASH=${DM_VERITY_SEPARATE_HASH}" >> $ENV
78
79 # Configured for single partition use of veritysetup? OK, we are done.
80 if [ ${DM_VERITY_SEPARATE_HASH} -eq 0 ]; then
81 return
82 fi
83
84 # Craft up the UUIDs that are part of the verity standard for root & hash
85 # while we are here and in shell. Re-read our output to get ROOT_HASH
86 # and then cut it in 1/2 ; HI for data UUID and LO for hash-data UUID.
87 # https://uapi-group.org/specifications/specs/discoverable_partitions_specification/
88
89 ROOT_HASH=$(cat $ENV | grep ^ROOT_HASH | sed 's/ROOT_HASH=//' | tr a-f A-F)
90 ROOT_HI=$(echo "obase=16;ibase=16;$ROOT_HASH/2^80" | /usr/bin/bc)
91 ROOT_LO=$(echo "obase=16;ibase=16;$ROOT_HASH%2^80" | /usr/bin/bc)
92
93 # Hyphenate as per UUID spec and as expected by wic+sgdisk parameters.
94 # Prefix with leading zeros, in case hash chunks weren't using highest bits
95 # "bc" needs upper case, /dev/disk/by-partuuid/ is lower case. <sigh>
96 ROOT_UUID=$(echo 00000000$ROOT_HI | sed 's/.*\(.\{32\}\)$/\1/' | \
97 sed 's/./-&/9;s/./-&/14;s/./-&/19;s/./-&/24' | tr A-F a-f )
98 RHASH_UUID=$(echo 00000000$ROOT_LO | sed 's/.*\(.\{32\}\)$/\1/' | \
99 sed 's/./-&/9;s/./-&/14;s/./-&/19;s/./-&/24' | tr A-F a-f )
100
101 # Emit the values needed for a veritysetup run in the initramfs
102 echo "ROOT_UUID=$ROOT_UUID" >> $ENV
103 echo "RHASH_UUID=$RHASH_UUID" >> $ENV
104
105 # Create wks.in fragment with build specific UUIDs for partitions.
106 # Unfortunately the wks.in does not support line continuations...
107 # First, the unappended filesystem data partition.
108 echo 'part / --source rawcopy --ondisk sda --sourceparams="file=${IMGDEPLOYDIR}/${DM_VERITY_IMAGE}-${MACHINE}.${DM_VERITY_IMAGE_TYPE}.verity" --part-name verityroot --part-type="${DM_VERITY_ROOT_GUID}"'" --uuid=\"$ROOT_UUID\"" > $WKS_INC
109
110 # note: no default mount point for hash data partition
111 echo 'part --source rawcopy --ondisk sda --sourceparams="file=${IMGDEPLOYDIR}/${DM_VERITY_IMAGE}-${MACHINE}.${DM_VERITY_IMAGE_TYPE}.vhash" --part-name verityhash --part-type="${DM_VERITY_RHASH_GUID}"'" --uuid=\"$RHASH_UUID\"" >> $WKS_INC
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500112}
113
114verity_setup() {
115 local TYPE=$1
116 local INPUT=${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.$TYPE
117 local SIZE=$(stat --printf="%s" $INPUT)
118 local OUTPUT=$INPUT.verity
Patrick Williams520786c2023-06-25 16:20:36 -0500119 local OUTPUT_HASH=$INPUT.verity
120 local HASH_OFFSET=""
121 local SETUP_ARGS=""
122 local SAVED_ARGS="${STAGING_VERITY_DIR}/${IMAGE_BASENAME}.$TYPE.verity.args"
123
124 install -d ${STAGING_VERITY_DIR}
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500125
Andrew Geissler2daf84b2023-03-31 09:57:23 -0500126 if [ ${DM_VERITY_IMAGE_DATA_BLOCK_SIZE} -ge ${DM_VERITY_IMAGE_HASH_BLOCK_SIZE} ]; then
127 align=${DM_VERITY_IMAGE_DATA_BLOCK_SIZE}
128 else
129 align=${DM_VERITY_IMAGE_HASH_BLOCK_SIZE}
130 fi
131 SIZE=$(expr \( $SIZE + $align - 1 \) / $align \* $align)
132
Patrick Williams520786c2023-06-25 16:20:36 -0500133 # Assume some users may want separate hash vs. appended hash
134 if [ ${DM_VERITY_SEPARATE_HASH} -eq 1 ]; then
135 OUTPUT_HASH=$INPUT.vhash
136 else
137 HASH_OFFSET="--hash-offset="$SIZE
138 fi
139
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500140 cp -a $INPUT $OUTPUT
141
Patrick Williams520786c2023-06-25 16:20:36 -0500142 SETUP_ARGS=" \
143 --data-block-size=${DM_VERITY_IMAGE_DATA_BLOCK_SIZE} \
144 --hash-block-size=${DM_VERITY_IMAGE_HASH_BLOCK_SIZE} \
145 $HASH_OFFSET format $OUTPUT $OUTPUT_HASH \
146 "
147
148 echo "veritysetup $SETUP_ARGS" > $SAVED_ARGS
149
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500150 # Let's drop the first line of output (doesn't contain any useful info)
151 # and feed the rest to another function.
Patrick Williams520786c2023-06-25 16:20:36 -0500152 veritysetup $SETUP_ARGS | tail -n +2 | process_verity
153}
154
155# make "dateless" symlink for the hash so the wks can find it.
156verity_hash() {
157 cd ${IMGDEPLOYDIR}
158 ln -sf ${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.${DM_VERITY_IMAGE_TYPE}.vhash \
159 ${IMAGE_BASENAME}-${MACHINE}.${DM_VERITY_IMAGE_TYPE}.vhash
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500160}
161
Andrew Geissler9347dd42023-03-03 12:38:41 -0600162VERITY_TYPES = " \
163 ext2.verity ext3.verity ext4.verity \
164 btrfs.verity \
165 erofs.verity erofs-lz4.verity erofs-lz4hc.verity \
166 squashfs.verity squashfs-xz.verity squashfs-lzo.verity squashfs-lz4.verity squashfs-zst.verity \
167"
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500168IMAGE_TYPES += "${VERITY_TYPES}"
169CONVERSIONTYPES += "verity"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500170CONVERSION_CMD:verity = "verity_setup ${type}"
Patrick Williams53961c22022-01-20 11:06:23 -0600171CONVERSION_DEPENDS_verity = "cryptsetup-native"
Patrick Williams520786c2023-06-25 16:20:36 -0500172IMAGE_CMD:vhash = "verity_hash"
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500173
174python __anonymous() {
175 verity_image = d.getVar('DM_VERITY_IMAGE')
176 verity_type = d.getVar('DM_VERITY_IMAGE_TYPE')
Patrick Williams520786c2023-06-25 16:20:36 -0500177 verity_hash = d.getVar('DM_VERITY_SEPARATE_HASH')
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500178 image_fstypes = d.getVar('IMAGE_FSTYPES')
179 pn = d.getVar('PN')
180
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500181 if not verity_image or not verity_type:
182 bb.warn('dm-verity-img class inherited but not used')
183 return
184
Andrew Geisslercc589282020-09-18 13:34:40 -0500185 if verity_image != pn:
186 return # This doesn't concern this image
187
Andrew Geissler2daf84b2023-03-31 09:57:23 -0500188 if len(verity_type.split()) != 1:
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500189 bb.fatal('DM_VERITY_IMAGE_TYPE must contain exactly one type')
190
191 d.appendVar('IMAGE_FSTYPES', ' %s.verity' % verity_type)
Patrick Williams520786c2023-06-25 16:20:36 -0500192 if verity_hash == "1":
193 d.appendVar('IMAGE_FSTYPES', ' vhash')
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500194
195 # If we're using wic: we'll have to use partition images and not the rootfs
196 # source plugin so add the appropriate dependency.
197 if 'wic' in image_fstypes:
Andrew Geisslerd5838332022-05-27 11:33:10 -0500198 dep = ' %s:do_image_%s' % (pn, verity_type.replace("-", "_"))
Andrew Geissler1fe918a2020-05-15 14:16:47 -0500199 d.appendVarFlag('do_image_wic', 'depends', dep)
200}