* [PATCH] btrfs: fix invalid leaf access due to inline extent during lseek
@ 2023-01-12 14:17 fdmanana
2023-01-12 14:49 ` David Sterba
0 siblings, 1 reply; 2+ messages in thread
From: fdmanana @ 2023-01-12 14:17 UTC (permalink / raw)
To: linux-btrfs
From: Filipe Manana <fdmanana@suse.com>
During lseek, for SEEK_DATA and SEEK_HOLE modes, we access the disk_bytenr
of anextent without checking its type. However inline extents have their
data starting the offset of the disk_bytenr field, so accessing that field
when we have an inline extent can result in either of the following:
1) Interpret the inline extent's data as a disk_bytenr value;
2) In case the inline data is less than 8 bytes, we access part of some
other item in the leaf, or unused space in the leaf;
3) In case the inline data is less than 8 bytes and the extent item is
the first item in the leaf, we can access beyond the leaf's limit.
So fix this by not accessing the disk_bytenr field if we have an inline
extent.
Fixes: b6e833567ea1 ("btrfs: make hole and data seeking a lot more efficient")
Reported-by: Matthias Schoepfer <matthias.schoepfer@googlemail.com>
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216908
Link: https://lore.kernel.org/linux-btrfs/7f25442f-b121-2a3a-5a3d-22bcaae83cd4@leemhuis.info/
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
fs/btrfs/file.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 834bbcb91102..af046d22300e 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -3541,6 +3541,7 @@ static loff_t find_desired_extent(struct file *file, loff_t offset, int whence)
struct extent_buffer *leaf = path->nodes[0];
struct btrfs_file_extent_item *extent;
u64 extent_end;
+ u8 type;
if (path->slots[0] >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, path);
@@ -3596,10 +3597,16 @@ static loff_t find_desired_extent(struct file *file, loff_t offset, int whence)
extent = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_file_extent_item);
+ type = btrfs_file_extent_type(leaf, extent);
- if (btrfs_file_extent_disk_bytenr(leaf, extent) == 0 ||
- btrfs_file_extent_type(leaf, extent) ==
- BTRFS_FILE_EXTENT_PREALLOC) {
+ /*
+ * Can't access the extent's disk_bytenr field if this is an
+ * inline extent, since at that offset, it's where the extent
+ * data starts.
+ */
+ if (type == BTRFS_FILE_EXTENT_PREALLOC ||
+ (type == BTRFS_FILE_EXTENT_REG &&
+ btrfs_file_extent_disk_bytenr(leaf, extent) == 0)) {
/*
* Explicit hole or prealloc extent, search for delalloc.
* A prealloc extent is treated like a hole.
--
2.35.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] btrfs: fix invalid leaf access due to inline extent during lseek
2023-01-12 14:17 [PATCH] btrfs: fix invalid leaf access due to inline extent during lseek fdmanana
@ 2023-01-12 14:49 ` David Sterba
0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2023-01-12 14:49 UTC (permalink / raw)
To: fdmanana; +Cc: linux-btrfs
On Thu, Jan 12, 2023 at 02:17:20PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> During lseek, for SEEK_DATA and SEEK_HOLE modes, we access the disk_bytenr
> of anextent without checking its type. However inline extents have their
> data starting the offset of the disk_bytenr field, so accessing that field
> when we have an inline extent can result in either of the following:
>
> 1) Interpret the inline extent's data as a disk_bytenr value;
>
> 2) In case the inline data is less than 8 bytes, we access part of some
> other item in the leaf, or unused space in the leaf;
>
> 3) In case the inline data is less than 8 bytes and the extent item is
> the first item in the leaf, we can access beyond the leaf's limit.
>
> So fix this by not accessing the disk_bytenr field if we have an inline
> extent.
>
> Fixes: b6e833567ea1 ("btrfs: make hole and data seeking a lot more efficient")
> Reported-by: Matthias Schoepfer <matthias.schoepfer@googlemail.com>
> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216908
> Link: https://lore.kernel.org/linux-btrfs/7f25442f-b121-2a3a-5a3d-22bcaae83cd4@leemhuis.info/
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
Added to misc-next, thanks.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-01-12 15:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-12 14:17 [PATCH] btrfs: fix invalid leaf access due to inline extent during lseek fdmanana
2023-01-12 14:49 ` David Sterba
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.