Blame SOURCES/0005-mount.cifs-fix-bad-free-of-string-returned-by-dirnam.patch

4c3126
From 7e2e95d0c84bd6960c46f1fa1c8227c50dd7a4b3 Mon Sep 17 00:00:00 2001
4c3126
From: Jeff Layton <jlayton@samba.org>
4c3126
Date: Thu, 10 Oct 2013 22:05:05 -0400
4c3126
Subject: [PATCH] mount.cifs: fix bad free() of string returned by dirname()
4c3126
4c3126
Coverity says:
4c3126
4c3126
  Error: CPPCHECK_WARNING: [#def10]
4c3126
  cifs-utils-6.2/mount.cifs.c:1518: error[memleakOnRealloc]: Common realloc mistake: 'mtabdir' nulled but not freed upon failure
4c3126
4c3126
del_mtab has a number of bugs in handling of allocated memory:
4c3126
4c3126
a) the return value of strdup() is not checked
4c3126
4c3126
b) It calls realloc() on a pointer that wasn't returned by an allocation
4c3126
   function (e.g. malloc, calloc, etc.)
4c3126
4c3126
c) If realloc() fails, it doesn't call free() on the original memory
4c3126
   returned by strdup()
4c3126
4c3126
Fix all of these bugs and add newlines to the end of the error messages
4c3126
in del_mtab.
4c3126
4c3126
Signed-off-by: Jeff Layton <jlayton@samba.org>
4c3126
---
4c3126
 mount.cifs.c | 29 ++++++++++++++++++-----------
4c3126
 1 file changed, 18 insertions(+), 11 deletions(-)
4c3126
4c3126
diff --git a/mount.cifs.c b/mount.cifs.c
4c3126
index 7206dcb..497665d 100644
4c3126
--- a/mount.cifs.c
4c3126
+++ b/mount.cifs.c
4c3126
@@ -1508,23 +1508,29 @@ add_mtab_exit:
4c3126
 static int
4c3126
 del_mtab(char *mountpoint)
4c3126
 {
4c3126
-	int tmprc, rc = 0;
4c3126
+	int len, tmprc, rc = 0;
4c3126
 	FILE *mnttmp, *mntmtab;
4c3126
 	struct mntent *mountent;
4c3126
-	char *mtabfile, *mtabdir, *mtabtmpfile;
4c3126
+	char *mtabfile, *mtabdir, *mtabtmpfile = NULL;
4c3126
 
4c3126
 	mtabfile = strdup(MOUNTED);
4c3126
-	mtabdir = dirname(mtabfile);
4c3126
-	mtabdir = realloc(mtabdir, strlen(mtabdir) + strlen(MNT_TMP_FILE) + 2);
4c3126
-	if (!mtabdir) {
4c3126
-		fprintf(stderr, "del_mtab: cannot determine current mtab path");
4c3126
+	if (!mtabfile) {
4c3126
+		fprintf(stderr, "del_mtab: cannot strdup MOUNTED\n");
4c3126
 		rc = EX_FILEIO;
4c3126
 		goto del_mtab_exit;
4c3126
 	}
4c3126
 
4c3126
-	mtabtmpfile = strcat(mtabdir, MNT_TMP_FILE);
4c3126
+	mtabdir = dirname(mtabfile);
4c3126
+	len = strlen(mtabdir) + strlen(MNT_TMP_FILE);
4c3126
+	mtabtmpfile = malloc(len + 1);
4c3126
 	if (!mtabtmpfile) {
4c3126
-		fprintf(stderr, "del_mtab: cannot allocate memory to tmp file");
4c3126
+		fprintf(stderr, "del_mtab: cannot allocate memory to tmp file\n");
4c3126
+		rc = EX_FILEIO;
4c3126
+		goto del_mtab_exit;
4c3126
+	}
4c3126
+
4c3126
+	if (sprintf(mtabtmpfile, "%s%s", mtabdir, MNT_TMP_FILE) != len) {
4c3126
+		fprintf(stderr, "del_mtab: error writing new string\n");
4c3126
 		rc = EX_FILEIO;
4c3126
 		goto del_mtab_exit;
4c3126
 	}
4c3126
@@ -1532,14 +1538,14 @@ del_mtab(char *mountpoint)
4c3126
 	atexit(unlock_mtab);
4c3126
 	rc = lock_mtab();
4c3126
 	if (rc) {
4c3126
-		fprintf(stderr, "del_mtab: cannot lock mtab");
4c3126
+		fprintf(stderr, "del_mtab: cannot lock mtab\n");
4c3126
 		rc = EX_FILEIO;
4c3126
 		goto del_mtab_exit;
4c3126
 	}
4c3126
 
4c3126
 	mtabtmpfile = mktemp(mtabtmpfile);
4c3126
 	if (!mtabtmpfile) {
4c3126
-		fprintf(stderr, "del_mtab: cannot setup tmp file destination");
4c3126
+		fprintf(stderr, "del_mtab: cannot setup tmp file destination\n");
4c3126
 		rc = EX_FILEIO;
4c3126
 		goto del_mtab_exit;
4c3126
 	}
4c3126
@@ -1587,7 +1593,8 @@ del_mtab(char *mountpoint)
4c3126
 
4c3126
 del_mtab_exit:
4c3126
 	unlock_mtab();
4c3126
-	free(mtabdir);
4c3126
+	free(mtabtmpfile);
4c3126
+	free(mtabfile);
4c3126
 	return rc;
4c3126
 
4c3126
 del_mtab_error:
4c3126
-- 
4c3126
1.8.3.1
4c3126