1. 28 Sep, 2022 2 commits
  2. 27 Sep, 2022 13 commits
    • Christian Schwarz's avatar
      DMU_BACKUP_FEATURE: indicate that bit 28 and 29 are reserved · e872ea16
      Christian Schwarz authored
      
      Bit 28 is used by an internal Nutanix feature which might be
      upstreamed in the future.
      
      Bit 29 is the last unused bit. It is reserved to indicate a
      to-be-designed extension to the stream format which will accomodate
      more feature flags.
      Reviewed-by: default avatarTino Reichardt <milky-zfs@mcmilk.de>
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarChristian Schwarz <christian.schwarz@nutanix.com>
      Issue #13795
      Closes #13796
      e872ea16
    • Christian Schwarz's avatar
      DMU_BACKUP_FEATURE: remove unused BLAKE3 feature · 5c966638
      Christian Schwarz authored
      Commit 985c33b1
      
       added DMU_BACKUP_FEATURE_BLAKE3 but it is not used by
      the code.
      Reviewed-by: default avatarTino Reichardt <milky-zfs@mcmilk.de>
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarChristian Schwarz <christian.schwarz@nutanix.com>
      Issue #13795
      Closes #13796
      5c966638
    • Richard Yao's avatar
      PAM: Fix uninitialized value read · 9a49c6b7
      Richard Yao authored
      
      Clang's static analyzer found that config.uid is uninitialized when
      zfs_key_config_load() returns an error.
      
      Oddly, this was not included in the unchecked return values that
      Coverity found.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13957 
      9a49c6b7
    • Richard Yao's avatar
      Fix unsafe string operations · a51288aa
      Richard Yao authored
      
      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: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13913 
      a51288aa
    • Richard Yao's avatar
      Cleanup spa_export_common() · 88b199c2
      Richard Yao authored
      
      Coverity complains about a possible NULL pointer dereference. This is
      impossible, but it suspects it because we do a NULL check against
      `spa->spa_root_vdev`. This NULL check was never necessary and makes the
      code harder to understand, so we drop it.
      
      In particular, we dereference `spa->spa_root_vdev` when `new_state !=
      POOL_STATE_UNINITIALIZED && !hardforce`. The first is only true when
      spa_reset is called, which only occurs under fault injection.  The
      second is true unless `zpool export -F $POOLNAME` is used.  Therefore,
      we effectively *always* dereference the pointer. In the cases where we
      do not, there is no reason to think it is unsafe.  Therefore this change
      is safe to make.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13905 
      88b199c2
    • Richard Yao's avatar
      LUA: Fix CVE-2014-5461 · 31b4e008
      Richard Yao authored
      Apply the fix from upstream.
      
      http://www.lua.org/bugs.html#5.2.2-1
      https://www.opencve.io/cve/CVE-2014-5461
      
      
      
      It should be noted that exploiting this requires the `SYS_CONFIG`
      privilege, and anyone with that privilege likely has other opportunities
      to do exploits, so it is unlikely that bad actors could exploit this
      unless system administrators are executing untrusted ZFS Channel
      Programs.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13949 
      31b4e008
    • Richard Yao's avatar
      Cleanup: Specify unsignedness on things that should not be signed · fdc2d303
      Richard Yao authored
      
      In #13871, zfs_vdev_aggregation_limit_non_rotating and
      zfs_vdev_aggregation_limit being signed was pointed out as a possible
      reason not to eliminate an unnecessary MAX(unsigned, 0) since the
      unsigned value was assigned from them.
      
      There is no reason for these module parameters to be signed and upon
      inspection, it was found that there are a number of other module
      parameters that are signed, but should not be, so we make them unsigned.
      Making them unsigned made it clear that some other variables in the code
      should also be unsigned, so we also make those unsigned. This prevents
      users from setting negative values that could potentially cause bad
      behaviors. It also makes the code slightly easier to understand.
      
      Mostly module parameters that deal with timeouts, limits, bitshifts and
      percentages are made unsigned by this. Any that are boolean are left
      signed, since whether booleans should be considered signed or unsigned
      does not matter.
      
      Making zfs_arc_lotsfree_percent unsigned caused a
      `zfs_arc_lotsfree_percent >= 0` check to become redundant, so it was
      removed. Removing the check was also necessary to prevent a compiler
      error from -Werror=type-limits.
      
      Several end of line comments had to be moved to their own lines because
      replacing int with uint_t caused us to exceed the 80 character limit
      enforced by cstyle.pl.
      
      The following were kept signed because they are passed to
      taskq_create(), which expects signed values and modifying the
      OpenSolaris/Illumos DDI is out of scope of this patch:
      
      	* metaslab_load_pct
      	* zfs_sync_taskq_batch_pct
      	* zfs_zil_clean_taskq_nthr_pct
      	* zfs_zil_clean_taskq_minalloc
      	* zfs_zil_clean_taskq_maxalloc
      	* zfs_arc_prune_task_threads
      
      Also, negative values in those parameters was found to be harmless.
      
      The following were left signed because either negative values make
      sense, or more analysis was needed to determine whether negative values
      should be disallowed:
      
      	* zfs_metaslab_switch_threshold
      	* zfs_pd_bytes_max
      	* zfs_livelist_min_percent_shared
      
      zfs_multihost_history was made static to be consistent with other
      parameters.
      
      A number of module parameters were marked as signed, but in reality
      referenced unsigned variables. upgrade_errlog_limit is one of the
      numerous examples. In the case of zfs_vdev_async_read_max_active, it was
      already uint32_t, but zdb had an extern int declaration for it.
      
      Interestingly, the documentation in zfs.4 was right for
      upgrade_errlog_limit despite the module parameter being wrongly marked,
      while the documentation for zfs_vdev_async_read_max_active (and friends)
      was wrong. It was also wrong for zstd_abort_size, which was unsigned,
      but was documented as signed.
      
      Also, the documentation in zfs.4 incorrectly described the following
      parameters as ulong when they were int:
      
      	* zfs_arc_meta_adjust_restarts
      	* zfs_override_estimate_recordsize
      
      They are now uint_t as of this patch and thus the man page has been
      updated to describe them as uint.
      
      dbuf_state_index was left alone since it does nothing and perhaps should
      be removed in another patch.
      
      If any module parameters were missed, they were not found by `grep -r
      'ZFS_MODULE_PARAM' | grep ', INT'`. I did find a few that grep missed,
      but only because they were in files that had hits.
      
      This patch intentionally did not attempt to address whether some of
      these module parameters should be elevated to 64-bit parameters, because
      the length of a long on 32-bit is 32-bit.
      
      Lastly, it was pointed out during review that uint_t is a better match
      for these variables than uint32_t because FreeBSD kernel parameter
      definitions are designed for uint_t, whose bit width can change in
      future memory models.  As a result, we change the existing parameters
      that are uint32_t to use uint_t.
      Reviewed-by: default avatarAlexander Motin <mav@FreeBSD.org>
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Reviewed-by: default avatarNeal Gompa <ngompa@datto.com>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13875 
      fdc2d303
    • Richard Yao's avatar
      Cleanup: Switch to strlcpy from strncpy · 7584fbe8
      Richard Yao authored
      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 04a479f7
      
       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: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Reviewed-by: default avatarRyan Moeller <ryan@iXsystems.com>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13876 
      7584fbe8
    • Jitendra Patidar's avatar
      Enforce "-F" flag on resuming recv of full/newfs on existing dataset · 3ed9d688
      Jitendra Patidar authored
      
      When receiving full/newfs on existing dataset, then it should be done
      with "-F" flag. Its enforced for initial receive in checks done in
      zfs_receive_one function of libzfs. Similarly, on resuming full/newfs
      recv on existing dataset, it should be done with "-F" flag.
      
      When dataset doesn't exist, then full/new recv is done on newly created
      dataset and it's marked INCONSISTENT. But when receiving on existing
      dataset, recv is first done on %recv and its marked INCONSISTENT.
      Existing dataset is not marked INCONSISTENT. Resume of full/newfs
      receive with dataset not INCONSISTENT indicates that its resuming newfs
      on existing dataset. So, enforce "-F" flag in this case.
      
      Also return an error from dmu_recv_resume_begin_check() in zfs kernel,
      when its resuming full/newfs recv without force.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Reviewed-by: default avatarChunwei Chen <david.chen@nutanix.com>
      Signed-off-by: default avatarJitendra Patidar <jitendra.patidar@nutanix.com>
      Closes #13856
      Closes #13857 
      3ed9d688
    • Richard Yao's avatar
      Fix bad free in skein code · a2163a96
      Richard Yao authored
      
      Clang's static analyzer found a bad free caused by skein_mac_atomic().
      It will allocate a context on the stack and then pass it to
      skein_final(), which attempts to free it. Upon inspection,
      skein_digest_atomic() also has the same problem.
      
      These functions were created to match the OpenSolaris ICP API, so I was
      curious how we avoided this in other providers and looked at the SHA2
      code. It appears that SHA2 has a SHA2Final() helper function that is
      called by the exported sha2_mac_final()/sha2_digest_final() as well as
      the sha2_mac_atomic() and sha2_digest_atomic() functions. The real work
      is done in SHA2Final() while some checks and the free are done in
      sha2_mac_final()/sha2_digest_final().
      
      We fix the use after free in the skein code by taking inspiration from
      the SHA2 code. We introduce a skein_final_nofree() that does most of the
      work, and make skein_final() into a function that calls it and then
      frees the memory.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Reviewed-by: default avatarTony Hutter <hutter2@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13954 
      a2163a96
    • Richard Yao's avatar
      Fix userspace memory leaks found by Clang Static Analzyer · f7bda2de
      Richard Yao authored
      
      Recently, I have been making a push to fix things that coverity found.
      However, I was curious what Clang's static analyzer reported, so I ran
      it and found things that coverity had missed.
      
      * contrib/pam_zfs_key/pam_zfs_key.c: If prop_mountpoint is passed more
        than once, we leak memory.
      * module/zfs/zcp_get.c: We leak memory on temporary properties in
        userspace.
      * tests/zfs-tests/cmd/draid.c: On error from vdev_draid_rand(), we leak
        memory if best_map had been allocated by a prior iteration.
      * tests/zfs-tests/cmd/mkfile.c: Memory used by the loop is not freed
        before program termination.
      
      Arguably, these are all minor issues, but if we ignore them, then they
      could obscure serious bugs, so we fix them.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13955 
      f7bda2de
    • Chris Zubrzycki's avatar
    • Richard Yao's avatar
      Cleanup: Remove ineffective unsigned comparisons against 0 · 8ef15f93
      Richard Yao authored
      
      Coverity found a number of places where we either do MAX(unsigned, 0) or
      do assertions that a unsigned variable is >= 0. These do nothing, so
      let us drop them all.
      
      It also found a spot where we do `if (unsigned >= 0 && ...)`. Let us
      also drop the unsigned >= 0 check.
      Reviewed-by: default avatarNeal Gompa <ngompa@datto.com>
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13871 
      8ef15f93
  3. 26 Sep, 2022 2 commits
  4. 23 Sep, 2022 4 commits
    • Richard Yao's avatar
      Fix userland resource leaks · ebe1d036
      Richard Yao authored
      
      Coverity caught these. With the exception of the file descriptor leak in
      tests/zfs-tests/cmd/draid.c, they are all memory leaks.
      
      Also, there is a piece of dead code in zfs_get_enclosure_sysfs_path().
      We delete it as cleanup.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Reviewed-by: default avatarRyan Moeller <ryan@iXsystems.com>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13921 
      ebe1d036
    • Richard Yao's avatar
      Fix unchecked return values and unused return values · 2a493a4c
      Richard Yao authored
      
      Coverity complained about unchecked return values and unused values that
      turned out to be unused return values.
      
      Different approaches were used to handle the different cases of
      unchecked return values:
      
      * cmd/zdb/zdb.c: VERIFY0 was used in one place since the existing code
        had no error handling. An error message was printed in another to
        match the rest of the code.
      
      * cmd/zed/agents/zfs_retire.c: We dismiss the return value with `(void)`
        because the value is expected to be potentially unset.
      
      * cmd/zpool_influxdb/zpool_influxdb.c: We dismiss the return value with
        `(void)` because the values are expected to be potentially unset.
      
      * cmd/ztest.c: VERIFY0 was used since we want failures if something goes
        wrong in ztest.
      
      * module/zfs/dsl_dir.c: We dismiss the return value with `(void)`
        because there is no guarantee that the zap entry will always be there.
        For example, old pools imported readonly would not have it and we do
        not want to fail here because of that.
      
      * module/zfs/zfs_fm.c: `fnvlist_add_*()` was used since the
        allocations sleep and thus can never fail.
      
      * module/zfs/zvol.c: We dismiss the return value with `(void)` because
        we do not need it. This matches what is already done in the analogous
        `zfs_replay_write2()`.
      
      * tests/zfs-tests/cmd/draid.c: We suppress one return value with
        `(void)` since the code handles errors already. The other return value
        is handled by switching to `fnvlist_lookup_uint8_array()`.
      
      * tests/zfs-tests/cmd/file/file_fadvise.c: We add error handling.
      
      * tests/zfs-tests/cmd/mmap_sync.c: We add error handling for munmap, but
        ignore failures on remove() with (void) since it is expected to be
        able to fail.
      
      * tests/zfs-tests/cmd/mmapwrite.c: We add error handling.
      
      As for unused return values, they were all in places where there was
      error handling, so logic was added to handle the return values.
      Reviewed-by: default avatarAlexander Motin <mav@FreeBSD.org>
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13920 
      2a493a4c
    • Richard Yao's avatar
      set_global_var_parse_kv() should pass the pointer from strdup() · d25153d5
      Richard Yao authored
      
      A comment says that the caller should free k_out, but the pointer passed
      via k_out is not the same pointer we received from strdup(). Instead,
      it is a pointer into the region we received from strdup(). The free
      function should always be called with the original pointer, so this is
      likely a bug.
      
      We solve this by calling `strdup()` a second time and then freeing the
      original pointer.
      
      Coverity reported this as a memory leak.
      Reviewed-by: default avatarNeal Gompa <ngompa@datto.com>
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarRichard Yao <richard.yao@alumni.stonybrook.edu>
      Closes #13867
      d25153d5
    • Tony Hutter's avatar
      zpool: Don't print "repairing" on force faulted drives · e9b12d41
      Tony Hutter authored
      
      If you force fault a drive that's resilvering, it's scan stats can get
      frozen in time, giving the false impression that it's being resilvered.
      This commit checks the vdev state to see if the vdev is healthy before
      reporting "resilvering" or "repairing" in zpool status.
      Reviewed-by: default avatarBrian Behlendorf <behlendorf1@llnl.gov>
      Signed-off-by: default avatarTony Hutter <hutter2@llnl.gov>
      Closes #13927
      Closes #13930 
      e9b12d41
  5. 22 Sep, 2022 4 commits
  6. 20 Sep, 2022 15 commits