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 { | |
8fcd94ad | 23 | struct crypto_shash *tfm; /* hash tfm, allocated on demand */ |
671e67b4 EB |
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 */ | |
a4bbf53d EB |
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; | |
671e67b4 EB |
32 | }; |
33 | ||
34 | /* Merkle tree parameters: hash algorithm, initial hash state, and topology */ | |
35 | struct merkle_tree_params { | |
32ab3c5e | 36 | const struct fsverity_hash_alg *hash_alg; /* the hash algorithm */ |
671e67b4 EB |
37 | const u8 *hashstate; /* initial hash state or NULL */ |
38 | unsigned int digest_size; /* same as hash_alg->digest_size */ | |
39 | unsigned int block_size; /* size of data and tree blocks */ | |
40 | unsigned int hashes_per_block; /* number of hashes per tree block */ | |
5306892a | 41 | unsigned int blocks_per_page; /* PAGE_SIZE / block_size */ |
579a12f7 EB |
42 | u8 log_digestsize; /* log2(digest_size) */ |
43 | u8 log_blocksize; /* log2(block_size) */ | |
44 | u8 log_arity; /* log2(hashes_per_block) */ | |
5306892a | 45 | u8 log_blocks_per_page; /* log2(blocks_per_page) */ |
671e67b4 EB |
46 | unsigned int num_levels; /* number of levels in Merkle tree */ |
47 | u64 tree_size; /* Merkle tree size in bytes */ | |
9098f36b | 48 | unsigned long tree_pages; /* Merkle tree size in pages */ |
671e67b4 EB |
49 | |
50 | /* | |
51 | * Starting block index for each tree level, ordered from leaf level (0) | |
52 | * to root level ('num_levels - 1') | |
53 | */ | |
284d5db5 | 54 | unsigned long level_start[FS_VERITY_MAX_LEVELS]; |
671e67b4 EB |
55 | }; |
56 | ||
6377a38b | 57 | /* |
fd2d1acf EB |
58 | * fsverity_info - cached verity metadata for an inode |
59 | * | |
60 | * When a verity file is first opened, an instance of this struct is allocated | |
61 | * and stored in ->i_verity_info; it remains until the inode is evicted. It | |
62 | * caches information about the Merkle tree that's needed to efficiently verify | |
ed45e201 EB |
63 | * data read from the file. It also caches the file digest. The Merkle tree |
64 | * pages themselves are not cached here, but the filesystem may cache them. | |
fd2d1acf EB |
65 | */ |
66 | struct fsverity_info { | |
67 | struct merkle_tree_params tree_params; | |
68 | u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE]; | |
ed45e201 | 69 | u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE]; |
fd2d1acf | 70 | const struct inode *inode; |
5306892a EB |
71 | unsigned long *hash_block_verified; |
72 | spinlock_t hash_page_init_lock; | |
fd2d1acf EB |
73 | }; |
74 | ||
432434c9 EB |
75 | #define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \ |
76 | sizeof(struct fsverity_descriptor)) | |
77 | ||
671e67b4 EB |
78 | /* hash_algs.c */ |
79 | ||
80 | extern struct fsverity_hash_alg fsverity_hash_algs[]; | |
81 | ||
32ab3c5e EB |
82 | const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode, |
83 | unsigned int num); | |
84 | const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg, | |
671e67b4 | 85 | const u8 *salt, size_t salt_size); |
f45555bf | 86 | int fsverity_hash_block(const struct merkle_tree_params *params, |
8fcd94ad | 87 | const struct inode *inode, const void *data, u8 *out); |
32ab3c5e | 88 | int fsverity_hash_buffer(const struct fsverity_hash_alg *alg, |
671e67b4 EB |
89 | const void *data, size_t size, u8 *out); |
90 | void __init fsverity_check_hash_algs(void); | |
91 | ||
92 | /* init.c */ | |
93 | ||
9cd6b593 | 94 | void __printf(3, 4) __cold |
671e67b4 EB |
95 | fsverity_msg(const struct inode *inode, const char *level, |
96 | const char *fmt, ...); | |
97 | ||
98 | #define fsverity_warn(inode, fmt, ...) \ | |
99 | fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) | |
100 | #define fsverity_err(inode, fmt, ...) \ | |
101 | fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) | |
102 | ||
fd2d1acf EB |
103 | /* open.c */ |
104 | ||
105 | int fsverity_init_merkle_tree_params(struct merkle_tree_params *params, | |
106 | const struct inode *inode, | |
107 | unsigned int hash_algorithm, | |
108 | unsigned int log_blocksize, | |
109 | const u8 *salt, size_t salt_size); | |
110 | ||
111 | struct fsverity_info *fsverity_create_info(const struct inode *inode, | |
b0487ede | 112 | struct fsverity_descriptor *desc); |
fd2d1acf EB |
113 | |
114 | void fsverity_set_info(struct inode *inode, struct fsverity_info *vi); | |
115 | ||
116 | void fsverity_free_info(struct fsverity_info *vi); | |
117 | ||
c2c82611 | 118 | int fsverity_get_descriptor(struct inode *inode, |
b0487ede | 119 | struct fsverity_descriptor **desc_ret); |
c2c82611 | 120 | |
e77000cc | 121 | void __init fsverity_init_info_cache(void); |
8a1d0f9c | 122 | |
432434c9 EB |
123 | /* signature.c */ |
124 | ||
125 | #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES | |
456ae5fe | 126 | extern int fsverity_require_signatures; |
432434c9 | 127 | int fsverity_verify_signature(const struct fsverity_info *vi, |
fab634c4 | 128 | const u8 *signature, size_t sig_size); |
432434c9 | 129 | |
e77000cc | 130 | void __init fsverity_init_signature(void); |
432434c9 EB |
131 | #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */ |
132 | static inline int | |
133 | fsverity_verify_signature(const struct fsverity_info *vi, | |
fab634c4 | 134 | const u8 *signature, size_t sig_size) |
432434c9 EB |
135 | { |
136 | return 0; | |
137 | } | |
138 | ||
e77000cc | 139 | static inline void fsverity_init_signature(void) |
432434c9 | 140 | { |
432434c9 EB |
141 | } |
142 | #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */ | |
143 | ||
8a1d0f9c EB |
144 | /* verify.c */ |
145 | ||
e77000cc | 146 | void __init fsverity_init_workqueue(void); |
fd2d1acf | 147 | |
671e67b4 | 148 | #endif /* _FSVERITY_PRIVATE_H */ |