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