1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012 Red Hat, Inc.
4 *
5 * Author: Mikulas Patocka <mpatocka@redhat.com>
6 *
7 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
8 *
9 * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set
10 * default prefetch value. Data are read in "prefetch_cluster" chunks from the
11 * hash device. Setting this greatly improves performance when data and hash
12 * are on the same disk on different partitions on devices with poor random
13 * access behavior.
14 */
15
16#include "dm-verity.h"
17#include "dm-verity-fec.h"
18#include "dm-verity-verify-sig.h"
19#include <linux/module.h>
20#include <linux/reboot.h>
21
22#define DM_MSG_PREFIX			"verity"
23
24#define DM_VERITY_ENV_LENGTH		42
25#define DM_VERITY_ENV_VAR_NAME		"DM_VERITY_ERR_BLOCK_NR"
26
27#define DM_VERITY_DEFAULT_PREFETCH_SIZE	262144
28
29#define DM_VERITY_MAX_CORRUPTED_ERRS	100
30
31#define DM_VERITY_OPT_LOGGING		"ignore_corruption"
32#define DM_VERITY_OPT_RESTART		"restart_on_corruption"
33#define DM_VERITY_OPT_PANIC		"panic_on_corruption"
34#define DM_VERITY_OPT_IGN_ZEROES	"ignore_zero_blocks"
35#define DM_VERITY_OPT_AT_MOST_ONCE	"check_at_most_once"
36
37#define DM_VERITY_OPTS_MAX		(3 + DM_VERITY_OPTS_FEC + \
38					 DM_VERITY_ROOT_HASH_VERIFICATION_OPTS)
39
40static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
41
42module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
43
44struct dm_verity_prefetch_work {
45	struct work_struct work;
46	struct dm_verity *v;
47	sector_t block;
48	unsigned n_blocks;
49};
50
51/*
52 * Auxiliary structure appended to each dm-bufio buffer. If the value
53 * hash_verified is nonzero, hash of the block has been verified.
54 *
55 * The variable hash_verified is set to 0 when allocating the buffer, then
56 * it can be changed to 1 and it is never reset to 0 again.
57 *
58 * There is no lock around this value, a race condition can at worst cause
59 * that multiple processes verify the hash of the same buffer simultaneously
60 * and write 1 to hash_verified simultaneously.
61 * This condition is harmless, so we don't need locking.
62 */
63struct buffer_aux {
64	int hash_verified;
65};
66
67/*
68 * Initialize struct buffer_aux for a freshly created buffer.
69 */
70static void dm_bufio_alloc_callback(struct dm_buffer *buf)
71{
72	struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
73
74	aux->hash_verified = 0;
75}
76
77/*
78 * Translate input sector number to the sector number on the target device.
79 */
80static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
81{
82	return v->data_start + dm_target_offset(v->ti, bi_sector);
83}
84
85/*
86 * Return hash position of a specified block at a specified tree level
87 * (0 is the lowest level).
88 * The lowest "hash_per_block_bits"-bits of the result denote hash position
89 * inside a hash block. The remaining bits denote location of the hash block.
90 */
91static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
92					 int level)
93{
94	return block >> (level * v->hash_per_block_bits);
95}
96
97static int verity_hash_update(struct dm_verity *v, struct ahash_request *req,
98				const u8 *data, size_t len,
99				struct crypto_wait *wait)
100{
101	struct scatterlist sg;
102
103	if (likely(!is_vmalloc_addr(data))) {
104		sg_init_one(&sg, data, len);
105		ahash_request_set_crypt(req, &sg, NULL, len);
106		return crypto_wait_req(crypto_ahash_update(req), wait);
107	} else {
108		do {
109			int r;
110			size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data));
111			flush_kernel_vmap_range((void *)data, this_step);
112			sg_init_table(&sg, 1);
113			sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data));
114			ahash_request_set_crypt(req, &sg, NULL, this_step);
115			r = crypto_wait_req(crypto_ahash_update(req), wait);
116			if (unlikely(r))
117				return r;
118			data += this_step;
119			len -= this_step;
120		} while (len);
121		return 0;
122	}
123}
124
125/*
126 * Wrapper for crypto_ahash_init, which handles verity salting.
127 */
128static int verity_hash_init(struct dm_verity *v, struct ahash_request *req,
129				struct crypto_wait *wait)
130{
131	int r;
132
133	ahash_request_set_tfm(req, v->tfm);
134	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
135					CRYPTO_TFM_REQ_MAY_BACKLOG,
136					crypto_req_done, (void *)wait);
137	crypto_init_wait(wait);
138
139	r = crypto_wait_req(crypto_ahash_init(req), wait);
140
141	if (unlikely(r < 0)) {
142		DMERR("crypto_ahash_init failed: %d", r);
143		return r;
144	}
145
146	if (likely(v->salt_size && (v->version >= 1)))
147		r = verity_hash_update(v, req, v->salt, v->salt_size, wait);
148
149	return r;
150}
151
152static int verity_hash_final(struct dm_verity *v, struct ahash_request *req,
153			     u8 *digest, struct crypto_wait *wait)
154{
155	int r;
156
157	if (unlikely(v->salt_size && (!v->version))) {
158		r = verity_hash_update(v, req, v->salt, v->salt_size, wait);
159
160		if (r < 0) {
161			DMERR("verity_hash_final failed updating salt: %d", r);
162			goto out;
163		}
164	}
165
166	ahash_request_set_crypt(req, NULL, digest, 0);
167	r = crypto_wait_req(crypto_ahash_final(req), wait);
168out:
169	return r;
170}
171
172int verity_hash(struct dm_verity *v, struct ahash_request *req,
173		const u8 *data, size_t len, u8 *digest)
174{
175	int r;
176	struct crypto_wait wait;
177
178	r = verity_hash_init(v, req, &wait);
179	if (unlikely(r < 0))
180		goto out;
181
182	r = verity_hash_update(v, req, data, len, &wait);
183	if (unlikely(r < 0))
184		goto out;
185
186	r = verity_hash_final(v, req, digest, &wait);
187
188out:
189	return r;
190}
191
192static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
193				 sector_t *hash_block, unsigned *offset)
194{
195	sector_t position = verity_position_at_level(v, block, level);
196	unsigned idx;
197
198	*hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
199
200	if (!offset)
201		return;
202
203	idx = position & ((1 << v->hash_per_block_bits) - 1);
204	if (!v->version)
205		*offset = idx * v->digest_size;
206	else
207		*offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
208}
209
210/*
211 * Handle verification errors.
212 */
213static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
214			     unsigned long long block)
215{
216	char verity_env[DM_VERITY_ENV_LENGTH];
217	char *envp[] = { verity_env, NULL };
218	const char *type_str = "";
219	struct mapped_device *md = dm_table_get_md(v->ti->table);
220
221	/* Corruption should be visible in device status in all modes */
222	v->hash_failed = 1;
223
224	if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
225		goto out;
226
227	v->corrupted_errs++;
228
229	switch (type) {
230	case DM_VERITY_BLOCK_TYPE_DATA:
231		type_str = "data";
232		break;
233	case DM_VERITY_BLOCK_TYPE_METADATA:
234		type_str = "metadata";
235		break;
236	default:
237		BUG();
238	}
239
240	DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name,
241		    type_str, block);
242
243	if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
244		DMERR("%s: reached maximum errors", v->data_dev->name);
245
246	snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
247		DM_VERITY_ENV_VAR_NAME, type, block);
248
249	kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
250
251out:
252	if (v->mode == DM_VERITY_MODE_LOGGING)
253		return 0;
254
255	if (v->mode == DM_VERITY_MODE_RESTART)
256		kernel_restart("dm-verity device corrupted");
257
258	if (v->mode == DM_VERITY_MODE_PANIC)
259		panic("dm-verity device corrupted");
260
261	return 1;
262}
263
264/*
265 * Verify hash of a metadata block pertaining to the specified data block
266 * ("block" argument) at a specified level ("level" argument).
267 *
268 * On successful return, verity_io_want_digest(v, io) contains the hash value
269 * for a lower tree level or for the data block (if we're at the lowest level).
270 *
271 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
272 * If "skip_unverified" is false, unverified buffer is hashed and verified
273 * against current value of verity_io_want_digest(v, io).
274 */
275static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
276			       sector_t block, int level, bool skip_unverified,
277			       u8 *want_digest)
278{
279	struct dm_buffer *buf;
280	struct buffer_aux *aux;
281	u8 *data;
282	int r;
283	sector_t hash_block;
284	unsigned offset;
285
286	verity_hash_at_level(v, block, level, &hash_block, &offset);
287
288	data = dm_bufio_read(v->bufio, hash_block, &buf);
289	if (IS_ERR(data))
290		return PTR_ERR(data);
291
292	aux = dm_bufio_get_aux_data(buf);
293
294	if (!aux->hash_verified) {
295		if (skip_unverified) {
296			r = 1;
297			goto release_ret_r;
298		}
299
300		r = verity_hash(v, verity_io_hash_req(v, io),
301				data, 1 << v->hash_dev_block_bits,
302				verity_io_real_digest(v, io));
303		if (unlikely(r < 0))
304			goto release_ret_r;
305
306		if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
307				  v->digest_size) == 0))
308			aux->hash_verified = 1;
309		else if (verity_fec_decode(v, io,
310					   DM_VERITY_BLOCK_TYPE_METADATA,
311					   hash_block, data, NULL) == 0)
312			aux->hash_verified = 1;
313		else if (verity_handle_err(v,
314					   DM_VERITY_BLOCK_TYPE_METADATA,
315					   hash_block)) {
316			r = -EIO;
317			goto release_ret_r;
318		}
319	}
320
321	data += offset;
322	memcpy(want_digest, data, v->digest_size);
323	r = 0;
324
325release_ret_r:
326	dm_bufio_release(buf);
327	return r;
328}
329
330/*
331 * Find a hash for a given block, write it to digest and verify the integrity
332 * of the hash tree if necessary.
333 */
334int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
335			  sector_t block, u8 *digest, bool *is_zero)
336{
337	int r = 0, i;
338
339	if (likely(v->levels)) {
340		/*
341		 * First, we try to get the requested hash for
342		 * the current block. If the hash block itself is
343		 * verified, zero is returned. If it isn't, this
344		 * function returns 1 and we fall back to whole
345		 * chain verification.
346		 */
347		r = verity_verify_level(v, io, block, 0, true, digest);
348		if (likely(r <= 0))
349			goto out;
350	}
351
352	memcpy(digest, v->root_digest, v->digest_size);
353
354	for (i = v->levels - 1; i >= 0; i--) {
355		r = verity_verify_level(v, io, block, i, false, digest);
356		if (unlikely(r))
357			goto out;
358	}
359out:
360	if (!r && v->zero_digest)
361		*is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
362	else
363		*is_zero = false;
364
365	return r;
366}
367
368/*
369 * Calculates the digest for the given bio
370 */
371static int verity_for_io_block(struct dm_verity *v, struct dm_verity_io *io,
372			       struct bvec_iter *iter, struct crypto_wait *wait)
373{
374	unsigned int todo = 1 << v->data_dev_block_bits;
375	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
376	struct scatterlist sg;
377	struct ahash_request *req = verity_io_hash_req(v, io);
378
379	do {
380		int r;
381		unsigned int len;
382		struct bio_vec bv = bio_iter_iovec(bio, *iter);
383
384		sg_init_table(&sg, 1);
385
386		len = bv.bv_len;
387
388		if (likely(len >= todo))
389			len = todo;
390		/*
391		 * Operating on a single page at a time looks suboptimal
392		 * until you consider the typical block size is 4,096B.
393		 * Going through this loops twice should be very rare.
394		 */
395		sg_set_page(&sg, bv.bv_page, len, bv.bv_offset);
396		ahash_request_set_crypt(req, &sg, NULL, len);
397		r = crypto_wait_req(crypto_ahash_update(req), wait);
398
399		if (unlikely(r < 0)) {
400			DMERR("verity_for_io_block crypto op failed: %d", r);
401			return r;
402		}
403
404		bio_advance_iter(bio, iter, len);
405		todo -= len;
406	} while (todo);
407
408	return 0;
409}
410
411/*
412 * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
413 * starting from iter.
414 */
415int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
416			struct bvec_iter *iter,
417			int (*process)(struct dm_verity *v,
418				       struct dm_verity_io *io, u8 *data,
419				       size_t len))
420{
421	unsigned todo = 1 << v->data_dev_block_bits;
422	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
423
424	do {
425		int r;
426		u8 *page;
427		unsigned len;
428		struct bio_vec bv = bio_iter_iovec(bio, *iter);
429
430		page = kmap_atomic(bv.bv_page);
431		len = bv.bv_len;
432
433		if (likely(len >= todo))
434			len = todo;
435
436		r = process(v, io, page + bv.bv_offset, len);
437		kunmap_atomic(page);
438
439		if (r < 0)
440			return r;
441
442		bio_advance_iter(bio, iter, len);
443		todo -= len;
444	} while (todo);
445
446	return 0;
447}
448
449static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
450			  u8 *data, size_t len)
451{
452	memset(data, 0, len);
453	return 0;
454}
455
456/*
457 * Moves the bio iter one data block forward.
458 */
459static inline void verity_bv_skip_block(struct dm_verity *v,
460					struct dm_verity_io *io,
461					struct bvec_iter *iter)
462{
463	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
464
465	bio_advance_iter(bio, iter, 1 << v->data_dev_block_bits);
466}
467
468/*
469 * Verify one "dm_verity_io" structure.
470 */
471static int verity_verify_io(struct dm_verity_io *io)
472{
473	bool is_zero;
474	struct dm_verity *v = io->v;
475	struct bvec_iter start;
476	unsigned b;
477	struct crypto_wait wait;
478	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
479
480	for (b = 0; b < io->n_blocks; b++) {
481		int r;
482		sector_t cur_block = io->block + b;
483		struct ahash_request *req = verity_io_hash_req(v, io);
484
485		if (v->validated_blocks && bio->bi_status == BLK_STS_OK &&
486		    likely(test_bit(cur_block, v->validated_blocks))) {
487			verity_bv_skip_block(v, io, &io->iter);
488			continue;
489		}
490
491		r = verity_hash_for_block(v, io, cur_block,
492					  verity_io_want_digest(v, io),
493					  &is_zero);
494		if (unlikely(r < 0))
495			return r;
496
497		if (is_zero) {
498			/*
499			 * If we expect a zero block, don't validate, just
500			 * return zeros.
501			 */
502			r = verity_for_bv_block(v, io, &io->iter,
503						verity_bv_zero);
504			if (unlikely(r < 0))
505				return r;
506
507			continue;
508		}
509
510		r = verity_hash_init(v, req, &wait);
511		if (unlikely(r < 0))
512			return r;
513
514		start = io->iter;
515		r = verity_for_io_block(v, io, &io->iter, &wait);
516		if (unlikely(r < 0))
517			return r;
518
519		r = verity_hash_final(v, req, verity_io_real_digest(v, io),
520					&wait);
521		if (unlikely(r < 0))
522			return r;
523
524		if (likely(memcmp(verity_io_real_digest(v, io),
525				  verity_io_want_digest(v, io), v->digest_size) == 0)) {
526			if (v->validated_blocks)
527				set_bit(cur_block, v->validated_blocks);
528			continue;
529		}
530		else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
531					   cur_block, NULL, &start) == 0)
532			continue;
533		else {
534			if (bio->bi_status) {
535				/*
536				 * Error correction failed; Just return error
537				 */
538				return -EIO;
539			}
540			if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
541					      cur_block))
542				return -EIO;
543		}
544	}
545
546	return 0;
547}
548
549/*
550 * Skip verity work in response to I/O error when system is shutting down.
551 */
552static inline bool verity_is_system_shutting_down(void)
553{
554	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
555		|| system_state == SYSTEM_RESTART;
556}
557
558/*
559 * End one "io" structure with a given error.
560 */
561static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
562{
563	struct dm_verity *v = io->v;
564	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
565
566	bio->bi_end_io = io->orig_bi_end_io;
567	bio->bi_status = status;
568
569	verity_fec_finish_io(io);
570
571	bio_endio(bio);
572}
573
574static void verity_work(struct work_struct *w)
575{
576	struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
577
578	verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
579}
580
581static void verity_end_io(struct bio *bio)
582{
583	struct dm_verity_io *io = bio->bi_private;
584
585	if (bio->bi_status &&
586	    (!verity_fec_is_enabled(io->v) ||
587	     verity_is_system_shutting_down() ||
588	     (bio->bi_opf & REQ_RAHEAD))) {
589		verity_finish_io(io, bio->bi_status);
590		return;
591	}
592
593	INIT_WORK(&io->work, verity_work);
594	queue_work(io->v->verify_wq, &io->work);
595}
596
597/*
598 * Prefetch buffers for the specified io.
599 * The root buffer is not prefetched, it is assumed that it will be cached
600 * all the time.
601 */
602static void verity_prefetch_io(struct work_struct *work)
603{
604	struct dm_verity_prefetch_work *pw =
605		container_of(work, struct dm_verity_prefetch_work, work);
606	struct dm_verity *v = pw->v;
607	int i;
608
609	for (i = v->levels - 2; i >= 0; i--) {
610		sector_t hash_block_start;
611		sector_t hash_block_end;
612		verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
613		verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
614		if (!i) {
615			unsigned cluster = READ_ONCE(dm_verity_prefetch_cluster);
616
617			cluster >>= v->data_dev_block_bits;
618			if (unlikely(!cluster))
619				goto no_prefetch_cluster;
620
621			if (unlikely(cluster & (cluster - 1)))
622				cluster = 1 << __fls(cluster);
623
624			hash_block_start &= ~(sector_t)(cluster - 1);
625			hash_block_end |= cluster - 1;
626			if (unlikely(hash_block_end >= v->hash_blocks))
627				hash_block_end = v->hash_blocks - 1;
628		}
629no_prefetch_cluster:
630		dm_bufio_prefetch(v->bufio, hash_block_start,
631				  hash_block_end - hash_block_start + 1);
632	}
633
634	kfree(pw);
635}
636
637static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
638{
639	sector_t block = io->block;
640	unsigned int n_blocks = io->n_blocks;
641	struct dm_verity_prefetch_work *pw;
642
643	if (v->validated_blocks) {
644		while (n_blocks && test_bit(block, v->validated_blocks)) {
645			block++;
646			n_blocks--;
647		}
648		while (n_blocks && test_bit(block + n_blocks - 1,
649					    v->validated_blocks))
650			n_blocks--;
651		if (!n_blocks)
652			return;
653	}
654
655	pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
656		GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
657
658	if (!pw)
659		return;
660
661	INIT_WORK(&pw->work, verity_prefetch_io);
662	pw->v = v;
663	pw->block = block;
664	pw->n_blocks = n_blocks;
665	queue_work(v->verify_wq, &pw->work);
666}
667
668/*
669 * Bio map function. It allocates dm_verity_io structure and bio vector and
670 * fills them. Then it issues prefetches and the I/O.
671 */
672static int verity_map(struct dm_target *ti, struct bio *bio)
673{
674	struct dm_verity *v = ti->private;
675	struct dm_verity_io *io;
676
677	bio_set_dev(bio, v->data_dev->bdev);
678	bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
679
680	if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
681	    ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
682		DMERR_LIMIT("unaligned io");
683		return DM_MAPIO_KILL;
684	}
685
686	if (bio_end_sector(bio) >>
687	    (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
688		DMERR_LIMIT("io out of range");
689		return DM_MAPIO_KILL;
690	}
691
692	if (bio_data_dir(bio) == WRITE)
693		return DM_MAPIO_KILL;
694
695	io = dm_per_bio_data(bio, ti->per_io_data_size);
696	io->v = v;
697	io->orig_bi_end_io = bio->bi_end_io;
698	io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
699	io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
700
701	bio->bi_end_io = verity_end_io;
702	bio->bi_private = io;
703	io->iter = bio->bi_iter;
704
705	verity_fec_init_io(io);
706
707	verity_submit_prefetch(v, io);
708
709	submit_bio_noacct(bio);
710
711	return DM_MAPIO_SUBMITTED;
712}
713
714/*
715 * Status: V (valid) or C (corruption found)
716 */
717static void verity_status(struct dm_target *ti, status_type_t type,
718			  unsigned status_flags, char *result, unsigned maxlen)
719{
720	struct dm_verity *v = ti->private;
721	unsigned args = 0;
722	unsigned sz = 0;
723	unsigned x;
724
725	switch (type) {
726	case STATUSTYPE_INFO:
727		DMEMIT("%c", v->hash_failed ? 'C' : 'V');
728		break;
729	case STATUSTYPE_TABLE:
730		DMEMIT("%u %s %s %u %u %llu %llu %s ",
731			v->version,
732			v->data_dev->name,
733			v->hash_dev->name,
734			1 << v->data_dev_block_bits,
735			1 << v->hash_dev_block_bits,
736			(unsigned long long)v->data_blocks,
737			(unsigned long long)v->hash_start,
738			v->alg_name
739			);
740		for (x = 0; x < v->digest_size; x++)
741			DMEMIT("%02x", v->root_digest[x]);
742		DMEMIT(" ");
743		if (!v->salt_size)
744			DMEMIT("-");
745		else
746			for (x = 0; x < v->salt_size; x++)
747				DMEMIT("%02x", v->salt[x]);
748		if (v->mode != DM_VERITY_MODE_EIO)
749			args++;
750		if (verity_fec_is_enabled(v))
751			args += DM_VERITY_OPTS_FEC;
752		if (v->zero_digest)
753			args++;
754		if (v->validated_blocks)
755			args++;
756		if (v->signature_key_desc)
757			args += DM_VERITY_ROOT_HASH_VERIFICATION_OPTS;
758		if (!args)
759			return;
760		DMEMIT(" %u", args);
761		if (v->mode != DM_VERITY_MODE_EIO) {
762			DMEMIT(" ");
763			switch (v->mode) {
764			case DM_VERITY_MODE_LOGGING:
765				DMEMIT(DM_VERITY_OPT_LOGGING);
766				break;
767			case DM_VERITY_MODE_RESTART:
768				DMEMIT(DM_VERITY_OPT_RESTART);
769				break;
770			case DM_VERITY_MODE_PANIC:
771				DMEMIT(DM_VERITY_OPT_PANIC);
772				break;
773			default:
774				BUG();
775			}
776		}
777		if (v->zero_digest)
778			DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
779		if (v->validated_blocks)
780			DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE);
781		sz = verity_fec_status_table(v, sz, result, maxlen);
782		if (v->signature_key_desc)
783			DMEMIT(" " DM_VERITY_ROOT_HASH_VERIFICATION_OPT_SIG_KEY
784				" %s", v->signature_key_desc);
785		break;
786	}
787}
788
789static int verity_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
790{
791	struct dm_verity *v = ti->private;
792
793	*bdev = v->data_dev->bdev;
794
795	if (v->data_start ||
796	    ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
797		return 1;
798	return 0;
799}
800
801static int verity_iterate_devices(struct dm_target *ti,
802				  iterate_devices_callout_fn fn, void *data)
803{
804	struct dm_verity *v = ti->private;
805
806	return fn(ti, v->data_dev, v->data_start, ti->len, data);
807}
808
809static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
810{
811	struct dm_verity *v = ti->private;
812
813	if (limits->logical_block_size < 1 << v->data_dev_block_bits)
814		limits->logical_block_size = 1 << v->data_dev_block_bits;
815
816	if (limits->physical_block_size < 1 << v->data_dev_block_bits)
817		limits->physical_block_size = 1 << v->data_dev_block_bits;
818
819	blk_limits_io_min(limits, limits->logical_block_size);
820}
821
822static void verity_dtr(struct dm_target *ti)
823{
824	struct dm_verity *v = ti->private;
825
826	if (v->verify_wq)
827		destroy_workqueue(v->verify_wq);
828
829	if (v->bufio)
830		dm_bufio_client_destroy(v->bufio);
831
832	kvfree(v->validated_blocks);
833	kfree(v->salt);
834	kfree(v->root_digest);
835	kfree(v->zero_digest);
836
837	if (v->tfm)
838		crypto_free_ahash(v->tfm);
839
840	kfree(v->alg_name);
841
842	if (v->hash_dev)
843		dm_put_device(ti, v->hash_dev);
844
845	if (v->data_dev)
846		dm_put_device(ti, v->data_dev);
847
848	verity_fec_dtr(v);
849
850	kfree(v->signature_key_desc);
851
852	kfree(v);
853}
854
855static int verity_alloc_most_once(struct dm_verity *v)
856{
857	struct dm_target *ti = v->ti;
858
859	/* the bitset can only handle INT_MAX blocks */
860	if (v->data_blocks > INT_MAX) {
861		ti->error = "device too large to use check_at_most_once";
862		return -E2BIG;
863	}
864
865	v->validated_blocks = kvcalloc(BITS_TO_LONGS(v->data_blocks),
866				       sizeof(unsigned long),
867				       GFP_KERNEL);
868	if (!v->validated_blocks) {
869		ti->error = "failed to allocate bitset for check_at_most_once";
870		return -ENOMEM;
871	}
872
873	return 0;
874}
875
876static int verity_alloc_zero_digest(struct dm_verity *v)
877{
878	int r = -ENOMEM;
879	struct ahash_request *req;
880	u8 *zero_data;
881
882	v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
883
884	if (!v->zero_digest)
885		return r;
886
887	req = kmalloc(v->ahash_reqsize, GFP_KERNEL);
888
889	if (!req)
890		return r; /* verity_dtr will free zero_digest */
891
892	zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
893
894	if (!zero_data)
895		goto out;
896
897	r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits,
898			v->zero_digest);
899
900out:
901	kfree(req);
902	kfree(zero_data);
903
904	return r;
905}
906
907static inline bool verity_is_verity_mode(const char *arg_name)
908{
909	return (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING) ||
910		!strcasecmp(arg_name, DM_VERITY_OPT_RESTART) ||
911		!strcasecmp(arg_name, DM_VERITY_OPT_PANIC));
912}
913
914static int verity_parse_verity_mode(struct dm_verity *v, const char *arg_name)
915{
916	if (v->mode)
917		return -EINVAL;
918
919	if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING))
920		v->mode = DM_VERITY_MODE_LOGGING;
921	else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART))
922		v->mode = DM_VERITY_MODE_RESTART;
923	else if (!strcasecmp(arg_name, DM_VERITY_OPT_PANIC))
924		v->mode = DM_VERITY_MODE_PANIC;
925
926	return 0;
927}
928
929static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
930				 struct dm_verity_sig_opts *verify_args)
931{
932	int r;
933	unsigned argc;
934	struct dm_target *ti = v->ti;
935	const char *arg_name;
936
937	static const struct dm_arg _args[] = {
938		{0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
939	};
940
941	r = dm_read_arg_group(_args, as, &argc, &ti->error);
942	if (r)
943		return -EINVAL;
944
945	if (!argc)
946		return 0;
947
948	do {
949		arg_name = dm_shift_arg(as);
950		argc--;
951
952		if (verity_is_verity_mode(arg_name)) {
953			r = verity_parse_verity_mode(v, arg_name);
954			if (r) {
955				ti->error = "Conflicting error handling parameters";
956				return r;
957			}
958			continue;
959
960		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
961			r = verity_alloc_zero_digest(v);
962			if (r) {
963				ti->error = "Cannot allocate zero digest";
964				return r;
965			}
966			continue;
967
968		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) {
969			r = verity_alloc_most_once(v);
970			if (r)
971				return r;
972			continue;
973
974		} else if (verity_is_fec_opt_arg(arg_name)) {
975			r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
976			if (r)
977				return r;
978			continue;
979		} else if (verity_verify_is_sig_opt_arg(arg_name)) {
980			r = verity_verify_sig_parse_opt_args(as, v,
981							     verify_args,
982							     &argc, arg_name);
983			if (r)
984				return r;
985			continue;
986
987		}
988
989		ti->error = "Unrecognized verity feature request";
990		return -EINVAL;
991	} while (argc && !r);
992
993	return r;
994}
995
996/*
997 * Target parameters:
998 *	<version>	The current format is version 1.
999 *			Vsn 0 is compatible with original Chromium OS releases.
1000 *	<data device>
1001 *	<hash device>
1002 *	<data block size>
1003 *	<hash block size>
1004 *	<the number of data blocks>
1005 *	<hash start block>
1006 *	<algorithm>
1007 *	<digest>
1008 *	<salt>		Hex string or "-" if no salt.
1009 */
1010static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
1011{
1012	struct dm_verity *v;
1013	struct dm_verity_sig_opts verify_args = {0};
1014	struct dm_arg_set as;
1015	unsigned int num;
1016	unsigned long long num_ll;
1017	int r;
1018	int i;
1019	sector_t hash_position;
1020	char dummy;
1021	char *root_hash_digest_to_validate;
1022
1023	v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
1024	if (!v) {
1025		ti->error = "Cannot allocate verity structure";
1026		return -ENOMEM;
1027	}
1028	ti->private = v;
1029	v->ti = ti;
1030
1031	r = verity_fec_ctr_alloc(v);
1032	if (r)
1033		goto bad;
1034
1035	if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
1036		ti->error = "Device must be readonly";
1037		r = -EINVAL;
1038		goto bad;
1039	}
1040
1041	if (argc < 10) {
1042		ti->error = "Not enough arguments";
1043		r = -EINVAL;
1044		goto bad;
1045	}
1046
1047	if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
1048	    num > 1) {
1049		ti->error = "Invalid version";
1050		r = -EINVAL;
1051		goto bad;
1052	}
1053	v->version = num;
1054
1055	r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
1056	if (r) {
1057		ti->error = "Data device lookup failed";
1058		goto bad;
1059	}
1060
1061	r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
1062	if (r) {
1063		ti->error = "Hash device lookup failed";
1064		goto bad;
1065	}
1066
1067	if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
1068	    !num || (num & (num - 1)) ||
1069	    num < bdev_logical_block_size(v->data_dev->bdev) ||
1070	    num > PAGE_SIZE) {
1071		ti->error = "Invalid data device block size";
1072		r = -EINVAL;
1073		goto bad;
1074	}
1075	v->data_dev_block_bits = __ffs(num);
1076
1077	if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
1078	    !num || (num & (num - 1)) ||
1079	    num < bdev_logical_block_size(v->hash_dev->bdev) ||
1080	    num > INT_MAX) {
1081		ti->error = "Invalid hash device block size";
1082		r = -EINVAL;
1083		goto bad;
1084	}
1085	v->hash_dev_block_bits = __ffs(num);
1086
1087	if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
1088	    (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
1089	    >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
1090		ti->error = "Invalid data blocks";
1091		r = -EINVAL;
1092		goto bad;
1093	}
1094	v->data_blocks = num_ll;
1095
1096	if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
1097		ti->error = "Data device is too small";
1098		r = -EINVAL;
1099		goto bad;
1100	}
1101
1102	if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
1103	    (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
1104	    >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
1105		ti->error = "Invalid hash start";
1106		r = -EINVAL;
1107		goto bad;
1108	}
1109	v->hash_start = num_ll;
1110
1111	v->alg_name = kstrdup(argv[7], GFP_KERNEL);
1112	if (!v->alg_name) {
1113		ti->error = "Cannot allocate algorithm name";
1114		r = -ENOMEM;
1115		goto bad;
1116	}
1117
1118	v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0);
1119	if (IS_ERR(v->tfm)) {
1120		ti->error = "Cannot initialize hash function";
1121		r = PTR_ERR(v->tfm);
1122		v->tfm = NULL;
1123		goto bad;
1124	}
1125
1126	/*
1127	 * dm-verity performance can vary greatly depending on which hash
1128	 * algorithm implementation is used.  Help people debug performance
1129	 * problems by logging the ->cra_driver_name.
1130	 */
1131	DMINFO("%s using implementation \"%s\"", v->alg_name,
1132	       crypto_hash_alg_common(v->tfm)->base.cra_driver_name);
1133
1134	v->digest_size = crypto_ahash_digestsize(v->tfm);
1135	if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
1136		ti->error = "Digest size too big";
1137		r = -EINVAL;
1138		goto bad;
1139	}
1140	v->ahash_reqsize = sizeof(struct ahash_request) +
1141		crypto_ahash_reqsize(v->tfm);
1142
1143	v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
1144	if (!v->root_digest) {
1145		ti->error = "Cannot allocate root digest";
1146		r = -ENOMEM;
1147		goto bad;
1148	}
1149	if (strlen(argv[8]) != v->digest_size * 2 ||
1150	    hex2bin(v->root_digest, argv[8], v->digest_size)) {
1151		ti->error = "Invalid root digest";
1152		r = -EINVAL;
1153		goto bad;
1154	}
1155	root_hash_digest_to_validate = argv[8];
1156
1157	if (strcmp(argv[9], "-")) {
1158		v->salt_size = strlen(argv[9]) / 2;
1159		v->salt = kmalloc(v->salt_size, GFP_KERNEL);
1160		if (!v->salt) {
1161			ti->error = "Cannot allocate salt";
1162			r = -ENOMEM;
1163			goto bad;
1164		}
1165		if (strlen(argv[9]) != v->salt_size * 2 ||
1166		    hex2bin(v->salt, argv[9], v->salt_size)) {
1167			ti->error = "Invalid salt";
1168			r = -EINVAL;
1169			goto bad;
1170		}
1171	}
1172
1173	argv += 10;
1174	argc -= 10;
1175
1176	/* Optional parameters */
1177	if (argc) {
1178		as.argc = argc;
1179		as.argv = argv;
1180
1181		r = verity_parse_opt_args(&as, v, &verify_args);
1182		if (r < 0)
1183			goto bad;
1184	}
1185
1186	/* Root hash signature is  a optional parameter*/
1187	r = verity_verify_root_hash(root_hash_digest_to_validate,
1188				    strlen(root_hash_digest_to_validate),
1189				    verify_args.sig,
1190				    verify_args.sig_size);
1191	if (r < 0) {
1192		ti->error = "Root hash verification failed";
1193		goto bad;
1194	}
1195	v->hash_per_block_bits =
1196		__fls((1 << v->hash_dev_block_bits) / v->digest_size);
1197
1198	v->levels = 0;
1199	if (v->data_blocks)
1200		while (v->hash_per_block_bits * v->levels < 64 &&
1201		       (unsigned long long)(v->data_blocks - 1) >>
1202		       (v->hash_per_block_bits * v->levels))
1203			v->levels++;
1204
1205	if (v->levels > DM_VERITY_MAX_LEVELS) {
1206		ti->error = "Too many tree levels";
1207		r = -E2BIG;
1208		goto bad;
1209	}
1210
1211	hash_position = v->hash_start;
1212	for (i = v->levels - 1; i >= 0; i--) {
1213		sector_t s;
1214		v->hash_level_block[i] = hash_position;
1215		s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
1216					>> ((i + 1) * v->hash_per_block_bits);
1217		if (hash_position + s < hash_position) {
1218			ti->error = "Hash device offset overflow";
1219			r = -E2BIG;
1220			goto bad;
1221		}
1222		hash_position += s;
1223	}
1224	v->hash_blocks = hash_position;
1225
1226	v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
1227		1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
1228		dm_bufio_alloc_callback, NULL);
1229	if (IS_ERR(v->bufio)) {
1230		ti->error = "Cannot initialize dm-bufio";
1231		r = PTR_ERR(v->bufio);
1232		v->bufio = NULL;
1233		goto bad;
1234	}
1235
1236	if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
1237		ti->error = "Hash device is too small";
1238		r = -E2BIG;
1239		goto bad;
1240	}
1241
1242	/* WQ_UNBOUND greatly improves performance when running on ramdisk */
1243	v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
1244	if (!v->verify_wq) {
1245		ti->error = "Cannot allocate workqueue";
1246		r = -ENOMEM;
1247		goto bad;
1248	}
1249
1250	ti->per_io_data_size = sizeof(struct dm_verity_io) +
1251				v->ahash_reqsize + v->digest_size * 2;
1252
1253	r = verity_fec_ctr(v);
1254	if (r)
1255		goto bad;
1256
1257	ti->per_io_data_size = roundup(ti->per_io_data_size,
1258				       __alignof__(struct dm_verity_io));
1259
1260	verity_verify_sig_opts_cleanup(&verify_args);
1261
1262	return 0;
1263
1264bad:
1265
1266	verity_verify_sig_opts_cleanup(&verify_args);
1267	verity_dtr(ti);
1268
1269	return r;
1270}
1271
1272static struct target_type verity_target = {
1273	.name		= "verity",
1274	.features	= DM_TARGET_IMMUTABLE,
1275	.version	= {1, 8, 0},
1276	.module		= THIS_MODULE,
1277	.ctr		= verity_ctr,
1278	.dtr		= verity_dtr,
1279	.map		= verity_map,
1280	.status		= verity_status,
1281	.prepare_ioctl	= verity_prepare_ioctl,
1282	.iterate_devices = verity_iterate_devices,
1283	.io_hints	= verity_io_hints,
1284};
1285
1286static int __init dm_verity_init(void)
1287{
1288	int r;
1289
1290	r = dm_register_target(&verity_target);
1291	if (r < 0)
1292		DMERR("register failed %d", r);
1293
1294	return r;
1295}
1296
1297static void __exit dm_verity_exit(void)
1298{
1299	dm_unregister_target(&verity_target);
1300}
1301
1302module_init(dm_verity_init);
1303module_exit(dm_verity_exit);
1304
1305MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1306MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1307MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1308MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
1309MODULE_LICENSE("GPL");
1310