btrfs: handle highmem folios in read_key_bytes
Subsystems (2)
PATCH 1/1 btrfs: handle highmem folios in read_key_bytes
On 32-bit systems with highmem, folio_address() can return NULL for unmapped highmem folios. When this NULL is passed as the dest parameter to read_key_bytes() with a non-NULL dest_folio, it violates the function's contract (which requires dest to be non-NULL when dest_folio is provided).
The original bug had two symptoms:
1. Unsigned len underflow when len -= copy_bytes executes (infinite loop)
2. The folio remains uninitialized because the copy block is skipped
Fix requires two changes:
1. Change "if (!dest)" to "if (!dest && !dest_folio)"
- Prevents the "counting-only" mode when dest_folio is provided
- Fixes the underflow/infinite loop
2. Change "if (dest)" to "if (dest || dest_folio)"
- Ensures the copy block executes when dest_folio is provided
- Allows kmap_local_folio() to properly map the highmem folio
- Actually writes data to the folio
Without the second change, the highmem folio is not populated even though the read succeeds, causing subsequent fs-verity verification to operate on stale or uninitialized data.
--- fs/btrfs/verity.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/verity.c +2 -2
@@ -351,7 +351,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, }/* desc = NULL to just sum all the item lengths */ -if (!dest) +if (!dest && !dest_folio) copy_end = item_end;else copy_end =min (offset + len, item_end);
@@ -362,7 +362,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,/* Offset from the start of item for copying */ copy_offset = offset - key.offset; -if (dest) { +if (dest || dest_folio) {if (dest_folio) kaddr =kmap_local_folio (dest_folio,0 );
Signature · 1 line
2.25.1
Quoted 4 lines · Hongling Zeng
On 32-bit systems with highmem,
folio_address()can return NULL for unmapped highmem folios. When this NULL is passed as the dest parameter toread_key_bytes()with a non-NULLdest_folio, it violates the function's contract (which requires dest to be non-NULL whendest_foliois provided).
But metadata folios are not allocated by page cache, but by btrfs itself, which always use GFP_NOFS then attach the folio to page cache, thus should not get highmem memory in the first place.
And there is no way to trigger read from userspace on btree inode, so there should be no highmem folios from the beginning.
Quoted 4 lines
The original bug had two symptoms:
1. Unsigned len underflow when len -=copy_bytesexecutes (infinite loop)
2. The folio remains uninitialized because the copy block is skipped
So did you really hit the problem in the real world?
Quoted 12 lines
Fix requires two changes:
1. Change "if (!dest)" to "if (!dest && !dest_folio)"
- Prevents the "counting-only" mode whendest_foliois provided
- Fixes the underflow/infinite loop2. Change "if (dest)" to "if (dest ||
dest_folio)"
- Ensures the copy block executes whendest_foliois provided
- Allowskmap_local_folio()to properly map the highmem folio
- Actually writes data to the folio
Quoted 7 lines
Without the second change, the highmem folio is not populated even though the read succeeds, causing subsequent fs-verity verification to operate on stale or uninitialized data.
Fixes 884937793db5 ("btrfs: convert read_key_bytes() to take a folio")Signed-off-by Hongling Zeng <[email protected]>
Missing LLM disclosure.
Quoted 26 lines
--- fs/btrfs/verity.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/btrfs/verity.c +2
-2@@ -351,7 +351,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, }/* desc = NULL to just sum all the item lengths */ -if (!dest) +if (!dest && !dest_folio) copy_end = item_end;else copy_end =min (offset + len, item_end);@@ -362,7 +362,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,/* Offset from the start of item for copying */ copy_offset = offset - key.offset; -if (dest) { +if (dest || dest_folio) {if (dest_folio) kaddr =kmap_local_folio (dest_folio,0 );
Quoted 11 lines · Qu Wenruo
Quoted 4 lines · Hongling Zeng
On 32-bit systems with highmem,
folio_address()can return NULL for unmapped highmem folios. When this NULL is passed as the dest parameter toread_key_bytes()with a non-NULLdest_folio, it violates the function's contract (which requires dest to be non-NULL whendest_foliois provided).But metadata folios are not allocated by page cache, but by btrfs itself, which always use
GFP_NOFSthen attach the folio to page cache, thus should not get highmem memory in the first place.And there is no way to trigger read from userspace on btree inode, so there should be no highmem folios from the beginning.
Thanks for the review.
I was not able to reproduce this on a real system. This was
identified through code analysis with assistance from glm 5.5, not from a
real-world encounter.
Your analysis about btrfs using ~__GFP_FS for folio allocation and
the lack
of userspace trigger paths makes sense. The proposed changes are
based on the
generic folio_address() behavior and the theoretical dest/dest_folio invariant, but I do not currently have evidence that a highmem folio can reach read_key_bytes() in this state.
I will withdraw this patch rather than claim a reachable highmem bug without a reproducer.
Quoted 54 lines
Quoted 5 lines
The original bug had two symptoms:
1. Unsigned len underflow when len -=copy_bytesexecutes (infinite loop)
2. The folio remains uninitialized because the copy block is skippedSo did you really hit the problem in the real world?
Quoted 12 lines
Fix requires two changes:
1. Change "if (!dest)" to "if (!dest && !dest_folio)"
- Prevents the "counting-only" mode whendest_foliois provided
- Fixes the underflow/infinite loop2. Change "if (dest)" to "if (dest ||
dest_folio)"
- Ensures the copy block executes whendest_foliois provided
- Allowskmap_local_folio()to properly map the highmem folio
- Actually writes data to the folioQuoted 7 lines
Without the second change, the highmem folio is not populated even though the read succeeds, causing subsequent fs-verity verification to operate on stale or uninitialized data.
Fixes 884937793db5 ("btrfs: convert read_key_bytes() to take a folio")Signed-off-by Hongling Zeng <[email protected]>Missing LLM disclosure.
Quoted 25 lines
--- fs/btrfs/verity.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/btrfs/verity.c +2
-1@@ -351,7 +351,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, }/* desc = NULL to just sum all the item lengths */ -if (!dest) +if (!dest && !dest_folio) copy_end = item_end;else copy_end =min (offset + len, item_end);@@ -362,7 +362,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,/* Offset from the start of item for copying */ copy_offset = offset - key.offset; - if (dest) { +if (dest || dest_folio) {if (dest_folio) kaddr =kmap_local_folio (dest_folio,0 );
Quoted 15 lines · Hongling Zeng
Quoted 11 lines · Qu Wenruo
Quoted 4 lines · Hongling Zeng
On 32-bit systems with highmem,
folio_address()can return NULL for unmapped highmem folios. When this NULL is passed as the dest parameter toread_key_bytes()with a non-NULLdest_folio, it violates the function's contract (which requires dest to be non-NULL whendest_foliois provided).But metadata folios are not allocated by page cache, but by btrfs itself, which always use
GFP_NOFSthen attach the folio to page cache, thus should not get highmem memory in the first place.And there is no way to trigger read from userspace on btree inode, so there should be no highmem folios from the beginning.
Thanks for the review.
Quoted 7 lines
I was not able to reproduce this on a real system. This was
identified through code analysis with assistance from glm 5.5, not from a
real-world encounter.Your analysis about btrfs using ~__GFP_FS for folio allocation and
the lack
of userspace trigger paths makes sense.
Sorry, it doesn't.
I mis-read the context and considered the folio to be from btree inode, but it's not.
So it can still be from page cache, which can be highmem.
But on the other hand, I do not think your fix is doing any good to the readability either.
I'll rework the involved function and caller to remove the folio parameter, and instead always pass a vaddr and length to read_key_bytes(), and let the caller to handle the kmap instead.
Quoted 63 lines
The proposed changes are based on the
genericfolio_address()behavior and the theoretical dest/dest_folio invariant, but I do not currently have evidence that a highmem folio can reachread_key_bytes()in this state.I will withdraw this patch rather than claim a reachable highmem bug without a reproducer.
Quoted 54 lines
Quoted 5 lines
The original bug had two symptoms:
1. Unsigned len underflow when len -=copy_bytesexecutes (infinite loop)
2. The folio remains uninitialized because the copy block is skippedSo did you really hit the problem in the real world?
Quoted 12 lines
Fix requires two changes:
1. Change "if (!dest)" to "if (!dest && !dest_folio)"
- Prevents the "counting-only" mode whendest_foliois provided
- Fixes the underflow/infinite loop2. Change "if (dest)" to "if (dest ||
dest_folio)"
- Ensures the copy block executes whendest_foliois provided
- Allowskmap_local_folio()to properly map the highmem folio
- Actually writes data to the folioQuoted 7 lines
Without the second change, the highmem folio is not populated even though the read succeeds, causing subsequent fs-verity verification to operate on stale or uninitialized data.
Fixes 884937793db5 ("btrfs: convert read_key_bytes() to take a folio")Signed-off-by Hongling Zeng <[email protected]>Missing LLM disclosure.
Quoted 25 lines
---
fs/btrfs/verity.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)fs/btrfs/verity.c +1
-1@@ -351,7 +351,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, } /* desc = NULL to just sum all the item lengths */ - if (!dest) + if (!dest && !dest_folio) copy_end = item_end;else
copy_end=min(offset + len, item_end);fs/btrfs/verity.c +1
-0@@ -362,7 +362,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, /* Offset from the start of item for copying */ copy_offset = offset - key.offset; - if (dest) { + if (dest || dest_folio) { if (dest_folio) kaddr = kmap_local_folio(dest_folio, 0);
Quoted 28 lines · Qu Wenruo
Quoted 15 lines · Hongling Zeng
Quoted 11 lines · Qu Wenruo
Quoted 4 lines · Hongling Zeng
On 32-bit systems with highmem,
folio_address()can return NULL for unmapped highmem folios. When this NULL is passed as the dest parameter toread_key_bytes()with a non-NULLdest_folio, it violates the function's contract (which requires dest to be non-NULL whendest_foliois provided).But metadata folios are not allocated by page cache, but by btrfs itself, which always use
GFP_NOFSthen attach the folio to page cache, thus should not get highmem memory in the first place.And there is no way to trigger read from userspace on btree inode, so there should be no highmem folios from the beginning.
Thanks for the review.
Quoted 7 lines
I was not able to reproduce this on a real system. This was
identified through code analysis with assistance from glm 5.5, not from a
real-world encounter.Your analysis about btrfs using ~__GFP_FS for folio allocation and
the lack
of userspace trigger paths makes sense.Sorry, it doesn't.
Quoted 7 lines
I mis-read the context and considered the folio to be from btree inode, but it's not.
So it can still be from page cache, which can be highmem.
But on the other hand, I do not think your fix is doing any good to the readability either.
I'll rework the involved function and caller to remove the folio parameter, and instead always pass a vaddr and length to
read_key_bytes(), and let the caller to handle the kmap instead.
Thanks for the feedback.
I understand your rework plan. Will it also address the folio_unlock issue in the error path? The current code leaves the folio permanently locked after filemap_add_folio() + read_key_bytes() failure.
If your rework doesn't cover this, could you help to review thefolio_unlock
fix patch?
Quoted 65 lines
Quoted 65 lines
The proposed changes are based on the
genericfolio_address()behavior and the theoretical dest/dest_folio invariant, but I do not currently have evidence that a highmem
folio can
reachread_key_bytes()in this state.I will withdraw this patch rather than claim a reachable highmem bug without a reproducer.
Quoted 54 lines
Quoted 5 lines
The original bug had two symptoms:
1. Unsigned len underflow when len -=copy_bytesexecutes (infinite loop)
2. The folio remains uninitialized because the copy block is skippedSo did you really hit the problem in the real world?
Quoted 12 lines
Fix requires two changes:
1. Change "if (!dest)" to "if (!dest && !dest_folio)"
- Prevents the "counting-only" mode whendest_foliois provided
- Fixes the underflow/infinite loop2. Change "if (dest)" to "if (dest ||
dest_folio)"
- Ensures the copy block executes whendest_foliois provided
- Allowskmap_local_folio()to properly map the highmem folio
- Actually writes data to the folioQuoted 7 lines
Without the second change, the highmem folio is not populated even though the read succeeds, causing subsequent fs-verity verification to operate on stale or uninitialized data.
Fixes 884937793db5 ("btrfs: convert read_key_bytes() to take a folio")Signed-off-by Hongling Zeng <[email protected]>Missing LLM disclosure.
Quoted 25 lines
--- fs/btrfs/verity.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/btrfs/verity.c +2
-1@@ -351,7 +351,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, }/* desc = NULL to just sum all the item lengths */ -if (!dest) +if (!dest && !dest_folio) copy_end = item_end;else copy_end =min (offset + len, item_end);@@ -362,7 +362,7 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,/* Offset from the start of item for copying */ copy_offset = offset - key.offset; - if (dest) { +if (dest || dest_folio) {if (dest_folio) kaddr =kmap_local_folio (dest_folio,0 );