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