btrfs-progs: use explicit length parameters for string copy

The macro strncpy_null uses sizeof the first argument for the length,
but there are no checks and this works only for buffers with static
length, i.e. not pointers. This is error prone.  Use the open coded
variant that makes the sizeof visible.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2024-06-17 20:32:30 +02:00 committed by Qu Wenruo
parent 84d88689c1
commit 0d37a301f0
6 changed files with 20 additions and 21 deletions

View file

@ -169,7 +169,7 @@ static int cmd_device_add(const struct cmd_struct *cmd,
}
memset(&ioctl_args, 0, sizeof(ioctl_args));
strncpy_null(ioctl_args.name, path);
__strncpy_null(ioctl_args.name, path, sizeof(ioctl_args.name));
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
if (res < 0) {
error("error adding device '%s': %m", path);
@ -287,7 +287,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd,
} else if (strcmp(argv[i], "missing") == 0 ||
cancel ||
path_is_block_device(argv[i]) == 1) {
strncpy_null(argv2.name, argv[i]);
__strncpy_null(argv2.name, argv[i], sizeof(argv2.name));
} else {
error("not a block device: %s", argv[i]);
ret++;
@ -312,7 +312,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd,
continue;
}
memset(&arg, 0, sizeof(arg));
strncpy_null(arg.name, argv[i]);
__strncpy_null(arg.name, argv[i], sizeof(arg.name));
res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
}
@ -396,7 +396,7 @@ static int btrfs_forget_devices(const char *path)
memset(&args, 0, sizeof(args));
if (path)
strncpy_null(args.name, path);
__strncpy_null(args.name, path, sizeof(args.name));
ret = ioctl(fd, BTRFS_IOC_FORGET_DEV, &args);
if (ret)
ret = -errno;
@ -557,7 +557,7 @@ static int cmd_device_ready(const struct cmd_struct *cmd, int argc, char **argv)
}
memset(&args, 0, sizeof(args));
strncpy_null(args.name, path);
__strncpy_null(args.name, path, sizeof(args.name));
ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
if (ret < 0) {
error("unable to determine if device '%s' is ready for mount: %m",

View file

@ -1441,7 +1441,7 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd,
memset(&args, 0, sizeof(args));
if (devid == (u64)-1) {
/* Ok to copy the string verbatim. */
strncpy_null(args.name, amount);
__strncpy_null(args.name, amount, sizeof(args.name));
} else {
/* The implicit devid 1 needs to be adjusted. */
snprintf(args.name, sizeof(args.name) - 1, "%llu:%s", devid, amount);

View file

@ -179,7 +179,7 @@ static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
}
if (*rctx->dest_dir_path == 0) {
strncpy_null(rctx->cur_subvol_path, path);
__strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path));
} else {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path,
path);
@ -209,7 +209,7 @@ static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
}
memset(&args_v1, 0, sizeof(args_v1));
strncpy_null(args_v1.name, path);
__strncpy_null(args_v1.name, path, sizeof(args_v1.name));
ret = ioctl(rctx->dest_dir_fd, BTRFS_IOC_SUBVOL_CREATE, &args_v1);
if (ret < 0) {
ret = -errno;
@ -249,7 +249,7 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
}
if (*rctx->dest_dir_path == 0) {
strncpy_null(rctx->cur_subvol_path, path);
__strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path));
} else {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path,
path);
@ -281,7 +281,7 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
}
memset(&args_v2, 0, sizeof(args_v2));
strncpy_null(args_v2.name, path);
__strncpy_null(args_v2.name, path, sizeof(args_v2.name));
parent_subvol = subvol_uuid_search(rctx->mnt_fd, 0, parent_uuid,
parent_ctransid, NULL,
@ -663,7 +663,7 @@ static int open_inode_for_write(struct btrfs_receive *rctx, const char *path)
error("cannot open %s: %m", path);
goto out;
}
strncpy_null(rctx->write_path, path);
__strncpy_null(rctx->write_path, path, sizeof(rctx->write_path));
out:
return ret;

View file

@ -197,7 +197,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
char dstdir_dup[PATH_MAX];
char *token;
strncpy_null(dstdir_dup, dstdir);
__strncpy_null(dstdir_dup, dstdir, sizeof(dstdir_dup));
if (dstdir_dup[0] == '/')
strcat(p, "/");
@ -233,7 +233,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
struct btrfs_ioctl_vol_args_v2 args;
memset(&args, 0, sizeof(args));
strncpy_null(args.name, newname);
__strncpy_null(args.name, newname, sizeof(args.name));
args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
args.size = btrfs_qgroup_inherit_size(inherit);
args.qgroup_inherit = inherit;
@ -243,8 +243,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
struct btrfs_ioctl_vol_args args;
memset(&args, 0, sizeof(args));
strncpy_null(args.name, newname);
__strncpy_null(args.name, newname, sizeof(args.name));
ret = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
}
@ -738,7 +737,7 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
args.size = btrfs_qgroup_inherit_size(inherit);
args.qgroup_inherit = inherit;
}
strncpy_null(args.name, newname);
__strncpy_null(args.name, newname, sizeof(args.name));
res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
if (res < 0) {

View file

@ -237,7 +237,7 @@ int btrfs_register_one_device(const char *fname)
return -errno;
}
memset(&args, 0, sizeof(args));
strncpy_null(args.name, fname);
__strncpy_null(args.name, fname, sizeof(args.name));
ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
if (ret < 0) {
error("device scan failed on '%s': %m", fname);
@ -468,7 +468,7 @@ int btrfs_scan_devices(int verbose)
if (!dev)
continue;
/* if we are here its definitely a btrfs disk*/
strncpy_null(path, blkid_dev_devname(dev));
__strncpy_null(path, blkid_dev_devname(dev), sizeof(path));
if (stat(path, &dev_stat) < 0)
continue;

View file

@ -193,10 +193,10 @@ static int is_same_blk_file(const char* a, const char* b)
char real_b[PATH_MAX];
if (!realpath(a, real_a))
strncpy_null(real_a, a);
__strncpy_null(real_a, a, sizeof(real_a));
if (!realpath(b, real_b))
strncpy_null(real_b, b);
__strncpy_null(real_b, b, sizeof(real_b));
/* Identical path? */
if (strcmp(real_a, real_b) == 0)
@ -403,7 +403,7 @@ int path_is_in_dir(const char *parent, const char *path)
char *curr_dir = tmp;
int ret;
strncpy_null(tmp, path);
__strncpy_null(tmp, path, sizeof(tmp));
while (strcmp(parent, curr_dir) != 0) {
if (strcmp(curr_dir, "/") == 0) {