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