]> Git Repo - linux.git/commitdiff
btrfs: dev-replace: properly validate device names
authorDavid Sterba <[email protected]>
Wed, 14 Feb 2024 15:19:24 +0000 (16:19 +0100)
committerDavid Sterba <[email protected]>
Thu, 22 Feb 2024 11:14:21 +0000 (12:14 +0100)
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/[email protected]/
Link: https://lore.kernel.org/linux-btrfs/[email protected]/
CC: [email protected] # 4.19+
CC: Edward Adam Davis <[email protected]>
Reported-and-tested-by: [email protected]
Reviewed-by: Boris Burkov <[email protected]>
Signed-off-by: David Sterba <[email protected]>
fs/btrfs/dev-replace.c

index 1502d664c89273eb54ba3516528b74eab094f3b3..79c4293ddf373f7d452b2cd05aeec4dd1d9fb5f7 100644 (file)
@@ -725,6 +725,23 @@ leave:
        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,
This page took 0.052851 seconds and 4 git commands to generate.