fsverity_private.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fs-verity: read-only file-based authenticity protection
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. #ifndef _FSVERITY_PRIVATE_H
  8. #define _FSVERITY_PRIVATE_H
  9. #ifdef CONFIG_FS_VERITY_DEBUG
  10. #define DEBUG
  11. #endif
  12. #define pr_fmt(fmt) "fs-verity: " fmt
  13. #include <crypto/sha.h>
  14. #include <linux/fsverity.h>
  15. #include <linux/mempool.h>
  16. struct ahash_request;
  17. /*
  18. * Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty;
  19. * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
  20. */
  21. #define FS_VERITY_MAX_LEVELS 8
  22. /*
  23. * Largest digest size among all hash algorithms supported by fs-verity.
  24. * Currently assumed to be <= size of fsverity_descriptor::root_hash.
  25. */
  26. #define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
  27. /* A hash algorithm supported by fs-verity */
  28. struct fsverity_hash_alg {
  29. struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
  30. const char *name; /* crypto API name, e.g. sha256 */
  31. unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
  32. unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
  33. mempool_t req_pool; /* mempool with a preallocated hash request */
  34. };
  35. /* Merkle tree parameters: hash algorithm, initial hash state, and topology */
  36. struct merkle_tree_params {
  37. struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
  38. const u8 *hashstate; /* initial hash state or NULL */
  39. unsigned int digest_size; /* same as hash_alg->digest_size */
  40. unsigned int block_size; /* size of data and tree blocks */
  41. unsigned int hashes_per_block; /* number of hashes per tree block */
  42. unsigned int log_blocksize; /* log2(block_size) */
  43. unsigned int log_arity; /* log2(hashes_per_block) */
  44. unsigned int num_levels; /* number of levels in Merkle tree */
  45. u64 tree_size; /* Merkle tree size in bytes */
  46. unsigned long level0_blocks; /* number of blocks in tree level 0 */
  47. /*
  48. * Starting block index for each tree level, ordered from leaf level (0)
  49. * to root level ('num_levels - 1')
  50. */
  51. u64 level_start[FS_VERITY_MAX_LEVELS];
  52. };
  53. /*
  54. * fsverity_info - cached verity metadata for an inode
  55. *
  56. * When a verity file is first opened, an instance of this struct is allocated
  57. * and stored in ->i_verity_info; it remains until the inode is evicted. It
  58. * caches information about the Merkle tree that's needed to efficiently verify
  59. * data read from the file. It also caches the file digest. The Merkle tree
  60. * pages themselves are not cached here, but the filesystem may cache them.
  61. */
  62. struct fsverity_info {
  63. struct merkle_tree_params tree_params;
  64. u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
  65. u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE];
  66. const struct inode *inode;
  67. };
  68. /* Arbitrary limit to bound the kmalloc() size. Can be changed. */
  69. #define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
  70. #define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
  71. sizeof(struct fsverity_descriptor))
  72. /* hash_algs.c */
  73. extern struct fsverity_hash_alg fsverity_hash_algs[];
  74. struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
  75. unsigned int num);
  76. struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg,
  77. gfp_t gfp_flags);
  78. void fsverity_free_hash_request(struct fsverity_hash_alg *alg,
  79. struct ahash_request *req);
  80. const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,
  81. const u8 *salt, size_t salt_size);
  82. int fsverity_hash_page(const struct merkle_tree_params *params,
  83. const struct inode *inode,
  84. struct ahash_request *req, struct page *page, u8 *out);
  85. int fsverity_hash_buffer(struct fsverity_hash_alg *alg,
  86. const void *data, size_t size, u8 *out);
  87. void __init fsverity_check_hash_algs(void);
  88. /* init.c */
  89. void __printf(3, 4) __cold
  90. fsverity_msg(const struct inode *inode, const char *level,
  91. const char *fmt, ...);
  92. #define fsverity_warn(inode, fmt, ...) \
  93. fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
  94. #define fsverity_err(inode, fmt, ...) \
  95. fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
  96. /* open.c */
  97. int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
  98. const struct inode *inode,
  99. unsigned int hash_algorithm,
  100. unsigned int log_blocksize,
  101. const u8 *salt, size_t salt_size);
  102. struct fsverity_info *fsverity_create_info(const struct inode *inode,
  103. struct fsverity_descriptor *desc,
  104. size_t desc_size);
  105. void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
  106. void fsverity_free_info(struct fsverity_info *vi);
  107. int fsverity_get_descriptor(struct inode *inode,
  108. struct fsverity_descriptor **desc_ret,
  109. size_t *desc_size_ret);
  110. int __init fsverity_init_info_cache(void);
  111. void __init fsverity_exit_info_cache(void);
  112. /* signature.c */
  113. #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
  114. int fsverity_verify_signature(const struct fsverity_info *vi,
  115. const u8 *signature, size_t sig_size);
  116. int __init fsverity_init_signature(void);
  117. #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
  118. static inline int
  119. fsverity_verify_signature(const struct fsverity_info *vi,
  120. const u8 *signature, size_t sig_size)
  121. {
  122. return 0;
  123. }
  124. static inline int fsverity_init_signature(void)
  125. {
  126. return 0;
  127. }
  128. #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
  129. /* verify.c */
  130. int __init fsverity_init_workqueue(void);
  131. void __init fsverity_exit_workqueue(void);
  132. #endif /* _FSVERITY_PRIVATE_H */