Blame SOURCES/autofs-5.1.7-fix-concat_options-error-handling.patch

288172
autofs-5.1.7 - fix concat_options() error handling
288172
288172
From: Ian Kent <raven@themaw.net>
288172
288172
There's a possibility of a memory leak in the mount options processing
288172
when calling concat_options() in parse_mount() of the Sun format map
288172
entry parsing.
288172
288172
There's also a case in do_init() of the Sun map format parsing where
288172
a previously freed value is used in a logging statement without being
288172
set to MULL.
288172
288172
So ensure concat_options() always frees it's arguments so that the
288172
handling can be consistent in all places.
288172
288172
Signed-off-by: Ian Kent <raven@themaw.net>
288172
---
288172
 CHANGELOG           |    1 +
288172
 modules/parse_sun.c |   24 +++++++++++-------------
288172
 2 files changed, 12 insertions(+), 13 deletions(-)
288172
288172
--- autofs-5.1.4.orig/CHANGELOG
288172
+++ autofs-5.1.4/CHANGELOG
288172
@@ -102,6 +102,7 @@
288172
 - fix hosts map deadlock on restart.
288172
 - fix deadlock with hosts map reload.
288172
 - fix memory leak in update_hosts_mounts().
288172
+- fix concat_options() error handling.
288172
 
288172
 xx/xx/2018 autofs-5.1.5
288172
 - fix flag file permission.
288172
--- autofs-5.1.4.orig/modules/parse_sun.c
288172
+++ autofs-5.1.4/modules/parse_sun.c
288172
@@ -380,7 +380,8 @@ static int do_init(int argc, const char
288172
 			if (!tmp) {
288172
 				char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
288172
 				logerr(MODPREFIX "concat_options: %s", estr);
288172
-				free(gbl_options);
288172
+				/* freed in concat_options */
288172
+				ctxt->optstr = NULL;
288172
 			} else
288172
 				ctxt->optstr = tmp;
288172
 		} else {
288172
@@ -492,12 +493,16 @@ static char *concat_options(char *left,
288172
 	char *ret;
288172
 
288172
 	if (left == NULL || *left == '\0') {
288172
+		if (!right || *right == '\0')
288172
+			return NULL;
288172
 		ret = strdup(right);
288172
 		free(right);
288172
 		return ret;
288172
 	}
288172
 
288172
 	if (right == NULL || *right == '\0') {
288172
+		if (left == NULL || *left == '\0')
288172
+			return NULL;
288172
 		ret = strdup(left);
288172
 		free(left);
288172
 		return ret;
288172
@@ -508,6 +513,8 @@ static char *concat_options(char *left,
288172
 	if (ret == NULL) {
288172
 		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
288172
 		logerr(MODPREFIX "malloc: %s", estr);
288172
+		free(left);
288172
+		free(right);
288172
 		return NULL;
288172
 	}
288172
 
288172
@@ -988,14 +995,13 @@ static int parse_mapent(const char *ent,
288172
 			if (newopt && strstr(newopt, myoptions)) {
288172
 				free(myoptions);
288172
 				myoptions = newopt;
288172
-			} else {
288172
+			} else if (newopt) {
288172
 				tmp = concat_options(myoptions, newopt);
288172
 				if (!tmp) {
288172
 					char *estr;
288172
 					estr = strerror_r(errno, buf, MAX_ERR_BUF);
288172
 					error(logopt, MODPREFIX
288172
 					      "concat_options: %s", estr);
288172
-					free(myoptions);
288172
 					return 0;
288172
 				}
288172
 				myoptions = tmp;
288172
@@ -1363,16 +1369,12 @@ dont_expand:
288172
 			if (mnt_options && noptions && strstr(noptions, mnt_options)) {
288172
 				free(mnt_options);
288172
 				mnt_options = noptions;
288172
-			} else {
288172
+			} else if (noptions) {
288172
 				tmp = concat_options(mnt_options, noptions);
288172
 				if (!tmp) {
288172
 					char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
288172
 					error(ap->logopt,
288172
 					      MODPREFIX "concat_options: %s", estr);
288172
-					if (noptions)
288172
-						free(noptions);
288172
-					if (mnt_options)
288172
-						free(mnt_options);
288172
 					free(options);
288172
 					free(pmapent);
288172
 					return 1;
288172
@@ -1392,15 +1394,11 @@ dont_expand:
288172
 			if (options && mnt_options && strstr(mnt_options, options)) {
288172
 				free(options);
288172
 				options = mnt_options;
288172
-			} else {
288172
+			} else if (mnt_options) {
288172
 				tmp = concat_options(options, mnt_options);
288172
 				if (!tmp) {
288172
 					char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
288172
 					error(ap->logopt, MODPREFIX "concat_options: %s", estr);
288172
-					if (options)
288172
-						free(options);
288172
-					if (mnt_options)
288172
-						free(mnt_options);
288172
 					free(pmapent);
288172
 					return 1;
288172
 				}