1 // SPDX-License-Identifier: GPL-2.0
3 * fs/f2fs/verity.c: fs-verity support for f2fs
5 * Copyright 2019 Google LLC
9 * Implementation of fsverity_operations for f2fs.
11 * Like ext4, f2fs stores the verity metadata (Merkle tree and
12 * fsverity_descriptor) past the end of the file, starting at the first 64K
13 * boundary beyond i_size. This approach works because (a) verity files are
14 * readonly, and (b) pages fully beyond i_size aren't visible to userspace but
15 * can be read/written internally by f2fs with only some relatively small
16 * changes to f2fs. Extended attributes cannot be used because (a) f2fs limits
17 * the total size of an inode's xattr entries to 4096 bytes, which wouldn't be
18 * enough for even a single Merkle tree block, and (b) f2fs encryption doesn't
19 * encrypt xattrs, yet the verity metadata *must* be encrypted when the file is
20 * because it contains hashes of the plaintext data.
22 * Using a 64K boundary rather than a 4K one keeps things ready for
23 * architectures with 64K pages, and it doesn't necessarily waste space on-disk
24 * since there can be a hole between i_size and the start of the Merkle tree.
27 #include <linux/f2fs_fs.h>
32 static inline loff_t f2fs_verity_metadata_pos(const struct inode *inode)
34 return round_up(inode->i_size, 65536);
38 * Read some verity metadata from the inode. __vfs_read() can't be used because
39 * we need to read beyond i_size.
41 static int pagecache_read(struct inode *inode, void *buf, size_t count,
45 size_t n = min_t(size_t, count,
46 PAGE_SIZE - offset_in_page(pos));
49 page = read_mapping_page(inode->i_mapping, pos >> PAGE_SHIFT,
54 memcpy_from_page(buf, page, offset_in_page(pos), n);
66 * Write some verity metadata to the inode for FS_IOC_ENABLE_VERITY.
67 * kernel_write() can't be used because the file descriptor is readonly.
69 static int pagecache_write(struct inode *inode, const void *buf, size_t count,
72 if (pos + count > inode->i_sb->s_maxbytes)
76 size_t n = min_t(size_t, count,
77 PAGE_SIZE - offset_in_page(pos));
82 res = pagecache_write_begin(NULL, inode->i_mapping, pos, n, 0,
87 memcpy_to_page(page, offset_in_page(pos), buf, n);
89 res = pagecache_write_end(NULL, inode->i_mapping, pos, n, n,
104 * Format of f2fs verity xattr. This points to the location of the verity
105 * descriptor within the file data rather than containing it directly because
106 * the verity descriptor *must* be encrypted when f2fs encryption is used. But,
107 * f2fs encryption does not encrypt xattrs.
109 struct fsverity_descriptor_location {
115 static int f2fs_begin_enable_verity(struct file *filp)
117 struct inode *inode = file_inode(filp);
120 if (f2fs_verity_in_progress(inode))
123 if (f2fs_is_atomic_file(inode) || f2fs_is_volatile_file(inode))
127 * Since the file was opened readonly, we have to initialize the quotas
128 * here and not rely on ->open() doing it. This must be done before
129 * evicting the inline data.
131 err = dquot_initialize(inode);
135 err = f2fs_convert_inline_inode(inode);
139 set_inode_flag(inode, FI_VERITY_IN_PROGRESS);
143 static int f2fs_end_enable_verity(struct file *filp, const void *desc,
144 size_t desc_size, u64 merkle_tree_size)
146 struct inode *inode = file_inode(filp);
147 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
148 u64 desc_pos = f2fs_verity_metadata_pos(inode) + merkle_tree_size;
149 struct fsverity_descriptor_location dloc = {
150 .version = cpu_to_le32(1),
151 .size = cpu_to_le32(desc_size),
152 .pos = cpu_to_le64(desc_pos),
154 int err = 0, err2 = 0;
157 * If an error already occurred (which fs/verity/ signals by passing
158 * desc == NULL), then only clean-up is needed.
163 /* Append the verity descriptor. */
164 err = pagecache_write(inode, desc, desc_size, desc_pos);
169 * Write all pages (both data and verity metadata). Note that this must
170 * happen before clearing FI_VERITY_IN_PROGRESS; otherwise pages beyond
171 * i_size won't be written properly. For crash consistency, this also
172 * must happen before the verity inode flag gets persisted.
174 err = filemap_write_and_wait(inode->i_mapping);
178 /* Set the verity xattr. */
179 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_VERITY,
180 F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc),
185 /* Finally, set the verity inode flag. */
186 file_set_verity(inode);
187 f2fs_set_inode_flags(inode);
188 f2fs_mark_inode_dirty_sync(inode, true);
190 clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
195 * Verity failed to be enabled, so clean up by truncating any verity
196 * metadata that was written beyond i_size (both from cache and from
197 * disk) and clearing FI_VERITY_IN_PROGRESS.
199 * Taking i_gc_rwsem[WRITE] is needed to stop f2fs garbage collection
200 * from re-instantiating cached pages we are truncating (since unlike
201 * normal file accesses, garbage collection isn't limited by i_size).
203 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
204 truncate_inode_pages(inode->i_mapping, inode->i_size);
205 err2 = f2fs_truncate(inode);
207 f2fs_err(sbi, "Truncating verity metadata failed (errno=%d)",
209 set_sbi_flag(sbi, SBI_NEED_FSCK);
211 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
212 clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
216 static int f2fs_get_verity_descriptor(struct inode *inode, void *buf,
219 struct fsverity_descriptor_location dloc;
224 /* Get the descriptor location */
225 res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_VERITY,
226 F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc), NULL);
227 if (res < 0 && res != -ERANGE)
229 if (res != sizeof(dloc) || dloc.version != cpu_to_le32(1)) {
230 f2fs_warn(F2FS_I_SB(inode), "unknown verity xattr format");
233 size = le32_to_cpu(dloc.size);
234 pos = le64_to_cpu(dloc.pos);
236 /* Get the descriptor */
237 if (pos + size < pos || pos + size > inode->i_sb->s_maxbytes ||
238 pos < f2fs_verity_metadata_pos(inode) || size > INT_MAX) {
239 f2fs_warn(F2FS_I_SB(inode), "invalid verity xattr");
240 return -EFSCORRUPTED;
245 res = pagecache_read(inode, buf, size, pos);
252 static struct page *f2fs_read_merkle_tree_page(struct inode *inode,
255 index += f2fs_verity_metadata_pos(inode) >> PAGE_SHIFT;
257 return read_mapping_page(inode->i_mapping, index, NULL);
260 static int f2fs_write_merkle_tree_block(struct inode *inode, const void *buf,
261 u64 index, int log_blocksize)
263 loff_t pos = f2fs_verity_metadata_pos(inode) + (index << log_blocksize);
265 return pagecache_write(inode, buf, 1 << log_blocksize, pos);
268 const struct fsverity_operations f2fs_verityops = {
269 .begin_enable_verity = f2fs_begin_enable_verity,
270 .end_enable_verity = f2fs_end_enable_verity,
271 .get_verity_descriptor = f2fs_get_verity_descriptor,
272 .read_merkle_tree_page = f2fs_read_merkle_tree_page,
273 .write_merkle_tree_block = f2fs_write_merkle_tree_block,