xref: /linux/drivers/md/dm-verity.h (revision d632ab86aff2cef21f794e337a8e7f2320ac3973)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.
4  * Copyright (C) 2015 Google, Inc.
5  *
6  * Author: Mikulas Patocka <mpatocka@redhat.com>
7  *
8  * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
9  */
10 
11 #ifndef DM_VERITY_H
12 #define DM_VERITY_H
13 
14 #include <linux/dm-io.h>
15 #include <linux/dm-bufio.h>
16 #include <linux/device-mapper.h>
17 #include <linux/interrupt.h>
18 #include <crypto/hash.h>
19 
20 #define DM_VERITY_MAX_LEVELS		63
21 
22 enum verity_mode {
23 	DM_VERITY_MODE_EIO,
24 	DM_VERITY_MODE_LOGGING,
25 	DM_VERITY_MODE_RESTART,
26 	DM_VERITY_MODE_PANIC
27 };
28 
29 enum verity_block_type {
30 	DM_VERITY_BLOCK_TYPE_DATA,
31 	DM_VERITY_BLOCK_TYPE_METADATA
32 };
33 
34 struct dm_verity_fec;
35 
36 struct dm_verity {
37 	struct dm_dev *data_dev;
38 	struct dm_dev *hash_dev;
39 	struct dm_target *ti;
40 	struct dm_bufio_client *bufio;
41 	char *alg_name;
42 	struct crypto_shash *shash_tfm;
43 	u8 *root_digest;	/* digest of the root block */
44 	u8 *salt;		/* salt: its size is salt_size */
45 	u8 *initial_hashstate;	/* salted initial state, if version >= 1 */
46 	u8 *zero_digest;	/* digest for a zero block */
47 #ifdef CONFIG_SECURITY
48 	u8 *root_digest_sig;	/* signature of the root digest */
49 	unsigned int sig_size;	/* root digest signature size */
50 #endif /* CONFIG_SECURITY */
51 	unsigned int salt_size;
52 	sector_t hash_start;	/* hash start in blocks */
53 	sector_t data_blocks;	/* the number of data blocks */
54 	sector_t hash_blocks;	/* the number of hash blocks */
55 	unsigned char data_dev_block_bits;	/* log2(data blocksize) */
56 	unsigned char hash_dev_block_bits;	/* log2(hash blocksize) */
57 	unsigned char hash_per_block_bits;	/* log2(hashes in hash block) */
58 	unsigned char levels;	/* the number of tree levels */
59 	unsigned char version;
60 	bool hash_failed:1;	/* set if hash of any block failed */
61 	bool use_bh_wq:1;	/* try to verify in BH wq before normal work-queue */
62 	unsigned int digest_size;	/* digest size for the current hash algorithm */
63 	enum verity_mode mode;	/* mode for handling verification errors */
64 	enum verity_mode error_mode;/* mode for handling I/O errors */
65 	unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
66 
67 	struct workqueue_struct *verify_wq;
68 
69 	/* starting blocks for each tree level. 0 is the lowest level. */
70 	sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
71 
72 	struct dm_verity_fec *fec;	/* forward error correction */
73 	unsigned long *validated_blocks; /* bitset blocks validated */
74 
75 	char *signature_key_desc; /* signature keyring reference */
76 
77 	struct dm_io_client *io;
78 	mempool_t recheck_pool;
79 };
80 
81 struct dm_verity_io {
82 	struct dm_verity *v;
83 
84 	/* original value of bio->bi_end_io */
85 	bio_end_io_t *orig_bi_end_io;
86 
87 	struct bvec_iter iter;
88 
89 	sector_t block;
90 	unsigned int n_blocks;
91 	bool in_bh;
92 	bool had_mismatch;
93 
94 	struct work_struct work;
95 	struct work_struct bh_work;
96 
97 	u8 real_digest[HASH_MAX_DIGESTSIZE];
98 	u8 want_digest[HASH_MAX_DIGESTSIZE];
99 
100 	/*
101 	 * Temporary space for hashing.  This is variable-length and must be at
102 	 * the end of the struct.  struct shash_desc is just the fixed part;
103 	 * it's followed by a context of size crypto_shash_descsize(shash_tfm).
104 	 */
105 	struct shash_desc hash_desc;
106 };
107 
verity_io_real_digest(struct dm_verity * v,struct dm_verity_io * io)108 static inline u8 *verity_io_real_digest(struct dm_verity *v,
109 					struct dm_verity_io *io)
110 {
111 	return io->real_digest;
112 }
113 
verity_io_want_digest(struct dm_verity * v,struct dm_verity_io * io)114 static inline u8 *verity_io_want_digest(struct dm_verity *v,
115 					struct dm_verity_io *io)
116 {
117 	return io->want_digest;
118 }
119 
120 extern int verity_hash(struct dm_verity *v, struct dm_verity_io *io,
121 		       const u8 *data, size_t len, u8 *digest);
122 
123 extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
124 				 sector_t block, u8 *digest, bool *is_zero);
125 
126 extern bool dm_is_verity_target(struct dm_target *ti);
127 extern int dm_verity_get_mode(struct dm_target *ti);
128 extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
129 				     unsigned int *digest_size);
130 
131 #endif /* DM_VERITY_H */
132