open.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Opening fs-verity files
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. #include "fsverity_private.h"
  8. #include <linux/slab.h>
  9. static struct kmem_cache *fsverity_info_cachep;
  10. /**
  11. * fsverity_init_merkle_tree_params() - initialize Merkle tree parameters
  12. * @params: the parameters struct to initialize
  13. * @inode: the inode for which the Merkle tree is being built
  14. * @hash_algorithm: number of hash algorithm to use
  15. * @log_blocksize: log base 2 of block size to use
  16. * @salt: pointer to salt (optional)
  17. * @salt_size: size of salt, possibly 0
  18. *
  19. * Validate the hash algorithm and block size, then compute the tree topology
  20. * (num levels, num blocks in each level, etc.) and initialize @params.
  21. *
  22. * Return: 0 on success, -errno on failure
  23. */
  24. int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
  25. const struct inode *inode,
  26. unsigned int hash_algorithm,
  27. unsigned int log_blocksize,
  28. const u8 *salt, size_t salt_size)
  29. {
  30. struct fsverity_hash_alg *hash_alg;
  31. int err;
  32. u64 blocks;
  33. u64 offset;
  34. int level;
  35. memset(params, 0, sizeof(*params));
  36. hash_alg = fsverity_get_hash_alg(inode, hash_algorithm);
  37. if (IS_ERR(hash_alg))
  38. return PTR_ERR(hash_alg);
  39. params->hash_alg = hash_alg;
  40. params->digest_size = hash_alg->digest_size;
  41. params->hashstate = fsverity_prepare_hash_state(hash_alg, salt,
  42. salt_size);
  43. if (IS_ERR(params->hashstate)) {
  44. err = PTR_ERR(params->hashstate);
  45. params->hashstate = NULL;
  46. fsverity_err(inode, "Error %d preparing hash state", err);
  47. goto out_err;
  48. }
  49. if (log_blocksize != PAGE_SHIFT) {
  50. fsverity_warn(inode, "Unsupported log_blocksize: %u",
  51. log_blocksize);
  52. err = -EINVAL;
  53. goto out_err;
  54. }
  55. params->log_blocksize = log_blocksize;
  56. params->block_size = 1 << log_blocksize;
  57. if (WARN_ON(!is_power_of_2(params->digest_size))) {
  58. err = -EINVAL;
  59. goto out_err;
  60. }
  61. if (params->block_size < 2 * params->digest_size) {
  62. fsverity_warn(inode,
  63. "Merkle tree block size (%u) too small for hash algorithm \"%s\"",
  64. params->block_size, hash_alg->name);
  65. err = -EINVAL;
  66. goto out_err;
  67. }
  68. params->log_arity = params->log_blocksize - ilog2(params->digest_size);
  69. params->hashes_per_block = 1 << params->log_arity;
  70. pr_debug("Merkle tree uses %s with %u-byte blocks (%u hashes/block), salt=%*phN\n",
  71. hash_alg->name, params->block_size, params->hashes_per_block,
  72. (int)salt_size, salt);
  73. /*
  74. * Compute the number of levels in the Merkle tree and create a map from
  75. * level to the starting block of that level. Level 'num_levels - 1' is
  76. * the root and is stored first. Level 0 is the level directly "above"
  77. * the data blocks and is stored last.
  78. */
  79. /* Compute number of levels and the number of blocks in each level */
  80. blocks = ((u64)inode->i_size + params->block_size - 1) >> log_blocksize;
  81. pr_debug("Data is %lld bytes (%llu blocks)\n", inode->i_size, blocks);
  82. while (blocks > 1) {
  83. if (params->num_levels >= FS_VERITY_MAX_LEVELS) {
  84. fsverity_err(inode, "Too many levels in Merkle tree");
  85. err = -EINVAL;
  86. goto out_err;
  87. }
  88. blocks = (blocks + params->hashes_per_block - 1) >>
  89. params->log_arity;
  90. /* temporarily using level_start[] to store blocks in level */
  91. params->level_start[params->num_levels++] = blocks;
  92. }
  93. params->level0_blocks = params->level_start[0];
  94. /* Compute the starting block of each level */
  95. offset = 0;
  96. for (level = (int)params->num_levels - 1; level >= 0; level--) {
  97. blocks = params->level_start[level];
  98. params->level_start[level] = offset;
  99. pr_debug("Level %d is %llu blocks starting at index %llu\n",
  100. level, blocks, offset);
  101. offset += blocks;
  102. }
  103. params->tree_size = offset << log_blocksize;
  104. return 0;
  105. out_err:
  106. kfree(params->hashstate);
  107. memset(params, 0, sizeof(*params));
  108. return err;
  109. }
  110. /*
  111. * Compute the file digest by hashing the fsverity_descriptor excluding the
  112. * signature and with the sig_size field set to 0.
  113. */
  114. static int compute_file_digest(struct fsverity_hash_alg *hash_alg,
  115. struct fsverity_descriptor *desc,
  116. u8 *file_digest)
  117. {
  118. __le32 sig_size = desc->sig_size;
  119. int err;
  120. desc->sig_size = 0;
  121. err = fsverity_hash_buffer(hash_alg, desc, sizeof(*desc), file_digest);
  122. desc->sig_size = sig_size;
  123. return err;
  124. }
  125. /*
  126. * Create a new fsverity_info from the given fsverity_descriptor (with optional
  127. * appended signature), and check the signature if present. The
  128. * fsverity_descriptor must have already undergone basic validation.
  129. */
  130. struct fsverity_info *fsverity_create_info(const struct inode *inode,
  131. struct fsverity_descriptor *desc,
  132. size_t desc_size)
  133. {
  134. struct fsverity_info *vi;
  135. int err;
  136. vi = kmem_cache_zalloc(fsverity_info_cachep, GFP_KERNEL);
  137. if (!vi)
  138. return ERR_PTR(-ENOMEM);
  139. vi->inode = inode;
  140. err = fsverity_init_merkle_tree_params(&vi->tree_params, inode,
  141. desc->hash_algorithm,
  142. desc->log_blocksize,
  143. desc->salt, desc->salt_size);
  144. if (err) {
  145. fsverity_err(inode,
  146. "Error %d initializing Merkle tree parameters",
  147. err);
  148. goto out;
  149. }
  150. memcpy(vi->root_hash, desc->root_hash, vi->tree_params.digest_size);
  151. err = compute_file_digest(vi->tree_params.hash_alg, desc,
  152. vi->file_digest);
  153. if (err) {
  154. fsverity_err(inode, "Error %d computing file digest", err);
  155. goto out;
  156. }
  157. pr_debug("Computed file digest: %s:%*phN\n",
  158. vi->tree_params.hash_alg->name,
  159. vi->tree_params.digest_size, vi->file_digest);
  160. err = fsverity_verify_signature(vi, desc->signature,
  161. le32_to_cpu(desc->sig_size));
  162. out:
  163. if (err) {
  164. fsverity_free_info(vi);
  165. vi = ERR_PTR(err);
  166. }
  167. return vi;
  168. }
  169. void fsverity_set_info(struct inode *inode, struct fsverity_info *vi)
  170. {
  171. /*
  172. * Multiple tasks may race to set ->i_verity_info, so use
  173. * cmpxchg_release(). This pairs with the smp_load_acquire() in
  174. * fsverity_get_info(). I.e., here we publish ->i_verity_info with a
  175. * RELEASE barrier so that other tasks can ACQUIRE it.
  176. */
  177. if (cmpxchg_release(&inode->i_verity_info, NULL, vi) != NULL) {
  178. /* Lost the race, so free the fsverity_info we allocated. */
  179. fsverity_free_info(vi);
  180. /*
  181. * Afterwards, the caller may access ->i_verity_info directly,
  182. * so make sure to ACQUIRE the winning fsverity_info.
  183. */
  184. (void)fsverity_get_info(inode);
  185. }
  186. }
  187. void fsverity_free_info(struct fsverity_info *vi)
  188. {
  189. if (!vi)
  190. return;
  191. kfree(vi->tree_params.hashstate);
  192. kmem_cache_free(fsverity_info_cachep, vi);
  193. }
  194. static bool validate_fsverity_descriptor(struct inode *inode,
  195. const struct fsverity_descriptor *desc,
  196. size_t desc_size)
  197. {
  198. if (desc_size < sizeof(*desc)) {
  199. fsverity_err(inode, "Unrecognized descriptor size: %zu bytes",
  200. desc_size);
  201. return false;
  202. }
  203. if (desc->version != 1) {
  204. fsverity_err(inode, "Unrecognized descriptor version: %u",
  205. desc->version);
  206. return false;
  207. }
  208. if (memchr_inv(desc->__reserved, 0, sizeof(desc->__reserved))) {
  209. fsverity_err(inode, "Reserved bits set in descriptor");
  210. return false;
  211. }
  212. if (desc->salt_size > sizeof(desc->salt)) {
  213. fsverity_err(inode, "Invalid salt_size: %u", desc->salt_size);
  214. return false;
  215. }
  216. if (le64_to_cpu(desc->data_size) != inode->i_size) {
  217. fsverity_err(inode,
  218. "Wrong data_size: %llu (desc) != %lld (inode)",
  219. le64_to_cpu(desc->data_size), inode->i_size);
  220. return false;
  221. }
  222. if (le32_to_cpu(desc->sig_size) > desc_size - sizeof(*desc)) {
  223. fsverity_err(inode, "Signature overflows verity descriptor");
  224. return false;
  225. }
  226. return true;
  227. }
  228. /*
  229. * Read the inode's fsverity_descriptor (with optional appended signature) from
  230. * the filesystem, and do basic validation of it.
  231. */
  232. int fsverity_get_descriptor(struct inode *inode,
  233. struct fsverity_descriptor **desc_ret,
  234. size_t *desc_size_ret)
  235. {
  236. int res;
  237. struct fsverity_descriptor *desc;
  238. res = inode->i_sb->s_vop->get_verity_descriptor(inode, NULL, 0);
  239. if (res < 0) {
  240. fsverity_err(inode,
  241. "Error %d getting verity descriptor size", res);
  242. return res;
  243. }
  244. if (res > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
  245. fsverity_err(inode, "Verity descriptor is too large (%d bytes)",
  246. res);
  247. return -EMSGSIZE;
  248. }
  249. desc = kmalloc(res, GFP_KERNEL);
  250. if (!desc)
  251. return -ENOMEM;
  252. res = inode->i_sb->s_vop->get_verity_descriptor(inode, desc, res);
  253. if (res < 0) {
  254. fsverity_err(inode, "Error %d reading verity descriptor", res);
  255. kfree(desc);
  256. return res;
  257. }
  258. if (!validate_fsverity_descriptor(inode, desc, res)) {
  259. kfree(desc);
  260. return -EINVAL;
  261. }
  262. *desc_ret = desc;
  263. *desc_size_ret = res;
  264. return 0;
  265. }
  266. /* Ensure the inode has an ->i_verity_info */
  267. static int ensure_verity_info(struct inode *inode)
  268. {
  269. struct fsverity_info *vi = fsverity_get_info(inode);
  270. struct fsverity_descriptor *desc;
  271. size_t desc_size;
  272. int err;
  273. if (vi)
  274. return 0;
  275. err = fsverity_get_descriptor(inode, &desc, &desc_size);
  276. if (err)
  277. return err;
  278. vi = fsverity_create_info(inode, desc, desc_size);
  279. if (IS_ERR(vi)) {
  280. err = PTR_ERR(vi);
  281. goto out_free_desc;
  282. }
  283. fsverity_set_info(inode, vi);
  284. err = 0;
  285. out_free_desc:
  286. kfree(desc);
  287. return err;
  288. }
  289. /**
  290. * fsverity_file_open() - prepare to open a verity file
  291. * @inode: the inode being opened
  292. * @filp: the struct file being set up
  293. *
  294. * When opening a verity file, deny the open if it is for writing. Otherwise,
  295. * set up the inode's ->i_verity_info if not already done.
  296. *
  297. * When combined with fscrypt, this must be called after fscrypt_file_open().
  298. * Otherwise, we won't have the key set up to decrypt the verity metadata.
  299. *
  300. * Return: 0 on success, -errno on failure
  301. */
  302. int fsverity_file_open(struct inode *inode, struct file *filp)
  303. {
  304. if (!IS_VERITY(inode))
  305. return 0;
  306. if (filp->f_mode & FMODE_WRITE) {
  307. pr_debug("Denying opening verity file (ino %lu) for write\n",
  308. inode->i_ino);
  309. return -EPERM;
  310. }
  311. return ensure_verity_info(inode);
  312. }
  313. EXPORT_SYMBOL_GPL(fsverity_file_open);
  314. /**
  315. * fsverity_prepare_setattr() - prepare to change a verity inode's attributes
  316. * @dentry: dentry through which the inode is being changed
  317. * @attr: attributes to change
  318. *
  319. * Verity files are immutable, so deny truncates. This isn't covered by the
  320. * open-time check because sys_truncate() takes a path, not a file descriptor.
  321. *
  322. * Return: 0 on success, -errno on failure
  323. */
  324. int fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr)
  325. {
  326. if (IS_VERITY(d_inode(dentry)) && (attr->ia_valid & ATTR_SIZE)) {
  327. pr_debug("Denying truncate of verity file (ino %lu)\n",
  328. d_inode(dentry)->i_ino);
  329. return -EPERM;
  330. }
  331. return 0;
  332. }
  333. EXPORT_SYMBOL_GPL(fsverity_prepare_setattr);
  334. /**
  335. * fsverity_cleanup_inode() - free the inode's verity info, if present
  336. * @inode: an inode being evicted
  337. *
  338. * Filesystems must call this on inode eviction to free ->i_verity_info.
  339. */
  340. void fsverity_cleanup_inode(struct inode *inode)
  341. {
  342. fsverity_free_info(inode->i_verity_info);
  343. inode->i_verity_info = NULL;
  344. }
  345. EXPORT_SYMBOL_GPL(fsverity_cleanup_inode);
  346. int __init fsverity_init_info_cache(void)
  347. {
  348. fsverity_info_cachep = KMEM_CACHE_USERCOPY(fsverity_info,
  349. SLAB_RECLAIM_ACCOUNT,
  350. file_digest);
  351. if (!fsverity_info_cachep)
  352. return -ENOMEM;
  353. return 0;
  354. }
  355. void __init fsverity_exit_info_cache(void)
  356. {
  357. kmem_cache_destroy(fsverity_info_cachep);
  358. fsverity_info_cachep = NULL;
  359. }