Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * fs-verity: read-only file-based authenticity protection |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | */ |
| 7 | |
| 8 | #ifndef _FSVERITY_PRIVATE_H |
| 9 | #define _FSVERITY_PRIVATE_H |
| 10 | |
| 11 | #ifdef CONFIG_FS_VERITY_DEBUG |
| 12 | #define DEBUG |
| 13 | #endif |
| 14 | |
| 15 | #define pr_fmt(fmt) "fs-verity: " fmt |
| 16 | |
Eric Biggers | a24d22b | 2020-11-12 21:20:21 -0800 | [diff] [blame] | 17 | #include <crypto/sha2.h> |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 18 | #include <linux/fsverity.h> |
Eric Biggers | 439bea1 | 2019-12-31 11:55:45 -0600 | [diff] [blame] | 19 | #include <linux/mempool.h> |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 20 | |
| 21 | struct ahash_request; |
| 22 | |
| 23 | /* |
| 24 | * Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty; |
| 25 | * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks. |
| 26 | */ |
| 27 | #define FS_VERITY_MAX_LEVELS 8 |
| 28 | |
| 29 | /* |
| 30 | * Largest digest size among all hash algorithms supported by fs-verity. |
| 31 | * Currently assumed to be <= size of fsverity_descriptor::root_hash. |
| 32 | */ |
Eric Biggers | add890c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 33 | #define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 34 | |
| 35 | /* A hash algorithm supported by fs-verity */ |
| 36 | struct fsverity_hash_alg { |
| 37 | struct crypto_ahash *tfm; /* hash tfm, allocated on demand */ |
| 38 | const char *name; /* crypto API name, e.g. sha256 */ |
| 39 | unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */ |
| 40 | unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */ |
Eric Biggers | 439bea1 | 2019-12-31 11:55:45 -0600 | [diff] [blame] | 41 | mempool_t req_pool; /* mempool with a preallocated hash request */ |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /* Merkle tree parameters: hash algorithm, initial hash state, and topology */ |
| 45 | struct merkle_tree_params { |
Eric Biggers | 439bea1 | 2019-12-31 11:55:45 -0600 | [diff] [blame] | 46 | struct fsverity_hash_alg *hash_alg; /* the hash algorithm */ |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 47 | const u8 *hashstate; /* initial hash state or NULL */ |
| 48 | unsigned int digest_size; /* same as hash_alg->digest_size */ |
| 49 | unsigned int block_size; /* size of data and tree blocks */ |
| 50 | unsigned int hashes_per_block; /* number of hashes per tree block */ |
| 51 | unsigned int log_blocksize; /* log2(block_size) */ |
| 52 | unsigned int log_arity; /* log2(hashes_per_block) */ |
| 53 | unsigned int num_levels; /* number of levels in Merkle tree */ |
| 54 | u64 tree_size; /* Merkle tree size in bytes */ |
Eric Biggers | fd39073 | 2020-01-06 12:55:33 -0800 | [diff] [blame] | 55 | unsigned long level0_blocks; /* number of blocks in tree level 0 */ |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Starting block index for each tree level, ordered from leaf level (0) |
| 59 | * to root level ('num_levels - 1') |
| 60 | */ |
| 61 | u64 level_start[FS_VERITY_MAX_LEVELS]; |
| 62 | }; |
| 63 | |
Eric Biggers | 6377a38 | 2020-05-11 12:21:17 -0700 | [diff] [blame] | 64 | /* |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 65 | * fsverity_info - cached verity metadata for an inode |
| 66 | * |
| 67 | * When a verity file is first opened, an instance of this struct is allocated |
| 68 | * and stored in ->i_verity_info; it remains until the inode is evicted. It |
| 69 | * caches information about the Merkle tree that's needed to efficiently verify |
Eric Biggers | ed45e20 | 2020-11-13 13:19:17 -0800 | [diff] [blame] | 70 | * data read from the file. It also caches the file digest. The Merkle tree |
| 71 | * pages themselves are not cached here, but the filesystem may cache them. |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 72 | */ |
| 73 | struct fsverity_info { |
| 74 | struct merkle_tree_params tree_params; |
| 75 | u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE]; |
Eric Biggers | ed45e20 | 2020-11-13 13:19:17 -0800 | [diff] [blame] | 76 | u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE]; |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 77 | const struct inode *inode; |
| 78 | }; |
| 79 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 80 | /* Arbitrary limit to bound the kmalloc() size. Can be changed. */ |
| 81 | #define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384 |
| 82 | |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 83 | #define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \ |
| 84 | sizeof(struct fsverity_descriptor)) |
| 85 | |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 86 | /* hash_algs.c */ |
| 87 | |
| 88 | extern struct fsverity_hash_alg fsverity_hash_algs[]; |
| 89 | |
Eric Biggers | 439bea1 | 2019-12-31 11:55:45 -0600 | [diff] [blame] | 90 | struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode, |
| 91 | unsigned int num); |
| 92 | struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg, |
| 93 | gfp_t gfp_flags); |
| 94 | void fsverity_free_hash_request(struct fsverity_hash_alg *alg, |
| 95 | struct ahash_request *req); |
| 96 | const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg, |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 97 | const u8 *salt, size_t salt_size); |
| 98 | int fsverity_hash_page(const struct merkle_tree_params *params, |
| 99 | const struct inode *inode, |
| 100 | struct ahash_request *req, struct page *page, u8 *out); |
Eric Biggers | 439bea1 | 2019-12-31 11:55:45 -0600 | [diff] [blame] | 101 | int fsverity_hash_buffer(struct fsverity_hash_alg *alg, |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 102 | const void *data, size_t size, u8 *out); |
| 103 | void __init fsverity_check_hash_algs(void); |
| 104 | |
| 105 | /* init.c */ |
| 106 | |
Eric Biggers | 9cd6b59 | 2020-05-11 12:21:18 -0700 | [diff] [blame] | 107 | void __printf(3, 4) __cold |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 108 | fsverity_msg(const struct inode *inode, const char *level, |
| 109 | const char *fmt, ...); |
| 110 | |
| 111 | #define fsverity_warn(inode, fmt, ...) \ |
| 112 | fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) |
| 113 | #define fsverity_err(inode, fmt, ...) \ |
| 114 | fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) |
| 115 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 116 | /* open.c */ |
| 117 | |
| 118 | int fsverity_init_merkle_tree_params(struct merkle_tree_params *params, |
| 119 | const struct inode *inode, |
| 120 | unsigned int hash_algorithm, |
| 121 | unsigned int log_blocksize, |
| 122 | const u8 *salt, size_t salt_size); |
| 123 | |
| 124 | struct fsverity_info *fsverity_create_info(const struct inode *inode, |
Eric Biggers | c2c8261 | 2021-01-15 10:18:14 -0800 | [diff] [blame] | 125 | struct fsverity_descriptor *desc, |
| 126 | size_t desc_size); |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 127 | |
| 128 | void fsverity_set_info(struct inode *inode, struct fsverity_info *vi); |
| 129 | |
| 130 | void fsverity_free_info(struct fsverity_info *vi); |
| 131 | |
Eric Biggers | c2c8261 | 2021-01-15 10:18:14 -0800 | [diff] [blame] | 132 | int fsverity_get_descriptor(struct inode *inode, |
| 133 | struct fsverity_descriptor **desc_ret, |
| 134 | size_t *desc_size_ret); |
| 135 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 136 | int __init fsverity_init_info_cache(void); |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 137 | void __init fsverity_exit_info_cache(void); |
| 138 | |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 139 | /* signature.c */ |
| 140 | |
| 141 | #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES |
| 142 | int fsverity_verify_signature(const struct fsverity_info *vi, |
Eric Biggers | fab634c | 2021-01-15 10:18:15 -0800 | [diff] [blame] | 143 | const u8 *signature, size_t sig_size); |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 144 | |
| 145 | int __init fsverity_init_signature(void); |
| 146 | #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */ |
| 147 | static inline int |
| 148 | fsverity_verify_signature(const struct fsverity_info *vi, |
Eric Biggers | fab634c | 2021-01-15 10:18:15 -0800 | [diff] [blame] | 149 | const u8 *signature, size_t sig_size) |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 150 | { |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static inline int fsverity_init_signature(void) |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */ |
| 159 | |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 160 | /* verify.c */ |
| 161 | |
| 162 | int __init fsverity_init_workqueue(void); |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 163 | void __init fsverity_exit_workqueue(void); |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 164 | |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 165 | #endif /* _FSVERITY_PRIVATE_H */ |