verity.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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> <hash_start>
  9. <data_block_size> <hash_block_size>
  10. <num_data_blocks> <hash_start_block>
  11. <algorithm> <digest> <salt>
  12. <version>
  13. This is the version number of the on-disk format.
  14. 0 is the original format used in the Chromium OS.
  15. The salt is appended when hashing, digests are stored continuously and
  16. the rest of the block is padded with zeros.
  17. 1 is the current format that should be used for new devices.
  18. The salt is prepended when hashing and each digest is
  19. padded with zeros to the power of two.
  20. <dev>
  21. This is the device containing the data the integrity of which needs to be
  22. checked. It may be specified as a path, like /dev/sdaX, or a device number,
  23. <major>:<minor>.
  24. <hash_dev>
  25. This is the device that that supplies the hash tree data. It may be
  26. specified similarly to the device path and may be the same device. If the
  27. same device is used, the hash_start should be outside of the dm-verity
  28. configured device size.
  29. <data_block_size>
  30. The block size on a data device. Each block corresponds to one digest on
  31. the hash device.
  32. <hash_block_size>
  33. The size of a hash block.
  34. <num_data_blocks>
  35. The number of data blocks on the data device. Additional blocks are
  36. inaccessible. You can place hashes to the same partition as data, in this
  37. case hashes are placed after <num_data_blocks>.
  38. <hash_start_block>
  39. This is the offset, in <hash_block_size>-blocks, from the start of hash_dev
  40. to the root block of the hash tree.
  41. <algorithm>
  42. The cryptographic hash algorithm used for this device. This should
  43. be the name of the algorithm, like "sha1".
  44. <digest>
  45. The hexadecimal encoding of the cryptographic hash of the root hash block
  46. and the salt. This hash should be trusted as there is no other authenticity
  47. beyond this point.
  48. <salt>
  49. The hexadecimal encoding of the salt value.
  50. Theory of operation
  51. ===================
  52. dm-verity is meant to be setup as part of a verified boot path. This
  53. may be anything ranging from a boot using tboot or trustedgrub to just
  54. booting from a known-good device (like a USB drive or CD).
  55. When a dm-verity device is configured, it is expected that the caller
  56. has been authenticated in some way (cryptographic signatures, etc).
  57. After instantiation, all hashes will be verified on-demand during
  58. disk access. If they cannot be verified up to the root node of the
  59. tree, the root hash, then the I/O will fail. This should identify
  60. tampering with any data on the device and the hash data.
  61. Cryptographic hashes are used to assert the integrity of the device on a
  62. per-block basis. This allows for a lightweight hash computation on first read
  63. into the page cache. Block hashes are stored linearly-aligned to the nearest
  64. block the size of a page.
  65. Hash Tree
  66. ---------
  67. Each node in the tree is a cryptographic hash. If it is a leaf node, the hash
  68. is of some block data on disk. If it is an intermediary node, then the hash is
  69. of a number of child nodes.
  70. Each entry in the tree is a collection of neighboring nodes that fit in one
  71. block. The number is determined based on block_size and the size of the
  72. selected cryptographic digest algorithm. The hashes are linearly-ordered in
  73. this entry and any unaligned trailing space is ignored but included when
  74. calculating the parent node.
  75. The tree looks something like:
  76. alg = sha256, num_blocks = 32768, block_size = 4096
  77. [ root ]
  78. / . . . \
  79. [entry_0] [entry_1]
  80. / . . . \ . . . \
  81. [entry_0_0] . . . [entry_0_127] . . . . [entry_1_127]
  82. / ... \ / . . . \ / \
  83. blk_0 ... blk_127 blk_16256 blk_16383 blk_32640 . . . blk_32767
  84. On-disk format
  85. ==============
  86. Below is the recommended on-disk format. The verity kernel code does not
  87. read the on-disk header. It only reads the hash blocks which directly
  88. follow the header. It is expected that a user-space tool will verify the
  89. integrity of the verity_header and then call dmsetup with the correct
  90. parameters. Alternatively, the header can be omitted and the dmsetup
  91. parameters can be passed via the kernel command-line in a rooted chain
  92. of trust where the command-line is verified.
  93. The on-disk format is especially useful in cases where the hash blocks
  94. are on a separate partition. The magic number allows easy identification
  95. of the partition contents. Alternatively, the hash blocks can be stored
  96. in the same partition as the data to be verified. In such a configuration
  97. the filesystem on the partition would be sized a little smaller than
  98. the full-partition, leaving room for the hash blocks.
  99. struct superblock {
  100. uint8_t signature[8]
  101. "verity\0\0";
  102. uint8_t version;
  103. 1 - current format
  104. uint8_t data_block_bits;
  105. log2(data block size)
  106. uint8_t hash_block_bits;
  107. log2(hash block size)
  108. uint8_t pad1[1];
  109. zero padding
  110. uint16_t salt_size;
  111. big-endian salt size
  112. uint8_t pad2[2];
  113. zero padding
  114. uint32_t data_blocks_hi;
  115. big-endian high 32 bits of the 64-bit number of data blocks
  116. uint32_t data_blocks_lo;
  117. big-endian low 32 bits of the 64-bit number of data blocks
  118. uint8_t algorithm[16];
  119. cryptographic algorithm
  120. uint8_t salt[384];
  121. salt (the salt size is specified above)
  122. uint8_t pad3[88];
  123. zero padding to 512-byte boundary
  124. }
  125. Directly following the header (and with sector number padded to the next hash
  126. block boundary) are the hash blocks which are stored a depth at a time
  127. (starting from the root), sorted in order of increasing index.
  128. Status
  129. ======
  130. V (for Valid) is returned if every check performed so far was valid.
  131. If any check failed, C (for Corruption) is returned.
  132. Example
  133. =======
  134. Setup a device:
  135. dmsetup create vroot --table \
  136. "0 2097152 "\
  137. "verity 1 /dev/sda1 /dev/sda2 4096 4096 2097152 1 "\
  138. "4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 "\
  139. "1234000000000000000000000000000000000000000000000000000000000000"
  140. A command line tool veritysetup is available to compute or verify
  141. the hash tree or activate the kernel driver. This is available from
  142. the LVM2 upstream repository and may be supplied as a package called
  143. device-mapper-verity-tools:
  144. git://sources.redhat.com/git/lvm2
  145. http://sourceware.org/git/?p=lvm2.git
  146. http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/verity?cvsroot=lvm2
  147. veritysetup -a vroot /dev/sda1 /dev/sda2 \
  148. 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076