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