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/rcupdate.h>
15 #include <linux/rhashtable.h>
16
17 /*
18 * Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty;
19 * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
20 */
21 #define FS_VERITY_MAX_LEVELS 8
22
23 /* A hash algorithm supported by fs-verity */
24 struct fsverity_hash_alg {
25 const char *name; /* crypto API name, e.g. sha256 */
26 unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
27 unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
28 /*
29 * The HASH_ALGO_* constant for this algorithm. This is different from
30 * FS_VERITY_HASH_ALG_*, which uses a different numbering scheme.
31 */
32 enum hash_algo algo_id;
33 };
34
35 union fsverity_hash_ctx {
36 struct sha256_ctx sha256;
37 struct sha512_ctx sha512;
38 };
39
40 /* Merkle tree parameters: hash algorithm, initial hash state, and topology */
41 struct merkle_tree_params {
42 const struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
43 /* initial hash state if salted, NULL if unsalted */
44 const union fsverity_hash_ctx *hashstate;
45 unsigned int digest_size; /* same as hash_alg->digest_size */
46 unsigned int block_size; /* size of data and tree blocks */
47 unsigned int hashes_per_block; /* number of hashes per tree block */
48 unsigned int blocks_per_page; /* PAGE_SIZE / block_size */
49 u8 log_digestsize; /* log2(digest_size) */
50 u8 log_blocksize; /* log2(block_size) */
51 u8 log_arity; /* log2(hashes_per_block) */
52 u8 log_blocks_per_page; /* log2(blocks_per_page) */
53 unsigned int num_levels; /* number of levels in Merkle tree */
54 u64 tree_size; /* Merkle tree size in bytes */
55 unsigned long tree_pages; /* Merkle tree size in pages */
56
57 /* the hash of an all-zeroes block */
58 u8 zero_digest[FS_VERITY_MAX_DIGEST_SIZE];
59
60 /*
61 * Starting block index for each tree level, ordered from leaf level (0)
62 * to root level ('num_levels - 1')
63 */
64 unsigned long level_start[FS_VERITY_MAX_LEVELS];
65 };
66
67 /*
68 * fsverity_info - cached verity metadata for an inode
69 *
70 * When a verity file is first opened, an instance of this struct is allocated
71 * and a pointer to it is stored in the global hash table, indexed by the inode
72 * pointer value. It remains alive until the inode is evicted. It caches
73 * information about the Merkle tree that's needed to efficiently verify data
74 * read from the file. It also caches the file digest. The Merkle tree pages
75 * themselves are not cached here, but the filesystem may cache them.
76 */
77 struct fsverity_info {
78 struct rhash_head rhash_head;
79 struct merkle_tree_params tree_params;
80 union {
81 u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
82
83 /*
84 * Used for final freeing of the struct, in which case root_hash
85 * is no longer needed. Specifically, the only fields needed
86 * besides the rcu_head are those needed by fsverity_free_info()
87 * itself, and those that can be accessed by fsverity_get_info()
88 * (rhash_head and inode) during the RCU-protected rhashtable
89 * chain search trying to find another inode's fsverity_info.
90 */
91 struct rcu_head rcu_head;
92 };
93 u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE];
94 struct inode *inode;
95 unsigned long *hash_block_verified;
96 };
97
98 #define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
99 sizeof(struct fsverity_descriptor))
100
101 /* hash_algs.c */
102
103 extern const struct fsverity_hash_alg fsverity_hash_algs[];
104
105 const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
106 unsigned int num);
107 union fsverity_hash_ctx *
108 fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg,
109 const u8 *salt, size_t salt_size);
110 void fsverity_hash_block(const struct merkle_tree_params *params,
111 const void *data, u8 *out);
112 void fsverity_hash_buffer(const struct fsverity_hash_alg *alg,
113 const void *data, size_t size, u8 *out);
114 void __init fsverity_check_hash_algs(void);
115
116 /* init.c */
117
118 void __printf(3, 4) __cold
119 fsverity_msg(const struct inode *inode, const char *level,
120 const char *fmt, ...);
121
122 #define fsverity_warn(inode, fmt, ...) \
123 fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
124 #define fsverity_err(inode, fmt, ...) \
125 fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
126
127 /* measure.c */
128
129 #ifdef CONFIG_BPF_SYSCALL
130 void __init fsverity_init_bpf(void);
131 #else
fsverity_init_bpf(void)132 static inline void fsverity_init_bpf(void)
133 {
134 }
135 #endif
136
137 /* open.c */
138
139 int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
140 const struct inode *inode,
141 unsigned int hash_algorithm,
142 unsigned int log_blocksize,
143 const u8 *salt, size_t salt_size);
144
145 struct fsverity_info *fsverity_create_info(struct inode *inode,
146 struct fsverity_descriptor *desc);
147
148 int fsverity_set_info(struct fsverity_info *vi);
149 void fsverity_free_info(struct fsverity_info *vi);
150 void fsverity_remove_info(struct fsverity_info *vi);
151
152 int fsverity_get_descriptor(struct inode *inode,
153 struct fsverity_descriptor **desc_ret);
154
155 void __init fsverity_init_info_cache(void);
156
157 /* signature.c */
158
159 #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
160 extern int fsverity_require_signatures;
161 int fsverity_verify_signature(const struct fsverity_info *vi,
162 const u8 *signature, size_t sig_size);
163
164 void __init fsverity_init_signature(void);
165 #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
166 static inline int
fsverity_verify_signature(const struct fsverity_info * vi,const u8 * signature,size_t sig_size)167 fsverity_verify_signature(const struct fsverity_info *vi,
168 const u8 *signature, size_t sig_size)
169 {
170 return 0;
171 }
172
fsverity_init_signature(void)173 static inline void fsverity_init_signature(void)
174 {
175 }
176 #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
177
178 /* verify.c */
179
180 void __init fsverity_init_workqueue(void);
181
182 #include <trace/events/fsverity.h>
183
184 #endif /* _FSVERITY_PRIVATE_H */
185