open.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/mm.h>
  9. #include <linux/slab.h>
  10. static struct kmem_cache *fsverity_info_cachep;
  11. /**
  12. * fsverity_init_merkle_tree_params() - initialize Merkle tree parameters
  13. * @params: the parameters struct to initialize
  14. * @inode: the inode for which the Merkle tree is being built
  15. * @hash_algorithm: number of hash algorithm to use
  16. * @log_blocksize: log base 2 of block size to use
  17. * @salt: pointer to salt (optional)
  18. * @salt_size: size of salt, possibly 0
  19. *
  20. * Validate the hash algorithm and block size, then compute the tree topology
  21. * (num levels, num blocks in each level, etc.) and initialize @params.
  22. *
  23. * Return: 0 on success, -errno on failure
  24. */
  25. int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
  26. const struct inode *inode,
  27. unsigned int hash_algorithm,
  28. unsigned int log_blocksize,
  29. const u8 *salt, size_t salt_size)
  30. {
  31. const struct fsverity_hash_alg *hash_alg;
  32. int err;
  33. u64 blocks;
  34. u64 blocks_in_level[FS_VERITY_MAX_LEVELS];
  35. u64 offset;
  36. int level;
  37. memset(params, 0, sizeof(*params));
  38. hash_alg = fsverity_get_hash_alg(inode, hash_algorithm);
  39. if (IS_ERR(hash_alg))
  40. return PTR_ERR(hash_alg);
  41. params->hash_alg = hash_alg;
  42. params->digest_size = hash_alg->digest_size;
  43. params->hashstate = fsverity_prepare_hash_state(hash_alg, salt,
  44. salt_size);
  45. if (IS_ERR(params->hashstate)) {
  46. err = PTR_ERR(params->hashstate);
  47. params->hashstate = NULL;
  48. fsverity_err(inode, "Error %d preparing hash state", err);
  49. goto out_err;
  50. }
  51. /*
  52. * fs/verity/ directly assumes that the Merkle tree block size is a
  53. * power of 2 less than or equal to PAGE_SIZE. Another restriction
  54. * arises from the interaction between fs/verity/ and the filesystems
  55. * themselves: filesystems expect to be able to verify a single
  56. * filesystem block of data at a time. Therefore, the Merkle tree block
  57. * size must also be less than or equal to the filesystem block size.
  58. *
  59. * The above are the only hard limitations, so in theory the Merkle tree
  60. * block size could be as small as twice the digest size. However,
  61. * that's not useful, and it would result in some unusually deep and
  62. * large Merkle trees. So we currently require that the Merkle tree
  63. * block size be at least 1024 bytes. That's small enough to test the
  64. * sub-page block case on systems with 4K pages, but not too small.
  65. */
  66. if (log_blocksize < 10 || log_blocksize > PAGE_SHIFT ||
  67. log_blocksize > inode->i_blkbits) {
  68. fsverity_warn(inode, "Unsupported log_blocksize: %u",
  69. log_blocksize);
  70. err = -EINVAL;
  71. goto out_err;
  72. }
  73. params->log_blocksize = log_blocksize;
  74. params->block_size = 1 << log_blocksize;
  75. params->log_blocks_per_page = PAGE_SHIFT - log_blocksize;
  76. params->blocks_per_page = 1 << params->log_blocks_per_page;
  77. if (WARN_ON_ONCE(!is_power_of_2(params->digest_size))) {
  78. err = -EINVAL;
  79. goto out_err;
  80. }
  81. if (params->block_size < 2 * params->digest_size) {
  82. fsverity_warn(inode,
  83. "Merkle tree block size (%u) too small for hash algorithm \"%s\"",
  84. params->block_size, hash_alg->name);
  85. err = -EINVAL;
  86. goto out_err;
  87. }
  88. params->log_digestsize = ilog2(params->digest_size);
  89. params->log_arity = log_blocksize - params->log_digestsize;
  90. params->hashes_per_block = 1 << params->log_arity;
  91. /*
  92. * Compute the number of levels in the Merkle tree and create a map from
  93. * level to the starting block of that level. Level 'num_levels - 1' is
  94. * the root and is stored first. Level 0 is the level directly "above"
  95. * the data blocks and is stored last.
  96. */
  97. /* Compute number of levels and the number of blocks in each level */
  98. blocks = ((u64)inode->i_size + params->block_size - 1) >> log_blocksize;
  99. while (blocks > 1) {
  100. if (params->num_levels >= FS_VERITY_MAX_LEVELS) {
  101. fsverity_err(inode, "Too many levels in Merkle tree");
  102. err = -EFBIG;
  103. goto out_err;
  104. }
  105. blocks = (blocks + params->hashes_per_block - 1) >>
  106. params->log_arity;
  107. blocks_in_level[params->num_levels++] = blocks;
  108. }
  109. /* Compute the starting block of each level */
  110. offset = 0;
  111. for (level = (int)params->num_levels - 1; level >= 0; level--) {
  112. params->level_start[level] = offset;
  113. offset += blocks_in_level[level];
  114. }
  115. /*
  116. * With block_size != PAGE_SIZE, an in-memory bitmap will need to be
  117. * allocated to track the "verified" status of hash blocks. Don't allow
  118. * this bitmap to get too large. For now, limit it to 1 MiB, which
  119. * limits the file size to about 4.4 TB with SHA-256 and 4K blocks.
  120. *
  121. * Together with the fact that the data, and thus also the Merkle tree,
  122. * cannot have more than ULONG_MAX pages, this implies that hash block
  123. * indices can always fit in an 'unsigned long'. But to be safe, we
  124. * explicitly check for that too. Note, this is only for hash block
  125. * indices; data block indices might not fit in an 'unsigned long'.
  126. */
  127. if ((params->block_size != PAGE_SIZE && offset > 1 << 23) ||
  128. offset > ULONG_MAX) {
  129. fsverity_err(inode, "Too many blocks in Merkle tree");
  130. err = -EFBIG;
  131. goto out_err;
  132. }
  133. params->tree_size = offset << log_blocksize;
  134. params->tree_pages = PAGE_ALIGN(params->tree_size) >> PAGE_SHIFT;
  135. return 0;
  136. out_err:
  137. kfree(params->hashstate);
  138. memset(params, 0, sizeof(*params));
  139. return err;
  140. }
  141. /*
  142. * Compute the file digest by hashing the fsverity_descriptor excluding the
  143. * builtin signature and with the sig_size field set to 0.
  144. */
  145. static int compute_file_digest(const struct fsverity_hash_alg *hash_alg,
  146. struct fsverity_descriptor *desc,
  147. u8 *file_digest)
  148. {
  149. __le32 sig_size = desc->sig_size;
  150. int err;
  151. desc->sig_size = 0;
  152. err = fsverity_hash_buffer(hash_alg, desc, sizeof(*desc), file_digest);
  153. desc->sig_size = sig_size;
  154. return err;
  155. }
  156. /*
  157. * Create a new fsverity_info from the given fsverity_descriptor (with optional
  158. * appended builtin signature), and check the signature if present. The
  159. * fsverity_descriptor must have already undergone basic validation.
  160. */
  161. struct fsverity_info *fsverity_create_info(const struct inode *inode,
  162. struct fsverity_descriptor *desc)
  163. {
  164. struct fsverity_info *vi;
  165. int err;
  166. vi = kmem_cache_zalloc(fsverity_info_cachep, GFP_KERNEL);
  167. if (!vi)
  168. return ERR_PTR(-ENOMEM);
  169. vi->inode = inode;
  170. err = fsverity_init_merkle_tree_params(&vi->tree_params, inode,
  171. desc->hash_algorithm,
  172. desc->log_blocksize,
  173. desc->salt, desc->salt_size);
  174. if (err) {
  175. fsverity_err(inode,
  176. "Error %d initializing Merkle tree parameters",
  177. err);
  178. goto fail;
  179. }
  180. memcpy(vi->root_hash, desc->root_hash, vi->tree_params.digest_size);
  181. err = compute_file_digest(vi->tree_params.hash_alg, desc,
  182. vi->file_digest);
  183. if (err) {
  184. fsverity_err(inode, "Error %d computing file digest", err);
  185. goto fail;
  186. }
  187. err = fsverity_verify_signature(vi, desc->signature,
  188. le32_to_cpu(desc->sig_size));
  189. if (err)
  190. goto fail;
  191. if (vi->tree_params.block_size != PAGE_SIZE) {
  192. /*
  193. * When the Merkle tree block size and page size differ, we use
  194. * a bitmap to keep track of which hash blocks have been
  195. * verified. This bitmap must contain one bit per hash block,
  196. * including alignment to a page boundary at the end.
  197. *
  198. * Eventually, to support extremely large files in an efficient
  199. * way, it might be necessary to make pages of this bitmap
  200. * reclaimable. But for now, simply allocating the whole bitmap
  201. * is a simple solution that works well on the files on which
  202. * fsverity is realistically used. E.g., with SHA-256 and 4K
  203. * blocks, a 100MB file only needs a 24-byte bitmap, and the
  204. * bitmap for any file under 17GB fits in a 4K page.
  205. */
  206. unsigned long num_bits =
  207. vi->tree_params.tree_pages <<
  208. vi->tree_params.log_blocks_per_page;
  209. vi->hash_block_verified = kvcalloc(BITS_TO_LONGS(num_bits),
  210. sizeof(unsigned long),
  211. GFP_KERNEL);
  212. if (!vi->hash_block_verified) {
  213. err = -ENOMEM;
  214. goto fail;
  215. }
  216. }
  217. return vi;
  218. fail:
  219. fsverity_free_info(vi);
  220. return ERR_PTR(err);
  221. }
  222. void fsverity_set_info(struct inode *inode, struct fsverity_info *vi)
  223. {
  224. /*
  225. * Multiple tasks may race to set ->i_verity_info, so use
  226. * cmpxchg_release(). This pairs with the smp_load_acquire() in
  227. * fsverity_get_info(). I.e., here we publish ->i_verity_info with a
  228. * RELEASE barrier so that other tasks can ACQUIRE it.
  229. */
  230. if (cmpxchg_release(&inode->i_verity_info, NULL, vi) != NULL) {
  231. /* Lost the race, so free the fsverity_info we allocated. */
  232. fsverity_free_info(vi);
  233. /*
  234. * Afterwards, the caller may access ->i_verity_info directly,
  235. * so make sure to ACQUIRE the winning fsverity_info.
  236. */
  237. (void)fsverity_get_info(inode);
  238. }
  239. }
  240. void fsverity_free_info(struct fsverity_info *vi)
  241. {
  242. if (!vi)
  243. return;
  244. kfree(vi->tree_params.hashstate);
  245. kvfree(vi->hash_block_verified);
  246. kmem_cache_free(fsverity_info_cachep, vi);
  247. }
  248. static bool validate_fsverity_descriptor(struct inode *inode,
  249. const struct fsverity_descriptor *desc,
  250. size_t desc_size)
  251. {
  252. if (desc_size < sizeof(*desc)) {
  253. fsverity_err(inode, "Unrecognized descriptor size: %zu bytes",
  254. desc_size);
  255. return false;
  256. }
  257. if (desc->version != 1) {
  258. fsverity_err(inode, "Unrecognized descriptor version: %u",
  259. desc->version);
  260. return false;
  261. }
  262. if (memchr_inv(desc->__reserved, 0, sizeof(desc->__reserved))) {
  263. fsverity_err(inode, "Reserved bits set in descriptor");
  264. return false;
  265. }
  266. if (desc->salt_size > sizeof(desc->salt)) {
  267. fsverity_err(inode, "Invalid salt_size: %u", desc->salt_size);
  268. return false;
  269. }
  270. if (le64_to_cpu(desc->data_size) != inode->i_size) {
  271. fsverity_err(inode,
  272. "Wrong data_size: %llu (desc) != %lld (inode)",
  273. le64_to_cpu(desc->data_size), inode->i_size);
  274. return false;
  275. }
  276. if (le32_to_cpu(desc->sig_size) > desc_size - sizeof(*desc)) {
  277. fsverity_err(inode, "Signature overflows verity descriptor");
  278. return false;
  279. }
  280. return true;
  281. }
  282. /*
  283. * Read the inode's fsverity_descriptor (with optional appended builtin
  284. * signature) from the filesystem, and do basic validation of it.
  285. */
  286. int fsverity_get_descriptor(struct inode *inode,
  287. struct fsverity_descriptor **desc_ret)
  288. {
  289. int res;
  290. struct fsverity_descriptor *desc;
  291. res = inode->i_sb->s_vop->get_verity_descriptor(inode, NULL, 0);
  292. if (res < 0) {
  293. fsverity_err(inode,
  294. "Error %d getting verity descriptor size", res);
  295. return res;
  296. }
  297. if (res > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
  298. fsverity_err(inode, "Verity descriptor is too large (%d bytes)",
  299. res);
  300. return -EMSGSIZE;
  301. }
  302. desc = kmalloc(res, GFP_KERNEL);
  303. if (!desc)
  304. return -ENOMEM;
  305. res = inode->i_sb->s_vop->get_verity_descriptor(inode, desc, res);
  306. if (res < 0) {
  307. fsverity_err(inode, "Error %d reading verity descriptor", res);
  308. kfree(desc);
  309. return res;
  310. }
  311. if (!validate_fsverity_descriptor(inode, desc, res)) {
  312. kfree(desc);
  313. return -EINVAL;
  314. }
  315. *desc_ret = desc;
  316. return 0;
  317. }
  318. /* Ensure the inode has an ->i_verity_info */
  319. static int ensure_verity_info(struct inode *inode)
  320. {
  321. struct fsverity_info *vi = fsverity_get_info(inode);
  322. struct fsverity_descriptor *desc;
  323. int err;
  324. if (vi)
  325. return 0;
  326. err = fsverity_get_descriptor(inode, &desc);
  327. if (err)
  328. return err;
  329. vi = fsverity_create_info(inode, desc);
  330. if (IS_ERR(vi)) {
  331. err = PTR_ERR(vi);
  332. goto out_free_desc;
  333. }
  334. fsverity_set_info(inode, vi);
  335. err = 0;
  336. out_free_desc:
  337. kfree(desc);
  338. return err;
  339. }
  340. int __fsverity_file_open(struct inode *inode, struct file *filp)
  341. {
  342. if (filp->f_mode & FMODE_WRITE)
  343. return -EPERM;
  344. return ensure_verity_info(inode);
  345. }
  346. EXPORT_SYMBOL_GPL(__fsverity_file_open);
  347. int __fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr)
  348. {
  349. if (attr->ia_valid & ATTR_SIZE)
  350. return -EPERM;
  351. return 0;
  352. }
  353. EXPORT_SYMBOL_GPL(__fsverity_prepare_setattr);
  354. void __fsverity_cleanup_inode(struct inode *inode)
  355. {
  356. fsverity_free_info(inode->i_verity_info);
  357. inode->i_verity_info = NULL;
  358. }
  359. EXPORT_SYMBOL_GPL(__fsverity_cleanup_inode);
  360. void __init fsverity_init_info_cache(void)
  361. {
  362. fsverity_info_cachep = KMEM_CACHE_USERCOPY(
  363. fsverity_info,
  364. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC,
  365. file_digest);
  366. }