Blame view

drivers/md/dm-verity-target.c 27.9 KB
a4ffc1521   Mikulas Patocka   dm: add verity ta...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * Copyright (C) 2012 Red Hat, Inc.
   *
   * Author: Mikulas Patocka <mpatocka@redhat.com>
   *
   * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
   *
   * This file is released under the GPLv2.
   *
   * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set
   * default prefetch value. Data are read in "prefetch_cluster" chunks from the
   * hash device. Setting this greatly improves performance when data and hash
   * are on the same disk on different partitions on devices with poor random
   * access behavior.
   */
ffa393807   Sami Tolvanen   dm verity: factor...
16
  #include "dm-verity.h"
a739ff3f5   Sami Tolvanen   dm verity: add su...
17
  #include "dm-verity-fec.h"
a4ffc1521   Mikulas Patocka   dm: add verity ta...
18
19
  
  #include <linux/module.h>
65ff5b7dd   Sami Tolvanen   dm verity: add er...
20
  #include <linux/reboot.h>
a4ffc1521   Mikulas Patocka   dm: add verity ta...
21
22
  
  #define DM_MSG_PREFIX			"verity"
65ff5b7dd   Sami Tolvanen   dm verity: add er...
23
24
  #define DM_VERITY_ENV_LENGTH		42
  #define DM_VERITY_ENV_VAR_NAME		"DM_VERITY_ERR_BLOCK_NR"
a4ffc1521   Mikulas Patocka   dm: add verity ta...
25
  #define DM_VERITY_DEFAULT_PREFETCH_SIZE	262144
65ff5b7dd   Sami Tolvanen   dm verity: add er...
26
27
28
29
  #define DM_VERITY_MAX_CORRUPTED_ERRS	100
  
  #define DM_VERITY_OPT_LOGGING		"ignore_corruption"
  #define DM_VERITY_OPT_RESTART		"restart_on_corruption"
0cc37c2df   Sami Tolvanen   dm verity: add ig...
30
  #define DM_VERITY_OPT_IGN_ZEROES	"ignore_zero_blocks"
a4ffc1521   Mikulas Patocka   dm: add verity ta...
31

0cc37c2df   Sami Tolvanen   dm verity: add ig...
32
  #define DM_VERITY_OPTS_MAX		(2 + DM_VERITY_OPTS_FEC)
753c1fd02   Sami Tolvanen   dm verity: separa...
33

a4ffc1521   Mikulas Patocka   dm: add verity ta...
34
35
36
  static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
  
  module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
3b6b7813b   Mikulas Patocka   dm verity: avoid ...
37
38
39
40
41
42
  struct dm_verity_prefetch_work {
  	struct work_struct work;
  	struct dm_verity *v;
  	sector_t block;
  	unsigned n_blocks;
  };
a4ffc1521   Mikulas Patocka   dm: add verity ta...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  /*
   * Auxiliary structure appended to each dm-bufio buffer. If the value
   * hash_verified is nonzero, hash of the block has been verified.
   *
   * The variable hash_verified is set to 0 when allocating the buffer, then
   * it can be changed to 1 and it is never reset to 0 again.
   *
   * There is no lock around this value, a race condition can at worst cause
   * that multiple processes verify the hash of the same buffer simultaneously
   * and write 1 to hash_verified simultaneously.
   * This condition is harmless, so we don't need locking.
   */
  struct buffer_aux {
  	int hash_verified;
  };
  
  /*
   * Initialize struct buffer_aux for a freshly created buffer.
   */
  static void dm_bufio_alloc_callback(struct dm_buffer *buf)
  {
  	struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
  
  	aux->hash_verified = 0;
  }
  
  /*
   * Translate input sector number to the sector number on the target device.
   */
  static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
  {
  	return v->data_start + dm_target_offset(v->ti, bi_sector);
  }
  
  /*
   * Return hash position of a specified block at a specified tree level
   * (0 is the lowest level).
   * The lowest "hash_per_block_bits"-bits of the result denote hash position
   * inside a hash block. The remaining bits denote location of the hash block.
   */
  static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
  					 int level)
  {
  	return block >> (level * v->hash_per_block_bits);
  }
6dbeda346   Sami Tolvanen   dm verity: clean ...
88
  /*
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
89
   * Callback function for asynchrnous crypto API completion notification
6dbeda346   Sami Tolvanen   dm verity: clean ...
90
   */
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
91
  static void verity_op_done(struct crypto_async_request *base, int err)
6dbeda346   Sami Tolvanen   dm verity: clean ...
92
  {
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
93
  	struct verity_result *res = (struct verity_result *)base->data;
6dbeda346   Sami Tolvanen   dm verity: clean ...
94

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
95
96
  	if (err == -EINPROGRESS)
  		return;
6dbeda346   Sami Tolvanen   dm verity: clean ...
97

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
98
99
100
  	res->err = err;
  	complete(&res->completion);
  }
6dbeda346   Sami Tolvanen   dm verity: clean ...
101

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
102
103
104
105
106
107
108
109
  /*
   * Wait for async crypto API callback
   */
  static inline int verity_complete_op(struct verity_result *res, int ret)
  {
  	switch (ret) {
  	case 0:
  		break;
6dbeda346   Sami Tolvanen   dm verity: clean ...
110

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
111
112
113
114
115
116
117
  	case -EINPROGRESS:
  	case -EBUSY:
  		ret = wait_for_completion_interruptible(&res->completion);
  		if (!ret)
  			ret = res->err;
  		reinit_completion(&res->completion);
  		break;
6dbeda346   Sami Tolvanen   dm verity: clean ...
118

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
119
120
  	default:
  		DMERR("verity_wait_hash: crypto op submission failed: %d", ret);
6dbeda346   Sami Tolvanen   dm verity: clean ...
121
  	}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
122
123
124
125
  	if (unlikely(ret < 0))
  		DMERR("verity_wait_hash: crypto op failed: %d", ret);
  
  	return ret;
6dbeda346   Sami Tolvanen   dm verity: clean ...
126
  }
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
127
128
129
  static int verity_hash_update(struct dm_verity *v, struct ahash_request *req,
  				const u8 *data, size_t len,
  				struct verity_result *res)
