* [PATCH 0/3] Ioctl buffer name/path validation
@ 2024-02-14 18:31 David Sterba
2024-02-14 18:31 ` [PATCH 1/3] btrfs: factor out validation of btrfs_ioctl_vol_args::name David Sterba
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: David Sterba @ 2024-02-14 18:31 UTC (permalink / raw)
To: linux-btrfs; +Cc: David Sterba, Edward Adam Davis
This is inspired by a report and fix [1] where device replace buffers
were not properly validated (third patch). The other two are doing
proper validation of vol args path or name so that an unterminated
string is reported as an error rather than relying on later actions like
open that would catch an invalid path.
(I'm OK to replace the third patch in favor of Edward as he spent time
analyzing it but we did not agree on a fix and I did not get a reply
with the suggestion I implemented in the end.)
[1] https://lore.kernel.org/linux-btrfs/tencent_44CA0665C9836EF9EEC80CB9E7E206DF5206@qq.com/
David Sterba (3):
btrfs: factor out validation of btrfs_ioctl_vol_args::name
btrfs: factor out validation of btrfs_ioctl_vol_args_v2::name
btrfs: dev-replace: properly validate device names
fs/btrfs/dev-replace.c | 24 +++++++++++++++----
fs/btrfs/fs.h | 2 ++
fs/btrfs/ioctl.c | 54 +++++++++++++++++++++++++++++++++++-------
fs/btrfs/super.c | 5 +++-
4 files changed, 72 insertions(+), 13 deletions(-)
--
2.42.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/3] btrfs: factor out validation of btrfs_ioctl_vol_args::name
2024-02-14 18:31 [PATCH 0/3] Ioctl buffer name/path validation David Sterba
@ 2024-02-14 18:31 ` David Sterba
2024-02-14 18:32 ` [PATCH 2/3] btrfs: factor out validation of btrfs_ioctl_vol_args_v2::name David Sterba
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2024-02-14 18:31 UTC (permalink / raw)
To: linux-btrfs; +Cc: David Sterba
The validation of vol args name in several ioctls is not done properly.
a terminating NUL is written to the end of the buffer unconditionally,
assuming that this would be the last place in case the buffer is used
completely. This does not communicate back the actual error (either an
invalid or too long path).
Factor out all such cases and use a helper to do the verification,
simply look for NUL in the buffer. There's no expected practical change,
the size of buffer is 4088, this is enough for most paths or names.
Signed-off-by: David Sterba <dsterba@suse.com>
---
fs/btrfs/fs.h | 2 ++
fs/btrfs/ioctl.c | 34 +++++++++++++++++++++++++++++-----
fs/btrfs/super.c | 5 ++++-
3 files changed, 35 insertions(+), 6 deletions(-)
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index f8bb73d6ab68..3c8fbc5ab082 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -922,6 +922,8 @@ void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
enum btrfs_exclusive_operation op);
+int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args);
+
/* Compatibility and incompatibility defines */
void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
const char *name);
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index ac3316e0d11c..69d2ad24a4ca 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -231,6 +231,13 @@ static int check_fsflags_compatible(struct btrfs_fs_info *fs_info,
return 0;
}
+int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
+{
+ if (memchr(vol_args->name, 0, sizeof(vol_args->name)) == NULL)
+ return -ENAMETOOLONG;
+ return 0;
+}
+
/*
* Set flags/xflags from the internal inode flags. The remaining items of
* fsxattr are zeroed.
@@ -1128,7 +1135,10 @@ static noinline int btrfs_ioctl_resize(struct file *file,
ret = PTR_ERR(vol_args);
goto out_drop;
}
- vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
+ ret = btrfs_check_ioctl_vol_args_path(vol_args);
+ if (ret < 0)
+ goto out_free;
+
sizestr = vol_args->name;
cancel = (strcmp("cancel", sizestr) == 0);
ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
@@ -1328,12 +1338,15 @@ static noinline int btrfs_ioctl_snap_create(struct file *file,
vol_args = memdup_user(arg, sizeof(*vol_args));
if (IS_ERR(vol_args))
return PTR_ERR(vol_args);
- vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
+ ret = btrfs_check_ioctl_vol_args_path(vol_args);
+ if (ret < 0)
+ goto out;
ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
vol_args->name, vol_args->fd, subvol,
false, NULL);
+out:
kfree(vol_args);
return ret;
}
@@ -2466,7 +2479,10 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
if (IS_ERR(vol_args))
return PTR_ERR(vol_args);
- vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
+ err = btrfs_check_ioctl_vol_args_path(vol_args);
+ if (err < 0)
+ goto out;
+
subvol_name = vol_args->name;
err = mnt_want_write_file(file);
@@ -2677,12 +2693,16 @@ static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
goto out;
}
- vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
+ ret = btrfs_check_ioctl_vol_args_path(vol_args);
+ if (ret < 0)
+ goto out_free;
+
ret = btrfs_init_new_device(fs_info, vol_args->name);
if (!ret)
btrfs_info(fs_info, "disk added %s", vol_args->name);
+out_free:
kfree(vol_args);
out:
if (restore_op)
@@ -2774,7 +2794,10 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
if (IS_ERR(vol_args))
return PTR_ERR(vol_args);
- vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
+ ret = btrfs_check_ioctl_vol_args_path(vol_args);
+ if (ret < 0)
+ goto out_free;
+
if (!strcmp("cancel", vol_args->name)) {
cancel = true;
} else {
@@ -2801,6 +2824,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
bdev_release(bdev_handle);
out:
btrfs_put_dev_args_from_path(&args);
+out_free:
kfree(vol_args);
return ret;
}
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 101f786963d4..bbc9efa7157b 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -2203,7 +2203,9 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
vol = memdup_user((void __user *)arg, sizeof(*vol));
if (IS_ERR(vol))
return PTR_ERR(vol);
- vol->name[BTRFS_PATH_NAME_MAX] = '\0';
+ ret = btrfs_check_ioctl_vol_args_path(vol);
+ if (ret < 0)
+ goto out;
switch (cmd) {
case BTRFS_IOC_SCAN_DEV:
@@ -2245,6 +2247,7 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
break;
}
+out:
kfree(vol);
return ret;
}
--
2.42.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/3] btrfs: factor out validation of btrfs_ioctl_vol_args_v2::name
2024-02-14 18:31 [PATCH 0/3] Ioctl buffer name/path validation David Sterba
2024-02-14 18:31 ` [PATCH 1/3] btrfs: factor out validation of btrfs_ioctl_vol_args::name David Sterba
@ 2024-02-14 18:32 ` David Sterba
2024-02-14 18:32 ` [PATCH 3/3] btrfs: dev-replace: properly validate device names David Sterba
2024-02-14 21:22 ` [PATCH 0/3] Ioctl buffer name/path validation Boris Burkov
3 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2024-02-14 18:32 UTC (permalink / raw)
To: linux-btrfs; +Cc: David Sterba
The validation of vol args v2 name in snapshot and device remove ioctls
is not done properly. A terminating NUL is written to the end of the
buffer unconditionally, assuming that this would be the last place in
case the buffer is used completely. This does not communicate back the
actual error (either an invalid or too long path).
Factor out all such cases and use a helper to do the verification,
simply look for NUL in the buffer. There's no expected practical
change, the size of buffer is 4088, this is enough for most paths or
names.
Signed-off-by: David Sterba <dsterba@suse.com>
---
fs/btrfs/ioctl.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 69d2ad24a4ca..b8895c0e8259 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -238,6 +238,13 @@ int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
return 0;
}
+static int btrfs_check_ioctl_vol_args2_subvol_name(const struct btrfs_ioctl_vol_args_v2 *vol_args2)
+{
+ if (memchr(vol_args2->name, 0, sizeof(vol_args2->name)) == NULL)
+ return -ENAMETOOLONG;
+ return 0;
+}
+
/*
* Set flags/xflags from the internal inode flags. The remaining items of
* fsxattr are zeroed.
@@ -1365,7 +1372,9 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
vol_args = memdup_user(arg, sizeof(*vol_args));
if (IS_ERR(vol_args))
return PTR_ERR(vol_args);
- vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
+ ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
+ if (ret < 0)
+ goto free_args;
if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
ret = -EOPNOTSUPP;
@@ -2395,7 +2404,9 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
* name, same as v1 currently does.
*/
if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
- vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
+ err = btrfs_check_ioctl_vol_args2_subvol_name(vol_args2);
+ if (err < 0)
+ goto out;
subvol_name = vol_args2->name;
err = mnt_want_write_file(file);
@@ -2734,7 +2745,10 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
goto out;
}
- vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
+ ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
+ if (ret < 0)
+ goto out;
+
if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
args.devid = vol_args->devid;
} else if (!strcmp("cancel", vol_args->name)) {
--
2.42.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/3] btrfs: dev-replace: properly validate device names
2024-02-14 18:31 [PATCH 0/3] Ioctl buffer name/path validation David Sterba
2024-02-14 18:31 ` [PATCH 1/3] btrfs: factor out validation of btrfs_ioctl_vol_args::name David Sterba
2024-02-14 18:32 ` [PATCH 2/3] btrfs: factor out validation of btrfs_ioctl_vol_args_v2::name David Sterba
@ 2024-02-14 18:32 ` David Sterba
2024-02-14 21:22 ` [PATCH 0/3] Ioctl buffer name/path validation Boris Burkov
3 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2024-02-14 18:32 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, stable, Edward Adam Davis,
syzbot+33f23b49ac24f986c9e8
There's a syzbot report that device name buffers passed to device
replace are not properly checked for string termination which could lead
to a read out of bounds in getname_kernel().
Add a helper that validates both source and target device name buffers.
For devid as the source initialize the buffer to empty string in case
something tries to read it later.
This was originally analyzed and fixed in a different way by Edward Adam
Davis (see links).
Link: https://lore.kernel.org/linux-btrfs/000000000000d1a1d1060cc9c5e7@google.com/
Link: https://lore.kernel.org/linux-btrfs/tencent_44CA0665C9836EF9EEC80CB9E7E206DF5206@qq.com/
CC: stable@vger.kernel.org # 4.19+
CC: Edward Adam Davis <eadavis@qq.com>
Reported-and-tested-by: syzbot+33f23b49ac24f986c9e8@syzkaller.appspotmail.com
Signed-off-by: David Sterba <dsterba@suse.com>
---
fs/btrfs/dev-replace.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 1502d664c892..79c4293ddf37 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -725,6 +725,23 @@ static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
return ret;
}
+static int btrfs_check_replace_dev_names(struct btrfs_ioctl_dev_replace_args *args)
+{
+ if (args->start.srcdevid == 0) {
+ if (memchr(args->start.srcdev_name, 0,
+ sizeof(args->start.srcdev_name)) == NULL)
+ return -ENAMETOOLONG;
+ } else {
+ args->start.srcdev_name[0] = 0;
+ }
+
+ if (memchr(args->start.tgtdev_name, 0,
+ sizeof(args->start.tgtdev_name)) == NULL)
+ return -ENAMETOOLONG;
+
+ return 0;
+}
+
int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
struct btrfs_ioctl_dev_replace_args *args)
{
@@ -737,10 +754,9 @@ int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
default:
return -EINVAL;
}
-
- if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
- args->start.tgtdev_name[0] == '\0')
- return -EINVAL;
+ ret = btrfs_check_replace_dev_names(args);
+ if (ret < 0)
+ return ret;
ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
args->start.srcdevid,
--
2.42.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 0/3] Ioctl buffer name/path validation
2024-02-14 18:31 [PATCH 0/3] Ioctl buffer name/path validation David Sterba
` (2 preceding siblings ...)
2024-02-14 18:32 ` [PATCH 3/3] btrfs: dev-replace: properly validate device names David Sterba
@ 2024-02-14 21:22 ` Boris Burkov
2024-02-15 15:13 ` David Sterba
3 siblings, 1 reply; 6+ messages in thread
From: Boris Burkov @ 2024-02-14 21:22 UTC (permalink / raw)
To: David Sterba; +Cc: linux-btrfs, Edward Adam Davis
On Wed, Feb 14, 2024 at 07:31:54PM +0100, David Sterba wrote:
> This is inspired by a report and fix [1] where device replace buffers
> were not properly validated (third patch). The other two are doing
> proper validation of vol args path or name so that an unterminated
> string is reported as an error rather than relying on later actions like
> open that would catch an invalid path.
>
> (I'm OK to replace the third patch in favor of Edward as he spent time
> analyzing it but we did not agree on a fix and I did not get a reply
> with the suggestion I implemented in the end.)
>
> [1] https://lore.kernel.org/linux-btrfs/tencent_44CA0665C9836EF9EEC80CB9E7E206DF5206@qq.com/
LGTM. One note/question: would it be helpful to pull out:
```
if (memchr(str, 0, str) == NULL)
return -ENAMETOOLONG;
return 0;
```
into a helper and use it in all these places?
Either way,
Reviewed-by: Boris Burkov <boris@bur.io>
>
> David Sterba (3):
> btrfs: factor out validation of btrfs_ioctl_vol_args::name
> btrfs: factor out validation of btrfs_ioctl_vol_args_v2::name
> btrfs: dev-replace: properly validate device names
>
> fs/btrfs/dev-replace.c | 24 +++++++++++++++----
> fs/btrfs/fs.h | 2 ++
> fs/btrfs/ioctl.c | 54 +++++++++++++++++++++++++++++++++++-------
> fs/btrfs/super.c | 5 +++-
> 4 files changed, 72 insertions(+), 13 deletions(-)
>
> --
> 2.42.1
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/3] Ioctl buffer name/path validation
2024-02-14 21:22 ` [PATCH 0/3] Ioctl buffer name/path validation Boris Burkov
@ 2024-02-15 15:13 ` David Sterba
0 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2024-02-15 15:13 UTC (permalink / raw)
To: Boris Burkov; +Cc: David Sterba, linux-btrfs, Edward Adam Davis
On Wed, Feb 14, 2024 at 01:22:32PM -0800, Boris Burkov wrote:
> On Wed, Feb 14, 2024 at 07:31:54PM +0100, David Sterba wrote:
> > This is inspired by a report and fix [1] where device replace buffers
> > were not properly validated (third patch). The other two are doing
> > proper validation of vol args path or name so that an unterminated
> > string is reported as an error rather than relying on later actions like
> > open that would catch an invalid path.
> >
> > (I'm OK to replace the third patch in favor of Edward as he spent time
> > analyzing it but we did not agree on a fix and I did not get a reply
> > with the suggestion I implemented in the end.)
> >
> > [1] https://lore.kernel.org/linux-btrfs/tencent_44CA0665C9836EF9EEC80CB9E7E206DF5206@qq.com/
>
> LGTM. One note/question: would it be helpful to pull out:
>
> ```
> if (memchr(str, 0, str) == NULL)
> return -ENAMETOOLONG;
> return 0;
> ```
>
> into a helper and use it in all these places?
I think not, it's a one liner and it reads as "see if there's 0 in the
string buffer", this not some obscure semantics where eg. incrementing a
value would mean to start some big process.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-02-15 15:14 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-14 18:31 [PATCH 0/3] Ioctl buffer name/path validation David Sterba
2024-02-14 18:31 ` [PATCH 1/3] btrfs: factor out validation of btrfs_ioctl_vol_args::name David Sterba
2024-02-14 18:32 ` [PATCH 2/3] btrfs: factor out validation of btrfs_ioctl_vol_args_v2::name David Sterba
2024-02-14 18:32 ` [PATCH 3/3] btrfs: dev-replace: properly validate device names David Sterba
2024-02-14 21:22 ` [PATCH 0/3] Ioctl buffer name/path validation Boris Burkov
2024-02-15 15:13 ` David Sterba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox