enable.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ioctl to enable verity on a file
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. #include "fsverity_private.h"
  8. #include <crypto/hash.h>
  9. #include <linux/mount.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/uaccess.h>
  12. struct block_buffer {
  13. u32 filled;
  14. bool is_root_hash;
  15. u8 *data;
  16. };
  17. /* Hash a block, writing the result to the next level's pending block buffer. */
  18. static int hash_one_block(struct inode *inode,
  19. const struct merkle_tree_params *params,
  20. struct block_buffer *cur)
  21. {
  22. struct block_buffer *next = cur + 1;
  23. int err;
  24. /*
  25. * Safety check to prevent a buffer overflow in case of a filesystem bug
  26. * that allows the file size to change despite deny_write_access(), or a
  27. * bug in the Merkle tree logic itself
  28. */
  29. if (WARN_ON_ONCE(next->is_root_hash && next->filled != 0))
  30. return -EINVAL;
  31. /* Zero-pad the block if it's shorter than the block size. */
  32. memset(&cur->data[cur->filled], 0, params->block_size - cur->filled);
  33. err = fsverity_hash_block(params, inode, cur->data,
  34. &next->data[next->filled]);
  35. if (err)
  36. return err;
  37. next->filled += params->digest_size;
  38. cur->filled = 0;
  39. return 0;
  40. }
  41. static int write_merkle_tree_block(struct inode *inode, const u8 *buf,
  42. unsigned long index,
  43. const struct merkle_tree_params *params)
  44. {
  45. u64 pos = (u64)index << params->log_blocksize;
  46. int err;
  47. err = inode->i_sb->s_vop->write_merkle_tree_block(inode, buf, pos,
  48. params->block_size);
  49. if (err)
  50. fsverity_err(inode, "Error %d writing Merkle tree block %lu",
  51. err, index);
  52. return err;
  53. }
  54. /*
  55. * Build the Merkle tree for the given file using the given parameters, and
  56. * return the root hash in @root_hash.
  57. *
  58. * The tree is written to a filesystem-specific location as determined by the
  59. * ->write_merkle_tree_block() method. However, the blocks that comprise the
  60. * tree are the same for all filesystems.
  61. */
  62. static int build_merkle_tree(struct file *filp,
  63. const struct merkle_tree_params *params,
  64. u8 *root_hash)
  65. {
  66. struct inode *inode = file_inode(filp);
  67. const u64 data_size = inode->i_size;
  68. const int num_levels = params->num_levels;
  69. struct block_buffer _buffers[1 + FS_VERITY_MAX_LEVELS + 1] = {};
  70. struct block_buffer *buffers = &_buffers[1];
  71. unsigned long level_offset[FS_VERITY_MAX_LEVELS];
  72. int level;
  73. u64 offset;
  74. int err;
  75. if (data_size == 0) {
  76. /* Empty file is a special case; root hash is all 0's */
  77. memset(root_hash, 0, params->digest_size);
  78. return 0;
  79. }
  80. /*
  81. * Allocate the block buffers. Buffer "-1" is for data blocks.
  82. * Buffers 0 <= level < num_levels are for the actual tree levels.
  83. * Buffer 'num_levels' is for the root hash.
  84. */
  85. for (level = -1; level < num_levels; level++) {
  86. buffers[level].data = kzalloc(params->block_size, GFP_KERNEL);
  87. if (!buffers[level].data) {
  88. err = -ENOMEM;
  89. goto out;
  90. }
  91. }
  92. buffers[num_levels].data = root_hash;
  93. buffers[num_levels].is_root_hash = true;
  94. BUILD_BUG_ON(sizeof(level_offset) != sizeof(params->level_start));
  95. memcpy(level_offset, params->level_start, sizeof(level_offset));
  96. /* Hash each data block, also hashing the tree blocks as they fill up */
  97. for (offset = 0; offset < data_size; offset += params->block_size) {
  98. ssize_t bytes_read;
  99. loff_t pos = offset;
  100. buffers[-1].filled = min_t(u64, params->block_size,
  101. data_size - offset);
  102. bytes_read = __kernel_read(filp, buffers[-1].data,
  103. buffers[-1].filled, &pos);
  104. if (bytes_read < 0) {
  105. err = bytes_read;
  106. fsverity_err(inode, "Error %d reading file data", err);
  107. goto out;
  108. }
  109. if (bytes_read != buffers[-1].filled) {
  110. err = -EINVAL;
  111. fsverity_err(inode, "Short read of file data");
  112. goto out;
  113. }
  114. err = hash_one_block(inode, params, &buffers[-1]);
  115. if (err)
  116. goto out;
  117. for (level = 0; level < num_levels; level++) {
  118. if (buffers[level].filled + params->digest_size <=
  119. params->block_size) {
  120. /* Next block at @level isn't full yet */
  121. break;
  122. }
  123. /* Next block at @level is full */
  124. err = hash_one_block(inode, params, &buffers[level]);
  125. if (err)
  126. goto out;
  127. err = write_merkle_tree_block(inode,
  128. buffers[level].data,
  129. level_offset[level],
  130. params);
  131. if (err)
  132. goto out;
  133. level_offset[level]++;
  134. }
  135. if (fatal_signal_pending(current)) {
  136. err = -EINTR;
  137. goto out;
  138. }
  139. cond_resched();
  140. }
  141. /* Finish all nonempty pending tree blocks. */
  142. for (level = 0; level < num_levels; level++) {
  143. if (buffers[level].filled != 0) {
  144. err = hash_one_block(inode, params, &buffers[level]);
  145. if (err)
  146. goto out;
  147. err = write_merkle_tree_block(inode,
  148. buffers[level].data,
  149. level_offset[level],
  150. params);
  151. if (err)
  152. goto out;
  153. }
  154. }
  155. /* The root hash was filled by the last call to hash_one_block(). */
  156. if (WARN_ON_ONCE(buffers[num_levels].filled != params->digest_size)) {
  157. err = -EINVAL;
  158. goto out;
  159. }
  160. err = 0;
  161. out:
  162. for (level = -1; level < num_levels; level++)
  163. kfree(buffers[level].data);
  164. return err;
  165. }
  166. static int enable_verity(struct file *filp,
  167. const struct fsverity_enable_arg *arg)
  168. {
  169. struct inode *inode = file_inode(filp);
  170. const struct fsverity_operations *vops = inode->i_sb->s_vop;
  171. struct merkle_tree_params params = { };
  172. struct fsverity_descriptor *desc;
  173. size_t desc_size = struct_size(desc, signature, arg->sig_size);
  174. struct fsverity_info *vi;
  175. int err;
  176. /* Start initializing the fsverity_descriptor */
  177. desc = kzalloc(desc_size, GFP_KERNEL);
  178. if (!desc)
  179. return -ENOMEM;
  180. desc->version = 1;
  181. desc->hash_algorithm = arg->hash_algorithm;
  182. desc->log_blocksize = ilog2(arg->block_size);
  183. /* Get the salt if the user provided one */
  184. if (arg->salt_size &&
  185. copy_from_user(desc->salt, u64_to_user_ptr(arg->salt_ptr),
  186. arg->salt_size)) {
  187. err = -EFAULT;
  188. goto out;
  189. }
  190. desc->salt_size = arg->salt_size;
  191. /* Get the builtin signature if the user provided one */
  192. if (arg->sig_size &&
  193. copy_from_user(desc->signature, u64_to_user_ptr(arg->sig_ptr),
  194. arg->sig_size)) {
  195. err = -EFAULT;
  196. goto out;
  197. }
  198. desc->sig_size = cpu_to_le32(arg->sig_size);
  199. desc->data_size = cpu_to_le64(inode->i_size);
  200. /* Prepare the Merkle tree parameters */
  201. err = fsverity_init_merkle_tree_params(&params, inode,
  202. arg->hash_algorithm,
  203. desc->log_blocksize,
  204. desc->salt, desc->salt_size);
  205. if (err)
  206. goto out;
  207. /*
  208. * Start enabling verity on this file, serialized by the inode lock.
  209. * Fail if verity is already enabled or is already being enabled.
  210. */
  211. inode_lock(inode);
  212. if (IS_VERITY(inode))
  213. err = -EEXIST;
  214. else
  215. err = vops->begin_enable_verity(filp);
  216. inode_unlock(inode);
  217. if (err)
  218. goto out;
  219. /*
  220. * Build the Merkle tree. Don't hold the inode lock during this, since
  221. * on huge files this may take a very long time and we don't want to
  222. * force unrelated syscalls like chown() to block forever. We don't
  223. * need the inode lock here because deny_write_access() already prevents
  224. * the file from being written to or truncated, and we still serialize
  225. * ->begin_enable_verity() and ->end_enable_verity() using the inode
  226. * lock and only allow one process to be here at a time on a given file.
  227. */
  228. BUILD_BUG_ON(sizeof(desc->root_hash) < FS_VERITY_MAX_DIGEST_SIZE);
  229. err = build_merkle_tree(filp, &params, desc->root_hash);
  230. if (err) {
  231. fsverity_err(inode, "Error %d building Merkle tree", err);
  232. goto rollback;
  233. }
  234. /*
  235. * Create the fsverity_info. Don't bother trying to save work by
  236. * reusing the merkle_tree_params from above. Instead, just create the
  237. * fsverity_info from the fsverity_descriptor as if it were just loaded
  238. * from disk. This is simpler, and it serves as an extra check that the
  239. * metadata we're writing is valid before actually enabling verity.
  240. */
  241. vi = fsverity_create_info(inode, desc);
  242. if (IS_ERR(vi)) {
  243. err = PTR_ERR(vi);
  244. goto rollback;
  245. }
  246. /*
  247. * Tell the filesystem to finish enabling verity on the file.
  248. * Serialized with ->begin_enable_verity() by the inode lock.
  249. */
  250. inode_lock(inode);
  251. err = vops->end_enable_verity(filp, desc, desc_size, params.tree_size);
  252. inode_unlock(inode);
  253. if (err) {
  254. fsverity_err(inode, "%ps() failed with err %d",
  255. vops->end_enable_verity, err);
  256. fsverity_free_info(vi);
  257. } else if (WARN_ON_ONCE(!IS_VERITY(inode))) {
  258. err = -EINVAL;
  259. fsverity_free_info(vi);
  260. } else {
  261. /* Successfully enabled verity */
  262. /*
  263. * Readers can start using ->i_verity_info immediately, so it
  264. * can't be rolled back once set. So don't set it until just
  265. * after the filesystem has successfully enabled verity.
  266. */
  267. fsverity_set_info(inode, vi);
  268. }
  269. out:
  270. kfree(params.hashstate);
  271. kfree(desc);
  272. return err;
  273. rollback:
  274. inode_lock(inode);
  275. (void)vops->end_enable_verity(filp, NULL, 0, params.tree_size);
  276. inode_unlock(inode);
  277. goto out;
  278. }
  279. /**
  280. * fsverity_ioctl_enable() - enable verity on a file
  281. * @filp: file to enable verity on
  282. * @uarg: user pointer to fsverity_enable_arg
  283. *
  284. * Enable fs-verity on a file. See the "FS_IOC_ENABLE_VERITY" section of
  285. * Documentation/filesystems/fsverity.rst for the documentation.
  286. *
  287. * Return: 0 on success, -errno on failure
  288. */
  289. int fsverity_ioctl_enable(struct file *filp, const void __user *uarg)
  290. {
  291. struct inode *inode = file_inode(filp);
  292. struct fsverity_enable_arg arg;
  293. int err;
  294. if (copy_from_user(&arg, uarg, sizeof(arg)))
  295. return -EFAULT;
  296. if (arg.version != 1)
  297. return -EINVAL;
  298. if (arg.__reserved1 ||
  299. memchr_inv(arg.__reserved2, 0, sizeof(arg.__reserved2)))
  300. return -EINVAL;
  301. if (!is_power_of_2(arg.block_size))
  302. return -EINVAL;
  303. if (arg.salt_size > sizeof_field(struct fsverity_descriptor, salt))
  304. return -EMSGSIZE;
  305. if (arg.sig_size > FS_VERITY_MAX_SIGNATURE_SIZE)
  306. return -EMSGSIZE;
  307. /*
  308. * Require a regular file with write access. But the actual fd must
  309. * still be readonly so that we can lock out all writers. This is
  310. * needed to guarantee that no writable fds exist to the file once it
  311. * has verity enabled, and to stabilize the data being hashed.
  312. */
  313. err = file_permission(filp, MAY_WRITE);
  314. if (err)
  315. return err;
  316. /*
  317. * __kernel_read() is used while building the Merkle tree. So, we can't
  318. * allow file descriptors that were opened for ioctl access only, using
  319. * the special nonstandard access mode 3. O_RDONLY only, please!
  320. */
  321. if (!(filp->f_mode & FMODE_READ))
  322. return -EBADF;
  323. if (IS_APPEND(inode))
  324. return -EPERM;
  325. if (S_ISDIR(inode->i_mode))
  326. return -EISDIR;
  327. if (!S_ISREG(inode->i_mode))
  328. return -EINVAL;
  329. err = mnt_want_write_file(filp);
  330. if (err) /* -EROFS */
  331. return err;
  332. err = deny_write_access(filp);
  333. if (err) /* -ETXTBSY */
  334. goto out_drop_write;
  335. err = enable_verity(filp, &arg);
  336. /*
  337. * We no longer drop the inode's pagecache after enabling verity. This
  338. * used to be done to try to avoid a race condition where pages could be
  339. * evicted after being used in the Merkle tree construction, then
  340. * re-instantiated by a concurrent read. Such pages are unverified, and
  341. * the backing storage could have filled them with different content, so
  342. * they shouldn't be used to fulfill reads once verity is enabled.
  343. *
  344. * But, dropping the pagecache has a big performance impact, and it
  345. * doesn't fully solve the race condition anyway. So for those reasons,
  346. * and also because this race condition isn't very important relatively
  347. * speaking (especially for small-ish files, where the chance of a page
  348. * being used, evicted, *and* re-instantiated all while enabling verity
  349. * is quite small), we no longer drop the inode's pagecache.
  350. */
  351. /*
  352. * allow_write_access() is needed to pair with deny_write_access().
  353. * Regardless, the filesystem won't allow writing to verity files.
  354. */
  355. allow_write_access(filp);
  356. out_drop_write:
  357. mnt_drop_write_file(filp);
  358. return err;
  359. }
  360. EXPORT_SYMBOL_GPL(fsverity_ioctl_enable);