xref: /OK3568_Linux_fs/kernel/fs/verity/fsverity_private.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * fs-verity: read-only file-based authenticity protection
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2019 Google LLC
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef _FSVERITY_PRIVATE_H
9*4882a593Smuzhiyun #define _FSVERITY_PRIVATE_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #ifdef CONFIG_FS_VERITY_DEBUG
12*4882a593Smuzhiyun #define DEBUG
13*4882a593Smuzhiyun #endif
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define pr_fmt(fmt) "fs-verity: " fmt
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <crypto/sha.h>
18*4882a593Smuzhiyun #include <linux/fsverity.h>
19*4882a593Smuzhiyun #include <linux/mempool.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun struct ahash_request;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * Implementation limit: maximum depth of the Merkle tree.  For now 8 is plenty;
25*4882a593Smuzhiyun  * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun #define FS_VERITY_MAX_LEVELS		8
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * Largest digest size among all hash algorithms supported by fs-verity.
31*4882a593Smuzhiyun  * Currently assumed to be <= size of fsverity_descriptor::root_hash.
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun #define FS_VERITY_MAX_DIGEST_SIZE	SHA512_DIGEST_SIZE
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* A hash algorithm supported by fs-verity */
36*4882a593Smuzhiyun struct fsverity_hash_alg {
37*4882a593Smuzhiyun 	struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
38*4882a593Smuzhiyun 	const char *name;	  /* crypto API name, e.g. sha256 */
39*4882a593Smuzhiyun 	unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
40*4882a593Smuzhiyun 	unsigned int block_size;  /* block size in bytes, e.g. 64 for SHA-256 */
41*4882a593Smuzhiyun 	mempool_t req_pool;	  /* mempool with a preallocated hash request */
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /* Merkle tree parameters: hash algorithm, initial hash state, and topology */
45*4882a593Smuzhiyun struct merkle_tree_params {
46*4882a593Smuzhiyun 	struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
47*4882a593Smuzhiyun 	const u8 *hashstate;		/* initial hash state or NULL */
48*4882a593Smuzhiyun 	unsigned int digest_size;	/* same as hash_alg->digest_size */
49*4882a593Smuzhiyun 	unsigned int block_size;	/* size of data and tree blocks */
50*4882a593Smuzhiyun 	unsigned int hashes_per_block;	/* number of hashes per tree block */
51*4882a593Smuzhiyun 	unsigned int log_blocksize;	/* log2(block_size) */
52*4882a593Smuzhiyun 	unsigned int log_arity;		/* log2(hashes_per_block) */
53*4882a593Smuzhiyun 	unsigned int num_levels;	/* number of levels in Merkle tree */
54*4882a593Smuzhiyun 	u64 tree_size;			/* Merkle tree size in bytes */
55*4882a593Smuzhiyun 	unsigned long level0_blocks;	/* number of blocks in tree level 0 */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/*
58*4882a593Smuzhiyun 	 * Starting block index for each tree level, ordered from leaf level (0)
59*4882a593Smuzhiyun 	 * to root level ('num_levels - 1')
60*4882a593Smuzhiyun 	 */
61*4882a593Smuzhiyun 	u64 level_start[FS_VERITY_MAX_LEVELS];
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * fsverity_info - cached verity metadata for an inode
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * When a verity file is first opened, an instance of this struct is allocated
68*4882a593Smuzhiyun  * and stored in ->i_verity_info; it remains until the inode is evicted.  It
69*4882a593Smuzhiyun  * caches information about the Merkle tree that's needed to efficiently verify
70*4882a593Smuzhiyun  * data read from the file.  It also caches the file digest.  The Merkle tree
71*4882a593Smuzhiyun  * pages themselves are not cached here, but the filesystem may cache them.
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun struct fsverity_info {
74*4882a593Smuzhiyun 	struct merkle_tree_params tree_params;
75*4882a593Smuzhiyun 	u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
76*4882a593Smuzhiyun 	u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE];
77*4882a593Smuzhiyun 	const struct inode *inode;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /* Arbitrary limit to bound the kmalloc() size.  Can be changed. */
81*4882a593Smuzhiyun #define FS_VERITY_MAX_DESCRIPTOR_SIZE	16384
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #define FS_VERITY_MAX_SIGNATURE_SIZE	(FS_VERITY_MAX_DESCRIPTOR_SIZE - \
84*4882a593Smuzhiyun 					 sizeof(struct fsverity_descriptor))
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* hash_algs.c */
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun extern struct fsverity_hash_alg fsverity_hash_algs[];
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
91*4882a593Smuzhiyun 						unsigned int num);
92*4882a593Smuzhiyun struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg,
93*4882a593Smuzhiyun 						  gfp_t gfp_flags);
94*4882a593Smuzhiyun void fsverity_free_hash_request(struct fsverity_hash_alg *alg,
95*4882a593Smuzhiyun 				struct ahash_request *req);
96*4882a593Smuzhiyun const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,
97*4882a593Smuzhiyun 				      const u8 *salt, size_t salt_size);
98*4882a593Smuzhiyun int fsverity_hash_page(const struct merkle_tree_params *params,
99*4882a593Smuzhiyun 		       const struct inode *inode,
100*4882a593Smuzhiyun 		       struct ahash_request *req, struct page *page, u8 *out);
101*4882a593Smuzhiyun int fsverity_hash_buffer(struct fsverity_hash_alg *alg,
102*4882a593Smuzhiyun 			 const void *data, size_t size, u8 *out);
103*4882a593Smuzhiyun void __init fsverity_check_hash_algs(void);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /* init.c */
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun void __printf(3, 4) __cold
108*4882a593Smuzhiyun fsverity_msg(const struct inode *inode, const char *level,
109*4882a593Smuzhiyun 	     const char *fmt, ...);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun #define fsverity_warn(inode, fmt, ...)		\
112*4882a593Smuzhiyun 	fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
113*4882a593Smuzhiyun #define fsverity_err(inode, fmt, ...)		\
114*4882a593Smuzhiyun 	fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /* open.c */
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
119*4882a593Smuzhiyun 				     const struct inode *inode,
120*4882a593Smuzhiyun 				     unsigned int hash_algorithm,
121*4882a593Smuzhiyun 				     unsigned int log_blocksize,
122*4882a593Smuzhiyun 				     const u8 *salt, size_t salt_size);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun struct fsverity_info *fsverity_create_info(const struct inode *inode,
125*4882a593Smuzhiyun 					   struct fsverity_descriptor *desc,
126*4882a593Smuzhiyun 					   size_t desc_size);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun void fsverity_free_info(struct fsverity_info *vi);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun int fsverity_get_descriptor(struct inode *inode,
133*4882a593Smuzhiyun 			    struct fsverity_descriptor **desc_ret,
134*4882a593Smuzhiyun 			    size_t *desc_size_ret);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun int __init fsverity_init_info_cache(void);
137*4882a593Smuzhiyun void __init fsverity_exit_info_cache(void);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* signature.c */
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
142*4882a593Smuzhiyun int fsverity_verify_signature(const struct fsverity_info *vi,
143*4882a593Smuzhiyun 			      const u8 *signature, size_t sig_size);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun int __init fsverity_init_signature(void);
146*4882a593Smuzhiyun #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
147*4882a593Smuzhiyun static inline int
fsverity_verify_signature(const struct fsverity_info * vi,const u8 * signature,size_t sig_size)148*4882a593Smuzhiyun fsverity_verify_signature(const struct fsverity_info *vi,
149*4882a593Smuzhiyun 			  const u8 *signature, size_t sig_size)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	return 0;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
fsverity_init_signature(void)154*4882a593Smuzhiyun static inline int fsverity_init_signature(void)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	return 0;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /* verify.c */
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun int __init fsverity_init_workqueue(void);
163*4882a593Smuzhiyun void __init fsverity_exit_workqueue(void);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun #endif /* _FSVERITY_PRIVATE_H */
166