6dbeda346   Sami Tolvanen   dm verity: clean ...
130
  {
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
131
  	struct scatterlist sg;
6dbeda346   Sami Tolvanen   dm verity: clean ...
132

0be812a80   Mikulas Patocka   dm verity: fix cr...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	if (likely(!is_vmalloc_addr(data))) {
  		sg_init_one(&sg, data, len);
  		ahash_request_set_crypt(req, &sg, NULL, len);
  		return verity_complete_op(res, crypto_ahash_update(req));
  	} else {
  		do {
  			int r;
  			size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data));
  			flush_kernel_vmap_range((void *)data, this_step);
  			sg_init_table(&sg, 1);
  			sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data));
  			ahash_request_set_crypt(req, &sg, NULL, this_step);
  			r = verity_complete_op(res, crypto_ahash_update(req));
  			if (unlikely(r))
  				return r;
  			data += this_step;
  			len -= this_step;
  		} while (len);
  		return 0;
  	}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  }
  
  /*
   * Wrapper for crypto_ahash_init, which handles verity salting.
   */
  static int verity_hash_init(struct dm_verity *v, struct ahash_request *req,
  				struct verity_result *res)
  {
  	int r;
  
  	ahash_request_set_tfm(req, v->tfm);
  	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
  					CRYPTO_TFM_REQ_MAY_BACKLOG,
  					verity_op_done, (void *)res);
  	init_completion(&res->completion);
  
  	r = verity_complete_op(res, crypto_ahash_init(req));
  
  	if (unlikely(r < 0)) {
  		DMERR("crypto_ahash_init failed: %d", r);
  		return r;
  	}
f52236e0b   Gilad Ben-Yossef   dm verity: fix no...
175
  	if (likely(v->salt_size && (v->version >= 1)))
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
176
  		r = verity_hash_update(v, req, v->salt, v->salt_size, res);
6dbeda346   Sami Tolvanen   dm verity: clean ...
177
178
179
  
  	return r;
  }
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
180
181
  static int verity_hash_final(struct dm_verity *v, struct ahash_request *req,
  			     u8 *digest, struct verity_result *res)
6dbeda346   Sami Tolvanen   dm verity: clean ...
182
183
  {
  	int r;
f52236e0b   Gilad Ben-Yossef   dm verity: fix no...
184
  	if (unlikely(v->salt_size && (!v->version))) {
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
185
  		r = verity_hash_update(v, req, v->salt, v->salt_size, res);
6dbeda346   Sami Tolvanen   dm verity: clean ...
186
187
  
  		if (r < 0) {
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
188
189
  			DMERR("verity_hash_final failed updating salt: %d", r);
  			goto out;
6dbeda346   Sami Tolvanen   dm verity: clean ...
190
191
  		}
  	}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
192
193
194
  	ahash_request_set_crypt(req, NULL, digest, 0);
  	r = verity_complete_op(res, crypto_ahash_final(req));
  out:
6dbeda346   Sami Tolvanen   dm verity: clean ...
195
196
  	return r;
  }
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
197
  int verity_hash(struct dm_verity *v, struct ahash_request *req,
ffa393807   Sami Tolvanen   dm verity: factor...
198
  		const u8 *data, size_t len, u8 *digest)
6dbeda346   Sami Tolvanen   dm verity: clean ...
199
200
  {
  	int r;
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
201
  	struct verity_result res;
6dbeda346   Sami Tolvanen   dm verity: clean ...
202

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
203
  	r = verity_hash_init(v, req, &res);
6dbeda346   Sami Tolvanen   dm verity: clean ...
204
  	if (unlikely(r < 0))
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
205
  		goto out;
6dbeda346   Sami Tolvanen   dm verity: clean ...
206

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
207
  	r = verity_hash_update(v, req, data, len, &res);
6dbeda346   Sami Tolvanen   dm verity: clean ...
208
  	if (unlikely(r < 0))
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
209
210
211
  		goto out;
  
  	r = verity_hash_final(v, req, digest, &res);
6dbeda346   Sami Tolvanen   dm verity: clean ...
212

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
213
214
  out:
  	return r;
6dbeda346   Sami Tolvanen   dm verity: clean ...
215
  }
a4ffc1521   Mikulas Patocka   dm: add verity ta...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
  				 sector_t *hash_block, unsigned *offset)
  {
  	sector_t position = verity_position_at_level(v, block, level);
  	unsigned idx;
  
  	*hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
  
  	if (!offset)
  		return;
  
  	idx = position & ((1 << v->hash_per_block_bits) - 1);
  	if (!v->version)
  		*offset = idx * v->digest_size;
  	else
  		*offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
  }
  
  /*
65ff5b7dd   Sami Tolvanen   dm verity: add er...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
   * Handle verification errors.
   */
  static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
  			     unsigned long long block)
  {
  	char verity_env[DM_VERITY_ENV_LENGTH];
  	char *envp[] = { verity_env, NULL };
  	const char *type_str = "";
  	struct mapped_device *md = dm_table_get_md(v->ti->table);
  
  	/* Corruption should be visible in device status in all modes */
  	v->hash_failed = 1;
  
  	if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
  		goto out;
  
  	v->corrupted_errs++;
  
  	switch (type) {
  	case DM_VERITY_BLOCK_TYPE_DATA:
  		type_str = "data";
  		break;
  	case DM_VERITY_BLOCK_TYPE_METADATA:
  		type_str = "metadata";
  		break;
  	default:
  		BUG();
  	}
  
  	DMERR("%s: %s block %llu is corrupted", v->data_dev->name, type_str,
  		block);
  
  	if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
  		DMERR("%s: reached maximum errors", v->data_dev->name);
  
  	snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
  		DM_VERITY_ENV_VAR_NAME, type, block);
  
  	kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
  
  out:
  	if (v->mode == DM_VERITY_MODE_LOGGING)
  		return 0;
  
  	if (v->mode == DM_VERITY_MODE_RESTART)
  		kernel_restart("dm-verity device corrupted");
  
  	return 1;
  }
  
  /*
a4ffc1521   Mikulas Patocka   dm: add verity ta...
286
287
288
   * Verify hash of a metadata block pertaining to the specified data block
   * ("block" argument) at a specified level ("level" argument).
   *
ffa393807   Sami Tolvanen   dm verity: factor...
289
290
   * On successful return, verity_io_want_digest(v, io) contains the hash value
   * for a lower tree level or for the data block (if we're at the lowest level).
a4ffc1521   Mikulas Patocka   dm: add verity ta...
291
292
293
   *
   * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
   * If "skip_unverified" is false, unverified buffer is hashed and verified
ffa393807   Sami Tolvanen   dm verity: factor...
294
   * against current value of verity_io_want_digest(v, io).
a4ffc1521   Mikulas Patocka   dm: add verity ta...
295
   */
