btrfs: Avoid potential out-of-bounds in btrfs_encode_fh()
PATCH 1/1 btrfs: Avoid potential out-of-bounds in btrfs_encode_fh()
Hello all,
The function btrfs_encode_fh() does not properly account for the three cases it handles.
Before writing to the file handle (fh), the function only returns to the user BTRFS_FID_SIZE_NON_CONNECTABLE (5 dwords, 20 bytes) or BTRFS_FID_SIZE_CONNECTABLE (8 dwords, 32 bytes).
However, when a parent exists and the root ID of the parent and the inode are different, the function writes BTRFS_FID_SIZE_CONNECTABLE_ROOT (10 dwords, 40 bytes).
If *max_len is not large enough, this write goes out of bounds because BTRFS_FID_SIZE_CONNECTABLE_ROOT is greater than BTRFS_FID_SIZE_CONNECTABLE originally returned.
This results in an 8-byte out-of-bounds write at fid->parent_root_objectid = parent_root_id.
A previous attempt to fix this issue was made but was lost.
https://lore.kernel.org/all/[email protected]/
Although this issue does not seem to be easily triggerable, it is a potential memory corruption bug that should be fixed. This patch resolves the issue by ensuring the function returns the appropriate size for all three cases and validates that *max_len is large enough before writing any data.
Tested on v6.17-rc4.
---
fs/btrfs/export.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/export.c +6 -1
@@ -23,7 +23,10 @@ static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,int type;if (parent && (len <BTRFS_FID_SIZE_CONNECTABLE )) { - *max_len =BTRFS_FID_SIZE_CONNECTABLE ; +if (btrfs_root_id (BTRFS_I (inode)->root) !=btrfs_root_id (BTRFS_I (parent)->root)) + *max_len =BTRFS_FID_SIZE_CONNECTABLE_ROOT ; +else + *max_len =BTRFS_FID_SIZE_CONNECTABLE ;return FILEID_INVALID ; }else if (len <BTRFS_FID_SIZE_NON_CONNECTABLE ) { *max_len =BTRFS_FID_SIZE_NON_CONNECTABLE ;
@@ -45,6 +48,8 @@ static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len, parent_root_id =btrfs_root_id (BTRFS_I (parent)->root);if (parent_root_id != fid->root_objectid) { +if (*max_len <BTRFS_FID_SIZE_CONNECTABLE_ROOT ) +return FILEID_INVALID ; fid->parent_root_objectid = parent_root_id; len =BTRFS_FID_SIZE_CONNECTABLE_ROOT ; type =FILEID_BTRFS_WITH_PARENT_ROOT ;
Quoted 30 lines · Anderson Nascimento
Hello all,
The function
btrfs_encode_fh()does not properly account for the three cases it handles.Before writing to the file handle (fh), the function only returns to the user
BTRFS_FID_SIZE_NON_CONNECTABLE(5 dwords, 20 bytes) or BTRFS_FID_SIZE_CONNECTABLE (8 dwords, 32 bytes).However, when a parent exists and the root ID of the parent and the inode are different, the function writes
BTRFS_FID_SIZE_CONNECTABLE_ROOT(10 dwords, 40 bytes).If
*max_lenis not large enough, this write goes out of bounds becauseBTRFS_FID_SIZE_CONNECTABLE_ROOTis greater than BTRFS_FID_SIZE_CONNECTABLE originally returned.This results in an 8-byte out-of-bounds write at
fid->parent_root_objectid = parent_root_id.A previous attempt to fix this issue was made but was lost.
https://lore.kernel.org/all/[email protected]/
Although this issue does not seem to be easily triggerable, it is a potential memory corruption bug that should be fixed. This patch resolves the issue by ensuring the function returns the appropriate size for all three cases and validates that
*max_lenis large enough before writing any data.
Quoted 4 lines
Tested on v6.17-rc4.
Fixes be6e8dc0ba84 ("NFS support for btrfs - v3")Signed-off-by Anderson Nascimento <[email protected]>
Thanks for finding the problem and the fix. It's 17 years old though the other patch was sent about 2 years after btrfs merge to linux kernel. I'll add it to for-next, with the minor whitespace issues fixed.
Thank you!
Quoted 40 lines · David Sterba
Quoted 30 lines · Anderson Nascimento
Hello all,
The function
btrfs_encode_fh()does not properly account for the three cases it handles.Before writing to the file handle (fh), the function only returns to the user
BTRFS_FID_SIZE_NON_CONNECTABLE(5 dwords, 20 bytes) orBTRFS_FID_SIZE_CONNECTABLE(8 dwords, 32 bytes).However, when a parent exists and the root ID of the parent and the inode are different, the function writes
BTRFS_FID_SIZE_CONNECTABLE_ROOT(10 dwords, 40 bytes).If
*max_lenis not large enough, this write goes out of bounds becauseBTRFS_FID_SIZE_CONNECTABLE_ROOTis greater thanBTRFS_FID_SIZE_CONNECTABLEoriginally returned.This results in an 8-byte out-of-bounds write at
fid->parent_root_objectid=parent_root_id.A previous attempt to fix this issue was made but was lost.
https://lore.kernel.org/all/[email protected]/
Although this issue does not seem to be easily triggerable, it is a potential memory corruption bug that should be fixed. This patch resolves the issue by ensuring the function returns the appropriate size for all three cases and validates that
*max_lenis large enough before writing any data.Quoted 4 lines
Tested on v6.17-rc4.
Fixes be6e8dc0ba84 ("NFS support for btrfs - v3")Signed-off-by Anderson Nascimento <[email protected]>Thanks for finding the problem and the fix. It's 17 years old though the other patch was sent about 2 years after btrfs merge to linux kernel. I'll add it to for-next, with the minor whitespace issues fixed.
Signature · 3 lines
Anderson Nascimento Allele Security Intelligence https://www.allelesecurity.com
Quoted 32 lines · David Sterba
Quoted 30 lines · Anderson Nascimento
Hello all,
The function
btrfs_encode_fh()does not properly account for the three cases it handles.Before writing to the file handle (fh), the function only returns to the user
BTRFS_FID_SIZE_NON_CONNECTABLE(5 dwords, 20 bytes) orBTRFS_FID_SIZE_CONNECTABLE(8 dwords, 32 bytes).However, when a parent exists and the root ID of the parent and the inode are different, the function writes
BTRFS_FID_SIZE_CONNECTABLE_ROOT(10 dwords, 40 bytes).If
*max_lenis not large enough, this write goes out of bounds becauseBTRFS_FID_SIZE_CONNECTABLE_ROOTis greater thanBTRFS_FID_SIZE_CONNECTABLEoriginally returned.This results in an 8-byte out-of-bounds write at
fid->parent_root_objectid=parent_root_id.A previous attempt to fix this issue was made but was lost.
https://lore.kernel.org/all/[email protected]/
Although this issue does not seem to be easily triggerable, it is a potential memory corruption bug that should be fixed. This patch resolves the issue by ensuring the function returns the appropriate size for all three cases and validates that
*max_lenis large enough before writing any data.
Quoted 8 lines
Quoted 4 lines
Tested on v6.17-rc4.
Fixes be6e8dc0ba84 ("NFS support for btrfs - v3")Signed-off-by Anderson Nascimento <[email protected]>Thanks for finding the problem and the fix. It's 17 years old though the other patch was sent about 2 years after btrfs merge to linux kernel. I'll add it to for-next, with the minor whitespace issues fixed.
David, has it been queued somewhere? I don't see it in any of your branches.
Signature · 3 lines
Anderson Nascimento Allele Security Intelligence https://www.allelesecurity.com
Quoted 35 lines · Anderson Nascimento
Quoted 34 lines · David Sterba
Quoted 32 lines · Anderson Nascimento
Hello all,
The function
btrfs_encode_fh()does not properly account for the three cases it handles.Before writing to the file handle (fh), the function only returns to the user
BTRFS_FID_SIZE_NON_CONNECTABLE(5 dwords, 20 bytes) orBTRFS_FID_SIZE_CONNECTABLE(8 dwords, 32 bytes).However, when a parent exists and the root ID of the parent and the inode are different, the function writes
BTRFS_FID_SIZE_CONNECTABLE_ROOT(10 dwords, 40 bytes).If
*max_lenis not large enough, this write goes out of bounds becauseBTRFS_FID_SIZE_CONNECTABLE_ROOTis greater thanBTRFS_FID_SIZE_CONNECTABLEoriginally returned.This results in an 8-byte out-of-bounds write at
fid->parent_root_objectid=parent_root_id.A previous attempt to fix this issue was made but was lost.
https://lore.kernel.org/all/[email protected]/
Although this issue does not seem to be easily triggerable, it is a potential memory corruption bug that should be fixed. This patch resolves the issue by ensuring the function returns the appropriate size for all three cases and validates that
*max_lenis large enough before writing any data.Tested on v6.17-rc4.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Fixes be6e8dc0ba84 ("NFS support for btrfs - v3")Signed-off-by Anderson Nascimento <[email protected]>Thanks for finding the problem and the fix. It's 17 years old though the other patch was sent about 2 years after btrfs merge to linux kernel. I'll add it to for-next, with the minor whitespace issues fixed.
David, has it been queued somewhere? I don't see it in any of your branches.
That's strange, I thought I'd applied it the same day but the patch is nowhere to be found. I'll add it to for-next again, sorry.
Quoted 40 lines · David Sterba
Quoted 38 lines · Anderson Nascimento
Quoted 37 lines · David Sterba
Quoted 34 lines · Anderson Nascimento
Hello all,
The function
btrfs_encode_fh()does not properly account for the three cases it handles.Before writing to the file handle (fh), the function only returns to the user
BTRFS_FID_SIZE_NON_CONNECTABLE(5 dwords, 20 bytes) orBTRFS_FID_SIZE_CONNECTABLE(8 dwords, 32 bytes).However, when a parent exists and the root ID of the parent and the inode are different, the function writes
BTRFS_FID_SIZE_CONNECTABLE_ROOT(10 dwords, 40 bytes).If
*max_lenis not large enough, this write goes out of bounds becauseBTRFS_FID_SIZE_CONNECTABLE_ROOTis greater thanBTRFS_FID_SIZE_CONNECTABLEoriginally returned.This results in an 8-byte out-of-bounds write at
fid->parent_root_objectid=parent_root_id.A previous attempt to fix this issue was made but was lost.
https://lore.kernel.org/all/[email protected]/
Although this issue does not seem to be easily triggerable, it is a potential memory corruption bug that should be fixed. This patch resolves the issue by ensuring the function returns the appropriate size for all three cases and validates that
*max_lenis large enough before writing any data.Tested on v6.17-rc4.
Fixes be6e8dc0ba84 ("NFS support for btrfs - v3")Signed-off-by Anderson Nascimento <[email protected]>
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Thanks for finding the problem and the fix. It's 17 years old though the other patch was sent about 2 years after btrfs merge to linux kernel. I'll add it to for-next, with the minor whitespace issues fixed.
David, has it been queued somewhere? I don't see it in any of your branches.
That's strange, I thought I'd applied it the same day but the patch is nowhere to be found. I'll add it to for-next again, sorry.
No worries, thank you very much.
Signature · 3 lines
Anderson Nascimento Allele Security Intelligence https://www.allelesecurity.com