btrfs: Fix for CVE-2019-18885
Subsystems (2)
cover btrfs: Fix for CVE-2019-18885
CVE Description:
NVD Site Link: https://nvd.nist.gov/vuln/detail?vulnId=CVE-2019-18885
It was discovered that the btrfs file system in the Linux kernel did not properly validate metadata, leading to a NULL pointer dereference. An attacker could use this to specially craft a file system image that, when mounted, could cause a denial of service (system crash).
[PATCH v4.19.y 1/2]:
Backporting of upsream commit 09ba3bc9dd15:
btrfs: merge btrfs_find_device and find_device
[PATCH v4.19.y 2/2]:
Backporting of upstream commit 62fdaa52a3d0:
btrfs: Detect unbalanced tree with empty leaf before crashing
On NVD site link of "commit 09ba3bc9dd150457c506e4661380a6183af651c1" was given as the fix for this CVE. But the issue was still reproducible. So had to apply patch "Commit 62fdaa52a3d00a875da771719b6dc537ca79fce1" to fix the issue.
PATCH 2/2 btrfs: Detect unbalanced tree with empty leaf before crashing btree operations
From: Qu Wenruo <[email protected]>
commit 62fdaa52a3d00a875da771719b6dc537ca79fce1 upstream
[BUG]
With crafted image, btrfs will panic at btree operations:
kernel BUG at fs/btrfs/ctree.c:3894!
invalid opcode: 0000 [#1] SMP PTI
CPU: 0 PID: 1138 Comm: btrfs-transacti Not tainted 5.0.0-rc8+ #9 RIP: 0010:__push_leaf_left+0x6b6/0x6e0 RSP: 0018:ffffc0bd4128b990 EFLAGS: 00010246 RAX: 0000000000000000 RBX: ffffa0a4ab8f0e38 RCX: 0000000000000000 RDX: ffffa0a280000000 RSI: 0000000000000000 RDI: ffffa0a4b3814000 RBP: ffffc0bd4128ba38 R08: 0000000000001000 R09: ffffc0bd4128b948 R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000240 R13: ffffa0a4b556fb60 R14: ffffa0a4ab8f0af0 R15: ffffa0a4ab8f0af0 FS: 0000000000000000(0000) GS:ffffa0a4b7a00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f2461c80020 CR3: 000000022b32a006 CR4: 00000000000206f0 Call Trace: ? _cond_resched+0x1a/0x50 push_leaf_left+0x179/0x190 btrfs_del_items+0x316/0x470 btrfs_del_csums+0x215/0x3a0 __btrfs_free_extent.isra.72+0x5a7/0xbe0 __btrfs_run_delayed_refs+0x539/0x1120 btrfs_run_delayed_refs+0xdb/0x1b0 btrfs_commit_transaction+0x52/0x950 ? start_transaction+0x94/0x450 transaction_kthread+0x163/0x190 kthread+0x105/0x140 ? btrfs_cleanup_transaction+0x560/0x560 ? kthread_destroy_worker+0x50/0x50 ret_from_fork+0x35/0x40 Modules linked in: ---[ end trace c2425e6e89b5558f ]---
[CAUSE]
The offending csum tree looks like this:
checksum tree key (CSUM_TREE ROOT_ITEM 0)
node 29741056 level 1 items 14 free 107 generation 19 owner CSUM_TREE
...
key (EXTENT_CSUM EXTENT_CSUM 85975040) block 29630464 gen 17
key (EXTENT_CSUM EXTENT_CSUM 89911296) block 29642752 gen 17 <<<
key (EXTENT_CSUM EXTENT_CSUM 92274688) block 29646848 gen 17
... leaf 29630464 items 6 free space 1 generation 17 owner CSUM_TREE
item 0 key (EXTENT_CSUM EXTENT_CSUM 85975040) itemoff 3987 itemsize 8
range start 85975040 end 85983232 length 8192
... leaf 29642752 items 0 free space 3995 generation 17 owner 0
^ empty leaf invalid owner ^
leaf 29646848 items 1 free space 602 generation 17 owner CSUM_TREE
item 0 key (EXTENT_CSUM EXTENT_CSUM 92274688) itemoff 627 itemsize 3368
range start 92274688 end 95723520 length 3448832So we have a corrupted csum tree where one tree leaf is completely empty, causing unbalanced btree, thus leading to unexpected btree balance error.
[FIX]
For this particular case, we handle it in two directions to catch it:
- Check if the tree block is empty through btrfs_verify_level_key() So that invalid tree blocks won't be read out through btrfs_search_slot() and its variants.
- Check 0 tree owner in tree checker
NO tree is using 0 as its tree owner, detect it and reject at tree block read time.
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=202821
--- fs/btrfs/disk-io.c | 10 ++++++++++ fs/btrfs/tree-checker.c | 6 ++++++ 2 files changed, 16 insertions(+)
fs/btrfs/disk-io.c +10 -0
@@ -438,6 +438,16 @@ int btrfs_verify_level_key(struct btrfs_fs_info *fs_info,*/ if (btrfs_header_generation (eb) > fs_info->last_trans_committed)return 0 ; + +/* We have @first_key, so this @eb must have at least one item */ +if (btrfs_header_nritems (eb) ==0 ) { +btrfs_err (fs_info, +"invalid tree nritems, bytenr=%llu nritems=0 expect >0" , + eb->start); +WARN_ON (IS_ENABLED (CONFIG_BTRFS_DEBUG )); +return -EUCLEAN ; + } +if (found_level)btrfs_node_key_to_cpu (eb, &found_key,0 );else
fs/btrfs/tree-checker.c +6 -0
@@ -509,6 +509,12 @@ static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf, owner);return -EUCLEAN ; } +/* Unknown tree */ +if (owner ==0 ) { +generic_err (fs_info, leaf,0 , +"invalid owner, root 0 is not defined" ); +return -EUCLEAN ; + } key.objectid = owner; key.type =BTRFS_ROOT_ITEM_KEY ; key.offset = (u64)-1 ;
Signature · 1 line
2.23.1
PATCH 1/2 btrfs: merge btrfs_find_device and find_device
In reply to Vikash BansalFrom: Anand Jain <[email protected]>
commit 09ba3bc9dd150457c506e4661380a6183af651c1 upstream
Both btrfs_find_device() and find_device() does the same thing except that the latter does not take the seed device onto account in the device scanning context. We can merge them.
[4.19.y backport notes:
Vikash : - To apply this patch, a portion of commit e4319cd9cace
was used to change the first argument of function "btrfs_find_device" from "struct btrfs_fs_info" to "struct btrfs_fs_devices".
--- fs/btrfs/dev-replace.c | 8 ++-- fs/btrfs/ioctl.c | 5 ++- fs/btrfs/scrub.c | 4 +- fs/btrfs/volumes.c | 84 +++++++++++++++++++++--------------------- fs/btrfs/volumes.h | 4 +- 5 files changed, 53 insertions(+), 52 deletions(-)
fs/btrfs/dev-replace.c +4 -4
@@ -112,11 +112,11 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)break ;case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED :case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED : - dev_replace->srcdev =btrfs_find_device (fs_info, src_devid, -NULL ,NULL ); - dev_replace->tgtdev =btrfs_find_device (fs_info, + dev_replace->srcdev =btrfs_find_device (fs_info->fs_devices, + src_devid,NULL ,NULL , true); + dev_replace->tgtdev =btrfs_find_device (fs_info->fs_devices,BTRFS_DEV_REPLACE_DEVID , -NULL ,NULL ); +NULL ,NULL , true);/* * allow 'btrfs dev replace_cancel' if src/tgt device is * missing
fs/btrfs/ioctl.c +3 -2
@@ -1642,7 +1642,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,btrfs_info (fs_info,"resizing devid %llu" , devid); } - device =btrfs_find_device (fs_info, devid,NULL ,NULL ); + device =btrfs_find_device (fs_info->fs_devices, devid,NULL ,NULL , true);if (!device) {btrfs_info (fs_info,"resizer unable to find device %llu" , devid);
@@ -3178,7 +3178,8 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, s_uuid = di_args->uuid;rcu_read_lock (); - dev =btrfs_find_device (fs_info, di_args->devid, s_uuid,NULL ); + dev =btrfs_find_device (fs_info->fs_devices, di_args->devid, s_uuid, +NULL , true);if (!dev) { ret = -ENODEV ;
fs/btrfs/scrub.c +2 -2
@@ -3835,7 +3835,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,return PTR_ERR (sctx);mutex_lock (&fs_info->fs_devices->device_list_mutex); - dev =btrfs_find_device (fs_info, devid,NULL ,NULL ); + dev =btrfs_find_device (fs_info->fs_devices, devid,NULL ,NULL , true);if (!dev || (test_bit (BTRFS_DEV_STATE_MISSING , &dev->dev_state) && !is_dev_replace)) {mutex_unlock (&fs_info->fs_devices->device_list_mutex);
@@ -4019,7 +4019,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,struct scrub_ctx *sctx =NULL ;mutex_lock (&fs_info->fs_devices->device_list_mutex); - dev =btrfs_find_device (fs_info, devid,NULL ,NULL ); + dev =btrfs_find_device (fs_info->fs_devices, devid,NULL ,NULL , true);if (dev) sctx = dev->scrub_ctx;if (sctx)
fs/btrfs/volumes.c +42 -42
155 lines not on this page · raw message
fs/btrfs/volumes.h +2 -2
@@ -430,8 +430,8 @@ void __exit btrfs_cleanup_fs_uuids(void);int btrfs_num_copies (struct btrfs_fs_info *fs_info,u64 logical,u64 len);int btrfs_grow_device (struct btrfs_trans_handle *trans,struct btrfs_device *device,u64 new_size); -struct btrfs_device *btrfs_find_device (struct btrfs_fs_info *fs_info,u64 devid, -u8 *uuid,u8 *fsid); +struct btrfs_device *btrfs_find_device (struct btrfs_fs_devices *fs_devices, +u64 devid,u8 *uuid,u8 *fsid,bool seed);int btrfs_shrink_device (struct btrfs_device *device,u64 new_size);int btrfs_init_new_device (struct btrfs_fs_info *fs_info,const char *path); int btrfs_balance(struct btrfs_fs_info *fs_info,
Signature · 1 line
2.23.1
Greg KH
maintainerQuoted 12 lines · Vikash Bansal
CVE Description:
NVD Site Link: https://nvd.nist.gov/vuln/detail?vulnId=CVE-2019-18885It was discovered that the btrfs file system in the Linux kernel did not properly validate metadata, leading to a NULL pointer dereference. An attacker could use this to specially craft a file system image that, when mounted, could cause a denial of service (system crash).
[PATCH v4.19.y 1/2]:
Backporting of upsream commit09ba3bc9dd15:
btrfs: mergebtrfs_find_deviceandfind_device
Quoted 8 lines
[PATCH v4.19.y 2/2]:
Backporting of upstream commit62fdaa52a3d0:
btrfs: Detect unbalanced tree with empty leaf before crashingOn NVD site link of "commit
09ba3bc9dd150457c506e4661380a6183af651c1" was given as the fix for this CVE. But the issue was still reproducible. So had to apply patch "Commit62fdaa52a3d00a875da771719b6dc537ca79fce1" to fix the issue.
Looks good, now queued up,t hanks.
greg k-h