fsverity: replace fsverity_hash_page() with fsverity_hash_block()
[linux-2.6-block.git] / include / linux / fsverity.h
CommitLineData
fd2d1acf
EB
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * fs-verity: read-only file-based authenticity protection
4 *
5 * This header declares the interface between the fs/verity/ support layer and
6 * filesystems that support fs-verity.
7 *
8 * Copyright 2019 Google LLC
9 */
10
11#ifndef _LINUX_FSVERITY_H
12#define _LINUX_FSVERITY_H
13
14#include <linux/fs.h>
246d9216
MZ
15#include <crypto/hash_info.h>
16#include <crypto/sha2.h>
fd2d1acf
EB
17#include <uapi/linux/fsverity.h>
18
246d9216
MZ
19/*
20 * Largest digest size among all hash algorithms supported by fs-verity.
21 * Currently assumed to be <= size of fsverity_descriptor::root_hash.
22 */
23#define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
24
38622010
BB
25/* Arbitrary limit to bound the kmalloc() size. Can be changed. */
26#define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
27
fd2d1acf
EB
28/* Verity operations for filesystems */
29struct fsverity_operations {
30
3fda4c61
EB
31 /**
32 * Begin enabling verity on the given file.
33 *
34 * @filp: a readonly file descriptor for the file
35 *
36 * The filesystem must do any needed filesystem-specific preparations
37 * for enabling verity, e.g. evicting inline data. It also must return
38 * -EBUSY if verity is already being enabled on the given file.
39 *
40 * i_rwsem is held for write.
41 *
42 * Return: 0 on success, -errno on failure
43 */
44 int (*begin_enable_verity)(struct file *filp);
45
46 /**
47 * End enabling verity on the given file.
48 *
49 * @filp: a readonly file descriptor for the file
50 * @desc: the verity descriptor to write, or NULL on failure
51 * @desc_size: size of verity descriptor, or 0 on failure
52 * @merkle_tree_size: total bytes the Merkle tree took up
53 *
54 * If desc == NULL, then enabling verity failed and the filesystem only
55 * must do any necessary cleanups. Else, it must also store the given
56 * verity descriptor to a fs-specific location associated with the inode
57 * and do any fs-specific actions needed to mark the inode as a verity
58 * inode, e.g. setting a bit in the on-disk inode. The filesystem is
59 * also responsible for setting the S_VERITY flag in the VFS inode.
60 *
61 * i_rwsem is held for write, but it may have been dropped between
62 * ->begin_enable_verity() and ->end_enable_verity().
63 *
64 * Return: 0 on success, -errno on failure
65 */
66 int (*end_enable_verity)(struct file *filp, const void *desc,
67 size_t desc_size, u64 merkle_tree_size);
68
fd2d1acf
EB
69 /**
70 * Get the verity descriptor of the given inode.
71 *
72 * @inode: an inode with the S_VERITY flag set
73 * @buf: buffer in which to place the verity descriptor
74 * @bufsize: size of @buf, or 0 to retrieve the size only
75 *
76 * If bufsize == 0, then the size of the verity descriptor is returned.
77 * Otherwise the verity descriptor is written to 'buf' and its actual
78 * size is returned; -ERANGE is returned if it's too large. This may be
79 * called by multiple processes concurrently on the same inode.
80 *
81 * Return: the size on success, -errno on failure
82 */
83 int (*get_verity_descriptor)(struct inode *inode, void *buf,
84 size_t bufsize);
8a1d0f9c
EB
85
86 /**
87 * Read a Merkle tree page of the given inode.
88 *
89 * @inode: the inode
90 * @index: 0-based index of the page within the Merkle tree
fd39073d
EB
91 * @num_ra_pages: The number of Merkle tree pages that should be
92 * prefetched starting at @index if the page at @index
93 * isn't already cached. Implementations may ignore this
94 * argument; it's only a performance optimization.
8a1d0f9c
EB
95 *
96 * This can be called at any time on an open verity file, as well as
97 * between ->begin_enable_verity() and ->end_enable_verity(). It may be
98 * called by multiple processes concurrently, even with the same page.
99 *
100 * Note that this must retrieve a *page*, not necessarily a *block*.
101 *
102 * Return: the page on success, ERR_PTR() on failure
103 */
104 struct page *(*read_merkle_tree_page)(struct inode *inode,
fd39073d
EB
105 pgoff_t index,
106 unsigned long num_ra_pages);
3fda4c61
EB
107
108 /**
109 * Write a Merkle tree block to the given inode.
110 *
111 * @inode: the inode for which the Merkle tree is being built
72ea15f0
EB
112 * @buf: the Merkle tree block to write
113 * @pos: the position of the block in the Merkle tree (in bytes)
114 * @size: the Merkle tree block size (in bytes)
3fda4c61
EB
115 *
116 * This is only called between ->begin_enable_verity() and
117 * ->end_enable_verity().
118 *
119 * Return: 0 on success, -errno on failure
120 */
121 int (*write_merkle_tree_block)(struct inode *inode, const void *buf,
72ea15f0 122 u64 pos, unsigned int size);
fd2d1acf
EB
123};
124
125#ifdef CONFIG_FS_VERITY
126
127static inline struct fsverity_info *fsverity_get_info(const struct inode *inode)
128{
f3db0bed
EB
129 /*
130 * Pairs with the cmpxchg_release() in fsverity_set_info().
131 * I.e., another task may publish ->i_verity_info concurrently,
132 * executing a RELEASE barrier. We need to use smp_load_acquire() here
133 * to safely ACQUIRE the memory the other task published.
134 */
135 return smp_load_acquire(&inode->i_verity_info);
fd2d1acf
EB
136}
137
3fda4c61
EB
138/* enable.c */
139
9cd6b593 140int fsverity_ioctl_enable(struct file *filp, const void __user *arg);
3fda4c61 141
4dd893d8
EB
142/* measure.c */
143
9cd6b593 144int fsverity_ioctl_measure(struct file *filp, void __user *arg);
246d9216
MZ
145int fsverity_get_digest(struct inode *inode,
146 u8 digest[FS_VERITY_MAX_DIGEST_SIZE],
147 enum hash_algo *alg);
4dd893d8 148
fd2d1acf
EB
149/* open.c */
150
a6528a96 151int __fsverity_file_open(struct inode *inode, struct file *filp);
01d90c07 152int __fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr);
9642946c
EB
153void __fsverity_cleanup_inode(struct inode *inode);
154
155/**
156 * fsverity_cleanup_inode() - free the inode's verity info, if present
157 * @inode: an inode being evicted
158 *
159 * Filesystems must call this on inode eviction to free ->i_verity_info.
160 */
161static inline void fsverity_cleanup_inode(struct inode *inode)
162{
163 if (inode->i_verity_info)
164 __fsverity_cleanup_inode(inode);
165}
fd2d1acf 166
e17fe657
EB
167/* read_metadata.c */
168
169int fsverity_ioctl_read_metadata(struct file *filp, const void __user *uarg);
170
8a1d0f9c
EB
171/* verify.c */
172
9cd6b593
EB
173bool fsverity_verify_page(struct page *page);
174void fsverity_verify_bio(struct bio *bio);
175void fsverity_enqueue_verify_work(struct work_struct *work);
8a1d0f9c 176
fd2d1acf
EB
177#else /* !CONFIG_FS_VERITY */
178
179static inline struct fsverity_info *fsverity_get_info(const struct inode *inode)
180{
181 return NULL;
182}
183
3fda4c61
EB
184/* enable.c */
185
186static inline int fsverity_ioctl_enable(struct file *filp,
187 const void __user *arg)
188{
189 return -EOPNOTSUPP;
190}
191
4dd893d8
EB
192/* measure.c */
193
194static inline int fsverity_ioctl_measure(struct file *filp, void __user *arg)
195{
196 return -EOPNOTSUPP;
197}
198
246d9216
MZ
199static inline int fsverity_get_digest(struct inode *inode,
200 u8 digest[FS_VERITY_MAX_DIGEST_SIZE],
201 enum hash_algo *alg)
202{
203 return -EOPNOTSUPP;
204}
205
fd2d1acf
EB
206/* open.c */
207
a6528a96 208static inline int __fsverity_file_open(struct inode *inode, struct file *filp)
fd2d1acf 209{
a6528a96 210 return -EOPNOTSUPP;
fd2d1acf
EB
211}
212
01d90c07
EB
213static inline int __fsverity_prepare_setattr(struct dentry *dentry,
214 struct iattr *attr)
c1d9b584 215{
01d90c07 216 return -EOPNOTSUPP;
c1d9b584
EB
217}
218
fd2d1acf
EB
219static inline void fsverity_cleanup_inode(struct inode *inode)
220{
221}
222
e17fe657
EB
223/* read_metadata.c */
224
225static inline int fsverity_ioctl_read_metadata(struct file *filp,
226 const void __user *uarg)
227{
228 return -EOPNOTSUPP;
229}
230
8a1d0f9c
EB
231/* verify.c */
232
233static inline bool fsverity_verify_page(struct page *page)
234{
235 WARN_ON(1);
236 return false;
237}
238
239static inline void fsverity_verify_bio(struct bio *bio)
240{
241 WARN_ON(1);
242}
243
244static inline void fsverity_enqueue_verify_work(struct work_struct *work)
245{
246 WARN_ON(1);
247}
248
fd2d1acf
EB
249#endif /* !CONFIG_FS_VERITY */
250
8a1d0f9c
EB
251/**
252 * fsverity_active() - do reads from the inode need to go through fs-verity?
6377a38b 253 * @inode: inode to check
8a1d0f9c
EB
254 *
255 * This checks whether ->i_verity_info has been set.
256 *
704528d8 257 * Filesystems call this from ->readahead() to check whether the pages need to
8a1d0f9c
EB
258 * be verified or not. Don't use IS_VERITY() for this purpose; it's subject to
259 * a race condition where the file is being read concurrently with
260 * FS_IOC_ENABLE_VERITY completing. (S_VERITY is set before ->i_verity_info.)
6377a38b
EB
261 *
262 * Return: true if reads need to go through fs-verity, otherwise false
8a1d0f9c
EB
263 */
264static inline bool fsverity_active(const struct inode *inode)
265{
266 return fsverity_get_info(inode) != NULL;
267}
268
a6528a96
EB
269/**
270 * fsverity_file_open() - prepare to open a verity file
271 * @inode: the inode being opened
272 * @filp: the struct file being set up
273 *
274 * When opening a verity file, deny the open if it is for writing. Otherwise,
275 * set up the inode's ->i_verity_info if not already done.
276 *
277 * When combined with fscrypt, this must be called after fscrypt_file_open().
278 * Otherwise, we won't have the key set up to decrypt the verity metadata.
279 *
280 * Return: 0 on success, -errno on failure
281 */
282static inline int fsverity_file_open(struct inode *inode, struct file *filp)
283{
284 if (IS_VERITY(inode))
285 return __fsverity_file_open(inode, filp);
286 return 0;
287}
288
01d90c07
EB
289/**
290 * fsverity_prepare_setattr() - prepare to change a verity inode's attributes
291 * @dentry: dentry through which the inode is being changed
292 * @attr: attributes to change
293 *
294 * Verity files are immutable, so deny truncates. This isn't covered by the
295 * open-time check because sys_truncate() takes a path, not a file descriptor.
296 *
297 * Return: 0 on success, -errno on failure
298 */
299static inline int fsverity_prepare_setattr(struct dentry *dentry,
300 struct iattr *attr)
301{
302 if (IS_VERITY(d_inode(dentry)))
303 return __fsverity_prepare_setattr(dentry, attr);
304 return 0;
305}
306
fd2d1acf 307#endif /* _LINUX_FSVERITY_H */