aboutsummaryrefslogtreecommitdiffstats
path: root/tests/zfs-tests/cmd/draid.c
diff options
context:
space:
mode:
authorRichard Yao <[email protected]>2022-09-27 19:35:29 -0400
committerGitHub <[email protected]>2022-09-27 16:35:29 -0700
commit7584fbe846b4127d5a16c5967a005e8f5c1e7b08 (patch)
tree75e37df0bbb0660c5cee6b3564a29e77fd04ae3e /tests/zfs-tests/cmd/draid.c
parent3ed9d6883bcf3c55f92cdaaa6bf1aee2e6fb4115 (diff)
Cleanup: Switch to strlcpy from strncpy
Coverity found a bug in `zfs_secpolicy_create_clone()` where it is possible for us to pass an unterminated string when `zfs_get_parent()` returns an error. Upon inspection, it is clear that using `strlcpy()` would have avoided this issue. Looking at the codebase, there are a number of other uses of `strncpy()` that are unsafe and even when it is used safely, switching to `strlcpy()` would make the code more readable. Therefore, we switch all instances where we use `strncpy()` to use `strlcpy()`. Unfortunately, we do not portably have access to `strlcpy()` in tests/zfs-tests/cmd/zfs_diff-socket.c because it does not link to libspl. Modifying the appropriate Makefile.am to try to link to it resulted in an error from the naming choice used in the file. Trying to disable the check on the file did not work on FreeBSD because Clang ignores `#undef` when a definition is provided by `-Dstrncpy(...)=...`. We workaround that by explictly including the C file from libspl into the test. This makes things build correctly everywhere. We add a deprecation warning to `config/Rules.am` and suppress it on the remaining `strncpy()` usage. `strlcpy()` is not portably avaliable in tests/zfs-tests/cmd/zfs_diff-socket.c, so we use `snprintf()` there as a substitute. This patch does not tackle the related problem of `strcpy()`, which is even less safe. Thankfully, a quick inspection found that it is used far more correctly than strncpy() was used. A quick inspection did not find any problems with `strcpy()` usage outside of zhack, but it should be said that I only checked around 90% of them. Lastly, some of the fields in kstat_t varied in size by 1 depending on whether they were in userspace or in the kernel. The origin of this discrepancy appears to be 04a479f7066ccdaa23a6546955303b172f4a6909 where it was made for no apparent reason. It conflicts with the comment on KSTAT_STRLEN, so we shrink the kernel field sizes to match the userspace field sizes. Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Ryan Moeller <[email protected]> Signed-off-by: Richard Yao <[email protected]> Closes #13876
Diffstat (limited to 'tests/zfs-tests/cmd/draid.c')
-rw-r--r--tests/zfs-tests/cmd/draid.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/tests/zfs-tests/cmd/draid.c b/tests/zfs-tests/cmd/draid.c
index 39b58a709..76fdb4e84 100644
--- a/tests/zfs-tests/cmd/draid.c
+++ b/tests/zfs-tests/cmd/draid.c
@@ -828,7 +828,7 @@ draid_generate(int argc, char *argv[])
}
if (argc > optind)
- strncpy(filename, argv[optind], MAXPATHLEN - 1);
+ strlcpy(filename, argv[optind], sizeof (filename));
else {
(void) fprintf(stderr, "A FILE must be specified.\n");
return (1);
@@ -960,9 +960,9 @@ draid_verify(int argc, char *argv[])
return (ENOMEM);
if (realpath(argv[optind], abspath) != NULL)
- strncpy(filename, abspath, MAXPATHLEN - 1);
+ strlcpy(filename, abspath, sizeof (filename));
else
- strncpy(filename, argv[optind], MAXPATHLEN - 1);
+ strlcpy(filename, argv[optind], sizeof (filename));
free(abspath);
} else {
@@ -1169,7 +1169,7 @@ draid_dump(int argc, char *argv[])
}
if (argc > optind)
- strncpy(filename, argv[optind], MAXPATHLEN - 1);
+ strlcpy(filename, argv[optind], sizeof (filename));
else {
(void) fprintf(stderr, "A FILE must be specified.\n");
return (1);
@@ -1206,7 +1206,7 @@ draid_table(int argc, char *argv[])
int error;
if (argc > optind)
- strncpy(filename, argv[optind], MAXPATHLEN - 1);
+ strlcpy(filename, argv[optind], sizeof (filename));
else {
(void) fprintf(stderr, "A FILE must be specified.\n");
return (1);
@@ -1340,7 +1340,7 @@ draid_merge(int argc, char *argv[])
return (1);
}
- strncpy(filename, argv[optind], MAXPATHLEN - 1);
+ strlcpy(filename, argv[optind], sizeof (filename));
optind++;
error = read_map(filename, &allcfgs);
@@ -1355,7 +1355,7 @@ draid_merge(int argc, char *argv[])
char srcfilename[MAXPATHLEN] = {0};
int merged = 0;
- strncpy(srcfilename, argv[optind], MAXPATHLEN - 1);
+ strlcpy(srcfilename, argv[optind], sizeof (srcfilename));
error = draid_merge_impl(allcfgs, srcfilename, &merged);
if (error) {