summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDeHackEd <[email protected]>2018-08-20 12:55:18 -0400
committerBrian Behlendorf <[email protected]>2018-08-20 09:55:18 -0700
commitedc05fdb34792e65508ac2ff91c58828b44f19b1 (patch)
tree6882eec9b530f59809d3b86260a56b9a1c4b4e70
parenta448a2557ec4938ed6944c7766fe0b8e6e5f6456 (diff)
Don't modify argv[] in user tools
argv[] gets modified during string parsing for input arguments. This is reflected in the live process listing. Don't do that. Reviewed-by: Serapheim Dimitropoulos <[email protected]> Reviewed-by: loli10K <[email protected]> Reviewed-by: Giuseppe Di Natale <[email protected]> Reviewed-by: George Melikov <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: DHE <[email protected]> Closes #7760
-rw-r--r--cmd/zfs/zfs_main.c18
-rw-r--r--cmd/zpool/zpool_main.c18
2 files changed, 32 insertions, 4 deletions
diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c
index 162cef6fb..cf2d8f2d5 100644
--- a/cmd/zfs/zfs_main.c
+++ b/cmd/zfs/zfs_main.c
@@ -7910,6 +7910,7 @@ main(int argc, char **argv)
int ret = 0;
int i = 0;
char *cmdname;
+ char **newargv;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
@@ -7963,16 +7964,25 @@ main(int argc, char **argv)
libzfs_print_on_error(g_zfs, B_TRUE);
/*
+ * Many commands modify input strings for string parsing reasons.
+ * We create a copy to protect the original argv.
+ */
+ newargv = malloc((argc + 1) * sizeof (newargv[0]));
+ for (i = 0; i < argc; i++)
+ newargv[i] = strdup(argv[i]);
+ newargv[argc] = NULL;
+
+ /*
* Run the appropriate command.
*/
libzfs_mnttab_cache(g_zfs, B_TRUE);
if (find_command_idx(cmdname, &i) == 0) {
current_command = &command_table[i];
- ret = command_table[i].func(argc - 1, argv + 1);
+ ret = command_table[i].func(argc - 1, newargv + 1);
} else if (strchr(cmdname, '=') != NULL) {
verify(find_command_idx("set", &i) == 0);
current_command = &command_table[i];
- ret = command_table[i].func(argc, argv);
+ ret = command_table[i].func(argc, newargv);
} else {
(void) fprintf(stderr, gettext("unrecognized "
"command '%s'\n"), cmdname);
@@ -7980,6 +7990,10 @@ main(int argc, char **argv)
ret = 1;
}
+ for (i = 0; i < argc; i++)
+ free(newargv[i]);
+ free(newargv);
+
if (ret == 0 && log_history)
(void) zpool_log_history(g_zfs, history_str);
diff --git a/cmd/zpool/zpool_main.c b/cmd/zpool/zpool_main.c
index 71943f2c0..73744a87c 100644
--- a/cmd/zpool/zpool_main.c
+++ b/cmd/zpool/zpool_main.c
@@ -8479,6 +8479,7 @@ main(int argc, char **argv)
int ret = 0;
int i = 0;
char *cmdname;
+ char **newargv;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
@@ -8512,15 +8513,24 @@ main(int argc, char **argv)
zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
/*
+ * Many commands modify input strings for string parsing reasons.
+ * We create a copy to protect the original argv.
+ */
+ newargv = malloc((argc + 1) * sizeof (newargv[0]));
+ for (i = 0; i < argc; i++)
+ newargv[i] = strdup(argv[i]);
+ newargv[argc] = NULL;
+
+ /*
* Run the appropriate command.
*/
if (find_command_idx(cmdname, &i) == 0) {
current_command = &command_table[i];
- ret = command_table[i].func(argc - 1, argv + 1);
+ ret = command_table[i].func(argc - 1, newargv + 1);
} else if (strchr(cmdname, '=')) {
verify(find_command_idx("set", &i) == 0);
current_command = &command_table[i];
- ret = command_table[i].func(argc, argv);
+ ret = command_table[i].func(argc, newargv);
} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
/*
* 'freeze' is a vile debugging abomination, so we treat
@@ -8544,6 +8554,10 @@ main(int argc, char **argv)
ret = 1;
}
+ for (i = 0; i < argc; i++)
+ free(newargv[i]);
+ free(newargv);
+
if (ret == 0 && log_history)
(void) zpool_log_history(g_zfs, history_str);