Skip to contentSkip to conversation

btrfs: prevent potential out of bounds in btrfs_ioctl_snap_create_v2()

PATCH In review Dan Carpenter 2 participants 2 messages Feb 17, 2021 – Feb 22, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: prevent potential out of bounds in btrfs_ioctl_snap_create_v2()

The problem is we're copying "inherit" from user space but we don't necessarily know that we're copying enough data for a 64 byte struct. Then the next problem is that "inherit" has a variable size array at the end, and we have to verify that array is the size we expected.

Fixes 6f72c7e20dba: ("Btrfs: add qgroup inheritance")

---
Presumably only root can create snapshots. Anyway, I have not tested this fix. I believe it is correct, of course. But perhaps it's best to check.

The calculation for the number of elements in the array was copied from btrfs_qgroup_inherit().

 fs/btrfs/ioctl.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
fs/btrfs/ioctl.c +20 -1
index 384d33ab02c7..9d7b24d3e3bd 100644
@@ -1941,15 +1941,34 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
 		readonly = true;
 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
-		if (vol_args->size > PAGE_SIZE) {
+		u64 nums;
+
+		if (vol_args->size < sizeof(*inherit) ||
+		    vol_args->size > PAGE_SIZE) {
 			ret = -EINVAL;
 			goto free_args;
 		}
+
 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
 		if (IS_ERR(inherit)) {
 			ret = PTR_ERR(inherit);
 			goto free_args;
 		}
+
+		/* quick and dirty checks to prevent integer overflows */
+		if (inherit->num_qgroups > PAGE_SIZE ||
+		    inherit->num_ref_copies > PAGE_SIZE ||
+		    inherit->num_excl_copies > PAGE_SIZE) {
+			ret = -EINVAL;
+			goto free_inherit;
+		}
+
+		nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
+		       2 * inherit->num_excl_copies;
+		if (vol_args->size != struct_size(inherit, qgroups, nums)) {
+			ret = -EINVAL;
+			goto free_inherit;
+		}
 	}
 
 	ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
Signature · 1 line
2.30.0
#1 lore raw
Quoted 10 lines · Dan Carpenter

The problem is we're copying "inherit" from user space but we don't necessarily know that we're copying enough data for a 64 byte struct. Then the next problem is that "inherit" has a variable size array at the end, and we have to verify that array is the size we expected.

Fixes 6f72c7e20dba: ("Btrfs: add qgroup inheritance")

---
Presumably only root can create snapshots.

Well, no. After first analysis there are some "interesting memory access patterns" possible, with a crafted data in the inherit member.

Quoted 3 lines

Anyway, I have not tested
this fix. I believe it is correct, of course. But perhaps it's best to check.

Yeah I'll write a test also to see where exactly the issues are. Thanks for the report/fix.