linux-stable/fs/btrfs
Omar Sandoval 6e78b3f7a1 Btrfs: fix btrfs_decompress_buf2page()
If btrfs_decompress_buf2page() is handed a bio with its page in the
middle of the working buffer, then we adjust the offset into the working
buffer. After we copy into the bio, we advance the iterator by the
number of bytes we copied. Then, we have some logic to handle the case
of discontiguous pages and adjust the offset into the working buffer
again. However, if we didn't advance the bio to a new page, we may enter
this case in error, essentially repeating the adjustment that we already
made when we entered the function. The end result is bogus data in the
bio.

Previously, we only checked for this case when we advanced to a new
page, but the conversion to bio iterators changed that. This restores
the old, correct behavior.

A case I saw when testing with zlib was:

    buf_start = 42769
    total_out = 46865
    working_bytes = total_out - buf_start = 4096
    start_byte = 45056

The condition (total_out > start_byte && buf_start < start_byte) is
true, so we adjust the offset:

    buf_offset = start_byte - buf_start = 2287
    working_bytes -= buf_offset = 1809
    current_buf_start = buf_start = 42769

Then, we copy

    bytes = min(bvec.bv_len, PAGE_SIZE - buf_offset, working_bytes) = 1809
    buf_offset += bytes = 4096
    working_bytes -= bytes = 0
    current_buf_start += bytes = 44578

After bio_advance(), we are still in the same page, so start_byte is the
same. Then, we check (total_out > start_byte && current_buf_start < start_byte),
which is true! So, we adjust the values again:

    buf_offset = start_byte - buf_start = 2287
    working_bytes = total_out - start_byte = 1809
    current_buf_start = buf_start + buf_offset = 45056

But note that working_bytes was already zero before this, so we should
have stopped copying.

Fixes: 974b1adc3b ("btrfs: use bio iterators for the decompression handlers")
Reported-by: Pat Erley <pat-lkml@erley.org>
Reviewed-by: Chris Mason <clm@fb.com>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Chris Mason <clm@fb.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Tested-by: Liu Bo <bo.li.liu@oracle.com>
2017-02-10 19:11:03 -08:00
..
tests btrfs: pull node/sector/stripe sizes out of root and into fs_info 2016-12-06 16:06:58 +01:00
acl.c posix_acl: Clear SGID bit when setting file permissions 2016-09-22 10:55:32 +02:00
async-thread.c btrfs: fix crash when tracepoint arguments are freed by wq callbacks 2017-01-09 11:24:50 +01:00
async-thread.h btrfs: limit async_work allocation and worker func duration 2016-12-13 11:01:30 -08:00
backref.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
backref.h
btrfs_inode.h Btrfs: add a flags field to btrfs_fs_info 2016-09-26 17:59:49 +02:00
check-integrity.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
check-integrity.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
compression.c Btrfs: fix btrfs_decompress_buf2page() 2017-02-10 19:11:03 -08:00
compression.h btrfs: use bio iterators for the decompression handlers 2016-11-30 13:45:19 +01:00
ctree.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
ctree.h Btrfs: don't WARN() in btrfs_transaction_abort() for IO errors 2016-12-09 06:00:28 -08:00
dedupe.h btrfs: expand cow_file_range() to support in-band dedup and subpage-blocksize 2016-07-26 13:52:25 +02:00
delayed-inode.c btrfs: limit async_work allocation and worker func duration 2016-12-13 11:01:30 -08:00
delayed-inode.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
delayed-ref.c btrfs: improve delayed refs iterations 2016-11-30 13:45:21 +01:00
delayed-ref.h Merge branch 'for-chris-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git/fdmanana/linux into for-linus-4.10 2016-12-13 09:14:42 -08:00
dev-replace.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
dev-replace.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
dir-item.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
disk-io.c Merge branch 'for-chris-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git/fdmanana/linux into for-linus-4.10 2016-12-13 09:14:42 -08:00
disk-io.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
export.c btrfs: root->fs_info cleanup, add fs_info convenience variables 2016-12-06 16:06:59 +01:00
export.h
extent-tree.c Btrfs: use down_read_nested to make lockdep silent 2017-01-03 15:19:17 +01:00
extent_io.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
extent_io.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
extent_map.c btrfs: Fix slab accounting flags 2016-07-26 13:52:25 +02:00
extent_map.h
file-item.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
file.c Merge branch 'for-chris-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git/fdmanana/linux into for-linus-4.10 2016-12-13 09:14:42 -08:00
free-space-cache.c btrfs: opencode chunk locking, remove helpers 2016-12-06 16:07:00 +01:00
free-space-cache.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
free-space-tree.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
free-space-tree.h
hash.c btrfs: advertise which crc32c implementation is being used at module load 2016-06-06 14:08:28 +02:00
hash.h btrfs: advertise which crc32c implementation is being used at module load 2016-06-06 14:08:28 +02:00
inode-item.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
inode-map.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
inode-map.h
inode.c Btrfs: remove ->{get, set}_acl() from btrfs_dir_ro_inode_operations 2017-01-26 15:48:56 -08:00
ioctl.c btrfs: fix btrfs_compat_ioctl failures on non-compat ioctls 2017-02-08 17:47:30 +01:00
Kconfig
locking.c
locking.h
lzo.c btrfs: use bio iterators for the decompression handlers 2016-11-30 13:45:19 +01:00
Makefile
math.h
ordered-data.c btrfs: root->fs_info cleanup, add fs_info convenience variables 2016-12-06 16:06:59 +01:00
ordered-data.h btrfs: pull node/sector/stripe sizes out of root and into fs_info 2016-12-06 16:06:58 +01:00
orphan.c
print-tree.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
print-tree.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
props.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
props.h
qgroup.c Merge branch 'for-chris-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git/fdmanana/linux into for-linus-4.10 2016-12-13 09:14:42 -08:00
qgroup.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
raid56.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
raid56.h btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
rcu-string.h
reada.c btrfs: take an fs_info directly when the root is not used otherwise 2016-12-06 16:06:59 +01:00
relocation.c Merge branch 'for-chris-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git/fdmanana/linux into for-linus-4.10 2016-12-13 09:14:42 -08:00
root-tree.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
scrub.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
send.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
send.h
struct-funcs.c btrfs: fix string and comment grammatical issues and typos 2016-05-25 22:35:14 +02:00
super.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
sysfs.c btrfs: convert printk(KERN_* to use pr_* calls 2016-09-26 18:08:44 +02:00
sysfs.h
transaction.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
transaction.h btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
tree-defrag.c
tree-log.c Btrfs: fix lockdep warning about log_mutex 2017-01-03 15:19:28 +01:00
tree-log.h Btrfs: fix lockdep warning on deadlock against an inode's log mutex 2016-08-25 03:58:32 -07:00
ulist.c btrfs: fix string and comment grammatical issues and typos 2016-05-25 22:35:14 +02:00
ulist.h
uuid-tree.c btrfs: return the actual error value from from btrfs_uuid_tree_iterate 2016-12-19 18:08:15 +01:00
volumes.c btrfs: opencode chunk locking, remove helpers 2016-12-06 16:07:00 +01:00
volumes.h btrfs: opencode chunk locking, remove helpers 2016-12-06 16:07:00 +01:00
xattr.c btrfs: remove root parameter from transaction commit/end routines 2016-12-06 16:07:00 +01:00
xattr.h btrfs: Switch to generic xattr handlers 2016-05-17 19:17:09 -04:00
zlib.c btrfs: use bio iterators for the decompression handlers 2016-11-30 13:45:19 +01:00