Skip to contentSkip to conversation

Btrfs: do not overwrite error return value in the device replace ioctl

PATCH In review [email protected] 3 participants 4 messages Dec 14, 2018 – Jan 8, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: do not overwrite error return value in the device replace ioctl

From: Filipe Manana <[email protected]>

If the call to btrfs_dev_replace_by_ioctl() failed we would overwrite the error returned to user space with -EFAULT if the call to copy_to_user() failed as well. Fix that by calling copy_to_user() only if no error happened before.

---
 fs/btrfs/ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ioctl.c +1 -1
index 0b101df822e9..abe45fd97ab5 100644
@@ -4401,7 +4401,7 @@ static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
 		break;
 	}
 
-	if (copy_to_user(arg, p, sizeof(*p)))
+	if (ret == 0 && copy_to_user(arg, p, sizeof(*p)))
 		ret = -EFAULT;
 out:
 	kfree(p);
Signature · 1 line
2.11.0
#1 lore raw
Quoted 17 lines · On 12/15/2018 03:45 AM, [email protected] wrote:

From: Filipe Manana <[email protected]>

If the call to btrfs_dev_replace_by_ioctl() failed we would overwrite the error returned to user space with -EFAULT if the call to copy_to_user() failed as well. Fix that by calling copy_to_user() only if no error happened before.

---
  fs/btrfs/ioctl.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ioctl.c +0 -0
index 0b101df822e9..abe45fd97ab5 100644

fs/btrfs/ioctl.c static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, · line 4404

Quoted 5 lines
fs/btrfs/ioctl.c +1 -1
@@ -4401,7 +4401,7 @@ static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
  		break;
  	}
 
-	if (copy_to_user(arg, p, sizeof(*p)))
+	if (ret == 0 && copy_to_user(arg, p, sizeof(*p)))

Its the same thing here too.. we copy ret to args->result even if it fails. Now with this patch, if ret is non-zero, its not in args->result at the userland anymore. If there is any tool which just checks args->result will be at problem, which I think is rare, should be ok. It would have been nice to have this design as in this patch, in the original code.

------
530 ret = btrfs_dev_replace_start(fs_info,
args->start.tgtdev_name,
531 args->start.srcdevid,
532 args->start.srcdev_name,
533
args->start.cont_reading_from_srcdev_mode);
534 args->result = ret;
--------

Quoted 4 lines
fs/btrfs/ioctl.c +0 -0
  		ret = -EFAULT;
  out:
  	kfree(p);
#2 lore raw

PATCH v2 1/1 Btrfs: do not overwrite error return value in the device replace ioctl

In reply to [email protected]

From: Filipe Manana <[email protected]>

If the call to btrfs_dev_replace_by_ioctl() failed we would overwrite the error returned to user space with -EFAULT if the call to copy_to_user() failed as well. Fix that by calling copy_to_user() only if no error happened before or a device replace operation was canceled.

---

V2: Considered the case where the device replace operation is canceled.

 fs/btrfs/ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ioctl.c +1 -1
index 8dbb4ed07da1..0db60bfee99a 100644
@@ -4462,7 +4462,7 @@ static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
 		break;
 	}
 
-	if (copy_to_user(arg, p, sizeof(*p)))
+	if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
 		ret = -EFAULT;
 out:
 	kfree(p);
Signature · 1 line
2.11.0
Reviewed-by on v2 1/1
#3 lore raw
Quoted 8 lines · On Tue, Jan 08, 2019 at 11:42:09AM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

If the call to btrfs_dev_replace_by_ioctl() failed we would overwrite the error returned to user space with -EFAULT if the call to copy_to_user() failed as well. Fix that by calling copy_to_user() only if no error happened before or a device replace operation was canceled.