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