Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 1 | dm-verity |
| 2 | ========== |
| 3 | |
| 4 | Device-Mapper's "verity" target provides transparent integrity checking of |
| 5 | block devices using a cryptographic digest provided by the kernel crypto API. |
| 6 | This target is read-only. |
| 7 | |
| 8 | Construction Parameters |
| 9 | ======================= |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 10 | <version> <dev> <hash_dev> |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 11 | <data_block_size> <hash_block_size> |
| 12 | <num_data_blocks> <hash_start_block> |
| 13 | <algorithm> <digest> <salt> |
Sami Tolvanen | 65ff5b7 | 2015-03-18 15:52:14 +0000 | [diff] [blame] | 14 | [<#opt_params> <opt_params>] |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 15 | |
| 16 | <version> |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 17 | This is the type of the on-disk hash format. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 18 | |
| 19 | 0 is the original format used in the Chromium OS. |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 20 | The salt is appended when hashing, digests are stored continuously and |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 21 | the rest of the block is padded with zeroes. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 22 | |
| 23 | 1 is the current format that should be used for new devices. |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 24 | The salt is prepended when hashing and each digest is |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 25 | padded with zeroes to the power of two. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 26 | |
| 27 | <dev> |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 28 | This is the device containing data, the integrity of which needs to be |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 29 | checked. It may be specified as a path, like /dev/sdaX, or a device number, |
| 30 | <major>:<minor>. |
| 31 | |
| 32 | <hash_dev> |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 33 | This is the device that supplies the hash tree data. It may be |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 34 | specified similarly to the device path and may be the same device. If the |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 35 | same device is used, the hash_start should be outside the configured |
| 36 | dm-verity device. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 37 | |
| 38 | <data_block_size> |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 39 | The block size on a data device in bytes. |
| 40 | Each block corresponds to one digest on the hash device. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 41 | |
| 42 | <hash_block_size> |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 43 | The size of a hash block in bytes. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 44 | |
| 45 | <num_data_blocks> |
| 46 | The number of data blocks on the data device. Additional blocks are |
| 47 | inaccessible. You can place hashes to the same partition as data, in this |
| 48 | case hashes are placed after <num_data_blocks>. |
| 49 | |
| 50 | <hash_start_block> |
| 51 | This is the offset, in <hash_block_size>-blocks, from the start of hash_dev |
| 52 | to the root block of the hash tree. |
| 53 | |
| 54 | <algorithm> |
| 55 | The cryptographic hash algorithm used for this device. This should |
| 56 | be the name of the algorithm, like "sha1". |
| 57 | |
| 58 | <digest> |
| 59 | The hexadecimal encoding of the cryptographic hash of the root hash block |
| 60 | and the salt. This hash should be trusted as there is no other authenticity |
| 61 | beyond this point. |
| 62 | |
| 63 | <salt> |
| 64 | The hexadecimal encoding of the salt value. |
| 65 | |
Sami Tolvanen | 65ff5b7 | 2015-03-18 15:52:14 +0000 | [diff] [blame] | 66 | <#opt_params> |
| 67 | Number of optional parameters. If there are no optional parameters, |
| 68 | the optional paramaters section can be skipped or #opt_params can be zero. |
| 69 | Otherwise #opt_params is the number of following arguments. |
| 70 | |
| 71 | Example of optional parameters section: |
| 72 | 1 ignore_corruption |
| 73 | |
Neeraj Upadhyay | 9677b54 | 2018-09-14 09:20:39 +0530 | [diff] [blame^] | 74 | device_wait |
| 75 | Wait (indefinitely) for target devices to show up. Useful for devices |
| 76 | that are detected asynchronously. |
| 77 | |
Sami Tolvanen | 65ff5b7 | 2015-03-18 15:52:14 +0000 | [diff] [blame] | 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 | |
Sami Tolvanen | 0cc37c2 | 2015-12-03 14:26:31 +0000 | [diff] [blame] | 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 | |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 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 | |
Patrik Torstensson | 142d4b5 | 2018-03-22 18:18:04 -0700 | [diff] [blame] | 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. |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 127 | |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 128 | Theory of operation |
| 129 | =================== |
| 130 | |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 131 | dm-verity is meant to be set up as part of a verified boot path. This |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 132 | may be anything ranging from a boot using tboot or trustedgrub to just |
| 133 | booting from a known-good device (like a USB drive or CD). |
| 134 | |
| 135 | When a dm-verity device is configured, it is expected that the caller |
| 136 | has been authenticated in some way (cryptographic signatures, etc). |
| 137 | After instantiation, all hashes will be verified on-demand during |
| 138 | disk access. If they cannot be verified up to the root node of the |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 139 | tree, the root hash, then the I/O will fail. This should detect |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 140 | tampering with any data on the device and the hash data. |
| 141 | |
| 142 | Cryptographic hashes are used to assert the integrity of the device on a |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 143 | per-block basis. This allows for a lightweight hash computation on first read |
| 144 | into the page cache. Block hashes are stored linearly, aligned to the nearest |
| 145 | block size. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 146 | |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 147 | If forward error correction (FEC) support is enabled any recovery of |
| 148 | corrupted data will be verified using the cryptographic hash of the |
| 149 | corresponding data. This is why combining error correction with |
| 150 | integrity checking is essential. |
| 151 | |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 152 | Hash Tree |
| 153 | --------- |
| 154 | |
| 155 | Each node in the tree is a cryptographic hash. If it is a leaf node, the hash |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 156 | of some data block on disk is calculated. If it is an intermediary node, |
| 157 | the hash of a number of child nodes is calculated. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 158 | |
| 159 | Each entry in the tree is a collection of neighboring nodes that fit in one |
| 160 | block. The number is determined based on block_size and the size of the |
| 161 | selected cryptographic digest algorithm. The hashes are linearly-ordered in |
| 162 | this entry and any unaligned trailing space is ignored but included when |
| 163 | calculating the parent node. |
| 164 | |
| 165 | The tree looks something like: |
| 166 | |
| 167 | alg = sha256, num_blocks = 32768, block_size = 4096 |
| 168 | |
| 169 | [ root ] |
| 170 | / . . . \ |
| 171 | [entry_0] [entry_1] |
| 172 | / . . . \ . . . \ |
| 173 | [entry_0_0] . . . [entry_0_127] . . . . [entry_1_127] |
| 174 | / ... \ / . . . \ / \ |
| 175 | blk_0 ... blk_127 blk_16256 blk_16383 blk_32640 . . . blk_32767 |
| 176 | |
| 177 | |
| 178 | On-disk format |
| 179 | ============== |
| 180 | |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 181 | The verity kernel code does not read the verity metadata on-disk header. |
| 182 | It only reads the hash blocks which directly follow the header. |
| 183 | It is expected that a user-space tool will verify the integrity of the |
| 184 | verity header. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 185 | |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 186 | Alternatively, the header can be omitted and the dmsetup parameters can |
| 187 | be passed via the kernel command-line in a rooted chain of trust where |
| 188 | the command-line is verified. |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 189 | |
| 190 | Directly following the header (and with sector number padded to the next hash |
| 191 | block boundary) are the hash blocks which are stored a depth at a time |
| 192 | (starting from the root), sorted in order of increasing index. |
| 193 | |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 194 | The full specification of kernel parameters and on-disk metadata format |
| 195 | is available at the cryptsetup project's wiki page |
Milan Broz | e44f23b | 2015-04-05 18:03:10 +0200 | [diff] [blame] | 196 | https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 197 | |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 198 | Status |
| 199 | ====== |
| 200 | V (for Valid) is returned if every check performed so far was valid. |
| 201 | If any check failed, C (for Corruption) is returned. |
| 202 | |
| 203 | Example |
| 204 | ======= |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 205 | Set up a device: |
| 206 | # dmsetup create vroot --readonly --table \ |
| 207 | "0 2097152 verity 1 /dev/sda1 /dev/sda2 4096 4096 262144 1 sha256 "\ |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 208 | "4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 "\ |
| 209 | "1234000000000000000000000000000000000000000000000000000000000000" |
| 210 | |
| 211 | A command line tool veritysetup is available to compute or verify |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 212 | the hash tree or activate the kernel device. This is available from |
Milan Broz | e44f23b | 2015-04-05 18:03:10 +0200 | [diff] [blame] | 213 | the cryptsetup upstream repository https://gitlab.com/cryptsetup/cryptsetup/ |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 214 | (as a libcryptsetup extension). |
Mikulas Patocka | a4ffc15 | 2012-03-28 18:43:38 +0100 | [diff] [blame] | 215 | |
Milan Broz | 18068bd | 2012-07-03 12:55:41 +0100 | [diff] [blame] | 216 | Create hash on the device: |
| 217 | # veritysetup format /dev/sda1 /dev/sda2 |
| 218 | ... |
| 219 | Root hash: 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 |
| 220 | |
| 221 | Activate the device: |
| 222 | # veritysetup create vroot /dev/sda1 /dev/sda2 \ |
| 223 | 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 |