Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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#ifdef CONFIG_FS_VERITY_DEBUG
12#define DEBUG
13#endif
14
15#define pr_fmt(fmt) "fs-verity: " fmt
16
17#include <linux/fsverity.h>
18#include <linux/mempool.h>
19
20struct ahash_request;
21
22/*
23 * Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty;
24 * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
25 */
26#define FS_VERITY_MAX_LEVELS 8
27
28/* A hash algorithm supported by fs-verity */
29struct fsverity_hash_alg {
30 struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
31 const char *name; /* crypto API name, e.g. sha256 */
32 unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
33 unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
34 mempool_t req_pool; /* mempool with a preallocated hash request */
35};
36
37/* Merkle tree parameters: hash algorithm, initial hash state, and topology */
38struct merkle_tree_params {
39 struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
40 const u8 *hashstate; /* initial hash state or NULL */
41 unsigned int digest_size; /* same as hash_alg->digest_size */
42 unsigned int block_size; /* size of data and tree blocks */
43 unsigned int hashes_per_block; /* number of hashes per tree block */
44 unsigned int log_blocksize; /* log2(block_size) */
45 unsigned int log_arity; /* log2(hashes_per_block) */
46 unsigned int num_levels; /* number of levels in Merkle tree */
47 u64 tree_size; /* Merkle tree size in bytes */
48 unsigned long level0_blocks; /* number of blocks in tree level 0 */
49
50 /*
51 * Starting block index for each tree level, ordered from leaf level (0)
52 * to root level ('num_levels - 1')
53 */
54 u64 level_start[FS_VERITY_MAX_LEVELS];
55};
56
57/*
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
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.
65 */
66struct fsverity_info {
67 struct merkle_tree_params tree_params;
68 u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
69 u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE];
70 const struct inode *inode;
71};
72
73/* Arbitrary limit to bound the kmalloc() size. Can be changed. */
74#define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
75
76#define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
77 sizeof(struct fsverity_descriptor))
78
79/* hash_algs.c */
80
81extern struct fsverity_hash_alg fsverity_hash_algs[];
82
83struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
84 unsigned int num);
85struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg,
86 gfp_t gfp_flags);
87void fsverity_free_hash_request(struct fsverity_hash_alg *alg,
88 struct ahash_request *req);
89const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,
90 const u8 *salt, size_t salt_size);
91int fsverity_hash_page(const struct merkle_tree_params *params,
92 const struct inode *inode,
93 struct ahash_request *req, struct page *page, u8 *out);
94int fsverity_hash_buffer(struct fsverity_hash_alg *alg,
95 const void *data, size_t size, u8 *out);
96void __init fsverity_check_hash_algs(void);
97
98/* init.c */
99
100void __printf(3, 4) __cold
101fsverity_msg(const struct inode *inode, const char *level,
102 const char *fmt, ...);
103
104#define fsverity_warn(inode, fmt, ...) \
105 fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
106#define fsverity_err(inode, fmt, ...) \
107 fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
108
109/* open.c */
110
111int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
112 const struct inode *inode,
113 unsigned int hash_algorithm,
114 unsigned int log_blocksize,
115 const u8 *salt, size_t salt_size);
116
117struct fsverity_info *fsverity_create_info(const struct inode *inode,
118 struct fsverity_descriptor *desc);
119
120void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
121
122void fsverity_free_info(struct fsverity_info *vi);
123
124int fsverity_get_descriptor(struct inode *inode,
125 struct fsverity_descriptor **desc_ret);
126
127int __init fsverity_init_info_cache(void);
128void __init fsverity_exit_info_cache(void);
129
130/* signature.c */
131
132#ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
133int fsverity_verify_signature(const struct fsverity_info *vi,
134 const u8 *signature, size_t sig_size);
135
136int __init fsverity_init_signature(void);
137#else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
138static inline int
139fsverity_verify_signature(const struct fsverity_info *vi,
140 const u8 *signature, size_t sig_size)
141{
142 return 0;
143}
144
145static inline int fsverity_init_signature(void)
146{
147 return 0;
148}
149#endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
150
151/* verify.c */
152
153int __init fsverity_init_workqueue(void);
154void __init fsverity_exit_workqueue(void);
155
156#endif /* _FSVERITY_PRIVATE_H */