6dbeda346   Sami Tolvanen   dm verity: clean ...
296
297
298
  static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
  			       sector_t block, int level, bool skip_unverified,
  			       u8 *want_digest)
a4ffc1521   Mikulas Patocka   dm: add verity ta...
299
  {
a4ffc1521   Mikulas Patocka   dm: add verity ta...
300
301
302
303
304
305
306
307
308
309
  	struct dm_buffer *buf;
  	struct buffer_aux *aux;
  	u8 *data;
  	int r;
  	sector_t hash_block;
  	unsigned offset;
  
  	verity_hash_at_level(v, block, level, &hash_block, &offset);
  
  	data = dm_bufio_read(v->bufio, hash_block, &buf);
fc0a44615   viresh kumar   dm: remove unlike...
310
  	if (IS_ERR(data))
a4ffc1521   Mikulas Patocka   dm: add verity ta...
311
312
313
314
315
  		return PTR_ERR(data);
  
  	aux = dm_bufio_get_aux_data(buf);
  
  	if (!aux->hash_verified) {
a4ffc1521   Mikulas Patocka   dm: add verity ta...
316
317
318
319
  		if (skip_unverified) {
  			r = 1;
  			goto release_ret_r;
  		}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
320
  		r = verity_hash(v, verity_io_hash_req(v, io),
6dbeda346   Sami Tolvanen   dm verity: clean ...
321
  				data, 1 << v->hash_dev_block_bits,
ffa393807   Sami Tolvanen   dm verity: factor...
322
  				verity_io_real_digest(v, io));
6dbeda346   Sami Tolvanen   dm verity: clean ...
323
  		if (unlikely(r < 0))
a4ffc1521   Mikulas Patocka   dm: add verity ta...
324
  			goto release_ret_r;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
325

ffa393807   Sami Tolvanen   dm verity: factor...
326
  		if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
6dbeda346   Sami Tolvanen   dm verity: clean ...
327
328
  				  v->digest_size) == 0))
  			aux->hash_verified = 1;
a739ff3f5   Sami Tolvanen   dm verity: add su...
329
330
331
332
  		else if (verity_fec_decode(v, io,
  					   DM_VERITY_BLOCK_TYPE_METADATA,
  					   hash_block, data, NULL) == 0)
  			aux->hash_verified = 1;
6dbeda346   Sami Tolvanen   dm verity: clean ...
333
334
335
336
  		else if (verity_handle_err(v,
  					   DM_VERITY_BLOCK_TYPE_METADATA,
  					   hash_block)) {
  			r = -EIO;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
337
338
  			goto release_ret_r;
  		}
a4ffc1521   Mikulas Patocka   dm: add verity ta...
339
340
341
  	}
  
  	data += offset;
6dbeda346   Sami Tolvanen   dm verity: clean ...
342
343
  	memcpy(want_digest, data, v->digest_size);
  	r = 0;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
344
345
346
  
  release_ret_r:
  	dm_bufio_release(buf);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
347
348
349
350
  	return r;
  }
  
  /*
6dbeda346   Sami Tolvanen   dm verity: clean ...
351
352
353
   * Find a hash for a given block, write it to digest and verify the integrity
   * of the hash tree if necessary.
   */
ffa393807   Sami Tolvanen   dm verity: factor...
354
  int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
0cc37c2df   Sami Tolvanen   dm verity: add ig...
355
  			  sector_t block, u8 *digest, bool *is_zero)
6dbeda346   Sami Tolvanen   dm verity: clean ...
356
  {
0cc37c2df   Sami Tolvanen   dm verity: add ig...
357
  	int r = 0, i;
6dbeda346   Sami Tolvanen   dm verity: clean ...
358
359
360
361
362
363
364
365
366
367
368
  
  	if (likely(v->levels)) {
  		/*
  		 * First, we try to get the requested hash for
  		 * the current block. If the hash block itself is
  		 * verified, zero is returned. If it isn't, this
  		 * function returns 1 and we fall back to whole
  		 * chain verification.
  		 */
  		r = verity_verify_level(v, io, block, 0, true, digest);
  		if (likely(r <= 0))
0cc37c2df   Sami Tolvanen   dm verity: add ig...
369
  			goto out;
6dbeda346   Sami Tolvanen   dm verity: clean ...
370
371
372
373
374
375
376
  	}
  
  	memcpy(digest, v->root_digest, v->digest_size);
  
  	for (i = v->levels - 1; i >= 0; i--) {
  		r = verity_verify_level(v, io, block, i, false, digest);
  		if (unlikely(r))
0cc37c2df   Sami Tolvanen   dm verity: add ig...
377
  			goto out;
6dbeda346   Sami Tolvanen   dm verity: clean ...
378
  	}
0cc37c2df   Sami Tolvanen   dm verity: add ig...
379
380
381
382
383
  out:
  	if (!r && v->zero_digest)
  		*is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
  	else
  		*is_zero = false;
6dbeda346   Sami Tolvanen   dm verity: clean ...
384

0cc37c2df   Sami Tolvanen   dm verity: add ig...
385
  	return r;
6dbeda346   Sami Tolvanen   dm verity: clean ...
386
387
388
  }
  
  /*
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
   * Calculates the digest for the given bio
   */
  int verity_for_io_block(struct dm_verity *v, struct dm_verity_io *io,
  			struct bvec_iter *iter, struct verity_result *res)
  {
  	unsigned int todo = 1 << v->data_dev_block_bits;
  	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  	struct scatterlist sg;
  	struct ahash_request *req = verity_io_hash_req(v, io);
  
  	do {
  		int r;
  		unsigned int len;
  		struct bio_vec bv = bio_iter_iovec(bio, *iter);
  
  		sg_init_table(&sg, 1);
  
  		len = bv.bv_len;
  
  		if (likely(len >= todo))
  			len = todo;
  		/*
  		 * Operating on a single page at a time looks suboptimal
  		 * until you consider the typical block size is 4,096B.
  		 * Going through this loops twice should be very rare.
  		 */
  		sg_set_page(&sg, bv.bv_page, len, bv.bv_offset);
  		ahash_request_set_crypt(req, &sg, NULL, len);
  		r = verity_complete_op(res, crypto_ahash_update(req));
  
  		if (unlikely(r < 0)) {
  			DMERR("verity_for_io_block crypto op failed: %d", r);
  			return r;
  		}
  
  		bio_advance_iter(bio, iter, len);
  		todo -= len;
  	} while (todo);
  
  	return 0;
  }
  
  /*
bb4d73ac5   Sami Tolvanen   dm verity: factor...
432
433
434
435
436
437
438
439
440
441
   * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
   * starting from iter.
   */
  int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
  			struct bvec_iter *iter,
  			int (*process)(struct dm_verity *v,
  				       struct dm_verity_io *io, u8 *data,
  				       size_t len))
  {
  	unsigned todo = 1 << v->data_dev_block_bits;
30187e1d4   Mike Snitzer   dm: rename target...
442
  	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
bb4d73ac5   Sami Tolvanen   dm verity: factor...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
  
  	do {
  		int r;
  		u8 *page;
  		unsigned len;
  		struct bio_vec bv = bio_iter_iovec(bio, *iter);
  
  		page = kmap_atomic(bv.bv_page);
  		len = bv.bv_len;
  
  		if (likely(len >= todo))
  			len = todo;
  
  		r = process(v, io, page + bv.bv_offset, len);
  		kunmap_atomic(page);
  
  		if (r < 0)
  			return r;
  
  		bio_advance_iter(bio, iter, len);
  		todo -= len;
  	} while (todo);
  
  	return 0;
  }
