From owner-svn-src-head@freebsd.org Tue Aug 14 18:11:07 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8524610805EC; Tue, 14 Aug 2018 18:11:07 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3ABE87FD98; Tue, 14 Aug 2018 18:11:07 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 1CA7A1F0AD; Tue, 14 Aug 2018 18:11:07 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w7EIB6xE039098; Tue, 14 Aug 2018 18:11:06 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w7EIB67Y039096; Tue, 14 Aug 2018 18:11:06 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201808141811.w7EIB67Y039096@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Tue, 14 Aug 2018 18:11:06 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r337800 - head/lib/libbe X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/lib/libbe X-SVN-Commit-Revision: 337800 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Aug 2018 18:11:07 -0000 Author: kevans Date: Tue Aug 14 18:11:06 2018 New Revision: 337800 URL: https://svnweb.freebsd.org/changeset/base/337800 Log: libbe(3): Fix leaky faucets Amongst them: - Resource leaks - Logically dead code - Unused values - Null termination issues Reported by: asomers (pointer to Coverity), Coverity CID: 1394777, 1394791, 1394830, 1394844, 1394872, 1394894, CID: 1394900, 1394907, 1394950, 1394965 Modified: head/lib/libbe/be.c head/lib/libbe/be_access.c head/lib/libbe/be_impl.h Modified: head/lib/libbe/be.c ============================================================================== --- head/lib/libbe/be.c Tue Aug 14 18:10:25 2018 (r337799) +++ head/lib/libbe/be.c Tue Aug 14 18:11:06 2018 (r337800) @@ -64,10 +64,13 @@ be_locate_rootfs(zfs_handle_t *chkds, void *data) if (lbh == NULL) return (1); + mntpoint = NULL; if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) { - strncpy(lbh->rootfs, zfs_get_name(chkds), BE_MAXPATHLEN); + strlcpy(lbh->rootfs, zfs_get_name(chkds), BE_MAXPATHLEN + 1); + free(mntpoint); return (1); - } + } else if(mntpoint != NULL) + free(mntpoint); return (0); } @@ -120,8 +123,8 @@ libbe_init(void) /* Remove leading 'zfs:' if present, otherwise use value as-is */ if (strcmp(lbh->root, "zfs:") == 0) - strncpy(lbh->root, strchr(lbh->root, ':') + sizeof(char), - BE_MAXPATHLEN); + strlcpy(lbh->root, strchr(lbh->root, ':') + sizeof(char), + BE_MAXPATHLEN + 1); if ((pos = strchr(lbh->root, '/')) == NULL) goto err; @@ -160,8 +163,6 @@ err: libzfs_fini(lbh->lzh); free(lbh); } - if (rootds != NULL) - zfs_close(rootds); free(poolname); return (NULL); } @@ -397,20 +398,16 @@ be_deep_clone(zfs_handle_t *ds, void *data) ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL) return (-1); - if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) { - switch (err) { - case EZFS_SUCCESS: - err = BE_ERR_SUCCESS; - break; - default: - err = BE_ERR_ZFSCLONE; - break; - } - } + if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) + err = BE_ERR_ZFSCLONE; nvlist_free(props); zfs_close(snap_hdl); + /* Failed to clone */ + if (err != BE_ERR_SUCCESS) + return (set_error(isdc->lbh, err)); + sdc.lbh = isdc->lbh; sdc.bename = NULL; sdc.snapname = isdc->snapname; @@ -451,14 +448,13 @@ be_create_from_existing_snap(libbe_handle_t *lbh, cons else bename++; - if ((parentname = strdup(snap_path)) == NULL) { - err = BE_ERR_UNKNOWN; - return (set_error(lbh, err)); - } + if ((parentname = strdup(snap_path)) == NULL) + return (set_error(lbh, BE_ERR_UNKNOWN)); + snapname = strchr(parentname, '@'); if (snapname == NULL) { - err = BE_ERR_UNKNOWN; - return (set_error(lbh, err)); + free(parentname); + return (set_error(lbh, BE_ERR_UNKNOWN)); } *snapname = '\0'; snapname++; @@ -471,6 +467,7 @@ be_create_from_existing_snap(libbe_handle_t *lbh, cons parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET); err = be_deep_clone(parent_hdl, &sdc); + free(parentname); return (set_error(lbh, err)); } @@ -502,7 +499,7 @@ int be_validate_snap(libbe_handle_t *lbh, const char *snap_name) { zfs_handle_t *zfs_hdl; - char buf[BE_MAXPATHLEN]; + char buf[BE_MAXPATHLEN + 1]; char *delim_pos; int err = BE_ERR_SUCCESS; @@ -513,7 +510,7 @@ be_validate_snap(libbe_handle_t *lbh, const char *snap ZFS_TYPE_SNAPSHOT)) return (BE_ERR_NOENT); - strncpy(buf, snap_name, BE_MAXPATHLEN); + strlcpy(buf, snap_name, BE_MAXPATHLEN + 1); /* Find the base filesystem of the snapshot */ if ((delim_pos = strchr(buf, '@')) == NULL) Modified: head/lib/libbe/be_access.c ============================================================================== --- head/lib/libbe/be_access.c Tue Aug 14 18:10:25 2018 (r337799) +++ head/lib/libbe/be_access.c Tue Aug 14 18:11:06 2018 (r337800) @@ -51,8 +51,10 @@ be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data) return (0); if (strcmp(mountpoint, info->path) == 0) { info->name = strdup(zfs_get_name(zfs_hdl)); + free(mountpoint); return (1); } + free(mountpoint); return (0); } @@ -106,7 +108,6 @@ be_mount(libbe_handle_t *lbh, char *bootenv, char *mou { char be[BE_MAXPATHLEN]; char mnt_temp[BE_MAXPATHLEN]; - char *path; int mntflags; int err; @@ -116,7 +117,7 @@ be_mount(libbe_handle_t *lbh, char *bootenv, char *mou if (!be_exists(lbh, bootenv)) return (set_error(lbh, BE_ERR_NOENT)); - if (is_mounted(lbh->lzh, be, &path)) + if (is_mounted(lbh->lzh, be, NULL)) return (set_error(lbh, BE_ERR_MOUNTED)); mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; Modified: head/lib/libbe/be_impl.h ============================================================================== --- head/lib/libbe/be_impl.h Tue Aug 14 18:10:25 2018 (r337799) +++ head/lib/libbe/be_impl.h Tue Aug 14 18:11:06 2018 (r337800) @@ -38,9 +38,9 @@ struct libbe_handle { libzfs_handle_t *lzh; zpool_handle_t *active_phandle; - char root[BE_MAXPATHLEN]; - char rootfs[BE_MAXPATHLEN]; - char bootfs[BE_MAXPATHLEN]; + char root[BE_MAXPATHLEN + 1]; + char rootfs[BE_MAXPATHLEN + 1]; + char bootfs[BE_MAXPATHLEN + 1]; be_error_t error; bool print_on_err; };