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