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