[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260208151728.3212554-1-clm@meta.com>
Date: Sun, 8 Feb 2026 07:16:31 -0800
From: Chris Mason <clm@...a.com>
To: Daniel Vacek <neelx@...e.com>
CC: Chris Mason <clm@...com>, Josef Bacik <josef@...icpanda.com>,
Eric Biggers
<ebiggers@...nel.org>,
"Theodore Y. Ts'o" <tytso@....edu>, Jaegeuk Kim
<jaegeuk@...nel.org>,
Jens Axboe <axboe@...nel.dk>, David Sterba
<dsterba@...e.com>,
<linux-block@...r.kernel.org>, <linux-fscrypt@...r.kernel.org>,
<linux-btrfs@...r.kernel.org>, <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v6 24/43] btrfs: add extent encryption context tree item type
Daniel Vacek <neelx@...e.com> wrote:
> From: Josef Bacik <josef@...icpanda.com>
>
> The fscrypt encryption context will be stored as a new tree item type.
> This gives us flexibility to include different things in the future.
>
> Also update the tree-checker to validate the new item type.
Hi everyone,
I'm running the btrfs for-next branch, along with the encryption patch set
through my AI review prompts:
https://github.com/masoncl/review-prompts
I'm working on some btrfs specific instructions, but hopefully these reviews
are already useful. I've kept the headers to make it easier to double check
that I'm actually replying to the right email, but please let me know if any
parts of this are incorrect:
commit 203bfb91a4db9c826fa7f72034c13a1810a1b605
Author: Josef Bacik <josef@...icpanda.com>
btrfs: add extent encryption context tree item type
The fscrypt encryption context will be stored as a new tree item type.
This gives us flexibility to include different things in the future.
Also update the tree-checker to validate the new item type.
> diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
> index 3c0db279f592e..639462164d089 100644
> --- a/fs/btrfs/file.c
> +++ b/fs/btrfs/file.c
[ ... ]
> @@ -496,6 +503,60 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
> args->extent_inserted = true;
> }
>
> + if (first_ctx <= last_ctx) {
> + int slot, nritems;
> +
> + btrfs_release_path(path);
> +
> + key.objectid = ino;
> + key.type = BTRFS_FSCRYPT_CTX_KEY;
> + key.offset = first_ctx;
> +
> + ret = btrfs_search_slot(trans, root, &key, path, modify_tree, !!modify_tree);
> + if (ret < 0)
> + goto out_ctx;
[ ... ]
> +out_ctx:
> + if (args->path && args->extent_inserted) {
> + btrfs_release_path(path);
> +
> + key.objectid = ino;
> + key.type = BTRFS_EXTENT_DATA_KEY;
> + key.offset = args->start;
> + ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
> + }
> + }
> +
Can btrfs_search_slot() at line 556 overwrite an error that occurred
earlier in the out_ctx path?
If btrfs_search_slot() at line 515 fails and execution jumps to out_ctx,
but args->path and args->extent_inserted are both true, the second
btrfs_search_slot() call will overwrite the original error stored in ret.
If this second search succeeds (returns 0 or >0), the function returns
success to the caller despite the fscrypt context cleanup having failed.
This could lead to inconsistent filesystem state where extent data was
inserted but the corresponding fscrypt context items were not properly
cleaned up.
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index 9675dbcd78a3b..776901f297fea 100644
> --- a/fs/btrfs/tree-checker.c
> +++ b/fs/btrfs/tree-checker.c
[ ... ]
> +static int check_fscrypt_context(struct extent_buffer *leaf,
> + struct btrfs_key *key, int slot,
> + struct btrfs_key *prev_key)
> +{
> + u32 sectorsize = leaf->fs_info->sectorsize;
> + u32 item_size = btrfs_item_size(leaf, slot);
> +
> + if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
> + file_extent_err(leaf, slot,
> +"unaligned file_offset for encryption context, have %llu should be aligned to %u",
> + key->offset, sectorsize);
> + return -EUCLEAN;
> + }
> +
> + /*
> + * Previous key must have the same key->objectid (ino).
> + * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
> + * But if objectids mismatch, it means we have a missing
> + * INODE_ITEM.
> + */
This isn't a bug, but the comment says "It can be XATTR_ITEM, INODE_ITEM
or just another EXTENT_DATA" - can EXTENT_DATA actually be the previous
key type?
For BTRFS_FSCRYPT_CTX_KEY (type=42), EXTENT_DATA (type=108) cannot be a
previous key because keys are sorted by (objectid, type, offset) and
108 > 42.
The correct previous key types would be: XATTR_ITEM, INODE_ITEM,
FSCRYPT_INODE_CTX_KEY, or another FSCRYPT_CTX_KEY.
> @@ -214,6 +248,7 @@ static int check_extent_data_item(struct extent_buffer *leaf,
> u32 item_size = btrfs_item_size(leaf, slot);
> u64 extent_end;
> u8 policy;
> + u8 fe_type;
>
> if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
> file_extent_err(leaf, slot,
[ ... ]
> @@ -298,6 +333,19 @@ static int check_extent_data_item(struct extent_buffer *leaf,
> return 0;
> }
>
> + if (policy == BTRFS_ENCRYPTION_FSCRYPT) {
> + /*
> + * Only regular and prealloc extents should have an encryption
> + * context.
> + */
> + if (unlikely(fe_type != BTRFS_FILE_EXTENT_REG &&
> + fe_type != BTRFS_FILE_EXTENT_PREALLOC)) {
> + file_extent_err(leaf, slot,
> + "invalid type for encrypted file extent, have %u", fe_type);
> + return -EUCLEAN;
> + }
> + }
Can this validation check detect encrypted inline extents? Looking at
the function flow, inline extents (BTRFS_FILE_EXTENT_INLINE) are handled
at lines 310-333 and return before reaching this check.
If a corrupted filesystem has an encrypted inline extent, it would pass
tree-checker validation. Should the encryption check be moved to also
cover the inline extent path, or should inline extents explicitly reject
BTRFS_ENCRYPTION_FSCRYPT?
Powered by blists - more mailing lists