verity.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. struct btrfs_path *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. ret = PTR_ERR(trans);
  117. goto out;
  118. }
  119. /*
  120. * Walk backwards through all the items until we find one that
  121. * isn't from our key type or objectid
  122. */
  123. key.objectid = btrfs_ino(inode);
  124. key.type = key_type;
  125. key.offset = (u64)-1;
  126. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  127. if (ret > 0) {
  128. ret = 0;
  129. /* No more keys of this type, we're done */
  130. if (path->slots[0] == 0)
  131. break;
  132. path->slots[0]--;
  133. } else if (ret < 0) {
  134. btrfs_end_transaction(trans);
  135. goto out;
  136. }
  137. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  138. /* No more keys of this type, we're done */
  139. if (key.objectid != btrfs_ino(inode) || key.type != key_type)
  140. break;
  141. /*
  142. * This shouldn't be a performance sensitive function because
  143. * it's not used as part of truncate. If it ever becomes
  144. * perf sensitive, change this to walk forward and bulk delete
  145. * items
  146. */
  147. ret = btrfs_del_items(trans, root, path, path->slots[0], 1);
  148. if (ret) {
  149. btrfs_end_transaction(trans);
  150. goto out;
  151. }
  152. count++;
  153. btrfs_release_path(path);
  154. btrfs_end_transaction(trans);
  155. }
  156. ret = count;
  157. btrfs_end_transaction(trans);
  158. out:
  159. btrfs_free_path(path);
  160. return ret;
  161. }
  162. /*
  163. * Drop all verity items
  164. *
  165. * @inode: inode to drop verity items for
  166. *
  167. * In most contexts where we are dropping verity items, we want to do it for all
  168. * the types of verity items, not a particular one.
  169. *
  170. * Returns: 0 on success, negative error code on failure.
  171. */
  172. int btrfs_drop_verity_items(struct btrfs_inode *inode)
  173. {
  174. int ret;
  175. ret = drop_verity_items(inode, BTRFS_VERITY_DESC_ITEM_KEY);
  176. if (ret < 0)
  177. return ret;
  178. ret = drop_verity_items(inode, BTRFS_VERITY_MERKLE_ITEM_KEY);
  179. if (ret < 0)
  180. return ret;
  181. return 0;
  182. }
  183. /*
  184. * Insert and write inode items with a given key type and offset.
  185. *
  186. * @inode: inode to insert for
  187. * @key_type: key type to insert
  188. * @offset: item offset to insert at
  189. * @src: source data to write
  190. * @len: length of source data to write
  191. *
  192. * Write len bytes from src into items of up to 2K length.
  193. * The inserted items will have key (ino, key_type, offset + off) where off is
  194. * consecutively increasing from 0 up to the last item ending at offset + len.
  195. *
  196. * Returns 0 on success and a negative error code on failure.
  197. */
  198. static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
  199. const char *src, u64 len)
  200. {
  201. struct btrfs_trans_handle *trans;
  202. struct btrfs_path *path;
  203. struct btrfs_root *root = inode->root;
  204. struct extent_buffer *leaf;
  205. struct btrfs_key key;
  206. unsigned long copy_bytes;
  207. unsigned long src_offset = 0;
  208. void *data;
  209. int ret = 0;
  210. path = btrfs_alloc_path();
  211. if (!path)
  212. return -ENOMEM;
  213. while (len > 0) {
  214. /* 1 for the new item being inserted */
  215. trans = btrfs_start_transaction(root, 1);
  216. if (IS_ERR(trans)) {
  217. ret = PTR_ERR(trans);
  218. break;
  219. }
  220. key.objectid = btrfs_ino(inode);
  221. key.type = key_type;
  222. key.offset = offset;
  223. /*
  224. * Insert 2K at a time mostly to be friendly for smaller leaf
  225. * size filesystems
  226. */
  227. copy_bytes = min_t(u64, len, 2048);
  228. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_bytes);
  229. if (ret) {
  230. btrfs_end_transaction(trans);
  231. break;
  232. }
  233. leaf = path->nodes[0];
  234. data = btrfs_item_ptr(leaf, path->slots[0], void);
  235. write_extent_buffer(leaf, src + src_offset,
  236. (unsigned long)data, copy_bytes);
  237. offset += copy_bytes;
  238. src_offset += copy_bytes;
  239. len -= copy_bytes;
  240. btrfs_release_path(path);
  241. btrfs_end_transaction(trans);
  242. }
  243. btrfs_free_path(path);
  244. return ret;
  245. }
  246. /*
  247. * Read inode items of the given key type and offset from the btree.
  248. *
  249. * @inode: inode to read items of
  250. * @key_type: key type to read
  251. * @offset: item offset to read from
  252. * @dest: Buffer to read into. This parameter has slightly tricky
  253. * semantics. If it is NULL, the function will not do any copying
  254. * and will just return the size of all the items up to len bytes.
  255. * If dest_page is passed, then the function will kmap_local the
  256. * page and ignore dest, but it must still be non-NULL to avoid the
  257. * counting-only behavior.
  258. * @len: length in bytes to read
  259. * @dest_folio: copy into this folio instead of the dest buffer
  260. *
  261. * Helper function to read items from the btree. This returns the number of
  262. * bytes read or < 0 for errors. We can return short reads if the items don't
  263. * exist on disk or aren't big enough to fill the desired length. Supports
  264. * reading into a provided buffer (dest) or into the page cache
  265. *
  266. * Returns number of bytes read or a negative error code on failure.
  267. */
  268. static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
  269. char *dest, u64 len, struct folio *dest_folio)
  270. {
  271. struct btrfs_path *path;
  272. struct btrfs_root *root = inode->root;
  273. struct extent_buffer *leaf;
  274. struct btrfs_key key;
  275. u64 item_end;
  276. u64 copy_end;
  277. int copied = 0;
  278. u32 copy_offset;
  279. unsigned long copy_bytes;
  280. unsigned long dest_offset = 0;
  281. void *data;
  282. char *kaddr = dest;
  283. int ret;
  284. path = btrfs_alloc_path();
  285. if (!path)
  286. return -ENOMEM;
  287. if (dest_folio)
  288. path->reada = READA_FORWARD;
  289. key.objectid = btrfs_ino(inode);
  290. key.type = key_type;
  291. key.offset = offset;
  292. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  293. if (ret < 0) {
  294. goto out;
  295. } else if (ret > 0) {
  296. ret = 0;
  297. if (path->slots[0] == 0)
  298. goto out;
  299. path->slots[0]--;
  300. }
  301. while (len > 0) {
  302. leaf = path->nodes[0];
  303. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  304. if (key.objectid != btrfs_ino(inode) || key.type != key_type)
  305. break;
  306. item_end = btrfs_item_size(leaf, path->slots[0]) + key.offset;
  307. if (copied > 0) {
  308. /*
  309. * Once we've copied something, we want all of the items
  310. * to be sequential
  311. */
  312. if (key.offset != offset)
  313. break;
  314. } else {
  315. /*
  316. * Our initial offset might be in the middle of an
  317. * item. Make sure it all makes sense.
  318. */
  319. if (key.offset > offset)
  320. break;
  321. if (item_end <= offset)
  322. break;
  323. }
  324. /* desc = NULL to just sum all the item lengths */
  325. if (!dest)
  326. copy_end = item_end;
  327. else
  328. copy_end = min(offset + len, item_end);
  329. /* Number of bytes in this item we want to copy */
  330. copy_bytes = copy_end - offset;
  331. /* Offset from the start of item for copying */
  332. copy_offset = offset - key.offset;
  333. if (dest) {
  334. if (dest_folio)
  335. kaddr = kmap_local_folio(dest_folio, 0);
  336. data = btrfs_item_ptr(leaf, path->slots[0], void);
  337. read_extent_buffer(leaf, kaddr + dest_offset,
  338. (unsigned long)data + copy_offset,
  339. copy_bytes);
  340. if (dest_folio)
  341. kunmap_local(kaddr);
  342. }
  343. offset += copy_bytes;
  344. dest_offset += copy_bytes;
  345. len -= copy_bytes;
  346. copied += copy_bytes;
  347. path->slots[0]++;
  348. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  349. /*
  350. * We've reached the last slot in this leaf and we need
  351. * to go to the next leaf.
  352. */
  353. ret = btrfs_next_leaf(root, path);
  354. if (ret < 0) {
  355. break;
  356. } else if (ret > 0) {
  357. ret = 0;
  358. break;
  359. }
  360. }
  361. }
  362. out:
  363. btrfs_free_path(path);
  364. if (!ret)
  365. ret = copied;
  366. return ret;
  367. }
  368. /*
  369. * Delete an fsverity orphan
  370. *
  371. * @trans: transaction to do the delete in
  372. * @inode: inode to orphan
  373. *
  374. * Capture verity orphan specific logic that is repeated in the couple places
  375. * we delete verity orphans. Specifically, handling ENOENT and ignoring inodes
  376. * with 0 links.
  377. *
  378. * Returns zero on success or a negative error code on failure.
  379. */
  380. static int del_orphan(struct btrfs_trans_handle *trans, struct btrfs_inode *inode)
  381. {
  382. struct btrfs_root *root = inode->root;
  383. int ret;
  384. /*
  385. * If the inode has no links, it is either already unlinked, or was
  386. * created with O_TMPFILE. In either case, it should have an orphan from
  387. * that other operation. Rather than reference count the orphans, we
  388. * simply ignore them here, because we only invoke the verity path in
  389. * the orphan logic when i_nlink is 1.
  390. */
  391. if (!inode->vfs_inode.i_nlink)
  392. return 0;
  393. ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
  394. if (ret == -ENOENT)
  395. ret = 0;
  396. return ret;
  397. }
  398. /*
  399. * Rollback in-progress verity if we encounter an error.
  400. *
  401. * @inode: inode verity had an error for
  402. *
  403. * We try to handle recoverable errors while enabling verity by rolling it back
  404. * and just failing the operation, rather than having an fs level error no
  405. * matter what. However, any error in rollback is unrecoverable.
  406. *
  407. * Returns 0 on success, negative error code on failure.
  408. */
  409. static int rollback_verity(struct btrfs_inode *inode)
  410. {
  411. struct btrfs_trans_handle *trans = NULL;
  412. struct btrfs_root *root = inode->root;
  413. int ret;
  414. btrfs_assert_inode_locked(inode);
  415. truncate_inode_pages(inode->vfs_inode.i_mapping, inode->vfs_inode.i_size);
  416. clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
  417. ret = btrfs_drop_verity_items(inode);
  418. if (ret) {
  419. btrfs_handle_fs_error(root->fs_info, ret,
  420. "failed to drop verity items in rollback %llu",
  421. (u64)inode->vfs_inode.i_ino);
  422. goto out;
  423. }
  424. /*
  425. * 1 for updating the inode flag
  426. * 1 for deleting the orphan
  427. */
  428. trans = btrfs_start_transaction(root, 2);
  429. if (IS_ERR(trans)) {
  430. ret = PTR_ERR(trans);
  431. trans = NULL;
  432. btrfs_handle_fs_error(root->fs_info, ret,
  433. "failed to start transaction in verity rollback %llu",
  434. (u64)inode->vfs_inode.i_ino);
  435. goto out;
  436. }
  437. inode->ro_flags &= ~BTRFS_INODE_RO_VERITY;
  438. btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);
  439. ret = btrfs_update_inode(trans, inode);
  440. if (ret) {
  441. btrfs_abort_transaction(trans, ret);
  442. goto out;
  443. }
  444. ret = del_orphan(trans, inode);
  445. if (ret) {
  446. btrfs_abort_transaction(trans, ret);
  447. goto out;
  448. }
  449. out:
  450. if (trans)
  451. btrfs_end_transaction(trans);
  452. return ret;
  453. }
  454. /*
  455. * Finalize making the file a valid verity file
  456. *
  457. * @inode: inode to be marked as verity
  458. * @desc: contents of the verity descriptor to write (not NULL)
  459. * @desc_size: size of the verity descriptor
  460. *
  461. * Do the actual work of finalizing verity after successfully writing the Merkle
  462. * tree:
  463. *
  464. * - write out the descriptor items
  465. * - mark the inode with the verity flag
  466. * - delete the orphan item
  467. * - mark the ro compat bit
  468. * - clear the in progress bit
  469. *
  470. * Returns 0 on success, negative error code on failure.
  471. */
  472. static int finish_verity(struct btrfs_inode *inode, const void *desc,
  473. size_t desc_size)
  474. {
  475. struct btrfs_trans_handle *trans = NULL;
  476. struct btrfs_root *root = inode->root;
  477. struct btrfs_verity_descriptor_item item;
  478. int ret;
  479. /* Write out the descriptor item */
  480. memset(&item, 0, sizeof(item));
  481. btrfs_set_stack_verity_descriptor_size(&item, desc_size);
  482. ret = write_key_bytes(inode, BTRFS_VERITY_DESC_ITEM_KEY, 0,
  483. (const char *)&item, sizeof(item));
  484. if (ret)
  485. goto out;
  486. /* Write out the descriptor itself */
  487. ret = write_key_bytes(inode, BTRFS_VERITY_DESC_ITEM_KEY, 1,
  488. desc, desc_size);
  489. if (ret)
  490. goto out;
  491. /*
  492. * 1 for updating the inode flag
  493. * 1 for deleting the orphan
  494. */
  495. trans = btrfs_start_transaction(root, 2);
  496. if (IS_ERR(trans)) {
  497. ret = PTR_ERR(trans);
  498. goto out;
  499. }
  500. inode->ro_flags |= BTRFS_INODE_RO_VERITY;
  501. btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);
  502. ret = btrfs_update_inode(trans, inode);
  503. if (ret)
  504. goto end_trans;
  505. ret = del_orphan(trans, inode);
  506. if (ret)
  507. goto end_trans;
  508. clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
  509. btrfs_set_fs_compat_ro(root->fs_info, VERITY);
  510. end_trans:
  511. btrfs_end_transaction(trans);
  512. out:
  513. return ret;
  514. }
  515. /*
  516. * fsverity op that begins enabling verity.
  517. *
  518. * @filp: file to enable verity on
  519. *
  520. * Begin enabling fsverity for the file. We drop any existing verity items, add
  521. * an orphan and set the in progress bit.
  522. *
  523. * Returns 0 on success, negative error code on failure.
  524. */
  525. static int btrfs_begin_enable_verity(struct file *filp)
  526. {
  527. struct btrfs_inode *inode = BTRFS_I(file_inode(filp));
  528. struct btrfs_root *root = inode->root;
  529. struct btrfs_trans_handle *trans;
  530. int ret;
  531. btrfs_assert_inode_locked(inode);
  532. if (test_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags))
  533. return -EBUSY;
  534. /*
  535. * This should almost never do anything, but theoretically, it's
  536. * possible that we failed to enable verity on a file, then were
  537. * interrupted or failed while rolling back, failed to cleanup the
  538. * orphan, and finally attempt to enable verity again.
  539. */
  540. ret = btrfs_drop_verity_items(inode);
  541. if (ret)
  542. return ret;
  543. /* 1 for the orphan item */
  544. trans = btrfs_start_transaction(root, 1);
  545. if (IS_ERR(trans))
  546. return PTR_ERR(trans);
  547. ret = btrfs_orphan_add(trans, inode);
  548. if (!ret)
  549. set_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
  550. btrfs_end_transaction(trans);
  551. return 0;
  552. }
  553. /*
  554. * fsverity op that ends enabling verity.
  555. *
  556. * @filp: file we are finishing enabling verity on
  557. * @desc: verity descriptor to write out (NULL in error conditions)
  558. * @desc_size: size of the verity descriptor (variable with signatures)
  559. * @merkle_tree_size: size of the merkle tree in bytes
  560. *
  561. * If desc is null, then VFS is signaling an error occurred during verity
  562. * enable, and we should try to rollback. Otherwise, attempt to finish verity.
  563. *
  564. * Returns 0 on success, negative error code on error.
  565. */
  566. static int btrfs_end_enable_verity(struct file *filp, const void *desc,
  567. size_t desc_size, u64 merkle_tree_size)
  568. {
  569. struct btrfs_inode *inode = BTRFS_I(file_inode(filp));
  570. int ret = 0;
  571. int rollback_ret;
  572. btrfs_assert_inode_locked(inode);
  573. if (desc == NULL)
  574. goto rollback;
  575. ret = finish_verity(inode, desc, desc_size);
  576. if (ret)
  577. goto rollback;
  578. return ret;
  579. rollback:
  580. rollback_ret = rollback_verity(inode);
  581. if (rollback_ret)
  582. btrfs_err(inode->root->fs_info,
  583. "failed to rollback verity items: %d", rollback_ret);
  584. return ret;
  585. }
  586. /*
  587. * fsverity op that gets the struct fsverity_descriptor.
  588. *
  589. * @inode: inode to get the descriptor of
  590. * @buf: output buffer for the descriptor contents
  591. * @buf_size: size of the output buffer. 0 to query the size
  592. *
  593. * fsverity does a two pass setup for reading the descriptor, in the first pass
  594. * it calls with buf_size = 0 to query the size of the descriptor, and then in
  595. * the second pass it actually reads the descriptor off disk.
  596. *
  597. * Returns the size on success or a negative error code on failure.
  598. */
  599. int btrfs_get_verity_descriptor(struct inode *inode, void *buf, size_t buf_size)
  600. {
  601. u64 true_size;
  602. int ret = 0;
  603. struct btrfs_verity_descriptor_item item;
  604. memset(&item, 0, sizeof(item));
  605. ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_DESC_ITEM_KEY, 0,
  606. (char *)&item, sizeof(item), NULL);
  607. if (ret < 0)
  608. return ret;
  609. if (item.reserved[0] != 0 || item.reserved[1] != 0)
  610. return -EUCLEAN;
  611. true_size = btrfs_stack_verity_descriptor_size(&item);
  612. if (true_size > INT_MAX)
  613. return -EUCLEAN;
  614. if (buf_size == 0)
  615. return true_size;
  616. if (buf_size < true_size)
  617. return -ERANGE;
  618. ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_DESC_ITEM_KEY, 1,
  619. buf, buf_size, NULL);
  620. if (ret < 0)
  621. return ret;
  622. if (ret != true_size)
  623. return -EIO;
  624. return true_size;
  625. }
  626. /*
  627. * fsverity op that reads and caches a merkle tree page.
  628. *
  629. * @inode: inode to read a merkle tree page for
  630. * @index: page index relative to the start of the merkle tree
  631. * @num_ra_pages: number of pages to readahead. Optional, we ignore it
  632. *
  633. * The Merkle tree is stored in the filesystem btree, but its pages are cached
  634. * with a logical position past EOF in the inode's mapping.
  635. *
  636. * Returns the page we read, or an ERR_PTR on error.
  637. */
  638. static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
  639. pgoff_t index,
  640. unsigned long num_ra_pages)
  641. {
  642. struct folio *folio;
  643. u64 off = (u64)index << PAGE_SHIFT;
  644. loff_t merkle_pos = merkle_file_pos(inode);
  645. int ret;
  646. if (merkle_pos < 0)
  647. return ERR_PTR(merkle_pos);
  648. if (merkle_pos > inode->i_sb->s_maxbytes - off - PAGE_SIZE)
  649. return ERR_PTR(-EFBIG);
  650. index += merkle_pos >> PAGE_SHIFT;
  651. again:
  652. folio = __filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0);
  653. if (!IS_ERR(folio)) {
  654. if (folio_test_uptodate(folio))
  655. goto out;
  656. folio_lock(folio);
  657. /* If it's not uptodate after we have the lock, we got a read error. */
  658. if (!folio_test_uptodate(folio)) {
  659. folio_unlock(folio);
  660. folio_put(folio);
  661. return ERR_PTR(-EIO);
  662. }
  663. folio_unlock(folio);
  664. goto out;
  665. }
  666. folio = filemap_alloc_folio(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS),
  667. 0);
  668. if (!folio)
  669. return ERR_PTR(-ENOMEM);
  670. ret = filemap_add_folio(inode->i_mapping, folio, index, GFP_NOFS);
  671. if (ret) {
  672. folio_put(folio);
  673. /* Did someone else insert a folio here? */
  674. if (ret == -EEXIST)
  675. goto again;
  676. return ERR_PTR(ret);
  677. }
  678. /*
  679. * Merkle item keys are indexed from byte 0 in the merkle tree.
  680. * They have the form:
  681. *
  682. * [ inode objectid, BTRFS_MERKLE_ITEM_KEY, offset in bytes ]
  683. */
  684. ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY, off,
  685. folio_address(folio), PAGE_SIZE, folio);
  686. if (ret < 0) {
  687. folio_put(folio);
  688. return ERR_PTR(ret);
  689. }
  690. if (ret < PAGE_SIZE)
  691. folio_zero_segment(folio, ret, PAGE_SIZE);
  692. folio_mark_uptodate(folio);
  693. folio_unlock(folio);
  694. out:
  695. return folio_file_page(folio, index);
  696. }
  697. /*
  698. * fsverity op that writes a Merkle tree block into the btree.
  699. *
  700. * @inode: inode to write a Merkle tree block for
  701. * @buf: Merkle tree block to write
  702. * @pos: the position of the block in the Merkle tree (in bytes)
  703. * @size: the Merkle tree block size (in bytes)
  704. *
  705. * Returns 0 on success or negative error code on failure
  706. */
  707. static int btrfs_write_merkle_tree_block(struct inode *inode, const void *buf,
  708. u64 pos, unsigned int size)
  709. {
  710. loff_t merkle_pos = merkle_file_pos(inode);
  711. if (merkle_pos < 0)
  712. return merkle_pos;
  713. if (merkle_pos > inode->i_sb->s_maxbytes - pos - size)
  714. return -EFBIG;
  715. return write_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY,
  716. pos, buf, size);
  717. }
  718. const struct fsverity_operations btrfs_verityops = {
  719. .begin_enable_verity = btrfs_begin_enable_verity,
  720. .end_enable_verity = btrfs_end_enable_verity,
  721. .get_verity_descriptor = btrfs_get_verity_descriptor,
  722. .read_merkle_tree_page = btrfs_read_merkle_tree_page,
  723. .write_merkle_tree_block = btrfs_write_merkle_tree_block,
  724. };