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