enable.c 12 KB

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