David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | ========= |
| 2 | dm-verity |
| 3 | ========= |
| 4 | |
| 5 | Device-Mapper's "verity" target provides transparent integrity checking of |
| 6 | block devices using a cryptographic digest provided by the kernel crypto API. |
| 7 | This target is read-only. |
| 8 | |
| 9 | Construction Parameters |
| 10 | ======================= |
| 11 | |
| 12 | :: |
| 13 | |
| 14 | <version> <dev> <hash_dev> |
| 15 | <data_block_size> <hash_block_size> |
| 16 | <num_data_blocks> <hash_start_block> |
| 17 | <algorithm> <digest> <salt> |
| 18 | [<#opt_params> <opt_params>] |
| 19 | |
| 20 | <version> |
| 21 | This is the type of the on-disk hash format. |
| 22 | |
| 23 | 0 is the original format used in the Chromium OS. |
| 24 | The salt is appended when hashing, digests are stored continuously and |
| 25 | the rest of the block is padded with zeroes. |
| 26 | |
| 27 | 1 is the current format that should be used for new devices. |
| 28 | The salt is prepended when hashing and each digest is |
| 29 | padded with zeroes to the power of two. |
| 30 | |
| 31 | <dev> |
| 32 | This is the device containing data, the integrity of which needs to be |
| 33 | checked. It may be specified as a path, like /dev/sdaX, or a device number, |
| 34 | <major>:<minor>. |
| 35 | |
| 36 | <hash_dev> |
| 37 | This is the device that supplies the hash tree data. It may be |
| 38 | specified similarly to the device path and may be the same device. If the |
| 39 | same device is used, the hash_start should be outside the configured |
| 40 | dm-verity device. |
| 41 | |
| 42 | <data_block_size> |
| 43 | The block size on a data device in bytes. |
| 44 | Each block corresponds to one digest on the hash device. |
| 45 | |
| 46 | <hash_block_size> |
| 47 | The size of a hash block in bytes. |
| 48 | |
| 49 | <num_data_blocks> |
| 50 | The number of data blocks on the data device. Additional blocks are |
| 51 | inaccessible. You can place hashes to the same partition as data, in this |
| 52 | case hashes are placed after <num_data_blocks>. |
| 53 | |
| 54 | <hash_start_block> |
| 55 | This is the offset, in <hash_block_size>-blocks, from the start of hash_dev |
| 56 | to the root block of the hash tree. |
| 57 | |
| 58 | <algorithm> |
| 59 | The cryptographic hash algorithm used for this device. This should |
| 60 | be the name of the algorithm, like "sha1". |
| 61 | |
| 62 | <digest> |
| 63 | The hexadecimal encoding of the cryptographic hash of the root hash block |
| 64 | and the salt. This hash should be trusted as there is no other authenticity |
| 65 | beyond this point. |
| 66 | |
| 67 | <salt> |
| 68 | The hexadecimal encoding of the salt value. |
| 69 | |
| 70 | <#opt_params> |
| 71 | Number of optional parameters. If there are no optional parameters, |
| 72 | the optional paramaters section can be skipped or #opt_params can be zero. |
| 73 | Otherwise #opt_params is the number of following arguments. |
| 74 | |
| 75 | Example of optional parameters section: |
| 76 | 1 ignore_corruption |
| 77 | |
| 78 | ignore_corruption |
| 79 | Log corrupted blocks, but allow read operations to proceed normally. |
| 80 | |
| 81 | restart_on_corruption |
| 82 | Restart the system when a corrupted block is discovered. This option is |
| 83 | not compatible with ignore_corruption and requires user space support to |
| 84 | avoid restart loops. |
| 85 | |
| 86 | ignore_zero_blocks |
| 87 | Do not verify blocks that are expected to contain zeroes and always return |
| 88 | zeroes instead. This may be useful if the partition contains unused blocks |
| 89 | that are not guaranteed to contain zeroes. |
| 90 | |
| 91 | use_fec_from_device <fec_dev> |
| 92 | Use forward error correction (FEC) to recover from corruption if hash |
| 93 | verification fails. Use encoding data from the specified device. This |
| 94 | may be the same device where data and hash blocks reside, in which case |
| 95 | fec_start must be outside data and hash areas. |
| 96 | |
| 97 | If the encoding data covers additional metadata, it must be accessible |
| 98 | on the hash device after the hash blocks. |
| 99 | |
| 100 | Note: block sizes for data and hash devices must match. Also, if the |
| 101 | verity <dev> is encrypted the <fec_dev> should be too. |
| 102 | |
| 103 | fec_roots <num> |
| 104 | Number of generator roots. This equals to the number of parity bytes in |
| 105 | the encoding data. For example, in RS(M, N) encoding, the number of roots |
| 106 | is M-N. |
| 107 | |
| 108 | fec_blocks <num> |
| 109 | The number of encoding data blocks on the FEC device. The block size for |
| 110 | the FEC device is <data_block_size>. |
| 111 | |
| 112 | fec_start <offset> |
| 113 | This is the offset, in <data_block_size> blocks, from the start of the |
| 114 | FEC device to the beginning of the encoding data. |
| 115 | |
| 116 | check_at_most_once |
| 117 | Verify data blocks only the first time they are read from the data device, |
| 118 | rather than every time. This reduces the overhead of dm-verity so that it |
| 119 | can be used on systems that are memory and/or CPU constrained. However, it |
| 120 | provides a reduced level of security because only offline tampering of the |
| 121 | data device's content will be detected, not online tampering. |
| 122 | |
| 123 | Hash blocks are still verified each time they are read from the hash device, |
| 124 | since verification of hash blocks is less performance critical than data |
| 125 | blocks, and a hash block will not be verified any more after all the data |
| 126 | blocks it covers have been verified anyway. |
| 127 | |
| 128 | root_hash_sig_key_desc <key_description> |
| 129 | This is the description of the USER_KEY that the kernel will lookup to get |
| 130 | the pkcs7 signature of the roothash. The pkcs7 signature is used to validate |
| 131 | the root hash during the creation of the device mapper block device. |
| 132 | Verification of roothash depends on the config DM_VERITY_VERIFY_ROOTHASH_SIG |
| 133 | being set in the kernel. |
| 134 | |
| 135 | Theory of operation |
| 136 | =================== |
| 137 | |
| 138 | dm-verity is meant to be set up as part of a verified boot path. This |
| 139 | may be anything ranging from a boot using tboot or trustedgrub to just |
| 140 | booting from a known-good device (like a USB drive or CD). |
| 141 | |
| 142 | When a dm-verity device is configured, it is expected that the caller |
| 143 | has been authenticated in some way (cryptographic signatures, etc). |
| 144 | After instantiation, all hashes will be verified on-demand during |
| 145 | disk access. If they cannot be verified up to the root node of the |
| 146 | tree, the root hash, then the I/O will fail. This should detect |
| 147 | tampering with any data on the device and the hash data. |
| 148 | |
| 149 | Cryptographic hashes are used to assert the integrity of the device on a |
| 150 | per-block basis. This allows for a lightweight hash computation on first read |
| 151 | into the page cache. Block hashes are stored linearly, aligned to the nearest |
| 152 | block size. |
| 153 | |
| 154 | If forward error correction (FEC) support is enabled any recovery of |
| 155 | corrupted data will be verified using the cryptographic hash of the |
| 156 | corresponding data. This is why combining error correction with |
| 157 | integrity checking is essential. |
| 158 | |
| 159 | Hash Tree |
| 160 | --------- |
| 161 | |
| 162 | Each node in the tree is a cryptographic hash. If it is a leaf node, the hash |
| 163 | of some data block on disk is calculated. If it is an intermediary node, |
| 164 | the hash of a number of child nodes is calculated. |
| 165 | |
| 166 | Each entry in the tree is a collection of neighboring nodes that fit in one |
| 167 | block. The number is determined based on block_size and the size of the |
| 168 | selected cryptographic digest algorithm. The hashes are linearly-ordered in |
| 169 | this entry and any unaligned trailing space is ignored but included when |
| 170 | calculating the parent node. |
| 171 | |
| 172 | The tree looks something like: |
| 173 | |
| 174 | alg = sha256, num_blocks = 32768, block_size = 4096 |
| 175 | |
| 176 | :: |
| 177 | |
| 178 | [ root ] |
| 179 | / . . . \ |
| 180 | [entry_0] [entry_1] |
| 181 | / . . . \ . . . \ |
| 182 | [entry_0_0] . . . [entry_0_127] . . . . [entry_1_127] |
| 183 | / ... \ / . . . \ / \ |
| 184 | blk_0 ... blk_127 blk_16256 blk_16383 blk_32640 . . . blk_32767 |
| 185 | |
| 186 | |
| 187 | On-disk format |
| 188 | ============== |
| 189 | |
| 190 | The verity kernel code does not read the verity metadata on-disk header. |
| 191 | It only reads the hash blocks which directly follow the header. |
| 192 | It is expected that a user-space tool will verify the integrity of the |
| 193 | verity header. |
| 194 | |
| 195 | Alternatively, the header can be omitted and the dmsetup parameters can |
| 196 | be passed via the kernel command-line in a rooted chain of trust where |
| 197 | the command-line is verified. |
| 198 | |
| 199 | Directly following the header (and with sector number padded to the next hash |
| 200 | block boundary) are the hash blocks which are stored a depth at a time |
| 201 | (starting from the root), sorted in order of increasing index. |
| 202 | |
| 203 | The full specification of kernel parameters and on-disk metadata format |
| 204 | is available at the cryptsetup project's wiki page |
| 205 | |
| 206 | https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity |
| 207 | |
| 208 | Status |
| 209 | ====== |
| 210 | V (for Valid) is returned if every check performed so far was valid. |
| 211 | If any check failed, C (for Corruption) is returned. |
| 212 | |
| 213 | Example |
| 214 | ======= |
| 215 | Set up a device:: |
| 216 | |
| 217 | # dmsetup create vroot --readonly --table \ |
| 218 | "0 2097152 verity 1 /dev/sda1 /dev/sda2 4096 4096 262144 1 sha256 "\ |
| 219 | "4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 "\ |
| 220 | "1234000000000000000000000000000000000000000000000000000000000000" |
| 221 | |
| 222 | A command line tool veritysetup is available to compute or verify |
| 223 | the hash tree or activate the kernel device. This is available from |
| 224 | the cryptsetup upstream repository https://gitlab.com/cryptsetup/cryptsetup/ |
| 225 | (as a libcryptsetup extension). |
| 226 | |
| 227 | Create hash on the device:: |
| 228 | |
| 229 | # veritysetup format /dev/sda1 /dev/sda2 |
| 230 | ... |
| 231 | Root hash: 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 |
| 232 | |
| 233 | Activate the device:: |
| 234 | |
| 235 | # veritysetup create vroot /dev/sda1 /dev/sda2 \ |
| 236 | 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 |