open.c 12 KB

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