Skip to contentSkip to conversation

btrfs: fix and cleanup for btrfs_control_ioctl()

PATCH In review [email protected] 2 participants 4 messages Nov 19 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: fix and cleanup for btrfs_control_ioctl()

From: Filipe Manana <[email protected]>

Details in the individual change logs.

Filipe Manana (2):
btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES
btrfs: use AUTO_KFREE() in btrfs_control_ioctl()

 fs/btrfs/super.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES

From: Filipe Manana <[email protected]>

In btrfs_control_ioctl() we always assume the argument from user space matches a 4096 bytes buffer, for a struct btrfs_ioctl_vol_args. That was true until commit c5868f8362f5 ("btrfs: add GET_SUPPORTED_FEATURES to the control device ioctls"), because back then all the control ioctls took such a structure as an argument (and for input). However after that commit we now have an ioctl that does not take that struct as an argument, it takes a buffer that can be much smaller than that and it's only used for output, so it does not make sense to treat it as a struct btrfs_ioctl_vol_args and to validate it as such. Plus doing the memdup_user() call can result in an -EFAULT error if user space passes a buffer much smaller than sizeof(struct btrfs_ioctl_vol_args), as attempting to read beyond its limit can cross an unmapped page.

Fix this by doing the memdup_user() and btrfs_check_ioctl_vol_args_path() only if the ioctl is not BTRFS_IOC_GET_SUPPORTED_FEATURES.

Fixes c5868f8362f5 ("btrfs: add GET_SUPPORTED_FEATURES to the control device ioctls")
---
 fs/btrfs/super.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
fs/btrfs/super.c +23 -7
index b424cc836bd4..90e45becb59b 100644
@@ -2240,7 +2240,7 @@ static int btrfs_control_open(struct inode *inode, struct file *file)
 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
 				unsigned long arg)
 {
-	struct btrfs_ioctl_vol_args *vol;
+	struct btrfs_ioctl_vol_args *vol = NULL;
 	struct btrfs_device *device = NULL;
 	dev_t devt = 0;
 	int ret = -ENOTTY;
@@ -2248,12 +2248,28 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	vol = memdup_user((void __user *)arg, sizeof(*vol));
-	if (IS_ERR(vol))
-		return PTR_ERR(vol);
-	ret = btrfs_check_ioctl_vol_args_path(vol);
-	if (ret < 0)
-		goto out;
+	switch (cmd) {
+	case BTRFS_IOC_SCAN_DEV:
+	case BTRFS_IOC_FORGET_DEV:
+	case BTRFS_IOC_DEVICES_READY:
+		/*
+		 * Not all control ioctls take a struct btrfs_ioctl_vol_args as
+		 * argument, some may pass a smaller buffer and attempting to
+		 * read more may result in an unexpected -EFAULT if the memory
+		 * crosses an unmapped page. BTRFS_IOC_GET_SUPPORTED_FEATURES
+		 * expects a much smaller buffer for a short array of
+		 * struct btrfs_ioctl_feature_flags elements. So attempt a copy
+		 * of a struct btrfs_ioctl_vol_args only for the ioctls that
+		 * expect such an argument.
+		 */
+		vol = memdup_user((void __user *)arg, sizeof(*vol));
+		if (IS_ERR(vol))
+			return PTR_ERR(vol);
+		ret = btrfs_check_ioctl_vol_args_path(vol);
+		if (ret < 0)
+			goto out;
+		break;
+	}
 
 	switch (cmd) {
 	case BTRFS_IOC_SCAN_DEV:
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 btrfs: use AUTO_KFREE() in btrfs_control_ioctl()

In reply to [email protected]

From: Filipe Manana <[email protected]>

Use AUTO_KFREE() to slightly simplify the code and avoid the explicit kfree(), remove one error label and return directly on error.

---
 fs/btrfs/super.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/btrfs/super.c +2 -4
index 90e45becb59b..2609d7783e8a 100644
@@ -2240,7 +2240,7 @@ static int btrfs_control_open(struct inode *inode, struct file *file)
 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
 				unsigned long arg)
 {
-	struct btrfs_ioctl_vol_args *vol = NULL;
+	struct btrfs_ioctl_vol_args AUTO_KFREE(vol);
 	struct btrfs_device *device = NULL;
 	dev_t devt = 0;
 	int ret = -ENOTTY;
@@ -2267,7 +2267,7 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
 			return PTR_ERR(vol);
 		ret = btrfs_check_ioctl_vol_args_path(vol);
 		if (ret < 0)
-			goto out;
+			return ret;
 		break;
 	}
 
@@ -2314,8 +2314,6 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
 		break;
 	}
 
-out:
-	kfree(vol);
 	return ret;
 }
 
Signature · 1 line
2.53.0
Reviewed-by on the v1 series
#3 lore raw
In reply to [email protected]
Quoted 3 lines · 在 2026/11/20 04:58, [email protected] 写道:

From: Filipe Manana <[email protected]>

Details in the individual change logs.

Thanks,
Qu

Quoted 8 lines

Filipe Manana (2):
btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES
btrfs: use AUTO_KFREE() in btrfs_control_ioctl()

  fs/btrfs/super.c | 32 +++++++++++++++++++++++---------
  1 file changed, 23 insertions(+), 9 deletions(-)