xref: /openbmc/linux/fs/verity/fsverity_private.h (revision 432434c9)
1671e67b4SEric Biggers /* SPDX-License-Identifier: GPL-2.0 */
2671e67b4SEric Biggers /*
3671e67b4SEric Biggers  * fs-verity: read-only file-based authenticity protection
4671e67b4SEric Biggers  *
5671e67b4SEric Biggers  * Copyright 2019 Google LLC
6671e67b4SEric Biggers  */
7671e67b4SEric Biggers 
8671e67b4SEric Biggers #ifndef _FSVERITY_PRIVATE_H
9671e67b4SEric Biggers #define _FSVERITY_PRIVATE_H
10671e67b4SEric Biggers 
11671e67b4SEric Biggers #ifdef CONFIG_FS_VERITY_DEBUG
12671e67b4SEric Biggers #define DEBUG
13671e67b4SEric Biggers #endif
14671e67b4SEric Biggers 
15671e67b4SEric Biggers #define pr_fmt(fmt) "fs-verity: " fmt
16671e67b4SEric Biggers 
17671e67b4SEric Biggers #include <crypto/sha.h>
18fd2d1acfSEric Biggers #include <linux/fsverity.h>
19671e67b4SEric Biggers 
20671e67b4SEric Biggers struct ahash_request;
21671e67b4SEric Biggers 
22671e67b4SEric Biggers /*
23671e67b4SEric Biggers  * Implementation limit: maximum depth of the Merkle tree.  For now 8 is plenty;
24671e67b4SEric Biggers  * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
25671e67b4SEric Biggers  */
26671e67b4SEric Biggers #define FS_VERITY_MAX_LEVELS		8
27671e67b4SEric Biggers 
28671e67b4SEric Biggers /*
29671e67b4SEric Biggers  * Largest digest size among all hash algorithms supported by fs-verity.
30671e67b4SEric Biggers  * Currently assumed to be <= size of fsverity_descriptor::root_hash.
31671e67b4SEric Biggers  */
32add890c9SEric Biggers #define FS_VERITY_MAX_DIGEST_SIZE	SHA512_DIGEST_SIZE
33671e67b4SEric Biggers 
34671e67b4SEric Biggers /* A hash algorithm supported by fs-verity */
35671e67b4SEric Biggers struct fsverity_hash_alg {
36671e67b4SEric Biggers 	struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
37671e67b4SEric Biggers 	const char *name;	  /* crypto API name, e.g. sha256 */
38671e67b4SEric Biggers 	unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
39671e67b4SEric Biggers 	unsigned int block_size;  /* block size in bytes, e.g. 64 for SHA-256 */
40671e67b4SEric Biggers };
41671e67b4SEric Biggers 
42671e67b4SEric Biggers /* Merkle tree parameters: hash algorithm, initial hash state, and topology */
43671e67b4SEric Biggers struct merkle_tree_params {
44671e67b4SEric Biggers 	const struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
45671e67b4SEric Biggers 	const u8 *hashstate;		/* initial hash state or NULL */
46671e67b4SEric Biggers 	unsigned int digest_size;	/* same as hash_alg->digest_size */
47671e67b4SEric Biggers 	unsigned int block_size;	/* size of data and tree blocks */
48671e67b4SEric Biggers 	unsigned int hashes_per_block;	/* number of hashes per tree block */
49671e67b4SEric Biggers 	unsigned int log_blocksize;	/* log2(block_size) */
50671e67b4SEric Biggers 	unsigned int log_arity;		/* log2(hashes_per_block) */
51671e67b4SEric Biggers 	unsigned int num_levels;	/* number of levels in Merkle tree */
52671e67b4SEric Biggers 	u64 tree_size;			/* Merkle tree size in bytes */
53671e67b4SEric Biggers 
54671e67b4SEric Biggers 	/*
55671e67b4SEric Biggers 	 * Starting block index for each tree level, ordered from leaf level (0)
56671e67b4SEric Biggers 	 * to root level ('num_levels - 1')
57671e67b4SEric Biggers 	 */
58671e67b4SEric Biggers 	u64 level_start[FS_VERITY_MAX_LEVELS];
59671e67b4SEric Biggers };
60671e67b4SEric Biggers 
61fd2d1acfSEric Biggers /**
62fd2d1acfSEric Biggers  * fsverity_info - cached verity metadata for an inode
63fd2d1acfSEric Biggers  *
64fd2d1acfSEric Biggers  * When a verity file is first opened, an instance of this struct is allocated
65fd2d1acfSEric Biggers  * and stored in ->i_verity_info; it remains until the inode is evicted.  It
66fd2d1acfSEric Biggers  * caches information about the Merkle tree that's needed to efficiently verify
67fd2d1acfSEric Biggers  * data read from the file.  It also caches the file measurement.  The Merkle
68fd2d1acfSEric Biggers  * tree pages themselves are not cached here, but the filesystem may cache them.
69fd2d1acfSEric Biggers  */
70fd2d1acfSEric Biggers struct fsverity_info {
71fd2d1acfSEric Biggers 	struct merkle_tree_params tree_params;
72fd2d1acfSEric Biggers 	u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
73fd2d1acfSEric Biggers 	u8 measurement[FS_VERITY_MAX_DIGEST_SIZE];
74fd2d1acfSEric Biggers 	const struct inode *inode;
75fd2d1acfSEric Biggers };
76fd2d1acfSEric Biggers 
77fd2d1acfSEric Biggers /*
78432434c9SEric Biggers  * Merkle tree properties.  The file measurement is the hash of this structure
79432434c9SEric Biggers  * excluding the signature and with the sig_size field set to 0.
80fd2d1acfSEric Biggers  */
81fd2d1acfSEric Biggers struct fsverity_descriptor {
82fd2d1acfSEric Biggers 	__u8 version;		/* must be 1 */
83fd2d1acfSEric Biggers 	__u8 hash_algorithm;	/* Merkle tree hash algorithm */
84fd2d1acfSEric Biggers 	__u8 log_blocksize;	/* log2 of size of data and tree blocks */
85fd2d1acfSEric Biggers 	__u8 salt_size;		/* size of salt in bytes; 0 if none */
86432434c9SEric Biggers 	__le32 sig_size;	/* size of signature in bytes; 0 if none */
87fd2d1acfSEric Biggers 	__le64 data_size;	/* size of file the Merkle tree is built over */
88fd2d1acfSEric Biggers 	__u8 root_hash[64];	/* Merkle tree root hash */
89fd2d1acfSEric Biggers 	__u8 salt[32];		/* salt prepended to each hashed block */
90fd2d1acfSEric Biggers 	__u8 __reserved[144];	/* must be 0's */
91432434c9SEric Biggers 	__u8 signature[];	/* optional PKCS#7 signature */
92fd2d1acfSEric Biggers };
93fd2d1acfSEric Biggers 
94fd2d1acfSEric Biggers /* Arbitrary limit to bound the kmalloc() size.  Can be changed. */
95fd2d1acfSEric Biggers #define FS_VERITY_MAX_DESCRIPTOR_SIZE	16384
96fd2d1acfSEric Biggers 
97432434c9SEric Biggers #define FS_VERITY_MAX_SIGNATURE_SIZE	(FS_VERITY_MAX_DESCRIPTOR_SIZE - \
98432434c9SEric Biggers 					 sizeof(struct fsverity_descriptor))
99432434c9SEric Biggers 
100432434c9SEric Biggers /*
101432434c9SEric Biggers  * Format in which verity file measurements are signed.  This is the same as
102432434c9SEric Biggers  * 'struct fsverity_digest', except here some magic bytes are prepended to
103432434c9SEric Biggers  * provide some context about what is being signed in case the same key is used
104432434c9SEric Biggers  * for non-fsverity purposes, and here the fields have fixed endianness.
105432434c9SEric Biggers  */
106432434c9SEric Biggers struct fsverity_signed_digest {
107432434c9SEric Biggers 	char magic[8];			/* must be "FSVerity" */
108432434c9SEric Biggers 	__le16 digest_algorithm;
109432434c9SEric Biggers 	__le16 digest_size;
110432434c9SEric Biggers 	__u8 digest[];
111432434c9SEric Biggers };
112432434c9SEric Biggers 
113671e67b4SEric Biggers /* hash_algs.c */
114671e67b4SEric Biggers 
115671e67b4SEric Biggers extern struct fsverity_hash_alg fsverity_hash_algs[];
116671e67b4SEric Biggers 
117671e67b4SEric Biggers const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
118671e67b4SEric Biggers 						      unsigned int num);
119671e67b4SEric Biggers const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg,
120671e67b4SEric Biggers 				      const u8 *salt, size_t salt_size);
121671e67b4SEric Biggers int fsverity_hash_page(const struct merkle_tree_params *params,
122671e67b4SEric Biggers 		       const struct inode *inode,
123671e67b4SEric Biggers 		       struct ahash_request *req, struct page *page, u8 *out);
124671e67b4SEric Biggers int fsverity_hash_buffer(const struct fsverity_hash_alg *alg,
125671e67b4SEric Biggers 			 const void *data, size_t size, u8 *out);
126671e67b4SEric Biggers void __init fsverity_check_hash_algs(void);
127671e67b4SEric Biggers 
128671e67b4SEric Biggers /* init.c */
129671e67b4SEric Biggers 
130671e67b4SEric Biggers extern void __printf(3, 4) __cold
131671e67b4SEric Biggers fsverity_msg(const struct inode *inode, const char *level,
132671e67b4SEric Biggers 	     const char *fmt, ...);
133671e67b4SEric Biggers 
134671e67b4SEric Biggers #define fsverity_warn(inode, fmt, ...)		\
135671e67b4SEric Biggers 	fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
136671e67b4SEric Biggers #define fsverity_err(inode, fmt, ...)		\
137671e67b4SEric Biggers 	fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
138671e67b4SEric Biggers 
139fd2d1acfSEric Biggers /* open.c */
140fd2d1acfSEric Biggers 
141fd2d1acfSEric Biggers int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
142fd2d1acfSEric Biggers 				     const struct inode *inode,
143fd2d1acfSEric Biggers 				     unsigned int hash_algorithm,
144fd2d1acfSEric Biggers 				     unsigned int log_blocksize,
145fd2d1acfSEric Biggers 				     const u8 *salt, size_t salt_size);
146fd2d1acfSEric Biggers 
147fd2d1acfSEric Biggers struct fsverity_info *fsverity_create_info(const struct inode *inode,
148432434c9SEric Biggers 					   void *desc, size_t desc_size);
149fd2d1acfSEric Biggers 
150fd2d1acfSEric Biggers void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
151fd2d1acfSEric Biggers 
152fd2d1acfSEric Biggers void fsverity_free_info(struct fsverity_info *vi);
153fd2d1acfSEric Biggers 
154fd2d1acfSEric Biggers int __init fsverity_init_info_cache(void);
1558a1d0f9cSEric Biggers void __init fsverity_exit_info_cache(void);
1568a1d0f9cSEric Biggers 
157432434c9SEric Biggers /* signature.c */
158432434c9SEric Biggers 
159432434c9SEric Biggers #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
160432434c9SEric Biggers int fsverity_verify_signature(const struct fsverity_info *vi,
161432434c9SEric Biggers 			      const struct fsverity_descriptor *desc,
162432434c9SEric Biggers 			      size_t desc_size);
163432434c9SEric Biggers 
164432434c9SEric Biggers int __init fsverity_init_signature(void);
165432434c9SEric Biggers #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
166432434c9SEric Biggers static inline int
167432434c9SEric Biggers fsverity_verify_signature(const struct fsverity_info *vi,
168432434c9SEric Biggers 			  const struct fsverity_descriptor *desc,
169432434c9SEric Biggers 			  size_t desc_size)
170432434c9SEric Biggers {
171432434c9SEric Biggers 	return 0;
172432434c9SEric Biggers }
173432434c9SEric Biggers 
174432434c9SEric Biggers static inline int fsverity_init_signature(void)
175432434c9SEric Biggers {
176432434c9SEric Biggers 	return 0;
177432434c9SEric Biggers }
178432434c9SEric Biggers #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
179432434c9SEric Biggers 
1808a1d0f9cSEric Biggers /* verify.c */
1818a1d0f9cSEric Biggers 
1828a1d0f9cSEric Biggers int __init fsverity_init_workqueue(void);
183432434c9SEric Biggers void __init fsverity_exit_workqueue(void);
184fd2d1acfSEric Biggers 
185671e67b4SEric Biggers #endif /* _FSVERITY_PRIVATE_H */
186