xref: /kernel/linux/linux-5.10/fs/f2fs/verity.c (revision 8c2ecf20)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/f2fs/verity.c: fs-verity support for f2fs
4 *
5 * Copyright 2019 Google LLC
6 */
7
8/*
9 * Implementation of fsverity_operations for f2fs.
10 *
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.
21 *
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.
25 */
26
27#include <linux/f2fs_fs.h>
28
29#include "f2fs.h"
30#include "xattr.h"
31
32#define F2FS_VERIFY_VER	(1)
33
34static inline loff_t f2fs_verity_metadata_pos(const struct inode *inode)
35{
36	return round_up(inode->i_size, 65536);
37}
38
39/*
40 * Read some verity metadata from the inode.  __vfs_read() can't be used because
41 * we need to read beyond i_size.
42 */
43static int pagecache_read(struct inode *inode, void *buf, size_t count,
44			  loff_t pos)
45{
46	while (count) {
47		size_t n = min_t(size_t, count,
48				 PAGE_SIZE - offset_in_page(pos));
49		struct page *page;
50
51		page = read_mapping_page(inode->i_mapping, pos >> PAGE_SHIFT,
52					 NULL);
53		if (IS_ERR(page))
54			return PTR_ERR(page);
55
56		memcpy_from_page(buf, page, offset_in_page(pos), n);
57
58		put_page(page);
59
60		buf += n;
61		pos += n;
62		count -= n;
63	}
64	return 0;
65}
66
67/*
68 * Write some verity metadata to the inode for FS_IOC_ENABLE_VERITY.
69 * kernel_write() can't be used because the file descriptor is readonly.
70 */
71static int pagecache_write(struct inode *inode, const void *buf, size_t count,
72			   loff_t pos)
73{
74	if (pos + count > inode->i_sb->s_maxbytes)
75		return -EFBIG;
76
77	while (count) {
78		size_t n = min_t(size_t, count,
79				 PAGE_SIZE - offset_in_page(pos));
80		struct page *page;
81		void *fsdata = NULL;
82		int res;
83
84		res = pagecache_write_begin(NULL, inode->i_mapping, pos, n, 0,
85					    &page, &fsdata);
86		if (res)
87			return res;
88
89		memcpy_to_page(page, offset_in_page(pos), buf, n);
90
91		res = pagecache_write_end(NULL, inode->i_mapping, pos, n, n,
92					  page, fsdata);
93		if (res < 0)
94			return res;
95		if (res != n)
96			return -EIO;
97
98		buf += n;
99		pos += n;
100		count -= n;
101	}
102	return 0;
103}
104
105/*
106 * Format of f2fs verity xattr.  This points to the location of the verity
107 * descriptor within the file data rather than containing it directly because
108 * the verity descriptor *must* be encrypted when f2fs encryption is used.  But,
109 * f2fs encryption does not encrypt xattrs.
110 */
111struct fsverity_descriptor_location {
112	__le32 version;
113	__le32 size;
114	__le64 pos;
115};
116
117static int f2fs_begin_enable_verity(struct file *filp)
118{
119	struct inode *inode = file_inode(filp);
120	int err;
121
122	if (f2fs_verity_in_progress(inode))
123		return -EBUSY;
124
125	if (f2fs_is_atomic_file(inode) || f2fs_is_volatile_file(inode))
126		return -EOPNOTSUPP;
127
128	/*
129	 * Since the file was opened readonly, we have to initialize the quotas
130	 * here and not rely on ->open() doing it.  This must be done before
131	 * evicting the inline data.
132	 */
133	err = dquot_initialize(inode);
134	if (err)
135		return err;
136
137	err = f2fs_convert_inline_inode(inode);
138	if (err)
139		return err;
140
141	set_inode_flag(inode, FI_VERITY_IN_PROGRESS);
142	return 0;
143}
144
145static int f2fs_end_enable_verity(struct file *filp, const void *desc,
146				  size_t desc_size, u64 merkle_tree_size)
147{
148	struct inode *inode = file_inode(filp);
149	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
150	u64 desc_pos = f2fs_verity_metadata_pos(inode) + merkle_tree_size;
151	struct fsverity_descriptor_location dloc = {
152		.version = cpu_to_le32(F2FS_VERIFY_VER),
153		.size = cpu_to_le32(desc_size),
154		.pos = cpu_to_le64(desc_pos),
155	};
156	int err = 0, err2 = 0;
157
158	/*
159	 * If an error already occurred (which fs/verity/ signals by passing
160	 * desc == NULL), then only clean-up is needed.
161	 */
162	if (desc == NULL)
163		goto cleanup;
164
165	/* Append the verity descriptor. */
166	err = pagecache_write(inode, desc, desc_size, desc_pos);
167	if (err)
168		goto cleanup;
169
170	/*
171	 * Write all pages (both data and verity metadata).  Note that this must
172	 * happen before clearing FI_VERITY_IN_PROGRESS; otherwise pages beyond
173	 * i_size won't be written properly.  For crash consistency, this also
174	 * must happen before the verity inode flag gets persisted.
175	 */
176	err = filemap_write_and_wait(inode->i_mapping);
177	if (err)
178		goto cleanup;
179
180	/* Set the verity xattr. */
181	err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_VERITY,
182			    F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc),
183			    NULL, XATTR_CREATE);
184	if (err)
185		goto cleanup;
186
187	/* Finally, set the verity inode flag. */
188	file_set_verity(inode);
189	f2fs_set_inode_flags(inode);
190	f2fs_mark_inode_dirty_sync(inode, true);
191
192	clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
193	return 0;
194
195cleanup:
196	/*
197	 * Verity failed to be enabled, so clean up by truncating any verity
198	 * metadata that was written beyond i_size (both from cache and from
199	 * disk) and clearing FI_VERITY_IN_PROGRESS.
200	 *
201	 * Taking i_gc_rwsem[WRITE] is needed to stop f2fs garbage collection
202	 * from re-instantiating cached pages we are truncating (since unlike
203	 * normal file accesses, garbage collection isn't limited by i_size).
204	 */
205	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
206	truncate_inode_pages(inode->i_mapping, inode->i_size);
207	err2 = f2fs_truncate(inode);
208	if (err2) {
209		f2fs_err(sbi, "Truncating verity metadata failed (errno=%d)",
210			 err2);
211		set_sbi_flag(sbi, SBI_NEED_FSCK);
212	}
213	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
214	clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
215	return err ?: err2;
216}
217
218static int f2fs_get_verity_descriptor(struct inode *inode, void *buf,
219				      size_t buf_size)
220{
221	struct fsverity_descriptor_location dloc;
222	int res;
223	u32 size;
224	u64 pos;
225
226	/* Get the descriptor location */
227	res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_VERITY,
228			    F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc), NULL);
229	if (res < 0 && res != -ERANGE)
230		return res;
231	if (res != sizeof(dloc) || dloc.version != cpu_to_le32(F2FS_VERIFY_VER)) {
232		f2fs_warn(F2FS_I_SB(inode), "unknown verity xattr format");
233		return -EINVAL;
234	}
235	size = le32_to_cpu(dloc.size);
236	pos = le64_to_cpu(dloc.pos);
237
238	/* Get the descriptor */
239	if (pos + size < pos || pos + size > inode->i_sb->s_maxbytes ||
240	    pos < f2fs_verity_metadata_pos(inode) || size > INT_MAX) {
241		f2fs_warn(F2FS_I_SB(inode), "invalid verity xattr");
242		return -EFSCORRUPTED;
243	}
244	if (buf_size) {
245		if (size > buf_size)
246			return -ERANGE;
247		res = pagecache_read(inode, buf, size, pos);
248		if (res)
249			return res;
250	}
251	return size;
252}
253
254static struct page *f2fs_read_merkle_tree_page(struct inode *inode,
255					       pgoff_t index,
256					       unsigned long num_ra_pages)
257{
258	struct page *page;
259
260	index += f2fs_verity_metadata_pos(inode) >> PAGE_SHIFT;
261
262	page = find_get_page_flags(inode->i_mapping, index, FGP_ACCESSED);
263	if (!page || !PageUptodate(page)) {
264		DEFINE_READAHEAD(ractl, NULL, inode->i_mapping, index);
265
266		if (page)
267			put_page(page);
268		else if (num_ra_pages > 1)
269			page_cache_ra_unbounded(&ractl, num_ra_pages, 0);
270		page = read_mapping_page(inode->i_mapping, index, NULL);
271	}
272	return page;
273}
274
275static int f2fs_write_merkle_tree_block(struct inode *inode, const void *buf,
276					u64 index, int log_blocksize)
277{
278	loff_t pos = f2fs_verity_metadata_pos(inode) + (index << log_blocksize);
279
280	return pagecache_write(inode, buf, 1 << log_blocksize, pos);
281}
282
283const struct fsverity_operations f2fs_verityops = {
284	.begin_enable_verity	= f2fs_begin_enable_verity,
285	.end_enable_verity	= f2fs_end_enable_verity,
286	.get_verity_descriptor	= f2fs_get_verity_descriptor,
287	.read_merkle_tree_page	= f2fs_read_merkle_tree_page,
288	.write_merkle_tree_block = f2fs_write_merkle_tree_block,
289};
290