0cc37c2df   Sami Tolvanen   dm verity: add ig...
468
469
470
471
472
473
  static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
  			  u8 *data, size_t len)
  {
  	memset(data, 0, len);
  	return 0;
  }
bb4d73ac5   Sami Tolvanen   dm verity: factor...
474
  /*
a4ffc1521   Mikulas Patocka   dm: add verity ta...
475
476
477
478
   * Verify one "dm_verity_io" structure.
   */
  static int verity_verify_io(struct dm_verity_io *io)
  {
0cc37c2df   Sami Tolvanen   dm verity: add ig...
479
  	bool is_zero;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
480
  	struct dm_verity *v = io->v;
bb4d73ac5   Sami Tolvanen   dm verity: factor...
481
  	struct bvec_iter start;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
482
  	unsigned b;
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
483
  	struct verity_result res;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
484
485
  
  	for (b = 0; b < io->n_blocks; b++) {
a4ffc1521   Mikulas Patocka   dm: add verity ta...
486
  		int r;
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
487
  		struct ahash_request *req = verity_io_hash_req(v, io);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
488

6dbeda346   Sami Tolvanen   dm verity: clean ...
489
  		r = verity_hash_for_block(v, io, io->block + b,
0cc37c2df   Sami Tolvanen   dm verity: add ig...
490
491
  					  verity_io_want_digest(v, io),
  					  &is_zero);
6dbeda346   Sami Tolvanen   dm verity: clean ...
492
493
  		if (unlikely(r < 0))
  			return r;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
494

0cc37c2df   Sami Tolvanen   dm verity: add ig...
495
496
497
498
499
500
501
502
503
504
505
506
  		if (is_zero) {
  			/*
  			 * If we expect a zero block, don't validate, just
  			 * return zeros.
  			 */
  			r = verity_for_bv_block(v, io, &io->iter,
  						verity_bv_zero);
  			if (unlikely(r < 0))
  				return r;
  
  			continue;
  		}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
507
  		r = verity_hash_init(v, req, &res);
6dbeda346   Sami Tolvanen   dm verity: clean ...
508
  		if (unlikely(r < 0))
a4ffc1521   Mikulas Patocka   dm: add verity ta...
509
  			return r;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
510

bb4d73ac5   Sami Tolvanen   dm verity: factor...
511
  		start = io->iter;
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
512
  		r = verity_for_io_block(v, io, &io->iter, &res);
bb4d73ac5   Sami Tolvanen   dm verity: factor...
513
514
  		if (unlikely(r < 0))
  			return r;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
515

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
516
517
  		r = verity_hash_final(v, req, verity_io_real_digest(v, io),
  					&res);
6dbeda346   Sami Tolvanen   dm verity: clean ...
518
  		if (unlikely(r < 0))
a4ffc1521   Mikulas Patocka   dm: add verity ta...
519
  			return r;
6dbeda346   Sami Tolvanen   dm verity: clean ...
520

ffa393807   Sami Tolvanen   dm verity: factor...
521
522
  		if (likely(memcmp(verity_io_real_digest(v, io),
  				  verity_io_want_digest(v, io), v->digest_size) == 0))
6dbeda346   Sami Tolvanen   dm verity: clean ...
523
  			continue;
a739ff3f5   Sami Tolvanen   dm verity: add su...
524
525
526
  		else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
  					   io->block + b, NULL, &start) == 0)
  			continue;
6dbeda346   Sami Tolvanen   dm verity: clean ...
527
  		else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
a739ff3f5   Sami Tolvanen   dm verity: add su...
528
  					   io->block + b))
6dbeda346   Sami Tolvanen   dm verity: clean ...
529
  			return -EIO;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
530
  	}
a4ffc1521   Mikulas Patocka   dm: add verity ta...
531
532
533
534
535
536
537
  
  	return 0;
  }
  
  /*
   * End one "io" structure with a given error.
   */
4e4cbee93   Christoph Hellwig   block: switch bio...
538
  static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
a4ffc1521   Mikulas Patocka   dm: add verity ta...
539
  {
a4ffc1521   Mikulas Patocka   dm: add verity ta...
540
  	struct dm_verity *v = io->v;
30187e1d4   Mike Snitzer   dm: rename target...
541
  	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
542
543
  
  	bio->bi_end_io = io->orig_bi_end_io;
4e4cbee93   Christoph Hellwig   block: switch bio...
544
  	bio->bi_status = status;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
545

a739ff3f5   Sami Tolvanen   dm verity: add su...
546
  	verity_fec_finish_io(io);
4246a0b63   Christoph Hellwig   block: add a bi_e...
547
  	bio_endio(bio);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
548
549
550
551
552
  }
  
  static void verity_work(struct work_struct *w)
  {
  	struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
4e4cbee93   Christoph Hellwig   block: switch bio...
553
  	verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
a4ffc1521   Mikulas Patocka   dm: add verity ta...
554
  }
