xref: /linux/drivers/md/dm-verity.h (revision 1553a1c48281243359a9529a10ddb551f3b967ab)
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_ahash *tfm;
43 	u8 *root_digest;	/* digest of the root block */
44 	u8 *salt;		/* salt: its size is salt_size */
45 	u8 *zero_digest;	/* digest for a zero block */
46 	unsigned int salt_size;
47 	sector_t data_start;	/* data offset in 512-byte sectors */
48 	sector_t hash_start;	/* hash start in blocks */
49 	sector_t data_blocks;	/* the number of data blocks */
50 	sector_t hash_blocks;	/* the number of hash blocks */
51 	unsigned char data_dev_block_bits;	/* log2(data blocksize) */
52 	unsigned char hash_dev_block_bits;	/* log2(hash blocksize) */
53 	unsigned char hash_per_block_bits;	/* log2(hashes in hash block) */
54 	unsigned char levels;	/* the number of tree levels */
55 	unsigned char version;
56 	bool hash_failed:1;	/* set if hash of any block failed */
57 	bool use_bh_wq:1;	/* try to verify in BH wq before normal work-queue */
58 	unsigned int digest_size;	/* digest size for the current hash algorithm */
59 	unsigned int ahash_reqsize;/* the size of temporary space for crypto */
60 	enum verity_mode mode;	/* mode for handling verification errors */
61 	unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
62 
63 	struct workqueue_struct *verify_wq;
64 
65 	/* starting blocks for each tree level. 0 is the lowest level. */
66 	sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
67 
68 	struct dm_verity_fec *fec;	/* forward error correction */
69 	unsigned long *validated_blocks; /* bitset blocks validated */
70 
71 	char *signature_key_desc; /* signature keyring reference */
72 
73 	struct dm_io_client *io;
74 	mempool_t recheck_pool;
75 };
76 
77 struct dm_verity_io {
78 	struct dm_verity *v;
79 
80 	/* original value of bio->bi_end_io */
81 	bio_end_io_t *orig_bi_end_io;
82 
83 	struct bvec_iter iter;
84 
85 	sector_t block;
86 	unsigned int n_blocks;
87 	bool in_bh;
88 
89 	struct work_struct work;
90 	struct work_struct bh_work;
91 
92 	char *recheck_buffer;
93 
94 	/*
95 	 * Three variably-size fields follow this struct:
96 	 *
97 	 * u8 hash_req[v->ahash_reqsize];
98 	 * u8 real_digest[v->digest_size];
99 	 * u8 want_digest[v->digest_size];
100 	 *
101 	 * To access them use: verity_io_hash_req(), verity_io_real_digest()
102 	 * and verity_io_want_digest().
103 	 */
104 };
105 
106 static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v,
107 						     struct dm_verity_io *io)
108 {
109 	return (struct ahash_request *)(io + 1);
110 }
111 
112 static inline u8 *verity_io_real_digest(struct dm_verity *v,
113 					struct dm_verity_io *io)
114 {
115 	return (u8 *)(io + 1) + v->ahash_reqsize;
116 }
117 
118 static inline u8 *verity_io_want_digest(struct dm_verity *v,
119 					struct dm_verity_io *io)
120 {
121 	return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size;
122 }
123 
124 extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
125 			       struct bvec_iter *iter,
126 			       int (*process)(struct dm_verity *v,
127 					      struct dm_verity_io *io,
128 					      u8 *data, size_t len));
129 
130 extern int verity_hash(struct dm_verity *v, struct ahash_request *req,
131 		       const u8 *data, size_t len, u8 *digest, bool may_sleep);
132 
133 extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
134 				 sector_t block, u8 *digest, bool *is_zero);
135 
136 extern bool dm_is_verity_target(struct dm_target *ti);
137 extern int dm_verity_get_mode(struct dm_target *ti);
138 extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest,
139 				     unsigned int *digest_size);
140 
141 #endif /* DM_VERITY_H */
142