verity.rst 10 KB

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