fsverity_private.h 5.3 KB

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