4246a0b63   Christoph Hellwig   block: add a bi_e...
555
  static void verity_end_io(struct bio *bio)
a4ffc1521   Mikulas Patocka   dm: add verity ta...
556
557
  {
  	struct dm_verity_io *io = bio->bi_private;
4e4cbee93   Christoph Hellwig   block: switch bio...
558
559
  	if (bio->bi_status && !verity_fec_is_enabled(io->v)) {
  		verity_finish_io(io, bio->bi_status);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
560
561
562
563
564
565
566
567
568
569
570
571
  		return;
  	}
  
  	INIT_WORK(&io->work, verity_work);
  	queue_work(io->v->verify_wq, &io->work);
  }
  
  /*
   * Prefetch buffers for the specified io.
   * The root buffer is not prefetched, it is assumed that it will be cached
   * all the time.
   */
3b6b7813b   Mikulas Patocka   dm verity: avoid ...
572
  static void verity_prefetch_io(struct work_struct *work)
a4ffc1521   Mikulas Patocka   dm: add verity ta...
573
  {
3b6b7813b   Mikulas Patocka   dm verity: avoid ...
574
575
576
  	struct dm_verity_prefetch_work *pw =
  		container_of(work, struct dm_verity_prefetch_work, work);
  	struct dm_verity *v = pw->v;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
577
578
579
580
581
  	int i;
  
  	for (i = v->levels - 2; i >= 0; i--) {
  		sector_t hash_block_start;
  		sector_t hash_block_end;
3b6b7813b   Mikulas Patocka   dm verity: avoid ...
582
583
  		verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
  		verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
584
  		if (!i) {
fe5fe9063   Mikulas Patocka   dm: use ACCESS_ON...
585
  			unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
586
587
588
589
590
591
  
  			cluster >>= v->data_dev_block_bits;
  			if (unlikely(!cluster))
  				goto no_prefetch_cluster;
  
  			if (unlikely(cluster & (cluster - 1)))
553d8fe02   Mikulas Patocka   dm verity: use __...
592
  				cluster = 1 << __fls(cluster);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
593
594
595
596
597
598
599
600
601
602
  
  			hash_block_start &= ~(sector_t)(cluster - 1);
  			hash_block_end |= cluster - 1;
  			if (unlikely(hash_block_end >= v->hash_blocks))
  				hash_block_end = v->hash_blocks - 1;
  		}
  no_prefetch_cluster:
  		dm_bufio_prefetch(v->bufio, hash_block_start,
  				  hash_block_end - hash_block_start + 1);
  	}
3b6b7813b   Mikulas Patocka   dm verity: avoid ...
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
  
  	kfree(pw);
  }
  
  static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
  {
  	struct dm_verity_prefetch_work *pw;
  
  	pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
  		GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  
  	if (!pw)
  		return;
  
  	INIT_WORK(&pw->work, verity_prefetch_io);
  	pw->v = v;
  	pw->block = io->block;
  	pw->n_blocks = io->n_blocks;
  	queue_work(v->verify_wq, &pw->work);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
622
623
624
625
626
627
  }
  
  /*
   * Bio map function. It allocates dm_verity_io structure and bio vector and
   * fills them. Then it issues prefetches and the I/O.
   */
7de3ee57d   Mikulas Patocka   dm: remove map_info
628
  static int verity_map(struct dm_target *ti, struct bio *bio)
a4ffc1521   Mikulas Patocka   dm: add verity ta...
629
630
631
  {
  	struct dm_verity *v = ti->private;
  	struct dm_verity_io *io;
74d46992e   Christoph Hellwig   block: replace bi...
632
  	bio_set_dev(bio, v->data_dev->bdev);
4f024f379   Kent Overstreet   block: Abstract o...
633
  	bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
634

4f024f379   Kent Overstreet   block: Abstract o...
635
  	if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
a4ffc1521   Mikulas Patocka   dm: add verity ta...
636
637
  	    ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
  		DMERR_LIMIT("unaligned io");
846785e6a   Christoph Hellwig   dm: don't return ...
638
  		return DM_MAPIO_KILL;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
639
  	}
f73a1c7d1   Kent Overstreet   block: Add bio_en...
640
  	if (bio_end_sector(bio) >>
a4ffc1521   Mikulas Patocka   dm: add verity ta...
641
642
  	    (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
  		DMERR_LIMIT("io out of range");
846785e6a   Christoph Hellwig   dm: don't return ...
643
  		return DM_MAPIO_KILL;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
644
645
646
  	}
  
  	if (bio_data_dir(bio) == WRITE)
846785e6a   Christoph Hellwig   dm: don't return ...
647
  		return DM_MAPIO_KILL;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
648

30187e1d4   Mike Snitzer   dm: rename target...
649
  	io = dm_per_bio_data(bio, ti->per_io_data_size);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
650
  	io->v = v;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
651
  	io->orig_bi_end_io = bio->bi_end_io;
4f024f379   Kent Overstreet   block: Abstract o...
652
653
  	io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
  	io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
654
655
656
  
  	bio->bi_end_io = verity_end_io;
  	bio->bi_private = io;
003b5c571   Kent Overstreet   block: Convert dr...
657
  	io->iter = bio->bi_iter;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
658

a739ff3f5   Sami Tolvanen   dm verity: add su...
659
  	verity_fec_init_io(io);
3b6b7813b   Mikulas Patocka   dm verity: avoid ...
660
  	verity_submit_prefetch(v, io);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
661
662
663
664
665
666
667
668
669
  
  	generic_make_request(bio);
  
  	return DM_MAPIO_SUBMITTED;
  }
  
  /*
   * Status: V (valid) or C (corruption found)
   */
fd7c092e7   Mikulas Patocka   dm: fix truncated...
670
671
  static void verity_status(struct dm_target *ti, status_type_t type,
  			  unsigned status_flags, char *result, unsigned maxlen)
