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 #define pr_fmt(fmt) "fs-verity: " fmt
12
13 #include <linux/fsverity.h>
14 #include <linux/mempool.h>
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
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 */
30 mempool_t req_pool; /* mempool with a preallocated hash request */
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;
36 };
37
38 /* Merkle tree parameters: hash algorithm, initial hash state, and topology */
39 struct merkle_tree_params {
40 struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
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 */
45 unsigned int blocks_per_page; /* PAGE_SIZE / block_size */
46 u8 log_digestsize; /* log2(digest_size) */
47 u8 log_blocksize; /* log2(block_size) */
48 u8 log_arity; /* log2(hashes_per_block) */
49 u8 log_blocks_per_page; /* log2(blocks_per_page) */
50 unsigned int num_levels; /* number of levels in Merkle tree */
51 u64 tree_size; /* Merkle tree size in bytes */
52 unsigned long tree_pages; /* Merkle tree size in pages */
53
54 /*
55 * Starting block index for each tree level, ordered from leaf level (0)
56 * to root level ('num_levels - 1')
57 */
58 unsigned long level_start[FS_VERITY_MAX_LEVELS];
59 };
60
61 /*
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
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.
69 */
70 struct fsverity_info {
71 struct merkle_tree_params tree_params;
72 u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
73 u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE];
74 const struct inode *inode;
75 unsigned long *hash_block_verified;
76 spinlock_t hash_page_init_lock;
77 };
78
79 /* Arbitrary limit to bound the kmalloc() size. Can be changed. */
80 #define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
81
82 #define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
83 sizeof(struct fsverity_descriptor))
84
85 /* hash_algs.c */
86
87 extern struct fsverity_hash_alg fsverity_hash_algs[];
88
89 struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
90 unsigned int num);
91 struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg,
92 gfp_t gfp_flags);
93 void fsverity_free_hash_request(struct fsverity_hash_alg *alg,
94 struct ahash_request *req);
95 const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,
96 const u8 *salt, size_t salt_size);
97 int fsverity_hash_block(const struct merkle_tree_params *params,
98 const struct inode *inode, struct ahash_request *req,
99 struct page *page, unsigned int offset, u8 *out);
100 int fsverity_hash_buffer(struct fsverity_hash_alg *alg,
101 const void *data, size_t size, u8 *out);
102 void __init fsverity_check_hash_algs(void);
103
104 /* init.c */
105
106 void __printf(3, 4) __cold
107 fsverity_msg(const struct inode *inode, const char *level,
108 const char *fmt, ...);
109
110 #define fsverity_warn(inode, fmt, ...) \
111 fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
112 #define fsverity_err(inode, fmt, ...) \
113 fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
114
115 /* open.c */
116
117 int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
118 const struct inode *inode,
119 unsigned int hash_algorithm,
120 unsigned int log_blocksize,
121 const u8 *salt, size_t salt_size);
122
123 struct fsverity_info *fsverity_create_info(const struct inode *inode,
124 struct fsverity_descriptor *desc);
125
126 void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
127
128 void fsverity_free_info(struct fsverity_info *vi);
129
130 int fsverity_get_descriptor(struct inode *inode,
131 struct fsverity_descriptor **desc_ret);
132
133 int __init fsverity_init_info_cache(void);
134 void __init fsverity_exit_info_cache(void);
135
136 /* signature.c */
137
138 #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
139 int fsverity_verify_signature(const struct fsverity_info *vi,
140 const u8 *signature, size_t sig_size);
141
142 int __init fsverity_init_signature(void);
143 #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
144 static inline int
fsverity_verify_signature(const struct fsverity_info * vi,const u8 * signature,size_t sig_size)145 fsverity_verify_signature(const struct fsverity_info *vi,
146 const u8 *signature, size_t sig_size)
147 {
148 return 0;
149 }
150
fsverity_init_signature(void)151 static inline int fsverity_init_signature(void)
152 {
153 return 0;
154 }
155 #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
156
157 /* verify.c */
158
159 int __init fsverity_init_workqueue(void);
160 void __init fsverity_exit_workqueue(void);
161
162 #endif /* _FSVERITY_PRIVATE_H */
163