Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 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 <uapi/linux/fsverity.h> |
| 16 | |
| 17 | /* Verity operations for filesystems */ |
| 18 | struct fsverity_operations { |
| 19 | |
| 20 | /** |
Eric Biggers | 3fda4c6 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 21 | * Begin enabling verity on the given file. |
| 22 | * |
| 23 | * @filp: a readonly file descriptor for the file |
| 24 | * |
| 25 | * The filesystem must do any needed filesystem-specific preparations |
| 26 | * for enabling verity, e.g. evicting inline data. It also must return |
| 27 | * -EBUSY if verity is already being enabled on the given file. |
| 28 | * |
| 29 | * i_rwsem is held for write. |
| 30 | * |
| 31 | * Return: 0 on success, -errno on failure |
| 32 | */ |
| 33 | int (*begin_enable_verity)(struct file *filp); |
| 34 | |
| 35 | /** |
| 36 | * End enabling verity on the given file. |
| 37 | * |
| 38 | * @filp: a readonly file descriptor for the file |
| 39 | * @desc: the verity descriptor to write, or NULL on failure |
| 40 | * @desc_size: size of verity descriptor, or 0 on failure |
| 41 | * @merkle_tree_size: total bytes the Merkle tree took up |
| 42 | * |
| 43 | * If desc == NULL, then enabling verity failed and the filesystem only |
| 44 | * must do any necessary cleanups. Else, it must also store the given |
| 45 | * verity descriptor to a fs-specific location associated with the inode |
| 46 | * and do any fs-specific actions needed to mark the inode as a verity |
| 47 | * inode, e.g. setting a bit in the on-disk inode. The filesystem is |
| 48 | * also responsible for setting the S_VERITY flag in the VFS inode. |
| 49 | * |
| 50 | * i_rwsem is held for write, but it may have been dropped between |
| 51 | * ->begin_enable_verity() and ->end_enable_verity(). |
| 52 | * |
| 53 | * Return: 0 on success, -errno on failure |
| 54 | */ |
| 55 | int (*end_enable_verity)(struct file *filp, const void *desc, |
| 56 | size_t desc_size, u64 merkle_tree_size); |
| 57 | |
| 58 | /** |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 59 | * Get the verity descriptor of the given inode. |
| 60 | * |
| 61 | * @inode: an inode with the S_VERITY flag set |
| 62 | * @buf: buffer in which to place the verity descriptor |
| 63 | * @bufsize: size of @buf, or 0 to retrieve the size only |
| 64 | * |
| 65 | * If bufsize == 0, then the size of the verity descriptor is returned. |
| 66 | * Otherwise the verity descriptor is written to 'buf' and its actual |
| 67 | * size is returned; -ERANGE is returned if it's too large. This may be |
| 68 | * called by multiple processes concurrently on the same inode. |
| 69 | * |
| 70 | * Return: the size on success, -errno on failure |
| 71 | */ |
| 72 | int (*get_verity_descriptor)(struct inode *inode, void *buf, |
| 73 | size_t bufsize); |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Read a Merkle tree page of the given inode. |
| 77 | * |
| 78 | * @inode: the inode |
| 79 | * @index: 0-based index of the page within the Merkle tree |
Eric Biggers | fd39073 | 2020-01-06 12:55:33 -0800 | [diff] [blame] | 80 | * @num_ra_pages: The number of Merkle tree pages that should be |
| 81 | * prefetched starting at @index if the page at @index |
| 82 | * isn't already cached. Implementations may ignore this |
| 83 | * argument; it's only a performance optimization. |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 84 | * |
| 85 | * This can be called at any time on an open verity file, as well as |
| 86 | * between ->begin_enable_verity() and ->end_enable_verity(). It may be |
| 87 | * called by multiple processes concurrently, even with the same page. |
| 88 | * |
| 89 | * Note that this must retrieve a *page*, not necessarily a *block*. |
| 90 | * |
| 91 | * Return: the page on success, ERR_PTR() on failure |
| 92 | */ |
| 93 | struct page *(*read_merkle_tree_page)(struct inode *inode, |
Eric Biggers | fd39073 | 2020-01-06 12:55:33 -0800 | [diff] [blame] | 94 | pgoff_t index, |
| 95 | unsigned long num_ra_pages); |
Eric Biggers | 3fda4c6 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Write a Merkle tree block to the given inode. |
| 99 | * |
| 100 | * @inode: the inode for which the Merkle tree is being built |
| 101 | * @buf: block to write |
| 102 | * @index: 0-based index of the block within the Merkle tree |
| 103 | * @log_blocksize: log base 2 of the Merkle tree block size |
| 104 | * |
| 105 | * This is only called between ->begin_enable_verity() and |
| 106 | * ->end_enable_verity(). |
| 107 | * |
| 108 | * Return: 0 on success, -errno on failure |
| 109 | */ |
| 110 | int (*write_merkle_tree_block)(struct inode *inode, const void *buf, |
| 111 | u64 index, int log_blocksize); |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | #ifdef CONFIG_FS_VERITY |
| 115 | |
| 116 | static inline struct fsverity_info *fsverity_get_info(const struct inode *inode) |
| 117 | { |
Eric Biggers | f3db0be | 2020-07-21 15:59:20 -0700 | [diff] [blame] | 118 | /* |
| 119 | * Pairs with the cmpxchg_release() in fsverity_set_info(). |
| 120 | * I.e., another task may publish ->i_verity_info concurrently, |
| 121 | * executing a RELEASE barrier. We need to use smp_load_acquire() here |
| 122 | * to safely ACQUIRE the memory the other task published. |
| 123 | */ |
| 124 | return smp_load_acquire(&inode->i_verity_info); |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Eric Biggers | 3fda4c6 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 127 | /* enable.c */ |
| 128 | |
Eric Biggers | 9cd6b59 | 2020-05-11 12:21:18 -0700 | [diff] [blame] | 129 | int fsverity_ioctl_enable(struct file *filp, const void __user *arg); |
Eric Biggers | 3fda4c6 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 130 | |
Eric Biggers | 4dd893d | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 131 | /* measure.c */ |
| 132 | |
Eric Biggers | 9cd6b59 | 2020-05-11 12:21:18 -0700 | [diff] [blame] | 133 | int fsverity_ioctl_measure(struct file *filp, void __user *arg); |
Eric Biggers | 4dd893d | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 134 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 135 | /* open.c */ |
| 136 | |
Eric Biggers | 9cd6b59 | 2020-05-11 12:21:18 -0700 | [diff] [blame] | 137 | int fsverity_file_open(struct inode *inode, struct file *filp); |
| 138 | int fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr); |
| 139 | void fsverity_cleanup_inode(struct inode *inode); |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 140 | |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 141 | /* verify.c */ |
| 142 | |
Eric Biggers | 9cd6b59 | 2020-05-11 12:21:18 -0700 | [diff] [blame] | 143 | bool fsverity_verify_page(struct page *page); |
| 144 | void fsverity_verify_bio(struct bio *bio); |
| 145 | void fsverity_enqueue_verify_work(struct work_struct *work); |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 146 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 147 | #else /* !CONFIG_FS_VERITY */ |
| 148 | |
| 149 | static inline struct fsverity_info *fsverity_get_info(const struct inode *inode) |
| 150 | { |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Eric Biggers | 3fda4c6 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 154 | /* enable.c */ |
| 155 | |
| 156 | static inline int fsverity_ioctl_enable(struct file *filp, |
| 157 | const void __user *arg) |
| 158 | { |
| 159 | return -EOPNOTSUPP; |
| 160 | } |
| 161 | |
Eric Biggers | 4dd893d | 2019-07-22 09:26:23 -0700 | [diff] [blame] | 162 | /* measure.c */ |
| 163 | |
| 164 | static inline int fsverity_ioctl_measure(struct file *filp, void __user *arg) |
| 165 | { |
| 166 | return -EOPNOTSUPP; |
| 167 | } |
| 168 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 169 | /* open.c */ |
| 170 | |
| 171 | static inline int fsverity_file_open(struct inode *inode, struct file *filp) |
| 172 | { |
| 173 | return IS_VERITY(inode) ? -EOPNOTSUPP : 0; |
| 174 | } |
| 175 | |
Eric Biggers | c1d9b58 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 176 | static inline int fsverity_prepare_setattr(struct dentry *dentry, |
| 177 | struct iattr *attr) |
| 178 | { |
| 179 | return IS_VERITY(d_inode(dentry)) ? -EOPNOTSUPP : 0; |
| 180 | } |
| 181 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 182 | static inline void fsverity_cleanup_inode(struct inode *inode) |
| 183 | { |
| 184 | } |
| 185 | |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 186 | /* verify.c */ |
| 187 | |
| 188 | static inline bool fsverity_verify_page(struct page *page) |
| 189 | { |
| 190 | WARN_ON(1); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | static inline void fsverity_verify_bio(struct bio *bio) |
| 195 | { |
| 196 | WARN_ON(1); |
| 197 | } |
| 198 | |
| 199 | static inline void fsverity_enqueue_verify_work(struct work_struct *work) |
| 200 | { |
| 201 | WARN_ON(1); |
| 202 | } |
| 203 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 204 | #endif /* !CONFIG_FS_VERITY */ |
| 205 | |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 206 | /** |
| 207 | * fsverity_active() - do reads from the inode need to go through fs-verity? |
Eric Biggers | 6377a38 | 2020-05-11 12:21:17 -0700 | [diff] [blame] | 208 | * @inode: inode to check |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 209 | * |
| 210 | * This checks whether ->i_verity_info has been set. |
| 211 | * |
| 212 | * Filesystems call this from ->readpages() to check whether the pages need to |
| 213 | * be verified or not. Don't use IS_VERITY() for this purpose; it's subject to |
| 214 | * a race condition where the file is being read concurrently with |
| 215 | * FS_IOC_ENABLE_VERITY completing. (S_VERITY is set before ->i_verity_info.) |
Eric Biggers | 6377a38 | 2020-05-11 12:21:17 -0700 | [diff] [blame] | 216 | * |
| 217 | * Return: true if reads need to go through fs-verity, otherwise false |
Eric Biggers | 8a1d0f9 | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 218 | */ |
| 219 | static inline bool fsverity_active(const struct inode *inode) |
| 220 | { |
| 221 | return fsverity_get_info(inode) != NULL; |
| 222 | } |
| 223 | |
Eric Biggers | fd2d1ac | 2019-07-22 09:26:22 -0700 | [diff] [blame] | 224 | #endif /* _LINUX_FSVERITY_H */ |