aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorLiu Bo <bo.li.liu@oracle.com>2017-08-09 13:10:16 -0400
committerDavid Sterba <dsterba@suse.com>2017-08-21 11:47:14 -0400
commitf716abd55d1e141772fd44a99c07c89f4aff1978 (patch)
tree10a5ae1cd289227f58c2826ee0ba9fde328e27a0 /fs
parentc59efa7eb2400ae356553a8ed5e3f3ef4cc47831 (diff)
Btrfs: fix out of bounds array access while reading extent buffer
There is a corner case that slips through the checkers in functions reading extent buffer, ie. if (start < eb->len) and (start + len > eb->len), then a) map_private_extent_buffer() returns immediately because it's thinking the range spans across two pages, b) and the checkers in read_extent_buffer(), WARN_ON(start > eb->len) and WARN_ON(start + len > eb->start + eb->len), both are OK in this corner case, but it'd actually try to access the eb->pages out of bounds because of (start + len > eb->len). The case is found by switching extent inline ref type from shared data ref to non-shared data ref, which is a kind of metadata corruption. It'd use the wrong helper to access the eb, eg. btrfs_extent_data_ref_root(eb, ref) is used but the %ref passing here is "struct btrfs_shared_data_ref". And if the extent item happens to be the first item in the eb, then offset/length will get over eb->len which ends up an invalid memory access. This is adding proper checks in order to avoid invalid memory access, ie. 'general protection fault', before it's too late. Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Liu Bo <bo.li.liu@oracle.com> Signed-off-by: Chris Mason <clm@fb.com> Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/extent_io.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index ea4947c97505..d17783d70228 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -5404,8 +5404,12 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
5404 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); 5404 size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
5405 unsigned long i = (start_offset + start) >> PAGE_SHIFT; 5405 unsigned long i = (start_offset + start) >> PAGE_SHIFT;
5406 5406
5407 WARN_ON(start > eb->len); 5407 if (start + len > eb->len) {
5408 WARN_ON(start + len > eb->start + eb->len); 5408 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5409 eb->start, eb->len, start, len);
5410 memset(dst, 0, len);
5411 return;
5412 }
5409 5413
5410 offset = (start_offset + start) & (PAGE_SIZE - 1); 5414 offset = (start_offset + start) & (PAGE_SIZE - 1);
5411 5415
@@ -5478,6 +5482,12 @@ int map_private_extent_buffer(const struct extent_buffer *eb,
5478 unsigned long end_i = (start_offset + start + min_len - 1) >> 5482 unsigned long end_i = (start_offset + start + min_len - 1) >>
5479 PAGE_SHIFT; 5483 PAGE_SHIFT;
5480 5484
5485 if (start + min_len > eb->len) {
5486 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5487 eb->start, eb->len, start, min_len);
5488 return -EINVAL;
5489 }
5490
5481 if (i != end_i) 5491 if (i != end_i)
5482 return 1; 5492 return 1;
5483 5493
@@ -5489,12 +5499,6 @@ int map_private_extent_buffer(const struct extent_buffer *eb,
5489 *map_start = ((u64)i << PAGE_SHIFT) - start_offset; 5499 *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
5490 } 5500 }
5491 5501
5492 if (start + min_len > eb->len) {
5493 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
5494 eb->start, eb->len, start, min_len);
5495 return -EINVAL;
5496 }
5497
5498 p = eb->pages[i]; 5502 p = eb->pages[i];
5499 kaddr = page_address(p); 5503 kaddr = page_address(p);
5500 *map = kaddr + offset; 5504 *map = kaddr + offset;