btrfs-progs: properly set up ioctl arguments
authorDavid Sterba <dsterba@suse.cz>
Fri, 12 Jun 2015 12:36:51 +0000 (14:36 +0200)
committerDavid Sterba <dsterba@suse.cz>
Fri, 12 Jun 2015 14:44:48 +0000 (16:44 +0200)
At some places we do not clear the whole ioctl structure and could
pass garbage to kernel. Zero the ioctl vol_args and use a helper for
copying the path.

Signed-off-by: David Sterba <dsterba@suse.cz>
cmds-device.c
cmds-filesystem.c
cmds-subvolume.c
utils.c

index 3a10438..c588a6e 100644 (file)
@@ -126,6 +126,7 @@ static int cmd_add_dev(int argc, char **argv)
                        goto error_out;
                }
 
+               memset(&ioctl_args, 0, sizeof(ioctl_args));
                strncpy_null(ioctl_args.name, path);
                res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
                e = errno;
@@ -175,6 +176,7 @@ static int cmd_rm_dev(int argc, char **argv)
                        ret++;
                        continue;
                }
+               memset(&arg, 0, sizeof(arg));
                strncpy_null(arg.name, argv[i]);
                res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
                e = errno;
@@ -312,7 +314,8 @@ static int cmd_ready_dev(int argc, char **argv)
                goto out;
        }
 
-       strncpy(args.name, path, BTRFS_PATH_NAME_MAX);
+       memset(&args, 0, sizeof(args));
+       strncpy_null(args.name, path);
        ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
        if (ret < 0) {
                fprintf(stderr, "ERROR: unable to determine if the device '%s'"
index ccf89c6..dcd3f47 100644 (file)
@@ -1270,6 +1270,7 @@ static int cmd_resize(int argc, char **argv)
        }
 
        printf("Resize '%s' of '%s'\n", path, amount);
+       memset(&args, 0, sizeof(args));
        strncpy_null(args.name, amount);
        res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
        e = errno;
index dfc3f1f..e88a6e6 100644 (file)
@@ -357,6 +357,7 @@ again:
        printf("Delete subvolume (%s): '%s/%s'\n",
                commit_mode == 2 || (commit_mode == 1 && cnt + 1 == argc)
                ? "commit" : "no-commit", dname, vname);
+       memset(&args, 0, sizeof(args));
        strncpy_null(args.name, vname);
        res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
        e = errno;
diff --git a/utils.c b/utils.c
index 087f371..591b2a1 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -1409,8 +1409,8 @@ int btrfs_register_one_device(const char *fname)
                        strerror(errno));
                return -errno;
        }
-       strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
-       args.name[BTRFS_PATH_NAME_MAX-1] = 0;
+       memset(&args, 0, sizeof(args));
+       strncpy_null(args.name, fname);
        ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
        e = errno;
        if (ret < 0) {