Device-mapper is infrastructure in the Linux kernel
that provides a generic way to create virtual layers of block devices.
Device-mapper verity target provides read-only transparent integrity checking
of block devices using kernel crypto API.
Basic documentation of dm-verity mapping table comes with kernel source and the latest version is available
in git repository.
The dm-verity was designed and developed by Chrome OS authors
for verified boot implementation.
To enable dm-verity support, enable CONFIG_DM_VERITY in Device Drivers/Multi-device support
(RAID and LVM) configuration option.
To configure you need userspace components: device mapper library
(part of LVM2 package) and veritysetup.
Device mapper verity table mapping line specification
Mapping table in device mapper is defined like
<start_sector> <size> <target name> <target mapping table>
start_sector is 0 (for tables using only one mapped segment, iow table contains only one line)
size is size of device in sectors
target name is name of mapping target, here "verity" for dm-verity
Sectors are always 512B sectors (even if device has bigger hw sector like 4k).
Table fields are separated by space.
dm-verity target version
Every device-mapper target has internal version which is increased when some new feature is added.
To check which version you have installed, load the dm target module
(dm-verity.ko for dm-verity) and use "dmsetup targets" to check version.
Mapping table for verity target
The basic syntax is common for all 1.x.y dm-verity target versions.
If some extension was added later, it is mentioned in the description.
<ver> <data_dev> <hash_dev> <data_blk_size> <hash_blk_size> <#blocks> <hash_start> <alg> <digest> <salt> [<#opt_params> <opt_params>]
ver: on-disk hash version
0 is the original format used in the Chromium OS.
The salt is appended when hashing, digests are stored continuously and
the rest of the block is padded with zeroes.
1 is the current format that should be used for new devices.
The salt is prepended when hashing and each digest is
padded with zeroes to the power of two.
data_dev: device containing the data the integrity of which needs to be checked.
It may be specified as a path, like /dev/sdaX, or a device number, major:minor.
hash_dev: device that that supplies the hash tree data.
It may be specified similarly to the data device path and may be the same device.
If the same device is used, the hash_start should be outside of the dm-verity configured device size.
data_blk_size: The block size in bytes on a data device. Each block corresponds to one digest on the hash device.
hash_blk_size: The size of a hash block in bytes.
#blocks: The number of data blocks on the data device. Additional blocks are inaccessible.
hash_start: offset, in hash_blk_size blocks, from the start of hash_device to the root block of the hash tree.
alg: The cryptographic hash algorithm used for this device. This should be the name of the algorithm, like "sha256".
digest: The hexadecimal encoding of the cryptographic hash of the root hash block and the salt.
This hash should be trusted.
salt: The hexadecimal encoding of the salt value.
#opt_params: Number of optional parameters.
If there are no optional parameters, the optional parameters section can be skipped or it can be zero.
Otherwise it is the number of following arguments. Available since: 1.2.0 (no proper version set) (kernel 4.1)
ignore_corruption: Log corrupted blocks, but allow read operations to proceed normally. Available since: 1.2.0 (no proper version set) (kernel 4.1)
restart_on_corruption: Restart the system when a corrupted block is discovered.
This option is not compatible with ignore_corruption and requires user space support to avoid restart loops. Available since: 1.2.0 (no proper version set) (kernel 4.1)
ignore_zero_blocks: Do not verify blocks that are expected to contain zeroes and always return zeroes instead.
This may be useful if the partition contains unused blocks that are not guaranteed to contain zeroes. Available since: 1.3.0 (kernel 4.5)
use_fec_from_device <fec_dev>: Use forward error correction (FEC) to recover from corruption if hash verification fails.
Use encoding data from the specified device. This may be the same device where data and hash blocks reside,
in which case fec_start must be outside data and hash areas.
If the encoding data covers additional metadata, it must be accessible on the hash device after the hash blocks.
Note: block sizes for data and hash devices must match. Also, if the verity <dev> is encrypted the <fec_dev> should be too. Available since: 1.3.0 (kernel 4.5)
fec_roots <num>: Number of generator roots. This equals to the number of parity bytes in the encoding data.
For example, in RS(M, N) encoding, the number of roots is M-N.
M is 255 and M-N is between 2 and 24 (including).
RS decoder can correct up to <num>/2 bytes that contains error and <num> errasures in each codeword.
Erasures are identified by hash information on hash_device. Available since: 1.3.0 (kernel 4.5)
fec_blocks <num>: The number of encoding data blocks on the FEC device.
The block size for the FEC device is <data_block_size>.
FEC is computed over: data blocks | hash blocks | metadata (optional).
We allow metadata after hash blocks to support a use case where all data is stored on the same device and FEC covers the entire area. Available since: 1.3.0 (kernel 4.5)
fec_start <offset>: This is the offset, in <data_block_size> blocks, from the start of the FEC device to the beginning of the encoding data. Available since: 1.3.0 (kernel 4.5)
Note: FEC extensions are not yet supported in veritysetup tool.
You can check the full mapping table using dmsetup table.
Note that for all device-mapper operations is required root privilege (CAP_SYSADMIN).
The newly created device then appears as /dev/mapper/name.
Configuration using veritysetup
Preparing hash device (formatting) veritysetup format /dev/sdb /dev/sdc
Activation of verity data device veritysetup create name /dev/sdb /dev/sdc <root_digest>
Theory of operation
Dm-verity is meant to be setup as part of a verified boot path.
When a dm-verity device is configured, it is expected that the caller
has been authenticated in some way. After instantiation, all hashes
will be verified on-demand during disk access. If they cannot be verified
up to the root node of the tree, the root hash, then the I/O will fail.
This should identify tampering with any data on the device and the hash data.
Cryptographic hashes are used to assert the integrity of the device
on a 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.
Each node in the tree is a cryptographic hash. If it is a leaf node,
the hash is calculated of some data block on disk. If it is
an intermediary node, the hash is calculated of a number of child nodes.
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.