freebsd-dev/contrib
Richard Yao a51288aabb
Fix unsafe string operations
Coverity caught unsafe use of `strcpy()` in `ztest_dmu_objset_own()`,
`nfs_init_tmpfile()` and `dump_snapshot()`. It also caught an unsafe use
of `strlcat()` in `nfs_init_tmpfile()`.

Inspired by this, I did an audit of every single usage of `strcpy()` and
`strcat()` in the code. If I could not prove that the usage was safe, I
changed the code to use either `strlcpy()` or `strlcat()`, depending on
which function was originally used. In some cases, `snprintf()` was used
to replace multiple uses of `strcat` because it was cleaner.

Whenever I changed a function, I preferred to use `sizeof(dst)` when the
compiler is able to provide the string size via that. When it could not
because the string was passed by a caller, I checked the entire call
tree of the function to find out how big the buffer was and hard coded
it. Hardcoding is less than ideal, but it is safe unless someone shrinks
the buffer sizes being passed.

Additionally, Coverity reported three more string related issues:

 * It caught a case where we do an overlapping memory copy in a call to
   `snprintf()`. We fix that via `kmem_strdup()` and `kmem_strfree()`.

 * It caught `sizeof (buf)` being used instead of `buflen` in
   `zdb_nicenum()`'s call to `zfs_nicenum()`, which is passed to
   `snprintf()`. We change that to pass `buflen`.

 * It caught a theoretical unterminated string passed to `strcmp()`.
   This one is likely a false positive, but we have the information
   needed to do this more safely, so we change this to silence the false
   positive not just in coverity, but potentially other static analysis
   tools too. We switch to `strncmp()`.

 * There was a false positive in tests/zfs-tests/cmd/dir_rd_update.c. We
   suppress it by switching to `snprintf()` since other static analysis
   tools might complain about it too. Interestingly, there is a possible
   real bug there too, since it assumes that the passed directory path
   ends with '/'. We add a '/' to fix that potential bug.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13913
2022-09-27 16:47:24 -07:00
..
bash_completion.d contrib: bash_completion.d: install, fix dist 2022-05-10 10:20:40 -07:00
bpftrace Replace EXTRA_DIST with dist_noinst_DATA 2022-05-26 09:24:50 -07:00
coverity Update coverity model 2022-09-16 13:45:15 -07:00
dracut contrib: dracut: zfs-snapshot-bootfs: exit status fix 2022-08-12 14:28:15 -07:00
initramfs Replace EXTRA_DIST with dist_noinst_DATA 2022-05-26 09:24:50 -07:00
intel_qat Add intel_QAT patches 2020-09-30 13:17:30 -07:00
pam_zfs_key Fix unsafe string operations 2022-09-27 16:47:24 -07:00
pyzfs Enforce "-F" flag on resuming recv of full/newfs on existing dataset 2022-09-27 16:34:27 -07:00
zcp Replace EXTRA_DIST with dist_noinst_DATA 2022-05-26 09:24:50 -07:00
Makefile.am autoconf: use include directives instead of recursing down contrib 2022-05-10 10:19:44 -07:00