xref: /linux/Documentation/admin-guide/device-mapper/verity.rst (revision 88cd3e6cfac915f50f7aa7b699bdf053afec866e)
1f0ba4377SMauro Carvalho Chehab=========
2a4ffc152SMikulas Patockadm-verity
3f0ba4377SMauro Carvalho Chehab=========
4a4ffc152SMikulas Patocka
5a4ffc152SMikulas PatockaDevice-Mapper's "verity" target provides transparent integrity checking of
6a4ffc152SMikulas Patockablock devices using a cryptographic digest provided by the kernel crypto API.
7a4ffc152SMikulas PatockaThis target is read-only.
8a4ffc152SMikulas Patocka
9a4ffc152SMikulas PatockaConstruction Parameters
10a4ffc152SMikulas Patocka=======================
11f0ba4377SMauro Carvalho Chehab
12f0ba4377SMauro Carvalho Chehab::
13f0ba4377SMauro Carvalho Chehab
1418068bddSMilan Broz    <version> <dev> <hash_dev>
15a4ffc152SMikulas Patocka    <data_block_size> <hash_block_size>
16a4ffc152SMikulas Patocka    <num_data_blocks> <hash_start_block>
17a4ffc152SMikulas Patocka    <algorithm> <digest> <salt>
1865ff5b7dSSami Tolvanen    [<#opt_params> <opt_params>]
19a4ffc152SMikulas Patocka
20a4ffc152SMikulas Patocka<version>
2118068bddSMilan Broz    This is the type of the on-disk hash format.
22a4ffc152SMikulas Patocka
23a4ffc152SMikulas Patocka    0 is the original format used in the Chromium OS.
24a4ffc152SMikulas Patocka      The salt is appended when hashing, digests are stored continuously and
25a739ff3fSSami Tolvanen      the rest of the block is padded with zeroes.
26a4ffc152SMikulas Patocka
27a4ffc152SMikulas Patocka    1 is the current format that should be used for new devices.
28a4ffc152SMikulas Patocka      The salt is prepended when hashing and each digest is
29a739ff3fSSami Tolvanen      padded with zeroes to the power of two.
30a4ffc152SMikulas Patocka
31a4ffc152SMikulas Patocka<dev>
3218068bddSMilan Broz    This is the device containing data, the integrity of which needs to be
33a4ffc152SMikulas Patocka    checked.  It may be specified as a path, like /dev/sdaX, or a device number,
34a4ffc152SMikulas Patocka    <major>:<minor>.
35a4ffc152SMikulas Patocka
36a4ffc152SMikulas Patocka<hash_dev>
3718068bddSMilan Broz    This is the device that supplies the hash tree data.  It may be
38a4ffc152SMikulas Patocka    specified similarly to the device path and may be the same device.  If the
3918068bddSMilan Broz    same device is used, the hash_start should be outside the configured
4018068bddSMilan Broz    dm-verity device.
41a4ffc152SMikulas Patocka
42a4ffc152SMikulas Patocka<data_block_size>
4318068bddSMilan Broz    The block size on a data device in bytes.
4418068bddSMilan Broz    Each block corresponds to one digest on the hash device.
45a4ffc152SMikulas Patocka
46a4ffc152SMikulas Patocka<hash_block_size>
4718068bddSMilan Broz    The size of a hash block in bytes.
48a4ffc152SMikulas Patocka
49a4ffc152SMikulas Patocka<num_data_blocks>
50a4ffc152SMikulas Patocka    The number of data blocks on the data device.  Additional blocks are
51a4ffc152SMikulas Patocka    inaccessible.  You can place hashes to the same partition as data, in this
52a4ffc152SMikulas Patocka    case hashes are placed after <num_data_blocks>.
53a4ffc152SMikulas Patocka
54a4ffc152SMikulas Patocka<hash_start_block>
55a4ffc152SMikulas Patocka    This is the offset, in <hash_block_size>-blocks, from the start of hash_dev
56a4ffc152SMikulas Patocka    to the root block of the hash tree.
57a4ffc152SMikulas Patocka
58a4ffc152SMikulas Patocka<algorithm>
59a4ffc152SMikulas Patocka    The cryptographic hash algorithm used for this device.  This should
60a4ffc152SMikulas Patocka    be the name of the algorithm, like "sha1".
61a4ffc152SMikulas Patocka
62a4ffc152SMikulas Patocka<digest>
63a4ffc152SMikulas Patocka    The hexadecimal encoding of the cryptographic hash of the root hash block
64a4ffc152SMikulas Patocka    and the salt.  This hash should be trusted as there is no other authenticity
65a4ffc152SMikulas Patocka    beyond this point.
66a4ffc152SMikulas Patocka
67a4ffc152SMikulas Patocka<salt>
68a4ffc152SMikulas Patocka    The hexadecimal encoding of the salt value.
69a4ffc152SMikulas Patocka
7065ff5b7dSSami Tolvanen<#opt_params>
7165ff5b7dSSami Tolvanen    Number of optional parameters. If there are no optional parameters,
7265ff5b7dSSami Tolvanen    the optional paramaters section can be skipped or #opt_params can be zero.
7365ff5b7dSSami Tolvanen    Otherwise #opt_params is the number of following arguments.
7465ff5b7dSSami Tolvanen
7565ff5b7dSSami Tolvanen    Example of optional parameters section:
7665ff5b7dSSami Tolvanen        1 ignore_corruption
7765ff5b7dSSami Tolvanen
7865ff5b7dSSami Tolvanenignore_corruption
7965ff5b7dSSami Tolvanen    Log corrupted blocks, but allow read operations to proceed normally.
8065ff5b7dSSami Tolvanen
8165ff5b7dSSami Tolvanenrestart_on_corruption
8265ff5b7dSSami Tolvanen    Restart the system when a corrupted block is discovered. This option is
8365ff5b7dSSami Tolvanen    not compatible with ignore_corruption and requires user space support to
8465ff5b7dSSami Tolvanen    avoid restart loops.
8565ff5b7dSSami Tolvanen
860cc37c2dSSami Tolvanenignore_zero_blocks
870cc37c2dSSami Tolvanen    Do not verify blocks that are expected to contain zeroes and always return
880cc37c2dSSami Tolvanen    zeroes instead. This may be useful if the partition contains unused blocks
890cc37c2dSSami Tolvanen    that are not guaranteed to contain zeroes.
900cc37c2dSSami Tolvanen
91a739ff3fSSami Tolvanenuse_fec_from_device <fec_dev>
92a739ff3fSSami Tolvanen    Use forward error correction (FEC) to recover from corruption if hash
93a739ff3fSSami Tolvanen    verification fails. Use encoding data from the specified device. This
94a739ff3fSSami Tolvanen    may be the same device where data and hash blocks reside, in which case
95a739ff3fSSami Tolvanen    fec_start must be outside data and hash areas.
96a739ff3fSSami Tolvanen
97a739ff3fSSami Tolvanen    If the encoding data covers additional metadata, it must be accessible
98a739ff3fSSami Tolvanen    on the hash device after the hash blocks.
99a739ff3fSSami Tolvanen
100a739ff3fSSami Tolvanen    Note: block sizes for data and hash devices must match. Also, if the
101a739ff3fSSami Tolvanen    verity <dev> is encrypted the <fec_dev> should be too.
102a739ff3fSSami Tolvanen
103a739ff3fSSami Tolvanenfec_roots <num>
104a739ff3fSSami Tolvanen    Number of generator roots. This equals to the number of parity bytes in
105a739ff3fSSami Tolvanen    the encoding data. For example, in RS(M, N) encoding, the number of roots
106a739ff3fSSami Tolvanen    is M-N.
107a739ff3fSSami Tolvanen
108a739ff3fSSami Tolvanenfec_blocks <num>
109a739ff3fSSami Tolvanen    The number of encoding data blocks on the FEC device. The block size for
110a739ff3fSSami Tolvanen    the FEC device is <data_block_size>.
111a739ff3fSSami Tolvanen
112a739ff3fSSami Tolvanenfec_start <offset>
113a739ff3fSSami Tolvanen    This is the offset, in <data_block_size> blocks, from the start of the
114a739ff3fSSami Tolvanen    FEC device to the beginning of the encoding data.
115a739ff3fSSami Tolvanen
116843f38d3SPatrik Torstenssoncheck_at_most_once
117843f38d3SPatrik Torstensson    Verify data blocks only the first time they are read from the data device,
118843f38d3SPatrik Torstensson    rather than every time.  This reduces the overhead of dm-verity so that it
119843f38d3SPatrik Torstensson    can be used on systems that are memory and/or CPU constrained.  However, it
120843f38d3SPatrik Torstensson    provides a reduced level of security because only offline tampering of the
121843f38d3SPatrik Torstensson    data device's content will be detected, not online tampering.
122843f38d3SPatrik Torstensson
123843f38d3SPatrik Torstensson    Hash blocks are still verified each time they are read from the hash device,
124843f38d3SPatrik Torstensson    since verification of hash blocks is less performance critical than data
125843f38d3SPatrik Torstensson    blocks, and a hash block will not be verified any more after all the data
126843f38d3SPatrik Torstensson    blocks it covers have been verified anyway.
127a739ff3fSSami Tolvanen
128*88cd3e6cSJaskaran Khuranaroot_hash_sig_key_desc <key_description>
129*88cd3e6cSJaskaran Khurana    This is the description of the USER_KEY that the kernel will lookup to get
130*88cd3e6cSJaskaran Khurana    the pkcs7 signature of the roothash. The pkcs7 signature is used to validate
131*88cd3e6cSJaskaran Khurana    the root hash during the creation of the device mapper block device.
132*88cd3e6cSJaskaran Khurana    Verification of roothash depends on the config DM_VERITY_VERIFY_ROOTHASH_SIG
133*88cd3e6cSJaskaran Khurana    being set in the kernel.
134*88cd3e6cSJaskaran Khurana
135a4ffc152SMikulas PatockaTheory of operation
136a4ffc152SMikulas Patocka===================
137a4ffc152SMikulas Patocka
138a4ffc152SMikulas Patockadm-verity is meant to be set up as part of a verified boot path.  This
139a4ffc152SMikulas Patockamay be anything ranging from a boot using tboot or trustedgrub to just
140a4ffc152SMikulas Patockabooting from a known-good device (like a USB drive or CD).
141a4ffc152SMikulas Patocka
142a4ffc152SMikulas PatockaWhen a dm-verity device is configured, it is expected that the caller
143a4ffc152SMikulas Patockahas been authenticated in some way (cryptographic signatures, etc).
144a4ffc152SMikulas PatockaAfter instantiation, all hashes will be verified on-demand during
145a4ffc152SMikulas Patockadisk access.  If they cannot be verified up to the root node of the
14618068bddSMilan Broztree, the root hash, then the I/O will fail.  This should detect
147a4ffc152SMikulas Patockatampering with any data on the device and the hash data.
148a4ffc152SMikulas Patocka
149a4ffc152SMikulas PatockaCryptographic hashes are used to assert the integrity of the device on a
150a4ffc152SMikulas Patockaper-block basis. This allows for a lightweight hash computation on first read
15118068bddSMilan Brozinto the page cache. Block hashes are stored linearly, aligned to the nearest
15218068bddSMilan Brozblock size.
153a4ffc152SMikulas Patocka
154a739ff3fSSami TolvanenIf forward error correction (FEC) support is enabled any recovery of
155a739ff3fSSami Tolvanencorrupted data will be verified using the cryptographic hash of the
156a739ff3fSSami Tolvanencorresponding data. This is why combining error correction with
157a739ff3fSSami Tolvanenintegrity checking is essential.
158a739ff3fSSami Tolvanen
159a4ffc152SMikulas PatockaHash Tree
160a4ffc152SMikulas Patocka---------
161a4ffc152SMikulas Patocka
162a4ffc152SMikulas PatockaEach node in the tree is a cryptographic hash.  If it is a leaf node, the hash
16318068bddSMilan Brozof some data block on disk is calculated. If it is an intermediary node,
16418068bddSMilan Brozthe hash of a number of child nodes is calculated.
165a4ffc152SMikulas Patocka
166a4ffc152SMikulas PatockaEach entry in the tree is a collection of neighboring nodes that fit in one
167a4ffc152SMikulas Patockablock.  The number is determined based on block_size and the size of the
168a4ffc152SMikulas Patockaselected cryptographic digest algorithm.  The hashes are linearly-ordered in
169a4ffc152SMikulas Patockathis entry and any unaligned trailing space is ignored but included when
170a4ffc152SMikulas Patockacalculating the parent node.
171a4ffc152SMikulas Patocka
172a4ffc152SMikulas PatockaThe tree looks something like:
173a4ffc152SMikulas Patocka
174a4ffc152SMikulas Patocka	alg = sha256, num_blocks = 32768, block_size = 4096
175a4ffc152SMikulas Patocka
176f0ba4377SMauro Carvalho Chehab::
177f0ba4377SMauro Carvalho Chehab
178a4ffc152SMikulas Patocka                                 [   root    ]
179a4ffc152SMikulas Patocka                                /    . . .    \
180a4ffc152SMikulas Patocka                     [entry_0]                 [entry_1]
181a4ffc152SMikulas Patocka                    /  . . .  \                 . . .   \
182a4ffc152SMikulas Patocka         [entry_0_0]   . . .  [entry_0_127]    . . . .  [entry_1_127]
183a4ffc152SMikulas Patocka           / ... \             /   . . .  \             /           \
184a4ffc152SMikulas Patocka     blk_0 ... blk_127  blk_16256   blk_16383      blk_32640 . . . blk_32767
185a4ffc152SMikulas Patocka
186a4ffc152SMikulas Patocka
187a4ffc152SMikulas PatockaOn-disk format
188a4ffc152SMikulas Patocka==============
189a4ffc152SMikulas Patocka
19018068bddSMilan BrozThe verity kernel code does not read the verity metadata on-disk header.
19118068bddSMilan BrozIt only reads the hash blocks which directly follow the header.
19218068bddSMilan BrozIt is expected that a user-space tool will verify the integrity of the
19318068bddSMilan Brozverity header.
194a4ffc152SMikulas Patocka
19518068bddSMilan BrozAlternatively, the header can be omitted and the dmsetup parameters can
19618068bddSMilan Brozbe passed via the kernel command-line in a rooted chain of trust where
19718068bddSMilan Brozthe command-line is verified.
198a4ffc152SMikulas Patocka
199a4ffc152SMikulas PatockaDirectly following the header (and with sector number padded to the next hash
200a4ffc152SMikulas Patockablock boundary) are the hash blocks which are stored a depth at a time
201a4ffc152SMikulas Patocka(starting from the root), sorted in order of increasing index.
202a4ffc152SMikulas Patocka
20318068bddSMilan BrozThe full specification of kernel parameters and on-disk metadata format
20418068bddSMilan Brozis available at the cryptsetup project's wiki page
205f0ba4377SMauro Carvalho Chehab
206e44f23b3SMilan Broz  https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity
20718068bddSMilan Broz
208a4ffc152SMikulas PatockaStatus
209a4ffc152SMikulas Patocka======
210a4ffc152SMikulas PatockaV (for Valid) is returned if every check performed so far was valid.
211a4ffc152SMikulas PatockaIf any check failed, C (for Corruption) is returned.
212a4ffc152SMikulas Patocka
213a4ffc152SMikulas PatockaExample
214a4ffc152SMikulas Patocka=======
215f0ba4377SMauro Carvalho ChehabSet up a device::
216f0ba4377SMauro Carvalho Chehab
21718068bddSMilan Broz  # dmsetup create vroot --readonly --table \
21818068bddSMilan Broz    "0 2097152 verity 1 /dev/sda1 /dev/sda2 4096 4096 262144 1 sha256 "\
219a4ffc152SMikulas Patocka    "4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076 "\
220a4ffc152SMikulas Patocka    "1234000000000000000000000000000000000000000000000000000000000000"
221a4ffc152SMikulas Patocka
222a4ffc152SMikulas PatockaA command line tool veritysetup is available to compute or verify
22318068bddSMilan Brozthe hash tree or activate the kernel device. This is available from
224e44f23b3SMilan Brozthe cryptsetup upstream repository https://gitlab.com/cryptsetup/cryptsetup/
22518068bddSMilan Broz(as a libcryptsetup extension).
226a4ffc152SMikulas Patocka
227f0ba4377SMauro Carvalho ChehabCreate hash on the device::
228f0ba4377SMauro Carvalho Chehab
22918068bddSMilan Broz  # veritysetup format /dev/sda1 /dev/sda2
23018068bddSMilan Broz  ...
23118068bddSMilan Broz  Root hash: 4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076
23218068bddSMilan Broz
233f0ba4377SMauro Carvalho ChehabActivate the device::
234f0ba4377SMauro Carvalho Chehab
23518068bddSMilan Broz  # veritysetup create vroot /dev/sda1 /dev/sda2 \
236a4ffc152SMikulas Patocka    4392712ba01368efdf14b05c76f9e4df0d53664630b5d48632ed17a137f39076
237