Thomas Gleixner | 55716d2 | 2019-06-01 10:08:42 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Red Hat, Inc. |
| 4 | * Copyright (C) 2015 Google, Inc. |
| 5 | * |
| 6 | * Author: Mikulas Patocka <mpatocka@redhat.com> |
| 7 | * |
| 8 | * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef DM_VERITY_H |
| 12 | #define DM_VERITY_H |
| 13 | |
Mikulas Patocka | afa53df | 2018-03-15 16:02:31 -0400 | [diff] [blame] | 14 | #include <linux/dm-bufio.h> |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 15 | #include <linux/device-mapper.h> |
| 16 | #include <crypto/hash.h> |
| 17 | |
| 18 | #define DM_VERITY_MAX_LEVELS 63 |
| 19 | |
| 20 | enum verity_mode { |
| 21 | DM_VERITY_MODE_EIO, |
| 22 | DM_VERITY_MODE_LOGGING, |
JeongHyeon Lee | e1fef0b | 2020-06-18 15:56:50 +0900 | [diff] [blame] | 23 | DM_VERITY_MODE_RESTART, |
| 24 | DM_VERITY_MODE_PANIC |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | enum verity_block_type { |
| 28 | DM_VERITY_BLOCK_TYPE_DATA, |
| 29 | DM_VERITY_BLOCK_TYPE_METADATA |
| 30 | }; |
| 31 | |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 32 | struct dm_verity_fec; |
| 33 | |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 34 | struct dm_verity { |
| 35 | struct dm_dev *data_dev; |
| 36 | struct dm_dev *hash_dev; |
| 37 | struct dm_target *ti; |
| 38 | struct dm_bufio_client *bufio; |
| 39 | char *alg_name; |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 40 | struct crypto_ahash *tfm; |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 41 | u8 *root_digest; /* digest of the root block */ |
| 42 | u8 *salt; /* salt: its size is salt_size */ |
Sami Tolvanen | 0cc37c2 | 2015-12-03 14:26:31 +0000 | [diff] [blame] | 43 | u8 *zero_digest; /* digest for a zero block */ |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 44 | unsigned salt_size; |
| 45 | sector_t data_start; /* data offset in 512-byte sectors */ |
| 46 | sector_t hash_start; /* hash start in blocks */ |
| 47 | sector_t data_blocks; /* the number of data blocks */ |
| 48 | sector_t hash_blocks; /* the number of hash blocks */ |
| 49 | unsigned char data_dev_block_bits; /* log2(data blocksize) */ |
| 50 | unsigned char hash_dev_block_bits; /* log2(hash blocksize) */ |
| 51 | unsigned char hash_per_block_bits; /* log2(hashes in hash block) */ |
| 52 | unsigned char levels; /* the number of tree levels */ |
| 53 | unsigned char version; |
| 54 | unsigned digest_size; /* digest size for the current hash algorithm */ |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 55 | unsigned int ahash_reqsize;/* the size of temporary space for crypto */ |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 56 | int hash_failed; /* set to 1 if hash of any block failed */ |
| 57 | enum verity_mode mode; /* mode for handling verification errors */ |
| 58 | unsigned corrupted_errs;/* Number of errors for corrupted blocks */ |
| 59 | |
| 60 | struct workqueue_struct *verify_wq; |
| 61 | |
| 62 | /* starting blocks for each tree level. 0 is the lowest level. */ |
| 63 | sector_t hash_level_block[DM_VERITY_MAX_LEVELS]; |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 64 | |
| 65 | struct dm_verity_fec *fec; /* forward error correction */ |
Patrik Torstensson | 843f38d | 2018-03-22 18:18:04 -0700 | [diff] [blame] | 66 | unsigned long *validated_blocks; /* bitset blocks validated */ |
Jaskaran Khurana | 88cd3e6 | 2019-07-17 17:46:15 -0700 | [diff] [blame] | 67 | |
| 68 | char *signature_key_desc; /* signature keyring reference */ |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | struct dm_verity_io { |
| 72 | struct dm_verity *v; |
| 73 | |
| 74 | /* original value of bio->bi_end_io */ |
| 75 | bio_end_io_t *orig_bi_end_io; |
| 76 | |
| 77 | sector_t block; |
| 78 | unsigned n_blocks; |
| 79 | |
| 80 | struct bvec_iter iter; |
| 81 | |
| 82 | struct work_struct work; |
| 83 | |
| 84 | /* |
| 85 | * Three variably-size fields follow this struct: |
| 86 | * |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 87 | * u8 hash_req[v->ahash_reqsize]; |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 88 | * u8 real_digest[v->digest_size]; |
| 89 | * u8 want_digest[v->digest_size]; |
| 90 | * |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 91 | * To access them use: verity_io_hash_req(), verity_io_real_digest() |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 92 | * and verity_io_want_digest(). |
| 93 | */ |
| 94 | }; |
| 95 | |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 96 | static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v, |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 97 | struct dm_verity_io *io) |
| 98 | { |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 99 | return (struct ahash_request *)(io + 1); |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static inline u8 *verity_io_real_digest(struct dm_verity *v, |
| 103 | struct dm_verity_io *io) |
| 104 | { |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 105 | return (u8 *)(io + 1) + v->ahash_reqsize; |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static inline u8 *verity_io_want_digest(struct dm_verity *v, |
| 109 | struct dm_verity_io *io) |
| 110 | { |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 111 | return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size; |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 114 | static inline u8 *verity_io_digest_end(struct dm_verity *v, |
| 115 | struct dm_verity_io *io) |
| 116 | { |
| 117 | return verity_io_want_digest(v, io) + v->digest_size; |
| 118 | } |
| 119 | |
Sami Tolvanen | bb4d73a | 2015-12-03 16:30:36 -0500 | [diff] [blame] | 120 | extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io, |
| 121 | struct bvec_iter *iter, |
| 122 | int (*process)(struct dm_verity *v, |
| 123 | struct dm_verity_io *io, |
| 124 | u8 *data, size_t len)); |
| 125 | |
Gilad Ben-Yossef | d1ac3ff | 2017-02-19 14:46:07 +0200 | [diff] [blame] | 126 | extern int verity_hash(struct dm_verity *v, struct ahash_request *req, |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 127 | const u8 *data, size_t len, u8 *digest); |
| 128 | |
| 129 | extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io, |
Sami Tolvanen | 0cc37c2 | 2015-12-03 14:26:31 +0000 | [diff] [blame] | 130 | sector_t block, u8 *digest, bool *is_zero); |
Sami Tolvanen | ffa3938 | 2015-12-03 16:01:51 -0500 | [diff] [blame] | 131 | |
| 132 | #endif /* DM_VERITY_H */ |