Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * fs-verity: read-only file-based authenticity protection
0004  *
0005  * Copyright 2019 Google LLC
0006  */
0007 
0008 #ifndef _FSVERITY_PRIVATE_H
0009 #define _FSVERITY_PRIVATE_H
0010 
0011 #ifdef CONFIG_FS_VERITY_DEBUG
0012 #define DEBUG
0013 #endif
0014 
0015 #define pr_fmt(fmt) "fs-verity: " fmt
0016 
0017 #include <linux/fsverity.h>
0018 #include <linux/mempool.h>
0019 
0020 struct ahash_request;
0021 
0022 /*
0023  * Implementation limit: maximum depth of the Merkle tree.  For now 8 is plenty;
0024  * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
0025  */
0026 #define FS_VERITY_MAX_LEVELS        8
0027 
0028 /* A hash algorithm supported by fs-verity */
0029 struct fsverity_hash_alg {
0030     struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
0031     const char *name;     /* crypto API name, e.g. sha256 */
0032     unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
0033     unsigned int block_size;  /* block size in bytes, e.g. 64 for SHA-256 */
0034     mempool_t req_pool;   /* mempool with a preallocated hash request */
0035 };
0036 
0037 /* Merkle tree parameters: hash algorithm, initial hash state, and topology */
0038 struct merkle_tree_params {
0039     struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
0040     const u8 *hashstate;        /* initial hash state or NULL */
0041     unsigned int digest_size;   /* same as hash_alg->digest_size */
0042     unsigned int block_size;    /* size of data and tree blocks */
0043     unsigned int hashes_per_block;  /* number of hashes per tree block */
0044     unsigned int log_blocksize; /* log2(block_size) */
0045     unsigned int log_arity;     /* log2(hashes_per_block) */
0046     unsigned int num_levels;    /* number of levels in Merkle tree */
0047     u64 tree_size;          /* Merkle tree size in bytes */
0048     unsigned long level0_blocks;    /* number of blocks in tree level 0 */
0049 
0050     /*
0051      * Starting block index for each tree level, ordered from leaf level (0)
0052      * to root level ('num_levels - 1')
0053      */
0054     u64 level_start[FS_VERITY_MAX_LEVELS];
0055 };
0056 
0057 /*
0058  * fsverity_info - cached verity metadata for an inode
0059  *
0060  * When a verity file is first opened, an instance of this struct is allocated
0061  * and stored in ->i_verity_info; it remains until the inode is evicted.  It
0062  * caches information about the Merkle tree that's needed to efficiently verify
0063  * data read from the file.  It also caches the file digest.  The Merkle tree
0064  * pages themselves are not cached here, but the filesystem may cache them.
0065  */
0066 struct fsverity_info {
0067     struct merkle_tree_params tree_params;
0068     u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
0069     u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE];
0070     const struct inode *inode;
0071 };
0072 
0073 /* Arbitrary limit to bound the kmalloc() size.  Can be changed. */
0074 #define FS_VERITY_MAX_DESCRIPTOR_SIZE   16384
0075 
0076 #define FS_VERITY_MAX_SIGNATURE_SIZE    (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
0077                      sizeof(struct fsverity_descriptor))
0078 
0079 /* hash_algs.c */
0080 
0081 extern struct fsverity_hash_alg fsverity_hash_algs[];
0082 
0083 struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
0084                         unsigned int num);
0085 struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg,
0086                           gfp_t gfp_flags);
0087 void fsverity_free_hash_request(struct fsverity_hash_alg *alg,
0088                 struct ahash_request *req);
0089 const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,
0090                       const u8 *salt, size_t salt_size);
0091 int fsverity_hash_page(const struct merkle_tree_params *params,
0092                const struct inode *inode,
0093                struct ahash_request *req, struct page *page, u8 *out);
0094 int fsverity_hash_buffer(struct fsverity_hash_alg *alg,
0095              const void *data, size_t size, u8 *out);
0096 void __init fsverity_check_hash_algs(void);
0097 
0098 /* init.c */
0099 
0100 void __printf(3, 4) __cold
0101 fsverity_msg(const struct inode *inode, const char *level,
0102          const char *fmt, ...);
0103 
0104 #define fsverity_warn(inode, fmt, ...)      \
0105     fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
0106 #define fsverity_err(inode, fmt, ...)       \
0107     fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
0108 
0109 /* open.c */
0110 
0111 int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
0112                      const struct inode *inode,
0113                      unsigned int hash_algorithm,
0114                      unsigned int log_blocksize,
0115                      const u8 *salt, size_t salt_size);
0116 
0117 struct fsverity_info *fsverity_create_info(const struct inode *inode,
0118                        struct fsverity_descriptor *desc);
0119 
0120 void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
0121 
0122 void fsverity_free_info(struct fsverity_info *vi);
0123 
0124 int fsverity_get_descriptor(struct inode *inode,
0125                 struct fsverity_descriptor **desc_ret);
0126 
0127 int __init fsverity_init_info_cache(void);
0128 void __init fsverity_exit_info_cache(void);
0129 
0130 /* signature.c */
0131 
0132 #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
0133 int fsverity_verify_signature(const struct fsverity_info *vi,
0134                   const u8 *signature, size_t sig_size);
0135 
0136 int __init fsverity_init_signature(void);
0137 #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
0138 static inline int
0139 fsverity_verify_signature(const struct fsverity_info *vi,
0140               const u8 *signature, size_t sig_size)
0141 {
0142     return 0;
0143 }
0144 
0145 static inline int fsverity_init_signature(void)
0146 {
0147     return 0;
0148 }
0149 #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
0150 
0151 /* verify.c */
0152 
0153 int __init fsverity_init_workqueue(void);
0154 void __init fsverity_exit_workqueue(void);
0155 
0156 #endif /* _FSVERITY_PRIVATE_H */