]> git.proxmox.com Git - mirror_zfs.git/commitdiff
Fix userland resource leaks
authorRichard Yao <richard.yao@alumni.stonybrook.edu>
Fri, 23 Sep 2022 23:55:26 +0000 (19:55 -0400)
committerGitHub <noreply@github.com>
Fri, 23 Sep 2022 23:55:26 +0000 (16:55 -0700)
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: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13921

cmd/zdb/zdb.c
cmd/zfs/zfs_main.c
cmd/zhack.c
cmd/zpool/zpool_main.c
lib/libzfs/libzfs_dataset.c
lib/libzutil/os/linux/zutil_device_path_os.c
tests/zfs-tests/cmd/btree_test.c
tests/zfs-tests/cmd/draid.c

index a17793c2d90e7a92f0f5debb458952f6d328fe3f..aa8b67a872cfdcdc039d47e6fbefadc5f4752a8b 100644 (file)
@@ -1137,6 +1137,8 @@ dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
        }
 
        if (size == 0) {
+               if (data == NULL)
+                       kmem_free(arr, oursize);
                (void) printf("\t\t[]\n");
                return;
        }
@@ -7062,8 +7064,11 @@ import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
                freecfg = B_TRUE;
        }
 
-       if (asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX) == -1)
+       if (asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX) == -1) {
+               if (target != poolname)
+                       free(poolname);
                return (NULL);
+       }
        fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
 
        error = spa_import(bogus_name, cfg, NULL,
@@ -7078,6 +7083,7 @@ import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
 
        if (new_path != NULL && path_start != NULL) {
                if (asprintf(new_path, "%s%s", bogus_name, path_start) == -1) {
+                       free(bogus_name);
                        if (path_start != NULL)
                                free(poolname);
                        return (NULL);
@@ -8198,8 +8204,7 @@ zdb_read_block(char *thing, spa_t *spa)
        vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
        if (vd == NULL) {
                (void) printf("***Invalid vdev: %s\n", vdev);
-               free(dup);
-               return;
+               goto done;
        } else {
                if (vd->vdev_path)
                        (void) fprintf(stderr, "Found vdev: %s\n",
index 008f1bea0ec9ce345ac2cdeef01da7c5e1adc218..6a1e37a2e3626bcd25d2fdfb21cdc95d435707fd 100644 (file)
@@ -7395,8 +7395,11 @@ unshare_unmount(int op, int argc, char **argv)
                    ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
                        nomem();
 
-               if ((mnttab = fopen(MNTTAB, "re")) == NULL)
+               if ((mnttab = fopen(MNTTAB, "re")) == NULL) {
+                       uu_avl_destroy(tree);
+                       uu_avl_pool_destroy(pool);
                        return (ENOENT);
+               }
 
                while (getmntent(mnttab, &entry) == 0) {
 
index 75d9c93b3649935e533ca975617cab7a5038a0c4..8797a53e476353158b6bf4c2e3b7d77ae073ca7a 100644 (file)
@@ -295,6 +295,8 @@ zhack_do_feature_enable(int argc, char **argv)
                        feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
                        break;
                case 'd':
+                       if (desc != NULL)
+                               free(desc);
                        desc = strdup(optarg);
                        break;
                default:
index f412c82f983be74c8054e1e2833532dc0f8dd983..39ea615f63195f3cbd61ef2b7e4e058a0b5b9ec8 100644 (file)
@@ -1196,6 +1196,7 @@ zpool_do_remove(int argc, char **argv)
                return (1);
 
        if (stop && noop) {
+               zpool_close(zhp);
                (void) fprintf(stderr, gettext("stop request ignored\n"));
                return (0);
        }
index 047a254882f7e4c5b4a350dc308a6738eea11052..29798af0371e37aa40661f0ff25e4953a4741beb 100644 (file)
@@ -2007,6 +2007,7 @@ zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
                goto error;
 
        if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
+               changelist_free(cl);
                return (zfs_standard_error(hdl, errno, errbuf));
        } else {
 
index 9f4c74f50f3cf28d233c5624727a402e3c25ed2a..05dbb39954fab31f7f9382e8da66ee1cc3a97fff 100644 (file)
@@ -344,6 +344,8 @@ zfs_get_enclosure_sysfs_path(const char *dev_name)
                if (strstr(ep->d_name, "enclosure_device") == NULL)
                        continue;
 
+               if (tmp2 != NULL)
+                       free(tmp2);
                if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1) {
                        tmp2 = NULL;
                        break;
@@ -372,14 +374,13 @@ zfs_get_enclosure_sysfs_path(const char *dev_name)
                if (tmp3 == NULL)
                        break;
 
+               if (path != NULL)
+                       free(path);
                if (asprintf(&path, "/sys/class/%s", tmp3) == -1) {
                        /* If asprintf() fails, 'path' is undefined */
                        path = NULL;
                        break;
                }
-
-               if (path == NULL)
-                       break;
        }
 
 end:
index aaad4e47e464adee57b46294cfebbf297beea9b4..456a36f31565875e1f5c7975cca868ea9f7343cc 100644 (file)
@@ -233,13 +233,13 @@ drain_tree(zfs_btree_t *bt, char *why)
                void *ret;
 
                u_longlong_t randval = random();
-               node = malloc(sizeof (int_node_t));
                if ((p = (uint64_t *)zfs_btree_find(bt, &randval, &bt_idx)) !=
                    NULL) {
                        continue;
                }
                zfs_btree_add_idx(bt, &randval, &bt_idx);
 
+               node = malloc(sizeof (int_node_t));
                node->data = randval;
                if ((ret = avl_find(&avl, node, &avl_idx)) != NULL) {
                        snprintf(why, BUFSIZE, "Found in avl: %llu\n", randval);
index 831039288a5593968d5be25745a8f454a6434686..869ca902d0832fbfdcef7d1d874a0f0ec5920340 100644 (file)
@@ -849,6 +849,7 @@ restart:
                        if (rc < 0) {
                                printf("Unable to read /dev/urandom: %s\n:",
                                    strerror(errno));
+                               close(fd);
                                return (1);
                        }
                        bytes_read += rc;