mirror_zfs/cmd
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
..
raidz_test Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
zdb Fix unsafe string operations 2022-09-27 16:47:24 -07:00
zed Fix unchecked return values and unused return values 2022-09-23 16:52:03 -07:00
zfs Fix unsafe string operations 2022-09-27 16:47:24 -07:00
zinject Fix unsafe string operations 2022-09-27 16:47:24 -07:00
zpool Fix userland resource leaks 2022-09-23 16:55:26 -07:00
zpool_influxdb Fix unchecked return values and unused return values 2022-09-23 16:52:03 -07:00
zstream Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
arc_summary cmd: move single-file binaries up, extract udev programs to udev/ 2022-05-10 10:20:34 -07:00
arcstat.in arcstat: fix -p option 2022-08-12 14:21:52 -07:00
dbufstat.in Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
fsck.zfs.in cmd: move single-file binaries up, extract udev programs to udev/ 2022-05-10 10:20:34 -07:00
Makefile.am Add zilstat script to report zil kstats in a user friendly manner 2022-09-02 13:24:07 -07:00
mount_zfs.c Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
zfs_ids_to_path.c Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
zgenhostid.c Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
zhack.c Fix userland resource leaks 2022-09-23 16:55:26 -07:00
zilstat.in Add zilstat script to report zil kstats in a user friendly manner 2022-09-02 13:24:07 -07:00
ztest.c Fix unsafe string operations 2022-09-27 16:47:24 -07:00
zvol_wait cmd: move single-file binaries up, extract udev programs to udev/ 2022-05-10 10:20:34 -07:00