verity.c 8.1 KB

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