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