This is a note to let you know that I've just added the patch titled fsverity: explicitly check for buffer overflow in build_merkle_tree() to the 6.3-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: fsverity-explicitly-check-for-buffer-overflow-in-build_merkle_tree.patch and it can be found in the queue-6.3 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From 39049b69ec9fc125fa1f314165dcc86f72cb72ec Mon Sep 17 00:00:00 2001 From: Eric Biggers <ebiggers@xxxxxxxxxx> Date: Mon, 27 Mar 2023 21:15:05 -0700 Subject: fsverity: explicitly check for buffer overflow in build_merkle_tree() From: Eric Biggers <ebiggers@xxxxxxxxxx> commit 39049b69ec9fc125fa1f314165dcc86f72cb72ec upstream. The new Merkle tree construction algorithm is a bit fragile in that it may overflow the 'root_hash' array if the tree actually generated does not match the calculated tree parameters. This should never happen unless there is a filesystem bug that allows the file size to change despite deny_write_access(), or a bug in the Merkle tree logic itself. Regardless, it's fairly easy to check for buffer overflow here, so let's do so. This is a robustness improvement only; this case is not currently known to be reachable. I've added a Fixes tag anyway, since I recommend that this be included in kernels that have the mentioned commit. Fixes: 56124d6c87fd ("fsverity: support enabling with tree block size < PAGE_SIZE") Cc: stable@xxxxxxxxxxxxxxx Link: https://lore.kernel.org/r/20230328041505.110162-1-ebiggers@xxxxxxxxxx Signed-off-by: Eric Biggers <ebiggers@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/verity/enable.c | 10 ++++++++++ 1 file changed, 10 insertions(+) --- a/fs/verity/enable.c +++ b/fs/verity/enable.c @@ -13,6 +13,7 @@ struct block_buffer { u32 filled; + bool is_root_hash; u8 *data; }; @@ -24,6 +25,14 @@ static int hash_one_block(struct inode * struct block_buffer *next = cur + 1; int err; + /* + * Safety check to prevent a buffer overflow in case of a filesystem bug + * that allows the file size to change despite deny_write_access(), or a + * bug in the Merkle tree logic itself + */ + if (WARN_ON_ONCE(next->is_root_hash && next->filled != 0)) + return -EINVAL; + /* Zero-pad the block if it's shorter than the block size. */ memset(&cur->data[cur->filled], 0, params->block_size - cur->filled); @@ -97,6 +106,7 @@ static int build_merkle_tree(struct file } } buffers[num_levels].data = root_hash; + buffers[num_levels].is_root_hash = true; BUILD_BUG_ON(sizeof(level_offset) != sizeof(params->level_start)); memcpy(level_offset, params->level_start, sizeof(level_offset)); Patches currently in stable-queue which might be from ebiggers@xxxxxxxxxx are queue-6.3/fsverity-explicitly-check-for-buffer-overflow-in-build_merkle_tree.patch queue-6.3/fsverity-reject-fs_ioc_enable_verity-on-mode-3-fds.patch