verity.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/fs.h>
  4. #include <linux/slab.h>
  5. #include <linux/rwsem.h>
  6. #include <linux/xattr.h>
  7. #include <linux/security.h>
  8. #include <linux/posix_acl_xattr.h>
  9. #include <linux/iversion.h>
  10. #include <linux/fsverity.h>
  11. #include <linux/sched/mm.h>
  12. #include "messages.h"
  13. #include "ctree.h"
  14. #include "btrfs_inode.h"
  15. #include "transaction.h"
  16. #include "locking.h"
  17. #include "fs.h"
  18. #include "accessors.h"
  19. #include "ioctl.h"
  20. #include "verity.h"
  21. #include "orphan.h"
  22. /*
  23. * Implementation of the interface defined in struct fsverity_operations.
  24. *
  25. * The main question is how and where to store the verity descriptor and the
  26. * Merkle tree. We store both in dedicated btree items in the filesystem tree,
  27. * together with the rest of the inode metadata. This means we'll need to do
  28. * extra work to encrypt them once encryption is supported in btrfs, but btrfs
  29. * has a lot of careful code around i_size and it seems better to make a new key
  30. * type than try and adjust all of our expectations for i_size.
  31. *
  32. * Note that this differs from the implementation in ext4 and f2fs, where
  33. * this data is stored as if it were in the file, but past EOF. However, btrfs
  34. * does not have a widespread mechanism for caching opaque metadata pages, so we
  35. * do pretend that the Merkle tree pages themselves are past EOF for the
  36. * purposes of caching them (as opposed to creating a virtual inode).
  37. *
  38. * fs verity items are stored under two different key types on disk.
  39. * The descriptor items:
  40. * [ inode objectid, BTRFS_VERITY_DESC_ITEM_KEY, offset ]
  41. *
  42. * At offset 0, we store a btrfs_verity_descriptor_item which tracks the
  43. * size of the descriptor item and some extra data for encryption.
  44. * Starting at offset 1, these hold the generic fs verity descriptor.
  45. * The latter are opaque to btrfs, we just read and write them as a blob for
  46. * the higher level verity code. The most common descriptor size is 256 bytes.
  47. *
  48. * The merkle tree items:
  49. * [ inode objectid, BTRFS_VERITY_MERKLE_ITEM_KEY, offset ]
  50. *
  51. * These also start at offset 0, and correspond to the merkle tree bytes.
  52. * So when fsverity asks for page 0 of the merkle tree, we pull up one page
  53. * starting at offset 0 for this key type. These are also opaque to btrfs,
  54. * we're blindly storing whatever fsverity sends down.
  55. *
  56. * Another important consideration is the fact that the Merkle tree data scales
  57. * linearly with the size of the file (with 4K pages/blocks and SHA-256, it's
  58. * ~1/127th the size) so for large files, writing the tree can be a lengthy
  59. * operation. For that reason, we guard the whole enable verity operation
  60. * (between begin_enable_verity and end_enable_verity) with an orphan item.
  61. * Again, because the data can be pretty large, it's quite possible that we
  62. * could run out of space writing it, so we try our best to handle errors by
  63. * stopping and rolling back rather than aborting the victim transaction.
  64. */
  65. #define MERKLE_START_ALIGN 65536
  66. /*
  67. * Compute the logical file offset where we cache the Merkle tree.
  68. *
  69. * @inode: inode of the verity file
  70. *
  71. * For the purposes of caching the Merkle tree pages, as required by
  72. * fs-verity, it is convenient to do size computations in terms of a file
  73. * offset, rather than in terms of page indices.
  74. *
  75. * Use 64K to be sure it's past the last page in the file, even with 64K pages.
  76. * That rounding operation itself can overflow loff_t, so we do it in u64 and
  77. * check.
  78. *
  79. * Returns the file offset on success, negative error code on failure.
  80. */
  81. static loff_t merkle_file_pos(const struct inode *inode)
  82. {
  83. u64 sz = inode->i_size;
  84. u64 rounded = round_up(sz, MERKLE_START_ALIGN);
  85. if (rounded > inode->i_sb->s_maxbytes)
  86. return -EFBIG;
  87. return rounded;
  88. }
  89. /*
  90. * Drop all the items for this inode with this key_type.
  91. *
  92. * @inode: inode to drop items for
  93. * @key_type: type of items to drop (BTRFS_VERITY_DESC_ITEM or
  94. * BTRFS_VERITY_MERKLE_ITEM)
  95. *
  96. * Before doing a verity enable we cleanup any existing verity items.
  97. * This is also used to clean up if a verity enable failed half way through.
  98. *
  99. * Returns number of dropped items on success, negative error code on failure.
  100. */
  101. static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
  102. {
  103. struct btrfs_trans_handle *trans;
  104. struct btrfs_root *root = inode->root;
  105. BTRFS_PATH_AUTO_FREE(path);
  106. struct btrfs_key key;
  107. int count = 0;
  108. int ret;
  109. path = btrfs_alloc_path();
  110. if (!path)
  111. return -ENOMEM;
  112. while (1) {
  113. /* 1 for the item being dropped */
  114. trans = btrfs_start_transaction(root, 1);
  115. if (IS_ERR(trans))
  116. return PTR_ERR(trans);
  117. /*
  118. * Walk backwards through all the items until we find one that
  119. * isn't from our key type or objectid
  120. */
  121. key.objectid = btrfs_ino(inode);
  122. key.type = key_type;
  123. key.offset = (u64)-1;
  124. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  125. if (ret > 0) {
  126. ret = 0;
  127. /* No more keys of this type, we're done */
  128. if (path->slots[0] == 0)
  129. break;
  130. path->slots[0]--;
  131. } else if (ret < 0) {
  132. btrfs_end_transaction(trans);
  133. return ret;
  134. }
  135. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  136. /* No more keys of this type, we're done */
  137. if (key.objectid != btrfs_ino(inode) || key.type != key_type)
  138. break;
  139. /*
  140. * This shouldn't be a performance sensitive function because
  141. * it's not used as part of truncate. If it ever becomes
  142. * perf sensitive, change this to walk forward and bulk delete
  143. * items
  144. */
  145. ret = btrfs_del_items(trans, root, path, path->slots[0], 1);
  146. if (ret) {
  147. btrfs_end_transaction(trans);
  148. return ret;
  149. }
  150. count++;
  151. btrfs_release_path(path);
  152. btrfs_end_transaction(trans);
  153. }
  154. btrfs_end_transaction(trans);
  155. return count;
  156. }
  157. /*
  158. * Drop all verity items
  159. *
  160. * @inode: inode to drop verity items for
  161. *
  162. * In most contexts where we are dropping verity items, we want to do it for all
  163. * the types of verity items, not a particular one.
  164. *
  165. * Returns: 0 on success, negative error code on failure.
  166. */
  167. int btrfs_drop_verity_items(struct btrfs_inode *inode)
  168. {
  169. int ret;
  170. ret = drop_verity_items(inode, BTRFS_VERITY_DESC_ITEM_KEY);
  171. if (ret < 0)
  172. return ret;
  173. ret = drop_verity_items(inode, BTRFS_VERITY_MERKLE_ITEM_KEY);
  174. if (ret < 0)
  175. return ret;
  176. return 0;
  177. }
  178. /*
  179. * Insert and write inode items with a given key type and offset.
  180. *
  181. * @inode: inode to insert for
  182. * @key_type: key type to insert
  183. * @offset: item offset to insert at
  184. * @src: source data to write
  185. * @len: length of source data to write
  186. *
  187. * Write len bytes from src into items of up to 2K length.
  188. * The inserted items will have key (ino, key_type, offset + off) where off is
  189. * consecutively increasing from 0 up to the last item ending at offset + len.
  190. *
  191. * Returns 0 on success and a negative error code on failure.
  192. */
  193. static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
  194. const char *src, u64 len)
  195. {
  196. struct btrfs_trans_handle *trans;
  197. BTRFS_PATH_AUTO_FREE(path);
  198. struct btrfs_root *root = inode->root;
  199. struct extent_buffer *leaf;
  200. struct btrfs_key key;
  201. unsigned long copy_bytes;
  202. unsigned long src_offset = 0;
  203. void *data;
  204. int ret = 0;
  205. path = btrfs_alloc_path();
  206. if (!path)
  207. return -ENOMEM;
  208. while (len > 0) {
  209. /* 1 for the new item being inserted */
  210. trans = btrfs_start_transaction(root, 1);
  211. if (IS_ERR(trans))
  212. return PTR_ERR(trans);
  213. key.objectid = btrfs_ino(inode);
  214. key.type = key_type;
  215. key.offset = offset;
  216. /*
  217. * Insert 2K at a time mostly to be friendly for smaller leaf
  218. * size filesystems
  219. */
  220. copy_bytes = min_t(u64, len, 2048);
  221. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_bytes);
  222. if (ret) {
  223. btrfs_end_transaction(trans);
  224. break;
  225. }
  226. leaf = path->nodes[0];
  227. data = btrfs_item_ptr(leaf, path->slots[0], void);
  228. write_extent_buffer(leaf, src + src_offset,
  229. (unsigned long)data, copy_bytes);
  230. offset += copy_bytes;
  231. src_offset += copy_bytes;
  232. len -= copy_bytes;
  233. btrfs_release_path(path);
  234. btrfs_end_transaction(trans);
  235. }
  236. return ret;
  237. }
  238. /*
  239. * Read inode items of the given key type and offset from the btree.
  240. *
  241. * @inode: inode to read items of
  242. * @key_type: key type to read
  243. * @offset: item offset to read from
  244. * @dest: Buffer to read into. This parameter has slightly tricky
  245. * semantics. If it is NULL, the function will not do any copying
  246. * and will just return the size of all the items up to len bytes.
  247. * If dest_page is passed, then the function will kmap_local the
  248. * page and ignore dest, but it must still be non-NULL to avoid the
  249. * counting-only behavior.
  250. * @len: length in bytes to read
  251. * @dest_folio: copy into this folio instead of the dest buffer
  252. *
  253. * Helper function to read items from the btree. This returns the number of
  254. * bytes read or < 0 for errors. We can return short reads if the items don't
  255. * exist on disk or aren't big enough to fill the desired length. Supports
  256. * reading into a provided buffer (dest) or into the page cache
  257. *
  258. * Returns number of bytes read or a negative error code on failure.
  259. */
  260. static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
  261. char *dest, u64 len, struct folio *dest_folio)
  262. {
  263. BTRFS_PATH_AUTO_FREE(path);
  264. struct btrfs_root *root = inode->root;
  265. struct extent_buffer *leaf;
  266. struct btrfs_key key;
  267. u64 item_end;
  268. u64 copy_end;
  269. int copied = 0;
  270. u32 copy_offset;
  271. unsigned long copy_bytes;
  272. unsigned long dest_offset = 0;
  273. void *data;
  274. char *kaddr = dest;
  275. int ret;
  276. path = btrfs_alloc_path();
  277. if (!path)
  278. return -ENOMEM;
  279. if (dest_folio)
  280. path->reada = READA_FORWARD;
  281. key.objectid = btrfs_ino(inode);
  282. key.type = key_type;
  283. key.offset = offset;
  284. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  285. if (ret < 0) {
  286. goto out;
  287. } else if (ret > 0) {
  288. ret = 0;
  289. if (path->slots[0] == 0)
  290. goto out;
  291. path->slots[0]--;
  292. }
  293. while (len > 0) {
  294. leaf = path->nodes[0];
  295. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  296. if (key.objectid != btrfs_ino(inode) || key.type != key_type)
  297. break;
  298. item_end = btrfs_item_size(leaf, path->slots[0]) + key.offset;
  299. if (copied > 0) {
  300. /*
  301. * Once we've copied something, we want all of the items
  302. * to be sequential
  303. */
  304. if (key.offset != offset)
  305. break;
  306. } else {
  307. /*
  308. * Our initial offset might be in the middle of an
  309. * item. Make sure it all makes sense.
  310. */
  311. if (key.offset > offset)
  312. break;
  313. if (item_end <= offset)
  314. break;
  315. }
  316. /* desc = NULL to just sum all the item lengths */
  317. if (!dest)
  318. copy_end = item_end;
  319. else
  320. copy_end = min(offset + len, item_end);
  321. /* Number of bytes in this item we want to copy */
  322. copy_bytes = copy_end - offset;
  323. /* Offset from the start of item for copying */
  324. copy_offset = offset - key.offset;
  325. if (dest) {
  326. if (dest_folio)
  327. kaddr = kmap_local_folio(dest_folio, 0);
  328. data = btrfs_item_ptr(leaf, path->slots[0], void);
  329. read_extent_buffer(leaf, kaddr + dest_offset,
  330. (unsigned long)data + copy_offset,
  331. copy_bytes);
  332. if (dest_folio)
  333. kunmap_local(kaddr);
  334. }
  335. offset += copy_bytes;
  336. dest_offset += copy_bytes;
  337. len -= copy_bytes;
  338. copied += copy_bytes;
  339. path->slots[0]++;
  340. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  341. /*
  342. * We've reached the last slot in this leaf and we need
  343. * to go to the next leaf.
  344. */
  345. ret = btrfs_next_leaf(root, path);
  346. if (ret < 0) {
  347. break;
  348. } else if (ret > 0) {
  349. ret = 0;
  350. break;
  351. }
  352. }
  353. }
  354. out:
  355. if (!ret)
  356. ret = copied;
  357. return ret;
  358. }
  359. /*
  360. * Delete an fsverity orphan
  361. *
  362. * @trans: transaction to do the delete in
  363. * @inode: inode to orphan
  364. *
  365. * Capture verity orphan specific logic that is repeated in the couple places
  366. * we delete verity orphans. Specifically, handling ENOENT and ignoring inodes
  367. * with 0 links.
  368. *
  369. * Returns zero on success or a negative error code on failure.
  370. */
  371. static int del_orphan(struct btrfs_trans_handle *trans, struct btrfs_inode *inode)
  372. {
  373. struct btrfs_root *root = inode->root;
  374. int ret;
  375. /*
  376. * If the inode has no links, it is either already unlinked, or was
  377. * created with O_TMPFILE. In either case, it should have an orphan from
  378. * that other operation. Rather than reference count the orphans, we
  379. * simply ignore them here, because we only invoke the verity path in
  380. * the orphan logic when i_nlink is 1.
  381. */
  382. if (!inode->vfs_inode.i_nlink)
  383. return 0;
  384. ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
  385. if (ret == -ENOENT)
  386. ret = 0;
  387. return ret;
  388. }
  389. /*
  390. * Rollback in-progress verity if we encounter an error.
  391. *
  392. * @inode: inode verity had an error for
  393. *
  394. * We try to handle recoverable errors while enabling verity by rolling it back
  395. * and just failing the operation, rather than having an fs level error no
  396. * matter what. However, any error in rollback is unrecoverable.
  397. *
  398. * Returns 0 on success, negative error code on failure.
  399. */
  400. static int rollback_verity(struct btrfs_inode *inode)
  401. {
  402. struct btrfs_trans_handle *trans = NULL;
  403. struct btrfs_root *root = inode->root;
  404. int ret;
  405. btrfs_assert_inode_locked(inode);
  406. truncate_inode_pages(inode->vfs_inode.i_mapping, inode->vfs_inode.i_size);
  407. clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
  408. ret = btrfs_drop_verity_items(inode);
  409. if (ret) {
  410. btrfs_handle_fs_error(root->fs_info, ret,
  411. "failed to drop verity items in rollback %llu",
  412. (u64)inode->vfs_inode.i_ino);
  413. goto out;
  414. }
  415. /*
  416. * 1 for updating the inode flag
  417. * 1 for deleting the orphan
  418. */
  419. trans = btrfs_start_transaction(root, 2);
  420. if (IS_ERR(trans)) {
  421. ret = PTR_ERR(trans);
  422. trans = NULL;
  423. btrfs_handle_fs_error(root->fs_info, ret,
  424. "failed to start transaction in verity rollback %llu",
  425. (u64)inode->vfs_inode.i_ino);
  426. goto out;
  427. }
  428. inode->ro_flags &= ~BTRFS_INODE_RO_VERITY;
  429. btrfs_sync_inode_flags_to_i_flags(inode);
  430. ret = btrfs_update_inode(trans, inode);
  431. if (unlikely(ret)) {
  432. btrfs_abort_transaction(trans, ret);
  433. goto out;
  434. }
  435. ret = del_orphan(trans, inode);
  436. if (unlikely(ret)) {
  437. btrfs_abort_transaction(trans, ret);
  438. goto out;
  439. }
  440. out:
  441. if (trans)
  442. btrfs_end_transaction(trans);
  443. return ret;
  444. }
  445. /*
  446. * Finalize making the file a valid verity file
  447. *
  448. * @inode: inode to be marked as verity
  449. * @desc: contents of the verity descriptor to write (not NULL)
  450. * @desc_size: size of the verity descriptor
  451. *
  452. * Do the actual work of finalizing verity after successfully writing the Merkle
  453. * tree:
  454. *
  455. * - write out the descriptor items
  456. * - mark the inode with the verity flag
  457. * - delete the orphan item
  458. * - mark the ro compat bit
  459. * - clear the in progress bit
  460. *
  461. * Returns 0 on success, negative error code on failure.
  462. */
  463. static int finish_verity(struct btrfs_inode *inode, const void *desc,
  464. size_t desc_size)
  465. {
  466. struct btrfs_trans_handle *trans = NULL;
  467. struct btrfs_root *root = inode->root;
  468. struct btrfs_verity_descriptor_item item;
  469. int ret;
  470. /* Write out the descriptor item */
  471. memset(&item, 0, sizeof(item));
  472. btrfs_set_stack_verity_descriptor_size(&item, desc_size);
  473. ret = write_key_bytes(inode, BTRFS_VERITY_DESC_ITEM_KEY, 0,
  474. (const char *)&item, sizeof(item));
  475. if (ret)
  476. return ret;
  477. /* Write out the descriptor itself */
  478. ret = write_key_bytes(inode, BTRFS_VERITY_DESC_ITEM_KEY, 1,
  479. desc, desc_size);
  480. if (ret)
  481. return ret;
  482. /*
  483. * 1 for updating the inode flag
  484. * 1 for deleting the orphan
  485. */
  486. trans = btrfs_start_transaction(root, 2);
  487. if (IS_ERR(trans))
  488. return PTR_ERR(trans);
  489. inode->ro_flags |= BTRFS_INODE_RO_VERITY;
  490. btrfs_sync_inode_flags_to_i_flags(inode);
  491. ret = btrfs_update_inode(trans, inode);
  492. if (ret)
  493. goto end_trans;
  494. ret = del_orphan(trans, inode);
  495. if (ret)
  496. goto end_trans;
  497. clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
  498. btrfs_set_fs_compat_ro(root->fs_info, VERITY);
  499. end_trans:
  500. btrfs_end_transaction(trans);
  501. return ret;
  502. }
  503. /*
  504. * fsverity op that begins enabling verity.
  505. *
  506. * @filp: file to enable verity on
  507. *
  508. * Begin enabling fsverity for the file. We drop any existing verity items, add
  509. * an orphan and set the in progress bit.
  510. *
  511. * Returns 0 on success, negative error code on failure.
  512. */
  513. static int btrfs_begin_enable_verity(struct file *filp)
  514. {
  515. struct btrfs_inode *inode = BTRFS_I(file_inode(filp));
  516. struct btrfs_root *root = inode->root;
  517. struct btrfs_trans_handle *trans;
  518. int ret;
  519. btrfs_assert_inode_locked(inode);
  520. if (IS_ENCRYPTED(&inode->vfs_inode))
  521. return -EOPNOTSUPP;
  522. if (test_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags))
  523. return -EBUSY;
  524. /*
  525. * This should almost never do anything, but theoretically, it's
  526. * possible that we failed to enable verity on a file, then were
  527. * interrupted or failed while rolling back, failed to cleanup the
  528. * orphan, and finally attempt to enable verity again.
  529. */
  530. ret = btrfs_drop_verity_items(inode);
  531. if (ret)
  532. return ret;
  533. /* 1 for the orphan item */
  534. trans = btrfs_start_transaction(root, 1);
  535. if (IS_ERR(trans))
  536. return PTR_ERR(trans);
  537. ret = btrfs_orphan_add(trans, inode);
  538. if (!ret)
  539. set_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
  540. btrfs_end_transaction(trans);
  541. return 0;
  542. }
  543. /*
  544. * fsverity op that ends enabling verity.
  545. *
  546. * @filp: file we are finishing enabling verity on
  547. * @desc: verity descriptor to write out (NULL in error conditions)
  548. * @desc_size: size of the verity descriptor (variable with signatures)
  549. * @merkle_tree_size: size of the merkle tree in bytes
  550. *
  551. * If desc is null, then VFS is signaling an error occurred during verity
  552. * enable, and we should try to rollback. Otherwise, attempt to finish verity.
  553. *
  554. * Returns 0 on success, negative error code on error.
  555. */
  556. static int btrfs_end_enable_verity(struct file *filp, const void *desc,
  557. size_t desc_size, u64 merkle_tree_size)
  558. {
  559. struct btrfs_inode *inode = BTRFS_I(file_inode(filp));
  560. int ret = 0;
  561. int rollback_ret;
  562. btrfs_assert_inode_locked(inode);
  563. if (desc == NULL)
  564. goto rollback;
  565. ret = finish_verity(inode, desc, desc_size);
  566. if (ret)
  567. goto rollback;
  568. return ret;
  569. rollback:
  570. rollback_ret = rollback_verity(inode);
  571. if (rollback_ret)
  572. btrfs_err(inode->root->fs_info,
  573. "failed to rollback verity items: %d", rollback_ret);
  574. return ret;
  575. }
  576. /*
  577. * fsverity op that gets the struct fsverity_descriptor.
  578. *
  579. * @inode: inode to get the descriptor of
  580. * @buf: output buffer for the descriptor contents
  581. * @buf_size: size of the output buffer. 0 to query the size
  582. *
  583. * fsverity does a two pass setup for reading the descriptor, in the first pass
  584. * it calls with buf_size = 0 to query the size of the descriptor, and then in
  585. * the second pass it actually reads the descriptor off disk.
  586. *
  587. * Returns the size on success or a negative error code on failure.
  588. */
  589. int btrfs_get_verity_descriptor(struct inode *inode, void *buf, size_t buf_size)
  590. {
  591. u64 true_size;
  592. int ret = 0;
  593. struct btrfs_verity_descriptor_item item;
  594. memset(&item, 0, sizeof(item));
  595. ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_DESC_ITEM_KEY, 0,
  596. (char *)&item, sizeof(item), NULL);
  597. if (ret < 0)
  598. return ret;
  599. if (unlikely(item.reserved[0] != 0 || item.reserved[1] != 0))
  600. return -EUCLEAN;
  601. true_size = btrfs_stack_verity_descriptor_size(&item);
  602. if (unlikely(true_size > INT_MAX))
  603. return -EUCLEAN;
  604. if (buf_size == 0)
  605. return true_size;
  606. if (buf_size < true_size)
  607. return -ERANGE;
  608. ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_DESC_ITEM_KEY, 1,
  609. buf, buf_size, NULL);
  610. if (ret < 0)
  611. return ret;
  612. if (ret != true_size)
  613. return -EIO;
  614. return true_size;
  615. }
  616. /*
  617. * fsverity op that reads and caches a merkle tree page.
  618. *
  619. * @inode: inode to read a merkle tree page for
  620. * @index: page index relative to the start of the merkle tree
  621. *
  622. * The Merkle tree is stored in the filesystem btree, but its pages are cached
  623. * with a logical position past EOF in the inode's mapping.
  624. *
  625. * Returns the page we read, or an ERR_PTR on error.
  626. */
  627. static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
  628. pgoff_t index)
  629. {
  630. struct folio *folio;
  631. u64 off = (u64)index << PAGE_SHIFT;
  632. loff_t merkle_pos = merkle_file_pos(inode);
  633. int ret;
  634. if (merkle_pos < 0)
  635. return ERR_PTR(merkle_pos);
  636. if (merkle_pos > inode->i_sb->s_maxbytes - off - PAGE_SIZE)
  637. return ERR_PTR(-EFBIG);
  638. index += merkle_pos >> PAGE_SHIFT;
  639. again:
  640. folio = __filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0);
  641. if (!IS_ERR(folio)) {
  642. if (folio_test_uptodate(folio))
  643. goto out;
  644. folio_lock(folio);
  645. /* If it's not uptodate after we have the lock, we got a read error. */
  646. if (!folio_test_uptodate(folio)) {
  647. folio_unlock(folio);
  648. folio_put(folio);
  649. return ERR_PTR(-EIO);
  650. }
  651. folio_unlock(folio);
  652. goto out;
  653. }
  654. folio = filemap_alloc_folio(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS),
  655. 0, NULL);
  656. if (!folio)
  657. return ERR_PTR(-ENOMEM);
  658. ret = filemap_add_folio(inode->i_mapping, folio, index, GFP_NOFS);
  659. if (ret) {
  660. folio_put(folio);
  661. /* Did someone else insert a folio here? */
  662. if (ret == -EEXIST)
  663. goto again;
  664. return ERR_PTR(ret);
  665. }
  666. /*
  667. * Merkle item keys are indexed from byte 0 in the merkle tree.
  668. * They have the form:
  669. *
  670. * [ inode objectid, BTRFS_MERKLE_ITEM_KEY, offset in bytes ]
  671. */
  672. ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY, off,
  673. folio_address(folio), PAGE_SIZE, folio);
  674. if (ret < 0) {
  675. folio_put(folio);
  676. return ERR_PTR(ret);
  677. }
  678. if (ret < PAGE_SIZE)
  679. folio_zero_segment(folio, ret, PAGE_SIZE);
  680. folio_mark_uptodate(folio);
  681. folio_unlock(folio);
  682. out:
  683. return folio_file_page(folio, index);
  684. }
  685. /*
  686. * fsverity op that writes a Merkle tree block into the btree.
  687. *
  688. * @file: file to write a Merkle tree block for
  689. * @buf: Merkle tree block to write
  690. * @pos: the position of the block in the Merkle tree (in bytes)
  691. * @size: the Merkle tree block size (in bytes)
  692. *
  693. * Returns 0 on success or negative error code on failure
  694. */
  695. static int btrfs_write_merkle_tree_block(struct file *file, const void *buf,
  696. u64 pos, unsigned int size)
  697. {
  698. struct inode *inode = file_inode(file);
  699. loff_t merkle_pos = merkle_file_pos(inode);
  700. if (merkle_pos < 0)
  701. return merkle_pos;
  702. if (merkle_pos > inode->i_sb->s_maxbytes - pos - size)
  703. return -EFBIG;
  704. return write_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY,
  705. pos, buf, size);
  706. }
  707. const struct fsverity_operations btrfs_verityops = {
  708. .begin_enable_verity = btrfs_begin_enable_verity,
  709. .end_enable_verity = btrfs_end_enable_verity,
  710. .get_verity_descriptor = btrfs_get_verity_descriptor,
  711. .read_merkle_tree_page = btrfs_read_merkle_tree_page,
  712. .write_merkle_tree_block = btrfs_write_merkle_tree_block,
  713. };