a4ffc1521   Mikulas Patocka   dm: add verity ta...
672
673
  {
  	struct dm_verity *v = ti->private;
a739ff3f5   Sami Tolvanen   dm verity: add su...
674
  	unsigned args = 0;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  	unsigned sz = 0;
  	unsigned x;
  
  	switch (type) {
  	case STATUSTYPE_INFO:
  		DMEMIT("%c", v->hash_failed ? 'C' : 'V');
  		break;
  	case STATUSTYPE_TABLE:
  		DMEMIT("%u %s %s %u %u %llu %llu %s ",
  			v->version,
  			v->data_dev->name,
  			v->hash_dev->name,
  			1 << v->data_dev_block_bits,
  			1 << v->hash_dev_block_bits,
  			(unsigned long long)v->data_blocks,
  			(unsigned long long)v->hash_start,
  			v->alg_name
  			);
  		for (x = 0; x < v->digest_size; x++)
  			DMEMIT("%02x", v->root_digest[x]);
  		DMEMIT(" ");
  		if (!v->salt_size)
  			DMEMIT("-");
  		else
  			for (x = 0; x < v->salt_size; x++)
  				DMEMIT("%02x", v->salt[x]);
a739ff3f5   Sami Tolvanen   dm verity: add su...
701
702
703
704
  		if (v->mode != DM_VERITY_MODE_EIO)
  			args++;
  		if (verity_fec_is_enabled(v))
  			args += DM_VERITY_OPTS_FEC;
0cc37c2df   Sami Tolvanen   dm verity: add ig...
705
706
  		if (v->zero_digest)
  			args++;
a739ff3f5   Sami Tolvanen   dm verity: add su...
707
708
709
  		if (!args)
  			return;
  		DMEMIT(" %u", args);
65ff5b7dd   Sami Tolvanen   dm verity: add er...
710
  		if (v->mode != DM_VERITY_MODE_EIO) {
a739ff3f5   Sami Tolvanen   dm verity: add su...
711
  			DMEMIT(" ");
65ff5b7dd   Sami Tolvanen   dm verity: add er...
712
713
714
715
716
717
718
719
720
721
722
  			switch (v->mode) {
  			case DM_VERITY_MODE_LOGGING:
  				DMEMIT(DM_VERITY_OPT_LOGGING);
  				break;
  			case DM_VERITY_MODE_RESTART:
  				DMEMIT(DM_VERITY_OPT_RESTART);
  				break;
  			default:
  				BUG();
  			}
  		}
0cc37c2df   Sami Tolvanen   dm verity: add ig...
723
724
  		if (v->zero_digest)
  			DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
a739ff3f5   Sami Tolvanen   dm verity: add su...
725
  		sz = verity_fec_status_table(v, sz, result, maxlen);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
726
727
  		break;
  	}
a4ffc1521   Mikulas Patocka   dm: add verity ta...
728
  }
e56f81e0b   Christoph Hellwig   dm: refactor ioct...
729
730
  static int verity_prepare_ioctl(struct dm_target *ti,
  		struct block_device **bdev, fmode_t *mode)
a4ffc1521   Mikulas Patocka   dm: add verity ta...
731
732
  {
  	struct dm_verity *v = ti->private;
e56f81e0b   Christoph Hellwig   dm: refactor ioct...
733
734
  
  	*bdev = v->data_dev->bdev;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
735
736
737
  
  	if (v->data_start ||
  	    ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
e56f81e0b   Christoph Hellwig   dm: refactor ioct...
738
739
  		return 1;
  	return 0;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
740
  }
a4ffc1521   Mikulas Patocka   dm: add verity ta...
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
  static int verity_iterate_devices(struct dm_target *ti,
  				  iterate_devices_callout_fn fn, void *data)
  {
  	struct dm_verity *v = ti->private;
  
  	return fn(ti, v->data_dev, v->data_start, ti->len, data);
  }
  
  static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
  {
  	struct dm_verity *v = ti->private;
  
  	if (limits->logical_block_size < 1 << v->data_dev_block_bits)
  		limits->logical_block_size = 1 << v->data_dev_block_bits;
  
  	if (limits->physical_block_size < 1 << v->data_dev_block_bits)
  		limits->physical_block_size = 1 << v->data_dev_block_bits;
  
  	blk_limits_io_min(limits, limits->logical_block_size);
  }
  
  static void verity_dtr(struct dm_target *ti)
  {
  	struct dm_verity *v = ti->private;
  
  	if (v->verify_wq)
  		destroy_workqueue(v->verify_wq);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
768
769
770
771
772
  	if (v->bufio)
  		dm_bufio_client_destroy(v->bufio);
  
  	kfree(v->salt);
  	kfree(v->root_digest);
0cc37c2df   Sami Tolvanen   dm verity: add ig...
773
  	kfree(v->zero_digest);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
774
775
  
  	if (v->tfm)
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
776
  		crypto_free_ahash(v->tfm);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
777
778
779
780
781
782
783
784
  
  	kfree(v->alg_name);
  
  	if (v->hash_dev)
  		dm_put_device(ti, v->hash_dev);
  
  	if (v->data_dev)
  		dm_put_device(ti, v->data_dev);
a739ff3f5   Sami Tolvanen   dm verity: add su...
785
  	verity_fec_dtr(v);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
786
787
  	kfree(v);
  }
0cc37c2df   Sami Tolvanen   dm verity: add ig...
788
789
790
  static int verity_alloc_zero_digest(struct dm_verity *v)
  {
  	int r = -ENOMEM;
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
791
  	struct ahash_request *req;
0cc37c2df   Sami Tolvanen   dm verity: add ig...
792
793
794
795
796
797
  	u8 *zero_data;
  
  	v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
  
  	if (!v->zero_digest)
  		return r;
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
798
  	req = kmalloc(v->ahash_reqsize, GFP_KERNEL);
0cc37c2df   Sami Tolvanen   dm verity: add ig...
799

d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
800
  	if (!req)
0cc37c2df   Sami Tolvanen   dm verity: add ig...
801
802
803
804
805
806
  		return r; /* verity_dtr will free zero_digest */
  
  	zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
  
  	if (!zero_data)
  		goto out;
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
807
  	r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits,
0cc37c2df   Sami Tolvanen   dm verity: add ig...
808
809
810
  			v->zero_digest);
  
  out:
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
811
  	kfree(req);
0cc37c2df   Sami Tolvanen   dm verity: add ig...
812
813
814
815
  	kfree(zero_data);
  
  	return r;
  }
