Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl()
@ 2026-11-19 18:28 fdmanana
  2026-11-19 18:28 ` [PATCH 1/2] btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES fdmanana
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: fdmanana @ 2026-11-19 18:28 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES
  2026-11-19 18:28 [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl() fdmanana
@ 2026-11-19 18:28 ` fdmanana
  2026-11-19 18:28 ` [PATCH 2/2] btrfs: use AUTO_KFREE() in btrfs_control_ioctl() fdmanana
  2026-11-19 21:14 ` [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl() Qu Wenruo
  2 siblings, 0 replies; 4+ messages in thread
From: fdmanana @ 2026-11-19 18:28 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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")
Link: https://sashiko.dev/#/bug/linux-7dac1d86-8580-4ba2-b8c5-c15fa9cdb0e8
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/super.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index b424cc836bd4..90e45becb59b 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -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:
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] btrfs: use AUTO_KFREE() in btrfs_control_ioctl()
  2026-11-19 18:28 [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl() fdmanana
  2026-11-19 18:28 ` [PATCH 1/2] btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES fdmanana
@ 2026-11-19 18:28 ` fdmanana
  2026-11-19 21:14 ` [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl() Qu Wenruo
  2 siblings, 0 replies; 4+ messages in thread
From: fdmanana @ 2026-11-19 18:28 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/super.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 90e45becb59b..2609d7783e8a 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -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;
 }
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl()
  2026-11-19 18:28 [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl() fdmanana
  2026-11-19 18:28 ` [PATCH 1/2] btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES fdmanana
  2026-11-19 18:28 ` [PATCH 2/2] btrfs: use AUTO_KFREE() in btrfs_control_ioctl() fdmanana
@ 2026-11-19 21:14 ` Qu Wenruo
  2 siblings, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-11-19 21:14 UTC (permalink / raw)
  To: linux-btrfs



在 2026/11/20 04:58, fdmanana@kernel.org 写道:
> From: Filipe Manana <fdmanana@suse.com>
> 
> Details in the individual change logs.

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> 
> 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(-)
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-19 21:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 18:28 [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl() fdmanana
2026-11-19 18:28 ` [PATCH 1/2] btrfs: fix incorrect memory copying in BTRFS_IOC_GET_SUPPORTED_FEATURES fdmanana
2026-11-19 18:28 ` [PATCH 2/2] btrfs: use AUTO_KFREE() in btrfs_control_ioctl() fdmanana
2026-11-19 21:14 ` [PATCH 0/2] btrfs: fix and cleanup for btrfs_control_ioctl() Qu Wenruo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox