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 | |
| 17 | #include <crypto/sha.h> |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 18 | #include <linux/fsverity.h> |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 19 | |
| 20 | struct ahash_request; |
| 21 | |
| 22 | /* |
| 23 | * Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty; |
| 24 | * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks. |
| 25 | */ |
| 26 | #define FS_VERITY_MAX_LEVELS 8 |
| 27 | |
| 28 | /* |
| 29 | * Largest digest size among all hash algorithms supported by fs-verity. |
| 30 | * Currently assumed to be <= size of fsverity_descriptor::root_hash. |
| 31 | */ |
Eric Biggers | add890c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 32 | #define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 33 | |
| 34 | /* A hash algorithm supported by fs-verity */ |
| 35 | struct fsverity_hash_alg { |
| 36 | struct crypto_ahash *tfm; /* hash tfm, allocated on demand */ |
| 37 | const char *name; /* crypto API name, e.g. sha256 */ |
| 38 | unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */ |
| 39 | unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */ |
| 40 | }; |
| 41 | |
| 42 | /* Merkle tree parameters: hash algorithm, initial hash state, and topology */ |
| 43 | struct merkle_tree_params { |
| 44 | const struct fsverity_hash_alg *hash_alg; /* the hash algorithm */ |
| 45 | const u8 *hashstate; /* initial hash state or NULL */ |
| 46 | unsigned int digest_size; /* same as hash_alg->digest_size */ |
| 47 | unsigned int block_size; /* size of data and tree blocks */ |
| 48 | unsigned int hashes_per_block; /* number of hashes per tree block */ |
| 49 | unsigned int log_blocksize; /* log2(block_size) */ |
| 50 | unsigned int log_arity; /* log2(hashes_per_block) */ |
| 51 | unsigned int num_levels; /* number of levels in Merkle tree */ |
| 52 | u64 tree_size; /* Merkle tree size in bytes */ |
Eric Biggers | fd39073 | 2020-01-06 12:55:33 -0800 | [diff] [blame^] | 53 | unsigned long level0_blocks; /* number of blocks in tree level 0 */ |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Starting block index for each tree level, ordered from leaf level (0) |
| 57 | * to root level ('num_levels - 1') |
| 58 | */ |
| 59 | u64 level_start[FS_VERITY_MAX_LEVELS]; |
| 60 | }; |
| 61 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 62 | /** |
| 63 | * fsverity_info - cached verity metadata for an inode |
| 64 | * |
| 65 | * When a verity file is first opened, an instance of this struct is allocated |
| 66 | * and stored in ->i_verity_info; it remains until the inode is evicted. It |
| 67 | * caches information about the Merkle tree that's needed to efficiently verify |
| 68 | * data read from the file. It also caches the file measurement. The Merkle |
| 69 | * tree pages themselves are not cached here, but the filesystem may cache them. |
| 70 | */ |
| 71 | struct fsverity_info { |
| 72 | struct merkle_tree_params tree_params; |
| 73 | u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE]; |
| 74 | u8 measurement[FS_VERITY_MAX_DIGEST_SIZE]; |
| 75 | const struct inode *inode; |
| 76 | }; |
| 77 | |
| 78 | /* |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 79 | * Merkle tree properties. The file measurement is the hash of this structure |
| 80 | * excluding the signature and with the sig_size field set to 0. |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 81 | */ |
| 82 | struct fsverity_descriptor { |
| 83 | __u8 version; /* must be 1 */ |
| 84 | __u8 hash_algorithm; /* Merkle tree hash algorithm */ |
| 85 | __u8 log_blocksize; /* log2 of size of data and tree blocks */ |
| 86 | __u8 salt_size; /* size of salt in bytes; 0 if none */ |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 87 | __le32 sig_size; /* size of signature in bytes; 0 if none */ |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 88 | __le64 data_size; /* size of file the Merkle tree is built over */ |
| 89 | __u8 root_hash[64]; /* Merkle tree root hash */ |
| 90 | __u8 salt[32]; /* salt prepended to each hashed block */ |
| 91 | __u8 __reserved[144]; /* must be 0's */ |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 92 | __u8 signature[]; /* optional PKCS#7 signature */ |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* Arbitrary limit to bound the kmalloc() size. Can be changed. */ |
| 96 | #define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384 |
| 97 | |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 98 | #define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \ |
| 99 | sizeof(struct fsverity_descriptor)) |
| 100 | |
| 101 | /* |
| 102 | * Format in which verity file measurements are signed. This is the same as |
| 103 | * 'struct fsverity_digest', except here some magic bytes are prepended to |
| 104 | * provide some context about what is being signed in case the same key is used |
| 105 | * for non-fsverity purposes, and here the fields have fixed endianness. |
| 106 | */ |
| 107 | struct fsverity_signed_digest { |
| 108 | char magic[8]; /* must be "FSVerity" */ |
| 109 | __le16 digest_algorithm; |
| 110 | __le16 digest_size; |
| 111 | __u8 digest[]; |
| 112 | }; |
| 113 | |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 114 | /* hash_algs.c */ |
| 115 | |
| 116 | extern struct fsverity_hash_alg fsverity_hash_algs[]; |
| 117 | |
| 118 | const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode, |
| 119 | unsigned int num); |
| 120 | const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg, |
| 121 | const u8 *salt, size_t salt_size); |
| 122 | int fsverity_hash_page(const struct merkle_tree_params *params, |
| 123 | const struct inode *inode, |
| 124 | struct ahash_request *req, struct page *page, u8 *out); |
| 125 | int fsverity_hash_buffer(const struct fsverity_hash_alg *alg, |
| 126 | const void *data, size_t size, u8 *out); |
| 127 | void __init fsverity_check_hash_algs(void); |
| 128 | |
| 129 | /* init.c */ |
| 130 | |
| 131 | extern void __printf(3, 4) __cold |
| 132 | fsverity_msg(const struct inode *inode, const char *level, |
| 133 | const char *fmt, ...); |
| 134 | |
| 135 | #define fsverity_warn(inode, fmt, ...) \ |
| 136 | fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) |
| 137 | #define fsverity_err(inode, fmt, ...) \ |
| 138 | fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) |
| 139 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 140 | /* open.c */ |
| 141 | |
| 142 | int fsverity_init_merkle_tree_params(struct merkle_tree_params *params, |
| 143 | const struct inode *inode, |
| 144 | unsigned int hash_algorithm, |
| 145 | unsigned int log_blocksize, |
| 146 | const u8 *salt, size_t salt_size); |
| 147 | |
| 148 | struct fsverity_info *fsverity_create_info(const struct inode *inode, |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 149 | void *desc, size_t desc_size); |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 150 | |
| 151 | void fsverity_set_info(struct inode *inode, struct fsverity_info *vi); |
| 152 | |
| 153 | void fsverity_free_info(struct fsverity_info *vi); |
| 154 | |
| 155 | int __init fsverity_init_info_cache(void); |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 156 | void __init fsverity_exit_info_cache(void); |
| 157 | |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 158 | /* signature.c */ |
| 159 | |
| 160 | #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES |
| 161 | int fsverity_verify_signature(const struct fsverity_info *vi, |
| 162 | const struct fsverity_descriptor *desc, |
| 163 | size_t desc_size); |
| 164 | |
| 165 | int __init fsverity_init_signature(void); |
| 166 | #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */ |
| 167 | static inline int |
| 168 | fsverity_verify_signature(const struct fsverity_info *vi, |
| 169 | const struct fsverity_descriptor *desc, |
| 170 | size_t desc_size) |
| 171 | { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static inline int fsverity_init_signature(void) |
| 176 | { |
| 177 | return 0; |
| 178 | } |
| 179 | #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */ |
| 180 | |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 181 | /* verify.c */ |
| 182 | |
| 183 | int __init fsverity_init_workqueue(void); |
Eric Biggers | 432434c | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 184 | void __init fsverity_exit_workqueue(void); |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 185 | |
Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame] | 186 | #endif /* _FSVERITY_PRIVATE_H */ |