753c1fd02   Sami Tolvanen   dm verity: separa...
816
817
818
819
820
821
  static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v)
  {
  	int r;
  	unsigned argc;
  	struct dm_target *ti = v->ti;
  	const char *arg_name;
5916a22b8   Eric Biggers   dm: constify argu...
822
  	static const struct dm_arg _args[] = {
753c1fd02   Sami Tolvanen   dm verity: separa...
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  		{0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
  	};
  
  	r = dm_read_arg_group(_args, as, &argc, &ti->error);
  	if (r)
  		return -EINVAL;
  
  	if (!argc)
  		return 0;
  
  	do {
  		arg_name = dm_shift_arg(as);
  		argc--;
  
  		if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
  			v->mode = DM_VERITY_MODE_LOGGING;
  			continue;
  
  		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
  			v->mode = DM_VERITY_MODE_RESTART;
  			continue;
a739ff3f5   Sami Tolvanen   dm verity: add su...
844

0cc37c2df   Sami Tolvanen   dm verity: add ig...
845
846
847
848
849
850
851
  		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
  			r = verity_alloc_zero_digest(v);
  			if (r) {
  				ti->error = "Cannot allocate zero digest";
  				return r;
  			}
  			continue;
a739ff3f5   Sami Tolvanen   dm verity: add su...
852
853
854
855
856
  		} else if (verity_is_fec_opt_arg(arg_name)) {
  			r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
  			if (r)
  				return r;
  			continue;
753c1fd02   Sami Tolvanen   dm verity: separa...
857
858
859
860
861
862
863
864
  		}
  
  		ti->error = "Unrecognized verity feature request";
  		return -EINVAL;
  	} while (argc && !r);
  
  	return r;
  }
a4ffc1521   Mikulas Patocka   dm: add verity ta...
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
  /*
   * Target parameters:
   *	<version>	The current format is version 1.
   *			Vsn 0 is compatible with original Chromium OS releases.
   *	<data device>
   *	<hash device>
   *	<data block size>
   *	<hash block size>
   *	<the number of data blocks>
   *	<hash start block>
   *	<algorithm>
   *	<digest>
   *	<salt>		Hex string or "-" if no salt.
   */
  static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
  {
  	struct dm_verity *v;
65ff5b7dd   Sami Tolvanen   dm verity: add er...
882
  	struct dm_arg_set as;
753c1fd02   Sami Tolvanen   dm verity: separa...
883
  	unsigned int num;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
884
885
886
887
888
889
890
891
892
893
894
895
896
  	unsigned long long num_ll;
  	int r;
  	int i;
  	sector_t hash_position;
  	char dummy;
  
  	v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
  	if (!v) {
  		ti->error = "Cannot allocate verity structure";
  		return -ENOMEM;
  	}
  	ti->private = v;
  	v->ti = ti;
a739ff3f5   Sami Tolvanen   dm verity: add su...
897
898
899
  	r = verity_fec_ctr_alloc(v);
  	if (r)
  		goto bad;
a4ffc1521   Mikulas Patocka   dm: add verity ta...
900
901
902
903
904
  	if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
  		ti->error = "Device must be readonly";
  		r = -EINVAL;
  		goto bad;
  	}
65ff5b7dd   Sami Tolvanen   dm verity: add er...
905
906
  	if (argc < 10) {
  		ti->error = "Not enough arguments";
a4ffc1521   Mikulas Patocka   dm: add verity ta...
907
908
909
  		r = -EINVAL;
  		goto bad;
  	}
5d8be8439   Mikulas Patocka   dm verity: remove...
910
911
  	if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
  	    num > 1) {
a4ffc1521   Mikulas Patocka   dm: add verity ta...
912
913
914
915
916
917
918
919
920
921
922
923
924
925
  		ti->error = "Invalid version";
  		r = -EINVAL;
  		goto bad;
  	}
  	v->version = num;
  
  	r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
  	if (r) {
  		ti->error = "Data device lookup failed";
  		goto bad;
  	}
  
  	r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
  	if (r) {
21ffe552e   Eric Biggers   dm verity: fix in...
926
  		ti->error = "Hash device lookup failed";
a4ffc1521   Mikulas Patocka   dm: add verity ta...
927
928
929
930
931
932
933
934
935
936
937
  		goto bad;
  	}
  
  	if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
  	    !num || (num & (num - 1)) ||
  	    num < bdev_logical_block_size(v->data_dev->bdev) ||
  	    num > PAGE_SIZE) {
  		ti->error = "Invalid data device block size";
  		r = -EINVAL;
  		goto bad;
  	}
553d8fe02   Mikulas Patocka   dm verity: use __...
938
  	v->data_dev_block_bits = __ffs(num);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
939
940
941
942
943
944
945
946
947
  
  	if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
  	    !num || (num & (num - 1)) ||
  	    num < bdev_logical_block_size(v->hash_dev->bdev) ||
  	    num > INT_MAX) {
  		ti->error = "Invalid hash device block size";
  		r = -EINVAL;
  		goto bad;
  	}
553d8fe02   Mikulas Patocka   dm verity: use __...
948
  	v->hash_dev_block_bits = __ffs(num);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
949
950
  
  	if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
1d55f6bcc   Mikulas Patocka   dm verity: fix ov...
951
952
  	    (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
  	    >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
a4ffc1521   Mikulas Patocka   dm: add verity ta...
953
954
955
956
957
958
959
960
961
962
963
964
965
  		ti->error = "Invalid data blocks";
  		r = -EINVAL;
  		goto bad;
  	}
  	v->data_blocks = num_ll;
  
  	if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
  		ti->error = "Data device is too small";
  		r = -EINVAL;
  		goto bad;
  	}
  
  	if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
1d55f6bcc   Mikulas Patocka   dm verity: fix ov...
966
967
  	    (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
  	    >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
a4ffc1521   Mikulas Patocka   dm: add verity ta...
968
969
970
971
972
973
974
975
976
977
978
979
  		ti->error = "Invalid hash start";
  		r = -EINVAL;
  		goto bad;
  	}
  	v->hash_start = num_ll;
  
  	v->alg_name = kstrdup(argv[7], GFP_KERNEL);
  	if (!v->alg_name) {
  		ti->error = "Cannot allocate algorithm name";
  		r = -ENOMEM;
  		goto bad;
  	}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
980
  	v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
981
982
983
984
985
986
  	if (IS_ERR(v->tfm)) {
  		ti->error = "Cannot initialize hash function";
  		r = PTR_ERR(v->tfm);
  		v->tfm = NULL;
  		goto bad;
  	}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
