verity.txt 5.8 KB
Newer Older
M
Mikulas Patocka 已提交
1 2 3 4 5 6 7 8 9
dm-verity
==========

Device-Mapper's "verity" target provides transparent integrity checking of
block devices using a cryptographic digest provided by the kernel crypto API.
This target is read-only.

Construction Parameters
=======================
M
Milan Broz 已提交
10
    <version> <dev> <hash_dev>
M
Mikulas Patocka 已提交
11 12 13 14 15
    <data_block_size> <hash_block_size>
    <num_data_blocks> <hash_start_block>
    <algorithm> <digest> <salt>

<version>
M
Milan Broz 已提交
16
    This is the type of the on-disk hash format.
M
Mikulas Patocka 已提交
17 18

    0 is the original format used in the Chromium OS.
M
Milan Broz 已提交
19 20
      The salt is appended when hashing, digests are stored continuously and
      the rest of the block is padded with zeros.
M
Mikulas Patocka 已提交
21 22

    1 is the current format that should be used for new devices.
M
Milan Broz 已提交
23 24
      The salt is prepended when hashing and each digest is
      padded with zeros to the power of two.
M
Mikulas Patocka 已提交
25 26

<dev>
M
Milan Broz 已提交
27
    This is the device containing data, the integrity of which needs to be
M
Mikulas Patocka 已提交
28 29 30 31
    checked.  It may be specified as a path, like /dev/sdaX, or a device number,
    <major>:<minor>.

<hash_dev>
M
Milan Broz 已提交
32
    This is the device that supplies the hash tree data.  It may be
M
Mikulas Patocka 已提交
33
    specified similarly to the device path and may be the same device.  If the
M
Milan Broz 已提交
34 35
    same device is used, the hash_start should be outside the configured
    dm-verity device.
M
Mikulas Patocka 已提交
36 37

<data_block_size>
M
Milan Broz 已提交
38 39
    The block size on a data device in bytes.
    Each block corresponds to one digest on the hash device.
M
Mikulas Patocka 已提交
40 41

<hash_block_size>
M
Milan Broz 已提交
42
    The size of a hash block in bytes.
M
Mikulas Patocka 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

<num_data_blocks>
    The number of data blocks on the data device.  Additional blocks are
    inaccessible.  You can place hashes to the same partition as data, in this
    case hashes are placed after <num_data_blocks>.

<hash_start_block>
    This is the offset, in <hash_block_size>-blocks, from the start of hash_dev
    to the root block of the hash tree.

<algorithm>
    The cryptographic hash algorithm used for this device.  This should
    be the name of the algorithm, like "sha1".

<digest>
    The hexadecimal encoding of the cryptographic hash of the root hash block
    and the salt.  This hash should be trusted as there is no other authenticity
    beyond this point.

<salt>
    The hexadecimal encoding of the salt value.

Theory of operation
===================

M
Milan Broz 已提交
68
dm-verity is meant to be set up as part of a verified boot path.  This
M
Mikulas Patocka 已提交
69 70 71 72 73 74 75
may be anything ranging from a boot using tboot or trustedgrub to just
booting from a known-good device (like a USB drive or CD).

When a dm-verity device is configured, it is expected that the caller
has been authenticated in some way (cryptographic signatures, etc).
After instantiation, all hashes will be verified on-demand during
disk access.  If they cannot be verified up to the root node of the
M
Milan Broz 已提交
76
tree, the root hash, then the I/O will fail.  This should detect
M
Mikulas Patocka 已提交
77 78 79
tampering with any data on the device and the hash data.

Cryptographic hashes are used to assert the integrity of the device on a
M
Milan Broz 已提交
80 81 82
per-block basis. This allows for a lightweight hash computation on first read
into the page cache. Block hashes are stored linearly, aligned to the nearest
block size.
M
Mikulas Patocka 已提交
83 84 85 86 87

Hash Tree
---------

Each node in the tree is a cryptographic hash.  If it is a leaf node, the hash
M
Milan Broz 已提交
88 89
of some data block on disk is calculated. If it is an intermediary node,
the hash of a number of child nodes is calculated.
M
Mikulas Patocka 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

Each entry in the tree is a collection of neighboring nodes that fit in one
block.  The number is determined based on block_size and the size of the
selected cryptographic digest algorithm.  The hashes are linearly-ordered in
this entry and any unaligned trailing space is ignored but included when
calculating the parent node.

The tree looks something like:

alg = sha256, num_blocks = 32768, block_size = 4096

                                 [   root    ]
                                /    . . .    \
                     [entry_0]                 [entry_1]
                    /  . . .  \                 . . .   \
         [entry_0_0]   . . .  [entry_0_127]    . . . .  [entry_1_127]
           / ... \             /   . . .  \             /           \
     blk_0 ... blk_127  blk_16256   blk_16383      blk_32640 . . . blk_32767


On-disk format
==============

M
Milan Broz 已提交
113 114 115 116
The verity kernel code does not read the verity metadata on-disk header.
It only reads the hash blocks which directly follow the header.
It is expected that a user-space tool will verify the integrity of the
verity header.
M
Mikulas Patocka 已提交
117

M
Milan Broz 已提交
118 119 120
Alternatively, the header can be omitted and the dmsetup parameters can
be passed via the kernel command-line in a rooted chain of trust where
the command-line is verified.
M
Mikulas Patocka 已提交
121 122 123 124 125

Directly following the header (and with sector number padded to the next hash
block boundary) are the hash blocks which are stored a depth at a time
(starting from the root), sorted in order of increasing index.

M
Milan Broz 已提交
126 127 128 129
The full specification of kernel parameters and on-disk metadata format
is available at the cryptsetup project's wiki page
  http://code.google.com/p/cryptsetup/wiki/DMVerity

M
Mikulas Patocka 已提交
130 131 132 133 134 135 136
Status
======
V (for Valid) is returned if every check performed so far was valid.
If any check failed, C (for Corruption) is returned.

Example
=======
M
Milan Broz 已提交
137 138 139
Set up a device:
  # dmsetup create vroot --readonly --table \
    "0 2097152 verity 1 /dev/sda1 /dev/sda2 4096 4096 262144 1 sha256 "\
M
Mikulas Patocka 已提交
140 141 142 143
    "4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 "\
    "1234000000000000000000000000000000000000000000000000000000000000"

A command line tool veritysetup is available to compute or verify
M
Milan Broz 已提交
144 145 146 147 148 149 150 151 152 153 154 155
the hash tree or activate the kernel device. This is available from
the cryptsetup upstream repository http://code.google.com/p/cryptsetup/
(as a libcryptsetup extension).

Create hash on the device:
  # veritysetup format /dev/sda1 /dev/sda2
  ...
  Root hash: 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076

Activate the device:
  # veritysetup create vroot /dev/sda1 /dev/sda2 \
    4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076