fsverity: support for non-4K pages
Subsystems (7)
Eric Biggers
maintainercover fsverity: support for non-4K pages
[This patchset applies to v6.1-rc2 + my other patch
https://lore.kernel.org/r/[email protected].
You can get everything from tag "fsverity-non4k-v1" of
https://git.kernel.org/pub/scm/fs/fscrypt/fscrypt.git]
Currently, filesystems (ext4, f2fs, and btrfs) only support fsverity when the Merkle tree block size, filesystem block size, and page size are all the same. In practice that means 4K, since increasing the page size, e.g. to 16K, forces the Merkle tree block size and filesystem block size to be increased accordingly. That can be impractical; for one, users want the same file signatures to work on all systems.
Therefore, this patchset reduces the coupling between these sizes.
First, patches 1-4 allow the Merkle tree block size to be less than the page size or filesystem block size, provided that it's not larger than either one. This involves, among other things, changing the way that fs/verity/verify.c tracks which hash blocks have been verified.
Second, patches 5-6 makes ext4 support fsverity when the filesystem block size is less than the page size. Note, f2fs doesn't need similar changes because f2fs always assumes that the filesystem block size and page size are the same anyway. I haven't looked into btrfs yet.
I've tested this patchset with changes to the verity tests in xfstests. I'll be sending out these xfstests changes separately.
Eric Biggers (6):
fsverity: support verification with tree block size < PAGE_SIZE
fsverity: support enabling with tree block size < PAGE_SIZE
ext4: simplify ext4_readpage_limit()
f2fs: simplify f2fs_readpage_limit()
fs/buffer.c: support fsverity in block_read_full_folio()
ext4: allow verity with fs block size < PAGE_SIZE
Documentation/filesystems/fsverity.rst | 76 +++--- fs/buffer.c | 66 ++++- fs/ext4/readpage.c | 3 +- fs/ext4/super.c | 5 - fs/f2fs/data.c | 3 +- fs/verity/enable.c | 268 ++++++++++---------- fs/verity/fsverity_private.h | 17 +- fs/verity/hash_algs.c | 48 ++-- fs/verity/open.c | 101 ++++++-- fs/verity/verify.c | 325 +++++++++++++++++-------- include/linux/fsverity.h | 14 +- 11 files changed, 575 insertions(+), 351 deletions(-)
Signature · 1 line
2.38.0
Eric Biggers
maintainerPATCH 1/6 fsverity: support verification with tree block size < PAGE_SIZE
From: Eric Biggers <[email protected]>
Add support for verifying data from verity files whose Merkle tree block size is less than the page size. The main use case for this is to allow a single Merkle tree block size to be used across all systems, so that only one set of fsverity file digests and signatures is needed.
To do this, eliminate various assumptions that the Merkle tree block size and the page size are the same:
- Make fsverity_verify_page() a wrapper around a new function fsverity_verify_blocks() which verifies one or more blocks in a page.
- When a Merkle tree block is needed, get the corresponding page and only verify and use the needed portion. (The Merkle tree continues to be read and cached in page-sized chunks; that doesn't need to change.)
- When the Merkle tree block size and page size differ, use a bitmap fsverity_info::hash_block_verified to keep track of which Merkle tree blocks have been verified, as PageChecked cannot be used directly.
--- Documentation/filesystems/fsverity.rst | 49 ++-- fs/verity/enable.c | 4 +- fs/verity/fsverity_private.h | 15 +- fs/verity/hash_algs.c | 24 +- fs/verity/open.c | 93 +++++-- fs/verity/verify.c | 325 +++++++++++++++++-------- include/linux/fsverity.h | 11 +- 7 files changed, 355 insertions(+), 166 deletions(-)
Documentation/filesystems/fsverity.rst +23 -26
@@ -571,47 +571,44 @@ For filesystems using Linux's pagecache, the ``->read_folio()`` and are marked Uptodate. Merely hooking ``->read_iter()`` would be insufficient, since ``->read_iter()`` is not used for memory maps. -Therefore, fs/verity/ provides a function fsverity_verify_page() which -verifies a page that has been read into the pagecache of a verity -inode, but is still locked and not Uptodate, so it's not yet readable -by userspace. As needed to do the verification, -fsverity_verify_page() will call back into the filesystem to read -Merkle tree pages via fsverity_operations::read_merkle_tree_page(). - -fsverity_verify_page() returns false if verification failed; in this +Therefore, fs/verity/ provides the function fsverity_verify_blocks() +which verifies data that has been read into the pagecache of a verity +inode. The containing page must still be locked and not Uptodate, so +it's not yet readable by userspace. As needed to do the verification, +fsverity_verify_blocks() will call back into the filesystem to read +hash blocks via fsverity_operations::read_merkle_tree_page(). + +fsverity_verify_blocks() returns false if verification failed; in this case, the filesystem must not set the page Uptodate. Following this, as per the usual Linux pagecache behavior, attempts by userspace to read() from the part of the file containing the page will fail with EIO, and accesses to the page within a memory map will raise SIGBUS. -fsverity_verify_page() currently only supports the case where the -Merkle tree block size is equal to PAGE_SIZE (often 4096 bytes). - -In principle, fsverity_verify_page() verifies the entire path in the -Merkle tree from the data page to the root hash. However, for -efficiency the filesystem may cache the hash pages. Therefore, -fsverity_verify_page() only ascends the tree reading hash pages until -an already-verified hash page is seen, as indicated by the PageChecked -bit being set. It then verifies the path to that page. +In principle, verifying a data block requires verifying the entire +path in the Merkle tree from the data block to the root hash. +However, for efficiency the filesystem may cache the hash blocks. +Therefore, fsverity_verify_blocks() only ascends the tree reading hash +blocks until an already-verified hash block is seen. It then verifies +the path to that block. This optimization, which is also used by dm-verity, results in excellent sequential read performance. This is because usually (e.g. -127 in 128 times for 4K blocks and SHA-256) the hash page from the +127 in 128 times for 4K blocks and SHA-256) the hash block from the bottom level of the tree will already be cached and checked from -reading a previous data page. However, random reads perform worse. +reading a previous data block. However, random reads perform worse. Block device based filesystems ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Block device based filesystems (e.g. ext4 and f2fs) in Linux also use the pagecache, so the above subsection applies too. However, they -also usually read many pages from a file at once, grouped into a +also usually read many data blocks from a file at once, grouped into a structure called a "bio". To make it easier for these types of filesystems to support fs-verity, fs/verity/ also provides a function -fsverity_verify_bio() which verifies all pages in a bio. +fsverity_verify_bio() which verifies all data blocks in a bio. ext4 and f2fs also support encryption. If a verity file is also -encrypted, the pages must be decrypted before being verified. To +encrypted, the data must be decrypted before being verified. To support this, these filesystems allocate a "post-read context" for each bio and store it in ``->bi_private``::
@@ -630,10 +627,10 @@ verification. Finally, pages where no decryption or verity error occurred are marked Uptodate, and the pages are unlocked. On many filesystems, files can contain holes. Normally, -``->readahead()`` simply zeroes holes and sets the corresponding pages -Uptodate; no bios are issued. To prevent this case from bypassing -fs-verity, these filesystems use fsverity_verify_page() to verify hole -pages. +``->readahead()`` simply zeroes hole blocks and considers the +corresponding data to be up-to-date; no bios are issued. To prevent +this case from bypassing fs-verity, filesystems use +fsverity_verify_blocks() to verify hole blocks. Filesystems also disable direct I/O on verity files, since otherwise direct I/O would bypass fs-verity.
fs/verity/enable.c +2 -2
@@ -103,8 +103,8 @@ static int build_merkle_tree_level(struct file *filp, unsigned int level, } } - err =fsverity_hash_page (params, inode, req, src_page, - &pending_hashes[pending_size]); + err =fsverity_hash_block (params, inode, req, src_page,0 , + &pending_hashes[pending_size]);put_page (src_page);if (err)return err;
fs/verity/fsverity_private.h +9 -6
@@ -41,17 +41,19 @@ struct merkle_tree_params {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 blocks_per_page;/* PAGE_SIZE / block_size */ unsigned int log_blocksize;/* log2(block_size) */ unsigned int log_arity;/* log2(hashes_per_block) */ +unsigned int log_blocks_per_page;/* log2(blocks_per_page) */ unsigned int num_levels;/* number of levels in Merkle tree */ u64 tree_size;/* Merkle tree size in bytes */ -unsigned long level0_blocks;/* number of blocks in tree level 0 */ +unsigned long tree_pages;/* Merkle tree size in pages */ /* * 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 ]; +unsigned long level_start[FS_VERITY_MAX_LEVELS ]; };/*
@@ -68,9 +70,10 @@ struct fsverity_info {u8 root_hash[FS_VERITY_MAX_DIGEST_SIZE ];u8 file_digest[FS_VERITY_MAX_DIGEST_SIZE ];const struct inode *inode; +unsigned long *hash_block_verified; +spinlock_t hash_page_init_lock; }; -#define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \ sizeof(struct fsverity_descriptor))
@@ -86,9 +89,9 @@ 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,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); +int fsverity_hash_block (const struct merkle_tree_params *params, +const struct inode *inode,struct ahash_request *req, +struct page *page,unsigned int offset,u8 *out);int fsverity_hash_buffer (struct fsverity_hash_alg *alg,const void *data,size_t size,u8 *out);void __init fsverity_check_hash_algs (void );
fs/verity/hash_algs.c +11 -13
@@ -218,35 +218,33 @@ const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg, }/** -* fsverity_hash_page() - hash a single data or hash page +* fsverity_hash_block() - hash a single data or hash block * @params: the Merkle tree's parameters * @inode: inode for which the hashing is being done * @req: preallocated hash request -* @page: the page to hash +* @page: the page containing the block to hash +* @offset: the offset of the block within @page * @out: output digest, size 'params->digest_size' bytes * -* Hash a single data or hash block, assuming block_size == PAGE_SIZE. -* The hash is salted if a salt is specified in the Merkle tree parameters. +* Hash a single data or hash block. The hash is salted if a salt is specified +* in the Merkle tree parameters. * * Return: 0 on success, -errno on failure */ -int fsverity_hash_page (const struct merkle_tree_params *params, -const struct inode *inode, -struct ahash_request *req,struct page *page,u8 *out) +int fsverity_hash_block (const struct merkle_tree_params *params, +const struct inode *inode,struct ahash_request *req, +struct page *page,unsigned int offset,u8 *out) {struct scatterlist sg;DECLARE_CRYPTO_WAIT (wait);int err; -if (WARN_ON (params->block_size !=PAGE_SIZE )) -return -EINVAL ; -sg_init_table (&sg,1 ); -sg_set_page (&sg, page,PAGE_SIZE ,0 ); +sg_set_page (&sg, page, params->block_size, offset);ahash_request_set_callback (req,CRYPTO_TFM_REQ_MAY_SLEEP |CRYPTO_TFM_REQ_MAY_BACKLOG , crypto_req_done, &wait); -ahash_request_set_crypt (req, &sg, out,PAGE_SIZE ); +ahash_request_set_crypt (req, &sg, out, params->block_size);if (params->hashstate) { err =crypto_ahash_import (req, params->hashstate);
@@ -262,7 +260,7 @@ int fsverity_hash_page(const struct merkle_tree_params *params, err =crypto_wait_req (err, &wait);if (err) -fsverity_err (inode,"Error %d computing page hash" , err); +fsverity_err (inode,"Error %d computing block hash" , err);return err; }
fs/verity/open.c +77 -16
170 lines not on this page · raw message
fs/verity/verify.c +224 -101
447 lines not on this page · raw message
include/linux/fsverity.h +9 -2
@@ -158,7 +158,8 @@ int fsverity_ioctl_read_metadata(struct file *filp, const void __user *uarg);/* verify.c */ -bool fsverity_verify_page (struct page *page); +bool fsverity_verify_blocks (struct page *page,unsigned int len, +unsigned int offset);void fsverity_verify_bio (struct bio *bio);void fsverity_enqueue_verify_work (struct work_struct *work);
@@ -218,7 +219,8 @@ static inline int fsverity_ioctl_read_metadata(struct file *filp,/* verify.c */ -static inline bool fsverity_verify_page (struct page *page) +static inline bool fsverity_verify_blocks (struct page *page,unsigned int len, +unsigned int offset) {WARN_ON (1 );return false;
@@ -236,6 +238,11 @@ static inline void fsverity_enqueue_verify_work(struct work_struct *work)#endif /* !CONFIG_FS_VERITY */ +static inline bool fsverity_verify_page (struct page *page) +{ +return fsverity_verify_blocks (page,PAGE_SIZE ,0 ); +} +/** * fsverity_active() - do reads from the inode need to go through fs-verity? * @inode: inode to check
Signature · 1 line
2.38.0
Eric Biggers
maintainerPATCH 2/6 fsverity: support enabling with tree block size < PAGE_SIZE
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Make FS_IOC_ENABLE_VERITY support values of fsverity_enable_arg::block_size other than PAGE_SIZE.
To make this possible, rework build_merkle_tree(), which was reading data and hash pages from the file and assuming that they were the same thing as "blocks".
For reading the data blocks, just replace the direct pagecache access with __kernel_read(), to naturally read one block at a time.
(A disadvantage of the above is that we lose the two optimizations of hashing the pagecache pages in-place and forcing the maximum readahead. That shouldn't be very important, though.)
The hash block reads are a bit more difficult to handle, as the only way to do them is through fsverity_operations::read_merkle_tree_page().
Instead, let's switch to the single-pass tree construction algorithm that fsverity-utils uses. This eliminates the need to read back any hash blocks while the tree is being built, at the small cost of an extra block-sized memory buffer per Merkle tree level.
Taken together, the above two changes result in page-size independent code that is also a bit simpler than what we had before.
--- Documentation/filesystems/fsverity.rst | 21 +- fs/verity/enable.c | 268 ++++++++++++------------- fs/verity/fsverity_private.h | 2 +- fs/verity/hash_algs.c | 24 ++- fs/verity/open.c | 8 +- include/linux/fsverity.h | 3 +- 6 files changed, 160 insertions(+), 166 deletions(-)
Documentation/filesystems/fsverity.rst +10 -11
@@ -118,10 +118,11 @@ as follows: - ``hash_algorithm`` must be the identifier for the hash algorithm to use for the Merkle tree, such as FS_VERITY_HASH_ALG_SHA256. See ``include/uapi/linux/fsverity.h`` for the list of possible values. -- ``block_size`` must be the Merkle tree block size. Currently, this - must be equal to the system page size, which is usually 4096 bytes. - Other sizes may be supported in the future. This value is not - necessarily the same as the filesystem block size. +- ``block_size`` is the Merkle tree block size, in bytes. In Linux + v6.2 and later, this can be any power of 2 between (inclusively) + 1024 and the minimum of the system page size and the filesystem + block size. In earlier versions, the system page size was the only + allowed value. - ``salt_size`` is the size of the salt in bytes, or 0 if no salt is provided. The salt is a value that is prepended to every hashed block; it can be used to personalize the hashing for a particular
@@ -161,6 +162,7 @@ FS_IOC_ENABLE_VERITY can fail with the following errors: - ``EBUSY``: this ioctl is already running on the file - ``EEXIST``: the file already has verity enabled - ``EFAULT``: the caller provided inaccessible memory +- ``EFBIG``: the file is too large to enable verity on - ``EINTR``: the operation was interrupted by a fatal signal - ``EINVAL``: unsupported version, hash algorithm, or block size; or reserved bits are set; or the file descriptor refers to neither a
@@ -518,9 +520,7 @@ support paging multi-gigabyte xattrs into memory, and to support encrypting xattrs. Note that the verity metadata *must* be encrypted when the file is, since it contains hashes of the plaintext data. -Currently, ext4 verity only supports the case where the Merkle tree -block size, filesystem block size, and page size are all the same. It -also only supports extent-based files. +ext4 only allows verity on extent-based files. f2fs ----
@@ -538,11 +538,10 @@ Like ext4, f2fs stores the verity metadata (Merkle tree and fsverity_descriptor) past the end of the file, starting at the first 64K boundary beyond i_size. See explanation for ext4 above. Moreover, f2fs supports at most 4096 bytes of xattr entries per inode -which wouldn't be enough for even a single Merkle tree block. +which usually wouldn't be enough for even a single Merkle tree block. -Currently, f2fs verity only supports a Merkle tree block size of 4096. -Also, f2fs doesn't support enabling verity on files that currently -have atomic or volatile writes pending. +f2fs doesn't support enabling verity on files that currently have +atomic or volatile writes pending. btrfs -----
fs/verity/enable.c +128 -140
312 lines not on this page · raw message
fs/verity/fsverity_private.h +1 -1
@@ -92,7 +92,7 @@ const u8 *fsverity_prepare_hash_state(struct fsverity_hash_alg *alg,int fsverity_hash_block (const struct merkle_tree_params *params,const struct inode *inode,struct ahash_request *req,struct page *page,unsigned int offset,u8 *out); -int fsverity_hash_buffer (struct fsverity_hash_alg *alg, +int fsverity_hash_buffer (const u8 *initial_state,struct ahash_request *req,const void *data,size_t size,u8 *out);void __init fsverity_check_hash_algs (void );
fs/verity/hash_algs.c +13 -11
@@ -266,37 +266,39 @@ int fsverity_hash_block(const struct merkle_tree_params *params,/** * fsverity_hash_buffer() - hash some data -* @alg: the hash algorithm to use +* @initial_state: optional salted initial hash state +* @req: preallocated hash request * @data: the data to hash * @size: size of data to hash, in bytes * @out: output digest, size 'alg->digest_size' bytes * * Hash some data which is located in physically contiguous memory (i.e. memory -* allocated by kmalloc(), not by vmalloc()). No salt is used. +* allocated by kmalloc(), not by vmalloc()). * * Return: 0 on success, -errno on failure */ -int fsverity_hash_buffer (struct fsverity_hash_alg *alg, +int fsverity_hash_buffer (const u8 *initial_state,struct ahash_request *req,const void *data,size_t size,u8 *out) { -struct ahash_request *req;struct scatterlist sg;DECLARE_CRYPTO_WAIT (wait);int err; -/* This allocation never fails, since it's mempool-backed. */ - req =fsverity_alloc_hash_request (alg,GFP_KERNEL ); -sg_init_one (&sg, data, size);ahash_request_set_callback (req,CRYPTO_TFM_REQ_MAY_SLEEP |CRYPTO_TFM_REQ_MAY_BACKLOG , crypto_req_done, &wait);ahash_request_set_crypt (req, &sg, out, size); - err =crypto_wait_req (crypto_ahash_digest (req), &wait); - -fsverity_free_hash_request (alg, req); -return err; +if (initial_state) { + err =crypto_ahash_import (req, initial_state); +if (err) +return err; + err =crypto_ahash_finup (req); + }else { + err =crypto_ahash_digest (req); + } +return crypto_wait_req (err, &wait); }void __init fsverity_check_hash_algs (void )
fs/verity/open.c +7 -1
@@ -168,13 +168,19 @@ static int compute_file_digest(struct fsverity_hash_alg *hash_alg,struct fsverity_descriptor *desc, u8 *file_digest) { + struct ahash_request *req;__le32 sig_size = desc->sig_size;int err; +/* This allocation never fails, since it's mempool-backed. */ + req =fsverity_alloc_hash_request (hash_alg,GFP_KERNEL ); + desc->sig_size =0 ; - err =fsverity_hash_buffer (hash_alg, desc,sizeof (*desc), file_digest); + err =fsverity_hash_buffer (NULL , req, desc,sizeof (*desc), file_digest); desc->sig_size = sig_size; +fsverity_free_hash_request (hash_alg, req); +return err; }
include/linux/fsverity.h +1 -2
@@ -93,8 +93,7 @@ struct fsverity_operations {* isn't already cached. Implementations may ignore this * argument; it's only a performance optimization. * -* This can be called at any time on an open verity file, as well as -* between ->begin_enable_verity() and ->end_enable_verity(). It may be +* This can be called at any time on an open verity file. It may be * called by multiple processes concurrently, even with the same page. * * Note that this must retrieve a *page*, not necessarily a *block*.
Signature · 8 lines
2.38.0 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Eric Biggers
maintainerPATCH 3/6 ext4: simplify ext4_readpage_limit()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Now that the implementation of FS_IOC_ENABLE_VERITY has changed to not involve reading back Merkle tree blocks that were previously written, there is no need for ext4_readpage_limit() to allow for this case.
--- fs/ext4/readpage.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/ext4/readpage.c +1 -2
@@ -211,8 +211,7 @@ static void ext4_set_bio_post_read_ctx(struct bio *bio,static inline loff_t ext4_readpage_limit (struct inode *inode) { -if (IS_ENABLED (CONFIG_FS_VERITY ) && - (IS_VERITY (inode) ||ext4_verity_in_progress (inode))) +if (IS_ENABLED (CONFIG_FS_VERITY ) &&IS_VERITY (inode))return inode->i_sb->s_maxbytes;return i_size_read (inode);
Signature · 1 line
2.38.0
Eric Biggers
maintainerPATCH 4/6 f2fs: simplify f2fs_readpage_limit()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Now that the implementation of FS_IOC_ENABLE_VERITY has changed to not involve reading back Merkle tree blocks that were previously written, there is no need for f2fs_readpage_limit() to allow for this case.
--- fs/f2fs/data.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/f2fs/data.c +1 -2
@@ -2043,8 +2043,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,static inline loff_t f2fs_readpage_limit (struct inode *inode) { -if (IS_ENABLED (CONFIG_FS_VERITY ) && - (IS_VERITY (inode) ||f2fs_verity_in_progress (inode))) +if (IS_ENABLED (CONFIG_FS_VERITY ) &&IS_VERITY (inode))return inode->i_sb->s_maxbytes;return i_size_read (inode);
Signature · 1 line
2.38.0
Eric Biggers
maintainerPATCH 5/6 fs/buffer.c: support fsverity in block_read_full_folio()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
After each filesystem block (as represented by a buffer_head) has been read from disk by block_read_full_folio(), verify it if needed. The verification is done on the fsverity_read_workqueue. Also allow reads of verity metadata past i_size, as required by ext4.
This is needed to support fsverity on ext4 filesystems where the filesystem block size is less than the page size.
The new code is compiled away when CONFIG_FS_VERITY=n.
--- fs/buffer.c | 66 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 10 deletions(-)
fs/buffer.c +56 -10
@@ -48,6 +48,7 @@#include <linux/sched/mm.h> #include <trace/events/block.h> #include <linux/fscrypt.h> +#include <linux/fsverity.h> #include "internal.h"
@@ -295,20 +296,51 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)return ; } -struct decrypt_bh_ctx { +struct postprocess_bh_ctx {struct work_struct work;struct buffer_head *bh; }; +static void verify_bh (struct work_struct *work) +{ +struct postprocess_bh_ctx *ctx = + container_of(work, struct postprocess_bh_ctx, work); +struct buffer_head *bh = ctx->bh; +bool valid; + + valid =fsverity_verify_blocks (bh->b_page, bh->b_size,bh_offset (bh)); +end_buffer_async_read (bh, valid); +kfree (ctx); +} + +static bool need_fsverity (struct buffer_head *bh) +{ +struct inode *inode = bh->b_page->mapping->host; + +return fsverity_active (inode) && +/* needed by ext4 */ + bh->b_page->index <DIV_ROUND_UP (inode->i_size,PAGE_SIZE ); +} +static void decrypt_bh (struct work_struct *work) { -struct decrypt_bh_ctx *ctx = - container_of(work, struct decrypt_bh_ctx, work); +struct postprocess_bh_ctx *ctx = + container_of(work, struct postprocess_bh_ctx, work);struct buffer_head *bh = ctx->bh;int err; err =fscrypt_decrypt_pagecache_blocks (bh->b_page, bh->b_size,bh_offset (bh)); +if (err ==0 &&need_fsverity (bh)) { +/* +* We use different work queues for decryption and for verity +* because verity may require reading metadata pages that need +* decryption, and we shouldn't recurse to the same workqueue. +*/ +INIT_WORK (&ctx->work, verify_bh); +fsverity_enqueue_verify_work (&ctx->work); +return ; + }end_buffer_async_read (bh, err ==0 );kfree (ctx); }
@@ -319,15 +351,24 @@ static void decrypt_bh(struct work_struct *work)*/ static void end_buffer_async_read_io (struct buffer_head *bh,int uptodate) { -/* Decrypt if needed */ -if (uptodate && -fscrypt_inode_uses_fs_layer_crypto (bh->b_page->mapping->host)) { -struct decrypt_bh_ctx *ctx =kmalloc (sizeof (*ctx),GFP_ATOMIC ); +struct inode *inode = bh->b_page->mapping->host; +bool decrypt =fscrypt_inode_uses_fs_layer_crypto (inode); +bool verify =need_fsverity (bh); + +/* Decrypt (with fscrypt) and/or verify (with fsverity) if needed. */ +if (uptodate && (decrypt || verify)) { +struct postprocess_bh_ctx *ctx = +kmalloc (sizeof (*ctx),GFP_ATOMIC );if (ctx) { -INIT_WORK (&ctx->work, decrypt_bh); ctx->bh = bh; -fscrypt_enqueue_decrypt_work (&ctx->work); +if (decrypt) { +INIT_WORK (&ctx->work, decrypt_bh); +fscrypt_enqueue_decrypt_work (&ctx->work); + }else { +INIT_WORK (&ctx->work, verify_bh); +fsverity_enqueue_verify_work (&ctx->work); + }return ; } uptodate =0 ;
@@ -2245,6 +2286,11 @@ int block_read_full_folio(struct folio *folio, get_block_t *get_block)int nr, i;int fully_mapped =1 ;bool page_error = false; +loff_t limit =i_size_read (inode); + +/* This is needed for ext4. */ +if (IS_ENABLED (CONFIG_FS_VERITY ) &&IS_VERITY (inode)) + limit = inode->i_sb->s_maxbytes;VM_BUG_ON_FOLIO (folio_test_large (folio), folio);
@@ -2253,7 +2299,7 @@ int block_read_full_folio(struct folio *folio, get_block_t *get_block) bbits =block_size_bits (blocksize); iblock = (sector_t )folio->index << (PAGE_SHIFT - bbits); - lblock = (i_size_read (inode)+blocksize-1 ) >> bbits; + lblock = (limit+blocksize-1 ) >> bbits; bh = head; nr =0 ; i =0 ;
Signature · 1 line
2.38.0
Eric Biggers
maintainerPATCH 6/6 ext4: allow verity with fs block size < PAGE_SIZE
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Now that the needed changes have been made to fs/buffer.c, ext4 is ready to support the verity feature when the filesystem block size is less than the page size. So remove the mount-time check that prevented this.
--- Documentation/filesystems/fsverity.rst | 8 +++++--- fs/ext4/super.c | 5 ----- 2 files changed, 5 insertions(+), 8 deletions(-)
Documentation/filesystems/fsverity.rst +5 -3
@@ -497,9 +497,11 @@ To create verity files on an ext4 filesystem, the filesystem must have been formatted with ``-O verity`` or had ``tune2fs -O verity`` run on it. "verity" is an RO_COMPAT filesystem feature, so once set, old kernels will only be able to mount the filesystem readonly, and old -versions of e2fsck will be unable to check the filesystem. Moreover, -currently ext4 only supports mounting a filesystem with the "verity" -feature when its block size is equal to PAGE_SIZE (often 4096 bytes). +versions of e2fsck will be unable to check the filesystem. + +Originally, an ext4 filesystem with the "verity" feature could only be +mounted when its block size was equal to the system page size +(typically 4096 bytes). In Linux v6.2, this limitation was removed. ext4 sets the EXT4_VERITY_FL on-disk inode flag on verity files. It can only be set by `FS_IOC_ENABLE_VERITY`_, and it cannot be cleared.
fs/ext4/super.c +0 -5
@@ -5339,11 +5339,6 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb) } } -if (ext4_has_feature_verity (sb) && sb->s_blocksize !=PAGE_SIZE ) { -ext4_msg (sb,KERN_ERR ,"Unsupported blocksize for fs-verity" ); - goto failed_mount_wq; - } -/* * Get the # of file system overhead blocks from the * superblock if present.
Signature · 1 line
2.38.0