2019-07-22 16:26:21 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/*
|
|
|
|
* fs-verity: read-only file-based authenticity protection
|
|
|
|
*
|
|
|
|
* Copyright 2019 Google LLC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _FSVERITY_PRIVATE_H
|
|
|
|
#define _FSVERITY_PRIVATE_H
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_VERITY_DEBUG
|
|
|
|
#define DEBUG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) "fs-verity: " fmt
|
|
|
|
|
|
|
|
#include <crypto/sha.h>
|
2019-07-22 16:26:22 +00:00
|
|
|
#include <linux/fsverity.h>
|
2019-12-31 17:55:45 +00:00
|
|
|
#include <linux/mempool.h>
|
2019-07-22 16:26:21 +00:00
|
|
|
|
|
|
|
struct ahash_request;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty;
|
|
|
|
* it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
|
|
|
|
*/
|
|
|
|
#define FS_VERITY_MAX_LEVELS 8
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Largest digest size among all hash algorithms supported by fs-verity.
|
|
|
|
* Currently assumed to be <= size of fsverity_descriptor::root_hash.
|
|
|
|
*/
|
2019-07-22 16:26:23 +00:00
|
|
|
#define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
|
2019-07-22 16:26:21 +00:00
|
|
|
|
|
|
|
/* A hash algorithm supported by fs-verity */
|
|
|
|
struct fsverity_hash_alg {
|
|
|
|
struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
|
|
|
|
const char *name; /* crypto API name, e.g. sha256 */
|
|
|
|
unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
|
|
|
|
unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
|
2019-12-31 17:55:45 +00:00
|
|
|
mempool_t req_pool; /* mempool with a preallocated hash request */
|
2019-07-22 16:26:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Merkle tree parameters: hash algorithm, initial hash state, and topology */
|
|
|
|
struct merkle_tree_params {
|
2019-12-31 17:55:45 +00:00
|
|
|
struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
|
2019-07-22 16:26:21 +00:00
|
|
|
const u8 *hashstate; /* initial hash state or NULL */
|
|
|
|
unsigned int digest_size; /* same as hash_alg->digest_size */
|
|
|
|
unsigned int block_size; /* size of data and tree blocks */
|
|
|
|
unsigned int hashes_per_block; /* number of hashes per tree block */
|
|
|
|
unsigned int log_blocksize; /* log2(block_size) */
|
|
|
|
unsigned int log_arity; /* log2(hashes_per_block) */
|
|
|
|
unsigned int num_levels; /* number of levels in Merkle tree */
|
|
|
|
u64 tree_size; /* Merkle tree size in bytes */
|
fs-verity: implement readahead of Merkle tree pages
When fs-verity verifies data pages, currently it reads each Merkle tree
page synchronously using read_mapping_page().
Therefore, when the Merkle tree pages aren't already cached, fs-verity
causes an extra 4 KiB I/O request for every 512 KiB of data (assuming
that the Merkle tree uses SHA-256 and 4 KiB blocks). This results in
more I/O requests and performance loss than is strictly necessary.
Therefore, implement readahead of the Merkle tree pages.
For simplicity, we take advantage of the fact that the kernel already
does readahead of the file's *data*, just like it does for any other
file. Due to this, we don't really need a separate readahead state
(struct file_ra_state) just for the Merkle tree, but rather we just need
to piggy-back on the existing data readahead requests.
We also only really need to bother with the first level of the Merkle
tree, since the usual fan-out factor is 128, so normally over 99% of
Merkle tree I/O requests are for the first level.
Therefore, make fsverity_verify_bio() enable readahead of the first
Merkle tree level, for up to 1/4 the number of pages in the bio, when it
sees that the REQ_RAHEAD flag is set on the bio. The readahead size is
then passed down to ->read_merkle_tree_page() for the filesystem to
(optionally) implement if it sees that the requested page is uncached.
While we're at it, also make build_merkle_tree_level() set the Merkle
tree readahead size, since it's easy to do there.
However, for now don't set the readahead size in fsverity_verify_page(),
since currently it's only used to verify holes on ext4 and f2fs, and it
would need parameters added to know how much to read ahead.
This patch significantly improves fs-verity sequential read performance.
Some quick benchmarks with 'cat'-ing a 250MB file after dropping caches:
On an ARM64 phone (using sha256-ce):
Before: 217 MB/s
After: 263 MB/s
(compare to sha256sum of non-verity file: 357 MB/s)
In an x86_64 VM (using sha256-avx2):
Before: 173 MB/s
After: 215 MB/s
(compare to sha256sum of non-verity file: 223 MB/s)
Link: https://lore.kernel.org/r/20200106205533.137005-1-ebiggers@kernel.org
Reviewed-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Eric Biggers <ebiggers@google.com>
2020-01-06 20:55:33 +00:00
|
|
|
unsigned long level0_blocks; /* number of blocks in tree level 0 */
|
2019-07-22 16:26:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Starting block index for each tree level, ordered from leaf level (0)
|
|
|
|
* to root level ('num_levels - 1')
|
|
|
|
*/
|
|
|
|
u64 level_start[FS_VERITY_MAX_LEVELS];
|
|
|
|
};
|
|
|
|
|
2019-07-22 16:26:22 +00:00
|
|
|
/**
|
|
|
|
* fsverity_info - cached verity metadata for an inode
|
|
|
|
*
|
|
|
|
* When a verity file is first opened, an instance of this struct is allocated
|
|
|
|
* and stored in ->i_verity_info; it remains until the inode is evicted. It
|
|
|
|
* caches information about the Merkle tree that's needed to efficiently verify
|
|
|
|
* data read from the file. It also caches the file measurement. The Merkle
|
|
|
|
* tree pages themselves are not cached here, but the filesystem may cache them.
|
|
|
|
*/
|
|
|
|
struct fsverity_info {
|
|
|
|
struct merkle_tree_params tree_params;
|
|
|
|
u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE];
|
|
|
|
u8 measurement[FS_VERITY_MAX_DIGEST_SIZE];
|
|
|
|
const struct inode *inode;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2019-07-22 16:26:23 +00:00
|
|
|
* Merkle tree properties. The file measurement is the hash of this structure
|
|
|
|
* excluding the signature and with the sig_size field set to 0.
|
2019-07-22 16:26:22 +00:00
|
|
|
*/
|
|
|
|
struct fsverity_descriptor {
|
|
|
|
__u8 version; /* must be 1 */
|
|
|
|
__u8 hash_algorithm; /* Merkle tree hash algorithm */
|
|
|
|
__u8 log_blocksize; /* log2 of size of data and tree blocks */
|
|
|
|
__u8 salt_size; /* size of salt in bytes; 0 if none */
|
2019-07-22 16:26:23 +00:00
|
|
|
__le32 sig_size; /* size of signature in bytes; 0 if none */
|
2019-07-22 16:26:22 +00:00
|
|
|
__le64 data_size; /* size of file the Merkle tree is built over */
|
|
|
|
__u8 root_hash[64]; /* Merkle tree root hash */
|
|
|
|
__u8 salt[32]; /* salt prepended to each hashed block */
|
|
|
|
__u8 __reserved[144]; /* must be 0's */
|
2019-07-22 16:26:23 +00:00
|
|
|
__u8 signature[]; /* optional PKCS#7 signature */
|
2019-07-22 16:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Arbitrary limit to bound the kmalloc() size. Can be changed. */
|
|
|
|
#define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
|
|
|
|
|
2019-07-22 16:26:23 +00:00
|
|
|
#define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
|
|
|
|
sizeof(struct fsverity_descriptor))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format in which verity file measurements are signed. This is the same as
|
|
|
|
* 'struct fsverity_digest', except here some magic bytes are prepended to
|
|
|
|
* provide some context about what is being signed in case the same key is used
|
|
|
|
* for non-fsverity purposes, and here the fields have fixed endianness.
|
|
|
|
*/
|
|
|
|
struct fsverity_signed_digest {
|
|
|
|
char magic[8]; /* must be "FSVerity" */
|
|
|
|
__le16 digest_algorithm;
|
|
|
|
__le16 digest_size;
|
|
|
|
__u8 digest[];
|
|
|
|
};
|
|
|
|
|
2019-07-22 16:26:21 +00:00
|
|
|
/* hash_algs.c */
|
|
|
|
|
|
|
|
extern struct fsverity_hash_alg fsverity_hash_algs[];
|
|
|
|
|
2019-12-31 17:55:45 +00:00
|
|
|
struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
|
|
|
|
unsigned int num);
|
|
|
|
struct ahash_request *fsverity_alloc_hash_request(struct fsverity_hash_alg *alg,
|
|
|
|
gfp_t gfp_flags);
|
|
|
|
void fsverity_free_hash_request(struct fsverity_hash_alg *alg,
|
|
|
|
struct ahash_request *req);
|
|
|
|
const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,
|
2019-07-22 16:26:21 +00:00
|
|
|
const u8 *salt, size_t salt_size);
|
|
|
|
int fsverity_hash_page(const struct merkle_tree_params *params,
|
|
|
|
const struct inode *inode,
|
|
|
|
struct ahash_request *req, struct page *page, u8 *out);
|
2019-12-31 17:55:45 +00:00
|
|
|
int fsverity_hash_buffer(struct fsverity_hash_alg *alg,
|
2019-07-22 16:26:21 +00:00
|
|
|
const void *data, size_t size, u8 *out);
|
|
|
|
void __init fsverity_check_hash_algs(void);
|
|
|
|
|
|
|
|
/* init.c */
|
|
|
|
|
|
|
|
extern void __printf(3, 4) __cold
|
|
|
|
fsverity_msg(const struct inode *inode, const char *level,
|
|
|
|
const char *fmt, ...);
|
|
|
|
|
|
|
|
#define fsverity_warn(inode, fmt, ...) \
|
|
|
|
fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
|
|
|
|
#define fsverity_err(inode, fmt, ...) \
|
|
|
|
fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
|
|
|
|
|
2019-07-22 16:26:22 +00:00
|
|
|
/* open.c */
|
|
|
|
|
|
|
|
int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
|
|
|
|
const struct inode *inode,
|
|
|
|
unsigned int hash_algorithm,
|
|
|
|
unsigned int log_blocksize,
|
|
|
|
const u8 *salt, size_t salt_size);
|
|
|
|
|
|
|
|
struct fsverity_info *fsverity_create_info(const struct inode *inode,
|
2019-07-22 16:26:23 +00:00
|
|
|
void *desc, size_t desc_size);
|
2019-07-22 16:26:22 +00:00
|
|
|
|
|
|
|
void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
|
|
|
|
|
|
|
|
void fsverity_free_info(struct fsverity_info *vi);
|
|
|
|
|
|
|
|
int __init fsverity_init_info_cache(void);
|
2019-07-22 16:26:22 +00:00
|
|
|
void __init fsverity_exit_info_cache(void);
|
|
|
|
|
2019-07-22 16:26:23 +00:00
|
|
|
/* signature.c */
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
|
|
|
|
int fsverity_verify_signature(const struct fsverity_info *vi,
|
|
|
|
const struct fsverity_descriptor *desc,
|
|
|
|
size_t desc_size);
|
|
|
|
|
|
|
|
int __init fsverity_init_signature(void);
|
|
|
|
#else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
|
|
|
|
static inline int
|
|
|
|
fsverity_verify_signature(const struct fsverity_info *vi,
|
|
|
|
const struct fsverity_descriptor *desc,
|
|
|
|
size_t desc_size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fsverity_init_signature(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
|
|
|
|
|
2019-07-22 16:26:22 +00:00
|
|
|
/* verify.c */
|
|
|
|
|
|
|
|
int __init fsverity_init_workqueue(void);
|
2019-07-22 16:26:23 +00:00
|
|
|
void __init fsverity_exit_workqueue(void);
|
2019-07-22 16:26:22 +00:00
|
|
|
|
2019-07-22 16:26:21 +00:00
|
|
|
#endif /* _FSVERITY_PRIVATE_H */
|