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