verity.txt 7.9 KB

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