fs: btrfs: fix out of bounds write
PATCH 1/1 fs: btrfs: fix out of bounds write
Fix btrfs_read/read_and_truncate_page write out of bounds of destination buffer. Old behavior break bootstd malloc'd buffers of exact file size. Previously this OOB write have not been noticed because distroboot usually read files into huge static memory areas.
---
fs/btrfs/inode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +2 -2
@@ -640,7 +640,7 @@ static int read_and_truncate_page(struct btrfs_path *path, extent_type =btrfs_file_extent_type (leaf, fi);if (extent_type ==BTRFS_FILE_EXTENT_INLINE ) { ret =btrfs_read_extent_inline (path, fi, buf); -memcpy (dest, buf + page_off,min (page_len, ret)); +memcpy (dest, buf + page_off,min (min (page_len, ret), len));free (buf);return len; }
@@ -652,7 +652,7 @@ static int read_and_truncate_page(struct btrfs_path *path,free (buf);return ret; } -memcpy (dest, buf + page_off, page_len); +memcpy (dest, buf + page_off,min (page_len, len));free (buf);return len; }
Signature · 1 line
2.34.1
Quoted 6 lines · Alex Shumsky
Fix btrfs_read/read_and_truncate_page write out of bounds of destination buffer. Old behavior break bootstd malloc'd buffers of exact file size. Previously this OOB write have not been noticed because distroboot usually read files into huge static memory areas.
Signed-off-by Alex Shumsky <[email protected]>
Thanks,
Qu
Quoted 27 lines
---
fs/btrfs/inode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/btrfs/inode.c +2
-2@@ -640,7 +640,7 @@ static int read_and_truncate_page(struct btrfs_path *path, extent_type =btrfs_file_extent_type (leaf, fi);if (extent_type ==BTRFS_FILE_EXTENT_INLINE ) { ret =btrfs_read_extent_inline (path, fi, buf); -memcpy (dest, buf + page_off,min (page_len, ret)); +memcpy (dest, buf + page_off,min (min (page_len, ret), len));free (buf);return len; }@@ -652,7 +652,7 @@ static int read_and_truncate_page(struct btrfs_path *path,free (buf);return ret; } -memcpy (dest, buf + page_off, page_len); +memcpy (dest, buf + page_off,min (page_len, len));free (buf);return len; }
Quoted 16 lines · Alex Shumsky
Fix btrfs_read/read_and_truncate_page write out of bounds of destination buffer. Old behavior break bootstd malloc'd buffers of exact file size. Previously this OOB write have not been noticed because distroboot usually read files into huge static memory areas.
Signed-off-by Alex Shumsky <[email protected]>---
fs/btrfs/inode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/btrfs/inode.c +0
-0
fs/btrfs/inode.c static int read_and_truncate_page(struct btrfs_path *path, · line 643
Quoted 5 lines
fs/btrfs/inode.c +1
-1@@ -640,7 +640,7 @@ static int read_and_truncate_page(struct btrfs_path *path, extent_type =btrfs_file_extent_type (leaf, fi);if (extent_type ==BTRFS_FILE_EXTENT_INLINE ) { ret =btrfs_read_extent_inline (path, fi, buf); -memcpy (dest, buf + page_off,min (page_len, ret)); +memcpy (dest, buf + page_off,min (min (page_len, ret), len));
This looks (still) broken for the compressed inline extent error paths, where ret=-errno .
nit: please prefix your patch subject with "uboot" so that casual linux-btrfs readers like me don't get confused by matching kernel file paths.
Quoted 6 lines · Alex Shumsky
Fix btrfs_read/read_and_truncate_page write out of bounds of destination buffer. Old behavior break bootstd malloc'd buffers of exact file size. Previously this OOB write have not been noticed because distroboot usually read files into huge static memory areas.
Signed-off-by Alex Shumsky <[email protected]>
Could you add a Fixes tag?
regards,
dan carpenter
Quoted 16 lines · On Mon, 17 Jun 2024 22:49:47 +0300 Alex Shumsky <[email protected]> wrote:
Fix btrfs_read/read_and_truncate_page write out of bounds of destination buffer. Old behavior break bootstd malloc'd buffers of exact file size. Previously this OOB write have not been noticed because distroboot usually read files into huge static memory areas.
Signed-off-by Alex Shumsky <[email protected]>---
fs/btrfs/inode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/btrfs/inode.c +0
-0
fs/btrfs/inode.c static int read_and_truncate_page(struct btrfs_path *path, · line 643
Quoted 5 lines
fs/btrfs/inode.c +1
-1@@ -640,7 +640,7 @@ static int read_and_truncate_page(struct btrfs_path *path, extent_type =btrfs_file_extent_type (leaf, fi);if (extent_type ==BTRFS_FILE_EXTENT_INLINE ) { ret =btrfs_read_extent_inline (path, fi, buf); -memcpy (dest, buf + page_off,min (page_len, ret)); +memcpy (dest, buf + page_off,min (min (page_len, ret), len));
Use min3() instead of min(min())