Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 1 | # 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 Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 13 | # Optionally, we can store the hash data on a separate device or partition |
| 14 | # for improved compartmentalization and ease of use/deployment. |
| 15 | # |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 16 | # Usage: |
| 17 | # DM_VERITY_IMAGE = "core-image-full-cmdline" # or other image |
| 18 | # DM_VERITY_IMAGE_TYPE = "ext4" # or ext2, ext3 & btrfs |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 19 | # DM_VERITY_SEPARATE_HASH = "1" # optional; store hash on separate dev |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 20 | # IMAGE_CLASSES += "dm-verity-img" |
| 21 | # |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 22 | # 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 Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 29 | # The resulting image can then be used to implement the device mapper block |
| 30 | # integrity checking on the target device. |
| 31 | |
Andrew Geissler | cc58928 | 2020-09-18 13:34:40 -0500 | [diff] [blame] | 32 | # 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. |
| 34 | STAGING_VERITY_DIR ?= "${TMPDIR}/work-shared/${MACHINE}/dm-verity" |
| 35 | |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 36 | # Define the data block size to use in veritysetup. |
| 37 | DM_VERITY_IMAGE_DATA_BLOCK_SIZE ?= "1024" |
| 38 | |
Andrew Geissler | 2daf84b | 2023-03-31 09:57:23 -0500 | [diff] [blame] | 39 | # Define the hash block size to use in veritysetup. |
| 40 | DM_VERITY_IMAGE_HASH_BLOCK_SIZE ?= "4096" |
| 41 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 42 | # Should we store the hash data on a separate device/partition? |
| 43 | DM_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. |
| 49 | DM_VERITY_ROOT_GUID ?= "4f68bce3-e8cd-4db1-96e7-fbcaf984b709" |
| 50 | DM_VERITY_RHASH_GUID ?= "2c7357ed-ebd2-46d9-aec1-23d437ec2bf5" |
| 51 | |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 52 | # 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. |
| 56 | process_verity() { |
Andrew Geissler | cc58928 | 2020-09-18 13:34:40 -0500 | [diff] [blame] | 57 | local ENV="${STAGING_VERITY_DIR}/${IMAGE_BASENAME}.$TYPE.verity.env" |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 58 | local WKS_INC="${STAGING_VERITY_DIR}/${IMAGE_BASENAME}.$TYPE.wks.in" |
Andrew Geissler | cc58928 | 2020-09-18 13:34:40 -0500 | [diff] [blame] | 59 | rm -f $ENV |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 60 | |
| 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 Geissler | cc58928 | 2020-09-18 13:34:40 -0500 | [diff] [blame] | 68 | printf '%s=%s\n' \ |
| 69 | "$(echo "$KEY" | tr '[:lower:]' '[:upper:]' | sed 's/ /_/g')" \ |
| 70 | "$(echo "$VAL" | tr -d ' \t')" >> $ENV |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 71 | done |
| 72 | |
| 73 | # Add partition size |
| 74 | echo "DATA_SIZE=$SIZE" >> $ENV |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 75 | |
| 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 Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | verity_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 Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 119 | 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 Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 125 | |
Andrew Geissler | 2daf84b | 2023-03-31 09:57:23 -0500 | [diff] [blame] | 126 | 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 Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 133 | # 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 Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 140 | cp -a $INPUT $OUTPUT |
| 141 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 142 | 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 Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 150 | # Let's drop the first line of output (doesn't contain any useful info) |
| 151 | # and feed the rest to another function. |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 152 | veritysetup $SETUP_ARGS | tail -n +2 | process_verity |
| 153 | } |
| 154 | |
| 155 | # make "dateless" symlink for the hash so the wks can find it. |
| 156 | verity_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 Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Andrew Geissler | 9347dd4 | 2023-03-03 12:38:41 -0600 | [diff] [blame] | 162 | VERITY_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 Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 168 | IMAGE_TYPES += "${VERITY_TYPES}" |
| 169 | CONVERSIONTYPES += "verity" |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 170 | CONVERSION_CMD:verity = "verity_setup ${type}" |
Patrick Williams | 53961c2 | 2022-01-20 11:06:23 -0600 | [diff] [blame] | 171 | CONVERSION_DEPENDS_verity = "cryptsetup-native" |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 172 | IMAGE_CMD:vhash = "verity_hash" |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 173 | |
| 174 | python __anonymous() { |
| 175 | verity_image = d.getVar('DM_VERITY_IMAGE') |
| 176 | verity_type = d.getVar('DM_VERITY_IMAGE_TYPE') |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 177 | verity_hash = d.getVar('DM_VERITY_SEPARATE_HASH') |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 178 | image_fstypes = d.getVar('IMAGE_FSTYPES') |
| 179 | pn = d.getVar('PN') |
| 180 | |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 181 | if not verity_image or not verity_type: |
| 182 | bb.warn('dm-verity-img class inherited but not used') |
| 183 | return |
| 184 | |
Andrew Geissler | cc58928 | 2020-09-18 13:34:40 -0500 | [diff] [blame] | 185 | if verity_image != pn: |
| 186 | return # This doesn't concern this image |
| 187 | |
Andrew Geissler | 2daf84b | 2023-03-31 09:57:23 -0500 | [diff] [blame] | 188 | if len(verity_type.split()) != 1: |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 189 | bb.fatal('DM_VERITY_IMAGE_TYPE must contain exactly one type') |
| 190 | |
| 191 | d.appendVar('IMAGE_FSTYPES', ' %s.verity' % verity_type) |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 192 | if verity_hash == "1": |
| 193 | d.appendVar('IMAGE_FSTYPES', ' vhash') |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 194 | |
| 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 Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 198 | dep = ' %s:do_image_%s' % (pn, verity_type.replace("-", "_")) |
Andrew Geissler | 1fe918a | 2020-05-15 14:16:47 -0500 | [diff] [blame] | 199 | d.appendVarFlag('do_image_wic', 'depends', dep) |
| 200 | } |