xref: /linux/Documentation/admin-guide/device-mapper/dm-integrity.rst (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1============
2dm-integrity
3============
4
5The dm-integrity target emulates a block device that has additional
6per-sector tags that can be used for storing integrity information.
7
8A general problem with storing integrity tags with every sector is that
9writing the sector and the integrity tag must be atomic - i.e. in case of
10crash, either both sector and integrity tag or none of them is written.
11
12To guarantee write atomicity, the dm-integrity target uses journal, it
13writes sector data and integrity tags into a journal, commits the journal
14and then copies the data and integrity tags to their respective location.
15
16The dm-integrity target can be used with the dm-crypt target - in this
17situation the dm-crypt target creates the integrity data and passes them
18to the dm-integrity target via bio_integrity_payload attached to the bio.
19In this mode, the dm-crypt and dm-integrity targets provide authenticated
20disk encryption - if the attacker modifies the encrypted device, an I/O
21error is returned instead of random data.
22
23The dm-integrity target can also be used as a standalone target, in this
24mode it calculates and verifies the integrity tag internally. In this
25mode, the dm-integrity target can be used to detect silent data
26corruption on the disk or in the I/O path.
27
28There's an alternate mode of operation where dm-integrity uses a bitmap
29instead of a journal. If a bit in the bitmap is 1, the corresponding
30region's data and integrity tags are not synchronized - if the machine
31crashes, the unsynchronized regions will be recalculated. The bitmap mode
32is faster than the journal mode, because we don't have to write the data
33twice, but it is also less reliable, because if data corruption happens
34when the machine crashes, it may not be detected.
35
36When loading the target for the first time, the kernel driver will format
37the device. But it will only format the device if the superblock contains
38zeroes. If the superblock is neither valid nor zeroed, the dm-integrity
39target can't be loaded.
40
41Accesses to the on-disk metadata area containing checksums (aka tags) are
42buffered using dm-bufio. When an access to any given metadata area
43occurs, each unique metadata area gets its own buffer(s). The buffer size
44is capped at the size of the metadata area, but may be smaller, thereby
45requiring multiple buffers to represent the full metadata area. A smaller
46buffer size will produce a smaller resulting read/write operation to the
47metadata area for small reads/writes. The metadata is still read even in
48a full write to the data covered by a single buffer.
49
50To use the target for the first time:
51
521. overwrite the superblock with zeroes
532. load the dm-integrity target with one-sector size, the kernel driver
54   will format the device
553. unload the dm-integrity target
564. read the "provided_data_sectors" value from the superblock
575. load the dm-integrity target with the target size
58   "provided_data_sectors"
596. if you want to use dm-integrity with dm-crypt, load the dm-crypt target
60   with the size "provided_data_sectors"
61
62
63Target arguments:
64
651. the underlying block device
66
672. the number of reserved sector at the beginning of the device - the
68   dm-integrity won't read of write these sectors
69
703. the size of the integrity tag (if "-" is used, the size is taken from
71   the internal-hash algorithm)
72
734. mode:
74
75	D - direct writes (without journal)
76		in this mode, journaling is
77		not used and data sectors and integrity tags are written
78		separately. In case of crash, it is possible that the data
79		and integrity tag doesn't match.
80	J - journaled writes
81		data and integrity tags are written to the
82		journal and atomicity is guaranteed. In case of crash,
83		either both data and tag or none of them are written. The
84		journaled mode degrades write throughput twice because the
85		data have to be written twice.
86	B - bitmap mode - data and metadata are written without any
87		synchronization, the driver maintains a bitmap of dirty
88		regions where data and metadata don't match. This mode can
89		only be used with internal hash.
90	R - recovery mode - in this mode, journal is not replayed,
91		checksums are not checked and writes to the device are not
92		allowed. This mode is useful for data recovery if the
93		device cannot be activated in any of the other standard
94		modes.
95	I - inline mode - in this mode, dm-integrity will store integrity
96		data directly in the underlying device sectors.
97		The underlying device must have an integrity profile that
98		allows storing user integrity data and provides enough
99		space for the selected integrity tag.
100
1015. the number of additional arguments
102
103Additional arguments:
104
105journal_sectors:number
106	The size of journal, this argument is used only if formatting the
107	device. If the device is already formatted, the value from the
108	superblock is used.
109
110interleave_sectors:number (default 32768)
111	The number of interleaved sectors. This values is rounded down to
112	a power of two. If the device is already formatted, the value from
113	the superblock is used.
114
115meta_device:device
116	Don't interleave the data and metadata on the device. Use a
117	separate device for metadata.
118
119buffer_sectors:number (default 128)
120	The number of sectors in one metadata buffer. The value is rounded
121	down to a power of two.
122
123journal_watermark:number (default 50)
124	The journal watermark in percents. When the size of the journal
125	exceeds this watermark, the thread that flushes the journal will
126	be started.
127
128commit_time:number (default 10000)
129	Commit time in milliseconds. When this time passes, the journal is
130	written. The journal is also written immediately if the FLUSH
131	request is received.
132
133internal_hash:algorithm(:key)	(the key is optional)
134	Use internal hash or crc.
135	When this argument is used, the dm-integrity target won't accept
136	integrity tags from the upper target, but it will automatically
137	generate and verify the integrity tags.
138
139	You can use a crc algorithm (such as crc32), then integrity target
140	will protect the data against accidental corruption.
141	You can also use a hmac algorithm (for example
142	"hmac(sha256):0123456789abcdef"), in this mode it will provide
143	cryptographic authentication of the data without encryption.
144
145	When this argument is not used, the integrity tags are accepted
146	from an upper layer target, such as dm-crypt. The upper layer
147	target should check the validity of the integrity tags.
148
149recalculate
150	Recalculate the integrity tags automatically. It is only valid
151	when using internal hash.
152
153journal_crypt:algorithm(:key)	(the key is optional)
154	Encrypt the journal using given algorithm to make sure that the
155	attacker can't read the journal. You can use a block cipher here
156	(such as "cbc(aes)") or a stream cipher (for example "chacha20"
157	or "ctr(aes)").
158
159	The journal contains history of last writes to the block device,
160	an attacker reading the journal could see the last sector numbers
161	that were written. From the sector numbers, the attacker can infer
162	the size of files that were written. To protect against this
163	situation, you can encrypt the journal.
164
165journal_mac:algorithm(:key)	(the key is optional)
166	Protect sector numbers in the journal from accidental or malicious
167	modification. To protect against accidental modification, use a
168	crc algorithm, to protect against malicious modification, use a
169	hmac algorithm with a key.
170
171	This option is not needed when using internal-hash because in this
172	mode, the integrity of journal entries is checked when replaying
173	the journal. Thus, modified sector number would be detected at
174	this stage.
175
176block_size:number (default 512)
177	The size of a data block in bytes. The larger the block size the
178	less overhead there is for per-block integrity metadata.
179	Supported values are 512, 1024, 2048 and 4096 bytes.
180
181sectors_per_bit:number
182	In the bitmap mode, this parameter specifies the number of
183	512-byte sectors that corresponds to one bitmap bit.
184
185bitmap_flush_interval:number
186	The bitmap flush interval in milliseconds. The metadata buffers
187	are synchronized when this interval expires.
188
189allow_discards
190	Allow block discard requests (a.k.a. TRIM) for the integrity device.
191	Discards are only allowed to devices using internal hash.
192
193	A discarded block is marked with a constant filler tag that anyone
194	with raw write access to the backing device can forge without the
195	key. Use allow_discards_keyed instead on new volumes.
196
197allow_discards_keyed
198	Like allow_discards, but marks a discarded block with a keyed
199	checksum of the sector number, HMAC_key(salt || sector), instead of
200	the constant filler tag, so it can't be forged without the
201	integrity key.
202
203	Not compatible with volumes that already have discarded blocks
204	marked the old way; only use on a freshly formatted volume.
205
206fix_padding
207	Use a smaller padding of the tag area that is more
208	space-efficient. If this option is not present, large padding is
209	used - that is for compatibility with older kernels.
210
211fix_hmac
212	Improve security of internal_hash and journal_mac:
213
214	- the section number is mixed to the mac, so that an attacker can't
215	  copy sectors from one journal section to another journal section
216	- the superblock is protected by journal_mac
217	- a 16-byte salt stored in the superblock is mixed to the mac, so
218	  that the attacker can't detect that two disks have the same hmac
219	  key and also to disallow the attacker to move sectors from one
220	  disk to another
221
222legacy_recalculate
223	Allow recalculating of volumes with HMAC keys. This is disabled by
224	default for security reasons - an attacker could modify the volume,
225	set recalc_sector to zero, and the kernel would not detect the
226	modification.
227
228The journal mode (D/J), buffer_sectors, journal_watermark, commit_time and
229allow_discards can be changed when reloading the target (load an inactive
230table and swap the tables with suspend and resume). The other arguments
231should not be changed when reloading the target because the layout of disk
232data depend on them and the reloaded target would be non-functional.
233
234For example, on a device using the default interleave_sectors of 32768, a
235block_size of 512, and an internal_hash of crc32c with a tag size of 4
236bytes, it will take 128 KiB of tags to track a full data area, requiring
237256 sectors of metadata per data area. With the default buffer_sectors of
238128, that means there will be 2 buffers per metadata area, or 2 buffers
239per 16 MiB of data.
240
241Status line:
242
2431. the number of integrity mismatches
2442. provided data sectors - that is the number of sectors that the user
245   could use
2463. the current recalculating position (or '-' if we didn't recalculate)
247
248
249The layout of the formatted block device:
250
251* reserved sectors
252    (they are not used by this target, they can be used for
253    storing LUKS metadata or for other purpose), the size of the reserved
254    area is specified in the target arguments
255
256* superblock (4kiB)
257	* magic string - identifies that the device was formatted
258	* version
259	* log2(interleave sectors)
260	* integrity tag size
261	* the number of journal sections
262	* provided data sectors - the number of sectors that this target
263	  provides (i.e. the size of the device minus the size of all
264	  metadata and padding). The user of this target should not send
265	  bios that access data beyond the "provided data sectors" limit.
266	* flags
267	    SB_FLAG_HAVE_JOURNAL_MAC
268		- a flag is set if journal_mac is used
269	    SB_FLAG_RECALCULATING
270		- recalculating is in progress
271	    SB_FLAG_DIRTY_BITMAP
272		- journal area contains the bitmap of dirty
273		  blocks
274	* log2(sectors per block)
275	* a position where recalculating finished
276* journal
277	The journal is divided into sections, each section contains:
278
279	* metadata area (4kiB), it contains journal entries
280
281	  - every journal entry contains:
282
283		* logical sector (specifies where the data and tag should
284		  be written)
285		* last 8 bytes of data
286		* integrity tag (the size is specified in the superblock)
287
288	  - every metadata sector ends with
289
290		* mac (8-bytes), all the macs in 8 metadata sectors form a
291		  64-byte value. It is used to store hmac of sector
292		  numbers in the journal section, to protect against a
293		  possibility that the attacker tampers with sector
294		  numbers in the journal.
295		* commit id
296
297	* data area (the size is variable; it depends on how many journal
298	  entries fit into the metadata area)
299
300	    - every sector in the data area contains:
301
302		* data (504 bytes of data, the last 8 bytes are stored in
303		  the journal entry)
304		* commit id
305
306	To test if the whole journal section was written correctly, every
307	512-byte sector of the journal ends with 8-byte commit id. If the
308	commit id matches on all sectors in a journal section, then it is
309	assumed that the section was written correctly. If the commit id
310	doesn't match, the section was written partially and it should not
311	be replayed.
312
313* one or more runs of interleaved tags and data.
314    Each run contains:
315
316	* tag area - it contains integrity tags. There is one tag for each
317	  sector in the data area. The size of this area is always 4KiB or
318	  greater.
319	* data area - it contains data sectors. The number of data sectors
320	  in one run must be a power of two. log2 of this value is stored
321	  in the superblock.
322