blob: 74768cf539daf5f1d2c4ccf9157fa931cfea4b22 [file] [log] [blame]
Eric Biggers671e67b2019-07-22 09:26:21 -07001/* 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 <crypto/sha.h>
Eric Biggersfd2d1ac2019-07-22 09:26:22 -070018#include <linux/fsverity.h>
Eric Biggers439bea12019-12-31 11:55:45 -060019#include <linux/mempool.h>
Eric Biggers671e67b2019-07-22 09:26:21 -070020
21struct ahash_request;
22
23/*
24 * Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty;
25 * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
26 */
27#define FS_VERITY_MAX_LEVELS 8
28
29/*
30 * Largest digest size among all hash algorithms supported by fs-verity.
31 * Currently assumed to be <= size of fsverity_descriptor::root_hash.
32 */
Eric Biggersadd890c2019-07-22 09:26:23 -070033#define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
Eric Biggers671e67b2019-07-22 09:26:21 -070034
35/* A hash algorithm supported by fs-verity */
36struct fsverity_hash_alg {
37 struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
38 const char *name; /* crypto API name, e.g. sha256 */
39 unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
40 unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
Eric Biggers439bea12019-12-31 11:55:45 -060041 mempool_t req_pool; /* mempool with a preallocated hash request */
Eric Biggers671e67b2019-07-22 09:26:21 -070042};
43
44/* Merkle tree parameters: hash algorithm, initial hash state, and topology */
45struct merkle_tree_params {
Eric Biggers439bea12019-12-31 11:55:45 -060046 struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
Eric Biggers671e67b2019-07-22 09:26:21 -070047 const u8 *hashstate; /* initial hash state or NULL */
48 unsigned int digest_size; /* same as hash_alg->digest_size */
49 unsigned int block_size; /* size of data and tree blocks */
50 unsigned int hashes_per_block; /* number of hashes per tree block */
51 unsigned int log_blocksize; /* log2(block_size) */
52 unsigned int log_arity; /* log2(hashes_per_block) */
53 unsigned int num_levels; /* number of levels in Merkle tree */
54 u64 tree_size; /* Merkle tree size in bytes */
Eric Biggersfd390732020-01-06 12:55:33 -080055 unsigned long level0_blocks; /* number of blocks in tree level 0 */
Eric Biggers671e67b2019-07-22 09:26:21 -070056
57 /*
58 * Starting block index for each tree level, ordered from leaf level (0)
59 * to root level ('num_levels - 1')
60 */
61 u64 level_start[FS_VERITY_MAX_LEVELS];
62};
63
Eric Biggersfd2d1ac2019-07-22 09:26:22 -070064/**
65 * fsverity_info - cached verity metadata for an inode
66 *
67 * When a verity file is first opened, an instance of this struct is allocated
68 * and stored in ->i_verity_info; it remains until the inode is evicted. It
69 * caches information about the Merkle tree that's needed to efficiently verify
70 * data read from the file. It also caches the file measurement. The Merkle
71 * tree pages themselves are not cached here, but the filesystem may cache them.
72 */
73struct fsverity_info {
74 struct merkle_tree_params tree_params;
75 u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
76 u8 measurement[FS_VERITY_MAX_DIGEST_SIZE];
77 const struct inode *inode;
78};
79
80/*
Eric Biggers432434c2019-07-22 09:26:23 -070081 * Merkle tree properties. The file measurement is the hash of this structure
82 * excluding the signature and with the sig_size field set to 0.
Eric Biggersfd2d1ac2019-07-22 09:26:22 -070083 */
84struct fsverity_descriptor {
85 __u8 version; /* must be 1 */
86 __u8 hash_algorithm; /* Merkle tree hash algorithm */
87 __u8 log_blocksize; /* log2 of size of data and tree blocks */
88 __u8 salt_size; /* size of salt in bytes; 0 if none */
Eric Biggers432434c2019-07-22 09:26:23 -070089 __le32 sig_size; /* size of signature in bytes; 0 if none */
Eric Biggersfd2d1ac2019-07-22 09:26:22 -070090 __le64 data_size; /* size of file the Merkle tree is built over */
91 __u8 root_hash[64]; /* Merkle tree root hash */
92 __u8 salt[32]; /* salt prepended to each hashed block */
93 __u8 __reserved[144]; /* must be 0's */
Eric Biggers432434c2019-07-22 09:26:23 -070094 __u8 signature[]; /* optional PKCS#7 signature */
Eric Biggersfd2d1ac2019-07-22 09:26:22 -070095};
96
97/* Arbitrary limit to bound the kmalloc() size. Can be changed. */
98#define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
99
Eric Biggers432434c2019-07-22 09:26:23 -0700100#define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
101 sizeof(struct fsverity_descriptor))
102
103/*
104 * Format in which verity file measurements are signed. This is the same as
105 * 'struct fsverity_digest', except here some magic bytes are prepended to
106 * provide some context about what is being signed in case the same key is used
107 * for non-fsverity purposes, and here the fields have fixed endianness.
108 */
109struct fsverity_signed_digest {
110 char magic[8]; /* must be "FSVerity" */
111 __le16 digest_algorithm;
112 __le16 digest_size;
113 __u8 digest[];
114};
115
Eric Biggers671e67b2019-07-22 09:26:21 -0700116/* hash_algs.c */
117
118extern struct fsverity_hash_alg fsverity_hash_algs[];
119
Eric Biggers439bea12019-12-31 11:55:45 -0600120struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
121 unsigned int num);
122struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg,
123 gfp_t gfp_flags);
124void fsverity_free_hash_request(struct fsverity_hash_alg *alg,
125 struct ahash_request *req);
126const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,
Eric Biggers671e67b2019-07-22 09:26:21 -0700127 const u8 *salt, size_t salt_size);
128int fsverity_hash_page(const struct merkle_tree_params *params,
129 const struct inode *inode,
130 struct ahash_request *req, struct page *page, u8 *out);
Eric Biggers439bea12019-12-31 11:55:45 -0600131int fsverity_hash_buffer(struct fsverity_hash_alg *alg,
Eric Biggers671e67b2019-07-22 09:26:21 -0700132 const void *data, size_t size, u8 *out);
133void __init fsverity_check_hash_algs(void);
134
135/* init.c */
136
137extern void __printf(3, 4) __cold
138fsverity_msg(const struct inode *inode, const char *level,
139 const char *fmt, ...);
140
141#define fsverity_warn(inode, fmt, ...) \
142 fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
143#define fsverity_err(inode, fmt, ...) \
144 fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
145
Eric Biggersfd2d1ac2019-07-22 09:26:22 -0700146/* open.c */
147
148int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
149 const struct inode *inode,
150 unsigned int hash_algorithm,
151 unsigned int log_blocksize,
152 const u8 *salt, size_t salt_size);
153
154struct fsverity_info *fsverity_create_info(const struct inode *inode,
Eric Biggers432434c2019-07-22 09:26:23 -0700155 void *desc, size_t desc_size);
Eric Biggersfd2d1ac2019-07-22 09:26:22 -0700156
157void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
158
159void fsverity_free_info(struct fsverity_info *vi);
160
161int __init fsverity_init_info_cache(void);
Eric Biggers8a1d0f92019-07-22 09:26:22 -0700162void __init fsverity_exit_info_cache(void);
163
Eric Biggers432434c2019-07-22 09:26:23 -0700164/* signature.c */
165
166#ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
167int fsverity_verify_signature(const struct fsverity_info *vi,
168 const struct fsverity_descriptor *desc,
169 size_t desc_size);
170
171int __init fsverity_init_signature(void);
172#else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
173static inline int
174fsverity_verify_signature(const struct fsverity_info *vi,
175 const struct fsverity_descriptor *desc,
176 size_t desc_size)
177{
178 return 0;
179}
180
181static inline int fsverity_init_signature(void)
182{
183 return 0;
184}
185#endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
186
Eric Biggers8a1d0f92019-07-22 09:26:22 -0700187/* verify.c */
188
189int __init fsverity_init_workqueue(void);
Eric Biggers432434c2019-07-22 09:26:23 -0700190void __init fsverity_exit_workqueue(void);
Eric Biggersfd2d1ac2019-07-22 09:26:22 -0700191
Eric Biggers671e67b2019-07-22 09:26:21 -0700192#endif /* _FSVERITY_PRIVATE_H */