verity.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 paramaters 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. ignore_zero_blocks
  69. Do not verify blocks that are expected to contain zeroes and always return
  70. zeroes instead. This may be useful if the partition contains unused blocks
  71. that are not guaranteed to contain zeroes.
  72. use_fec_from_device <fec_dev>
  73. Use forward error correction (FEC) to recover from corruption if hash
  74. verification fails. Use encoding data from the specified device. This
  75. may be the same device where data and hash blocks reside, in which case
  76. fec_start must be outside data and hash areas.
  77. If the encoding data covers additional metadata, it must be accessible
  78. on the hash device after the hash blocks.
  79. Note: block sizes for data and hash devices must match. Also, if the
  80. verity <dev> is encrypted the <fec_dev> should be too.
  81. fec_roots <num>
  82. Number of generator roots. This equals to the number of parity bytes in
  83. the encoding data. For example, in RS(M, N) encoding, the number of roots
  84. is M-N.
  85. fec_blocks <num>
  86. The number of encoding data blocks on the FEC device. The block size for
  87. the FEC device is <data_block_size>.
  88. fec_start <offset>
  89. This is the offset, in <data_block_size> blocks, from the start of the
  90. FEC device to the beginning of the encoding data.
  91. check_at_most_once
  92. Verify data blocks only the first time they are read from the data device,
  93. rather than every time. This reduces the overhead of dm-verity so that it
  94. can be used on systems that are memory and/or CPU constrained. However, it
  95. provides a reduced level of security because only offline tampering of the
  96. data device's content will be detected, not online tampering.
  97. Hash blocks are still verified each time they are read from the hash device,
  98. since verification of hash blocks is less performance critical than data
  99. blocks, and a hash block will not be verified any more after all the data
  100. blocks it covers have been verified anyway.
  101. root_hash_sig_key_desc <key_description>
  102. This is the description of the USER_KEY that the kernel will lookup to get
  103. the pkcs7 signature of the roothash. The pkcs7 signature is used to validate
  104. the root hash during the creation of the device mapper block device.
  105. Verification of roothash depends on the config DM_VERITY_VERIFY_ROOTHASH_SIG
  106. being set in the kernel.
  107. Theory of operation
  108. ===================
  109. dm-verity is meant to be set up as part of a verified boot path. This
  110. may be anything ranging from a boot using tboot or trustedgrub to just
  111. booting from a known-good device (like a USB drive or CD).
  112. When a dm-verity device is configured, it is expected that the caller
  113. has been authenticated in some way (cryptographic signatures, etc).
  114. After instantiation, all hashes will be verified on-demand during
  115. disk access. If they cannot be verified up to the root node of the
  116. tree, the root hash, then the I/O will fail. This should detect
  117. tampering with any data on the device and the hash data.
  118. Cryptographic hashes are used to assert the integrity of the device on a
  119. per-block basis. This allows for a lightweight hash computation on first read
  120. into the page cache. Block hashes are stored linearly, aligned to the nearest
  121. block size.
  122. If forward error correction (FEC) support is enabled any recovery of
  123. corrupted data will be verified using the cryptographic hash of the
  124. corresponding data. This is why combining error correction with
  125. integrity checking is essential.
  126. Hash Tree
  127. ---------
  128. Each node in the tree is a cryptographic hash. If it is a leaf node, the hash
  129. of some data block on disk is calculated. If it is an intermediary node,
  130. the hash of a number of child nodes is calculated.
  131. Each entry in the tree is a collection of neighboring nodes that fit in one
  132. block. The number is determined based on block_size and the size of the
  133. selected cryptographic digest algorithm. The hashes are linearly-ordered in
  134. this entry and any unaligned trailing space is ignored but included when
  135. calculating the parent node.
  136. The tree looks something like:
  137. alg = sha256, num_blocks = 32768, block_size = 4096
  138. ::
  139. [ root ]
  140. / . . . \
  141. [entry_0] [entry_1]
  142. / . . . \ . . . \
  143. [entry_0_0] . . . [entry_0_127] . . . . [entry_1_127]
  144. / ... \ / . . . \ / \
  145. blk_0 ... blk_127 blk_16256 blk_16383 blk_32640 . . . blk_32767
  146. On-disk format
  147. ==============
  148. The verity kernel code does not read the verity metadata on-disk header.
  149. It only reads the hash blocks which directly follow the header.
  150. It is expected that a user-space tool will verify the integrity of the
  151. verity header.
  152. Alternatively, the header can be omitted and the dmsetup parameters can
  153. be passed via the kernel command-line in a rooted chain of trust where
  154. the command-line is verified.
  155. Directly following the header (and with sector number padded to the next hash
  156. block boundary) are the hash blocks which are stored a depth at a time
  157. (starting from the root), sorted in order of increasing index.
  158. The full specification of kernel parameters and on-disk metadata format
  159. is available at the cryptsetup project's wiki page
  160. https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity
  161. Status
  162. ======
  163. V (for Valid) is returned if every check performed so far was valid.
  164. If any check failed, C (for Corruption) is returned.
  165. Example
  166. =======
  167. Set up a device::
  168. # dmsetup create vroot --readonly --table \
  169. "0 2097152 verity 1 /dev/sda1 /dev/sda2 4096 4096 262144 1 sha256 "\
  170. "4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 "\
  171. "1234000000000000000000000000000000000000000000000000000000000000"
  172. A command line tool veritysetup is available to compute or verify
  173. the hash tree or activate the kernel device. This is available from
  174. the cryptsetup upstream repository https://gitlab.com/cryptsetup/cryptsetup/
  175. (as a libcryptsetup extension).
  176. Create hash on the device::
  177. # veritysetup format /dev/sda1 /dev/sda2
  178. ...
  179. Root hash: 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076
  180. Activate the device::
  181. # veritysetup create vroot /dev/sda1 /dev/sda2 \
  182. 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076