Skip to content

Commit 671e67b

Browse files
committed
fs-verity: add Kconfig and the helper functions for hashing
Add the beginnings of the fs/verity/ support layer, including the Kconfig option and various helper functions for hashing. To start, only SHA-256 is supported, but other hash algorithms can easily be added. Reviewed-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent fe9918d commit 671e67b

7 files changed

Lines changed: 449 additions & 0 deletions

File tree

‎fs/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ config MANDATORY_FILE_LOCKING
112112

113113
source "fs/crypto/Kconfig"
114114

115+
source "fs/verity/Kconfig"
116+
115117
source "fs/notify/Kconfig"
116118

117119
source "fs/quota/Kconfig"

‎fs/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ obj-$(CONFIG_AIO) += aio.o
3434
obj-$(CONFIG_IO_URING) += io_uring.o
3535
obj-$(CONFIG_FS_DAX) += dax.o
3636
obj-$(CONFIG_FS_ENCRYPTION) += crypto/
37+
obj-$(CONFIG_FS_VERITY) += verity/
3738
obj-$(CONFIG_FILE_LOCKING) += locks.o
3839
obj-$(CONFIG_COMPAT) += compat.o compat_ioctl.o
3940
obj-$(CONFIG_BINFMT_AOUT) += binfmt_aout.o

‎fs/verity/Kconfig‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
config FS_VERITY
4+
bool "FS Verity (read-only file-based authenticity protection)"
5+
select CRYPTO
6+
# SHA-256 is selected as it's intended to be the default hash algorithm.
7+
# To avoid bloat, other wanted algorithms must be selected explicitly.
8+
select CRYPTO_SHA256
9+
help
10+
This option enables fs-verity. fs-verity is the dm-verity
11+
mechanism implemented at the file level. On supported
12+
filesystems (currently EXT4 and F2FS), userspace can use an
13+
ioctl to enable verity for a file, which causes the filesystem
14+
to build a Merkle tree for the file. The filesystem will then
15+
transparently verify any data read from the file against the
16+
Merkle tree. The file is also made read-only.
17+
18+
This serves as an integrity check, but the availability of the
19+
Merkle tree root hash also allows efficiently supporting
20+
various use cases where normally the whole file would need to
21+
be hashed at once, such as: (a) auditing (logging the file's
22+
hash), or (b) authenticity verification (comparing the hash
23+
against a known good value, e.g. from a digital signature).
24+
25+
fs-verity is especially useful on large files where not all
26+
the contents may actually be needed. Also, fs-verity verifies
27+
data each time it is paged back in, which provides better
28+
protection against malicious disks vs. an ahead-of-time hash.
29+
30+
If unsure, say N.
31+
32+
config FS_VERITY_DEBUG
33+
bool "FS Verity debugging"
34+
depends on FS_VERITY
35+
help
36+
Enable debugging messages related to fs-verity by default.
37+
38+
Say N unless you are an fs-verity developer.

‎fs/verity/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
obj-$(CONFIG_FS_VERITY) += hash_algs.o \
4+
init.o

‎fs/verity/fsverity_private.h‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
8+
#ifndef _FSVERITY_PRIVATE_H
9+
#define _FSVERITY_PRIVATE_H
10+
11+
#ifdef CONFIG_FS_VERITY_DEBUG
12+
#define DEBUG
13+
#endif
14+
15+
#define pr_fmt(fmt) "fs-verity: " fmt
16+
17+
#include <crypto/sha.h>
18+
#include <linux/fs.h>
19+
#include <uapi/linux/fsverity.h>
20+
21+
struct ahash_request;
22+
23+
/*
24+
* Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty;
25+
* it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks.
26+
*/
27+
#define FS_VERITY_MAX_LEVELS 8
28+
29+
/*
30+
* Largest digest size among all hash algorithms supported by fs-verity.
31+
* Currently assumed to be <= size of fsverity_descriptor::root_hash.
32+
*/
33+
#define FS_VERITY_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE
34+
35+
/* A hash algorithm supported by fs-verity */
36+
struct fsverity_hash_alg {
37+
struct crypto_ahash *tfm; /* hash tfm, allocated on demand */
38+
const char *name; /* crypto API name, e.g. sha256 */
39+
unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
40+
unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
41+
};
42+
43+
/* Merkle tree parameters: hash algorithm, initial hash state, and topology */
44+
struct merkle_tree_params {
45+
const struct fsverity_hash_alg *hash_alg; /* the hash algorithm */
46+
const u8 *hashstate; /* initial hash state or NULL */
47+
unsigned int digest_size; /* same as hash_alg->digest_size */
48+
unsigned int block_size; /* size of data and tree blocks */
49+
unsigned int hashes_per_block; /* number of hashes per tree block */
50+
unsigned int log_blocksize; /* log2(block_size) */
51+
unsigned int log_arity; /* log2(hashes_per_block) */
52+
unsigned int num_levels; /* number of levels in Merkle tree */
53+
u64 tree_size; /* Merkle tree size in bytes */
54+
55+
/*
56+
* Starting block index for each tree level, ordered from leaf level (0)
57+
* to root level ('num_levels - 1')
58+
*/
59+
u64 level_start[FS_VERITY_MAX_LEVELS];
60+
};
61+
62+
/* hash_algs.c */
63+
64+
extern struct fsverity_hash_alg fsverity_hash_algs[];
65+
66+
const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
67+
unsigned int num);
68+
const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg,
69+
const u8 *salt, size_t salt_size);
70+
int fsverity_hash_page(const struct merkle_tree_params *params,
71+
const struct inode *inode,
72+
struct ahash_request *req, struct page *page, u8 *out);
73+
int fsverity_hash_buffer(const struct fsverity_hash_alg *alg,
74+
const void *data, size_t size, u8 *out);
75+
void __init fsverity_check_hash_algs(void);
76+
77+
/* init.c */
78+
79+
extern void __printf(3, 4) __cold
80+
fsverity_msg(const struct inode *inode, const char *level,
81+
const char *fmt, ...);
82+
83+
#define fsverity_warn(inode, fmt, ...) \
84+
fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
85+
#define fsverity_err(inode, fmt, ...) \
86+
fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
87+
88+
#endif /* _FSVERITY_PRIVATE_H */

0 commit comments

Comments
 (0)