987
  	v->digest_size = crypto_ahash_digestsize(v->tfm);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
988
989
990
991
992
  	if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
  		ti->error = "Digest size too big";
  		r = -EINVAL;
  		goto bad;
  	}
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
993
994
  	v->ahash_reqsize = sizeof(struct ahash_request) +
  		crypto_ahash_reqsize(v->tfm);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
  
  	v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
  	if (!v->root_digest) {
  		ti->error = "Cannot allocate root digest";
  		r = -ENOMEM;
  		goto bad;
  	}
  	if (strlen(argv[8]) != v->digest_size * 2 ||
  	    hex2bin(v->root_digest, argv[8], v->digest_size)) {
  		ti->error = "Invalid root digest";
  		r = -EINVAL;
  		goto bad;
  	}
  
  	if (strcmp(argv[9], "-")) {
  		v->salt_size = strlen(argv[9]) / 2;
  		v->salt = kmalloc(v->salt_size, GFP_KERNEL);
  		if (!v->salt) {
  			ti->error = "Cannot allocate salt";
  			r = -ENOMEM;
  			goto bad;
  		}
  		if (strlen(argv[9]) != v->salt_size * 2 ||
  		    hex2bin(v->salt, argv[9], v->salt_size)) {
  			ti->error = "Invalid salt";
  			r = -EINVAL;
  			goto bad;
  		}
  	}
65ff5b7dd   Sami Tolvanen   dm verity: add er...
1024
1025
1026
1027
1028
1029
1030
  	argv += 10;
  	argc -= 10;
  
  	/* Optional parameters */
  	if (argc) {
  		as.argc = argc;
  		as.argv = argv;
753c1fd02   Sami Tolvanen   dm verity: separa...
1031
1032
  		r = verity_parse_opt_args(&as, v);
  		if (r < 0)
65ff5b7dd   Sami Tolvanen   dm verity: add er...
1033
  			goto bad;
65ff5b7dd   Sami Tolvanen   dm verity: add er...
1034
  	}
a4ffc1521   Mikulas Patocka   dm: add verity ta...
1035
  	v->hash_per_block_bits =
553d8fe02   Mikulas Patocka   dm verity: use __...
1036
  		__fls((1 << v->hash_dev_block_bits) / v->digest_size);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  
  	v->levels = 0;
  	if (v->data_blocks)
  		while (v->hash_per_block_bits * v->levels < 64 &&
  		       (unsigned long long)(v->data_blocks - 1) >>
  		       (v->hash_per_block_bits * v->levels))
  			v->levels++;
  
  	if (v->levels > DM_VERITY_MAX_LEVELS) {
  		ti->error = "Too many tree levels";
  		r = -E2BIG;
  		goto bad;
  	}
  
  	hash_position = v->hash_start;
  	for (i = v->levels - 1; i >= 0; i--) {
  		sector_t s;
  		v->hash_level_block[i] = hash_position;
b1bf2de07   Mikulas Patocka   dm verity: fix in...
1055
1056
  		s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
  					>> ((i + 1) * v->hash_per_block_bits);
a4ffc1521   Mikulas Patocka   dm: add verity ta...
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
  		if (hash_position + s < hash_position) {
  			ti->error = "Hash device offset overflow";
  			r = -E2BIG;
  			goto bad;
  		}
  		hash_position += s;
  	}
  	v->hash_blocks = hash_position;
  
  	v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
  		1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
  		dm_bufio_alloc_callback, NULL);
  	if (IS_ERR(v->bufio)) {
  		ti->error = "Cannot initialize dm-bufio";
  		r = PTR_ERR(v->bufio);
  		v->bufio = NULL;
  		goto bad;
  	}
  
  	if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
  		ti->error = "Hash device is too small";
  		r = -E2BIG;
  		goto bad;
  	}
a4ffc1521   Mikulas Patocka   dm: add verity ta...
1081
1082
1083
1084
1085
1086
1087
  	/* WQ_UNBOUND greatly improves performance when running on ramdisk */
  	v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
  	if (!v->verify_wq) {
  		ti->error = "Cannot allocate workqueue";
  		r = -ENOMEM;
  		goto bad;
  	}
30187e1d4   Mike Snitzer   dm: rename target...
1088
  	ti->per_io_data_size = sizeof(struct dm_verity_io) +
d1ac3ff00   Gilad Ben-Yossef   dm verity: switch...
1089
  				v->ahash_reqsize + v->digest_size * 2;
a739ff3f5   Sami Tolvanen   dm verity: add su...
1090
1091
1092
1093
  
  	r = verity_fec_ctr(v);
  	if (r)
  		goto bad;
30187e1d4   Mike Snitzer   dm: rename target...
1094
1095
  	ti->per_io_data_size = roundup(ti->per_io_data_size,
  				       __alignof__(struct dm_verity_io));
a739ff3f5   Sami Tolvanen   dm verity: add su...
1096

a4ffc1521   Mikulas Patocka   dm: add verity ta...
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
  	return 0;
  
  bad:
  	verity_dtr(ti);
  
  	return r;
  }
  
  static struct target_type verity_target = {
  	.name		= "verity",
a739ff3f5   Sami Tolvanen   dm verity: add su...
1107
  	.version	= {1, 3, 0},
a4ffc1521   Mikulas Patocka   dm: add verity ta...
1108
1109
1110
1111
1112
  	.module		= THIS_MODULE,
  	.ctr		= verity_ctr,
  	.dtr		= verity_dtr,
  	.map		= verity_map,
  	.status		= verity_status,
e56f81e0b   Christoph Hellwig   dm: refactor ioct...
1113
  	.prepare_ioctl	= verity_prepare_ioctl,
a4ffc1521   Mikulas Patocka   dm: add verity ta...
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  	.iterate_devices = verity_iterate_devices,
  	.io_hints	= verity_io_hints,
  };
  
  static int __init dm_verity_init(void)
  {
  	int r;
  
  	r = dm_register_target(&verity_target);
  	if (r < 0)
  		DMERR("register failed %d", r);
  
  	return r;
  }
  
  static void __exit dm_verity_exit(void)
  {
  	dm_unregister_target(&verity_target);
  }
  
  module_init(dm_verity_init);
  module_exit(dm_verity_exit);
  
  MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
  MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
  MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
  MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
  MODULE_LICENSE("GPL");