Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 14 Aug 2018 18:11:06 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r337800 - head/lib/libbe
Message-ID:  <201808141811.w7EIB67Y039096@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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;
 };



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808141811.w7EIB67Y039096>