verity.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. * Implementation of fsverity_operations for f2fs.
  9. *
  10. * Like ext4, f2fs stores the verity metadata (Merkle tree and
  11. * fsverity_descriptor) past the end of the file, starting at the first 64K
  12. * boundary beyond i_size. This approach works because (a) verity files are
  13. * readonly, and (b) pages fully beyond i_size aren't visible to userspace but
  14. * can be read/written internally by f2fs with only some relatively small
  15. * changes to f2fs. Extended attributes cannot be used because (a) f2fs limits
  16. * the total size of an inode's xattr entries to 4096 bytes, which wouldn't be
  17. * enough for even a single Merkle tree block, and (b) f2fs encryption doesn't
  18. * encrypt xattrs, yet the verity metadata *must* be encrypted when the file is
  19. * because it contains hashes of the plaintext data.
  20. *
  21. * Using a 64K boundary rather than a 4K one keeps things ready for
  22. * architectures with 64K pages, and it doesn't necessarily waste space on-disk
  23. * since there can be a hole between i_size and the start of the Merkle tree.
  24. */
  25. #include <linux/f2fs_fs.h>
  26. #include "f2fs.h"
  27. #include "xattr.h"
  28. #define F2FS_VERIFY_VER (1)
  29. static inline loff_t f2fs_verity_metadata_pos(const struct inode *inode)
  30. {
  31. return round_up(inode->i_size, 65536);
  32. }
  33. /*
  34. * Read some verity metadata from the inode. __vfs_read() can't be used because
  35. * we need to read beyond i_size.
  36. */
  37. static int pagecache_read(struct inode *inode, void *buf, size_t count,
  38. loff_t pos)
  39. {
  40. while (count) {
  41. size_t n = min_t(size_t, count,
  42. PAGE_SIZE - offset_in_page(pos));
  43. struct page *page;
  44. page = read_mapping_page(inode->i_mapping, pos >> PAGE_SHIFT,
  45. NULL);
  46. if (IS_ERR(page))
  47. return PTR_ERR(page);
  48. memcpy_from_page(buf, page, offset_in_page(pos), n);
  49. put_page(page);
  50. buf += n;
  51. pos += n;
  52. count -= n;
  53. }
  54. return 0;
  55. }
  56. /*
  57. * Write some verity metadata to the inode for FS_IOC_ENABLE_VERITY.
  58. * kernel_write() can't be used because the file descriptor is readonly.
  59. */
  60. static int pagecache_write(struct inode *inode, const void *buf, size_t count,
  61. loff_t pos)
  62. {
  63. struct address_space *mapping = inode->i_mapping;
  64. const struct address_space_operations *aops = mapping->a_ops;
  65. if (pos + count > inode->i_sb->s_maxbytes)
  66. return -EFBIG;
  67. while (count) {
  68. size_t n = min_t(size_t, count,
  69. PAGE_SIZE - offset_in_page(pos));
  70. struct page *page;
  71. void *fsdata = NULL;
  72. int res;
  73. res = aops->write_begin(NULL, mapping, pos, n, &page, &fsdata);
  74. if (res)
  75. return res;
  76. memcpy_to_page(page, offset_in_page(pos), buf, n);
  77. res = aops->write_end(NULL, mapping, pos, n, n, page, fsdata);
  78. if (res < 0)
  79. return res;
  80. if (res != n)
  81. return -EIO;
  82. buf += n;
  83. pos += n;
  84. count -= n;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * Format of f2fs verity xattr. This points to the location of the verity
  90. * descriptor within the file data rather than containing it directly because
  91. * the verity descriptor *must* be encrypted when f2fs encryption is used. But,
  92. * f2fs encryption does not encrypt xattrs.
  93. */
  94. struct fsverity_descriptor_location {
  95. __le32 version;
  96. __le32 size;
  97. __le64 pos;
  98. };
  99. static int f2fs_begin_enable_verity(struct file *filp)
  100. {
  101. struct inode *inode = file_inode(filp);
  102. int err;
  103. if (f2fs_verity_in_progress(inode))
  104. return -EBUSY;
  105. if (f2fs_is_atomic_file(inode))
  106. return -EOPNOTSUPP;
  107. /*
  108. * Since the file was opened readonly, we have to initialize the quotas
  109. * here and not rely on ->open() doing it. This must be done before
  110. * evicting the inline data.
  111. */
  112. err = f2fs_dquot_initialize(inode);
  113. if (err)
  114. return err;
  115. err = f2fs_convert_inline_inode(inode);
  116. if (err)
  117. return err;
  118. set_inode_flag(inode, FI_VERITY_IN_PROGRESS);
  119. return 0;
  120. }
  121. static int f2fs_end_enable_verity(struct file *filp, const void *desc,
  122. size_t desc_size, u64 merkle_tree_size)
  123. {
  124. struct inode *inode = file_inode(filp);
  125. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  126. u64 desc_pos = f2fs_verity_metadata_pos(inode) + merkle_tree_size;
  127. struct fsverity_descriptor_location dloc = {
  128. .version = cpu_to_le32(F2FS_VERIFY_VER),
  129. .size = cpu_to_le32(desc_size),
  130. .pos = cpu_to_le64(desc_pos),
  131. };
  132. int err = 0, err2 = 0;
  133. /*
  134. * If an error already occurred (which fs/verity/ signals by passing
  135. * desc == NULL), then only clean-up is needed.
  136. */
  137. if (desc == NULL)
  138. goto cleanup;
  139. /* Append the verity descriptor. */
  140. err = pagecache_write(inode, desc, desc_size, desc_pos);
  141. if (err)
  142. goto cleanup;
  143. /*
  144. * Write all pages (both data and verity metadata). Note that this must
  145. * happen before clearing FI_VERITY_IN_PROGRESS; otherwise pages beyond
  146. * i_size won't be written properly. For crash consistency, this also
  147. * must happen before the verity inode flag gets persisted.
  148. */
  149. err = filemap_write_and_wait(inode->i_mapping);
  150. if (err)
  151. goto cleanup;
  152. /* Set the verity xattr. */
  153. err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_VERITY,
  154. F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc),
  155. NULL, XATTR_CREATE);
  156. if (err)
  157. goto cleanup;
  158. /* Finally, set the verity inode flag. */
  159. file_set_verity(inode);
  160. f2fs_set_inode_flags(inode);
  161. f2fs_mark_inode_dirty_sync(inode, true);
  162. clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
  163. return 0;
  164. cleanup:
  165. /*
  166. * Verity failed to be enabled, so clean up by truncating any verity
  167. * metadata that was written beyond i_size (both from cache and from
  168. * disk) and clearing FI_VERITY_IN_PROGRESS.
  169. *
  170. * Taking i_gc_rwsem[WRITE] is needed to stop f2fs garbage collection
  171. * from re-instantiating cached pages we are truncating (since unlike
  172. * normal file accesses, garbage collection isn't limited by i_size).
  173. */
  174. f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  175. truncate_inode_pages(inode->i_mapping, inode->i_size);
  176. err2 = f2fs_truncate(inode);
  177. if (err2) {
  178. f2fs_err(sbi, "Truncating verity metadata failed (errno=%d)",
  179. err2);
  180. set_sbi_flag(sbi, SBI_NEED_FSCK);
  181. }
  182. f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  183. clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
  184. return err ?: err2;
  185. }
  186. static int f2fs_get_verity_descriptor(struct inode *inode, void *buf,
  187. size_t buf_size)
  188. {
  189. struct fsverity_descriptor_location dloc;
  190. int res;
  191. u32 size;
  192. u64 pos;
  193. /* Get the descriptor location */
  194. res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_VERITY,
  195. F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc), NULL);
  196. if (res < 0 && res != -ERANGE)
  197. return res;
  198. if (res != sizeof(dloc) || dloc.version != cpu_to_le32(F2FS_VERIFY_VER)) {
  199. f2fs_warn(F2FS_I_SB(inode), "unknown verity xattr format");
  200. return -EINVAL;
  201. }
  202. size = le32_to_cpu(dloc.size);
  203. pos = le64_to_cpu(dloc.pos);
  204. /* Get the descriptor */
  205. if (pos + size < pos || pos + size > inode->i_sb->s_maxbytes ||
  206. pos < f2fs_verity_metadata_pos(inode) || size > INT_MAX) {
  207. f2fs_warn(F2FS_I_SB(inode), "invalid verity xattr");
  208. f2fs_handle_error(F2FS_I_SB(inode),
  209. ERROR_CORRUPTED_VERITY_XATTR);
  210. return -EFSCORRUPTED;
  211. }
  212. if (buf_size) {
  213. if (size > buf_size)
  214. return -ERANGE;
  215. res = pagecache_read(inode, buf, size, pos);
  216. if (res)
  217. return res;
  218. }
  219. return size;
  220. }
  221. static struct page *f2fs_read_merkle_tree_page(struct inode *inode,
  222. pgoff_t index,
  223. unsigned long num_ra_pages)
  224. {
  225. struct page *page;
  226. index += f2fs_verity_metadata_pos(inode) >> PAGE_SHIFT;
  227. page = find_get_page_flags(inode->i_mapping, index, FGP_ACCESSED);
  228. if (!page || !PageUptodate(page)) {
  229. DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, index);
  230. if (page)
  231. put_page(page);
  232. else if (num_ra_pages > 1)
  233. page_cache_ra_unbounded(&ractl, num_ra_pages, 0);
  234. page = read_mapping_page(inode->i_mapping, index, NULL);
  235. }
  236. return page;
  237. }
  238. static int f2fs_write_merkle_tree_block(struct inode *inode, const void *buf,
  239. u64 pos, unsigned int size)
  240. {
  241. pos += f2fs_verity_metadata_pos(inode);
  242. return pagecache_write(inode, buf, size, pos);
  243. }
  244. const struct fsverity_operations f2fs_verityops = {
  245. .begin_enable_verity = f2fs_begin_enable_verity,
  246. .end_enable_verity = f2fs_end_enable_verity,
  247. .get_verity_descriptor = f2fs_get_verity_descriptor,
  248. .read_merkle_tree_page = f2fs_read_merkle_tree_page,
  249. .write_merkle_tree_block = f2fs_write_merkle_tree_block,
  250. };