Blame SOURCES/autofs-5.1.3-fix-possible-memory-leak-during-amd-parse.patch

304803
autofs-5.1.3 - fix possible memory leak during amd parse
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
If an amd map entry option is given more than once subsequent assignment
304803
could result in a memory leak.
304803
304803
Signed-off-by: Ian Kent <raven@themaw.net>
304803
---
304803
 CHANGELOG           |    1 
304803
 modules/amd_parse.y |   92 +++++++++++++++++++++++++++++-----------------------
304803
 2 files changed, 54 insertions(+), 39 deletions(-)
304803
304803
--- autofs-5.0.7.orig/CHANGELOG
304803
+++ autofs-5.0.7/CHANGELOG
304803
@@ -276,6 +276,7 @@
304803
 - refactor amd_parse.c.
304803
 - fix amd parser double quote handling.
304803
 - fix expandamdent() quote handling.
304803
+- fix possible memory leak during amd parse.
304803
 
304803
 25/07/2012 autofs-5.0.7
304803
 =======================
304803
--- autofs-5.0.7.orig/modules/amd_parse.y
304803
+++ autofs-5.0.7/modules/amd_parse.y
304803
@@ -41,6 +41,7 @@ extern int amd_lex(void);
304803
 extern void amd_set_scan_buffer(const char *);
304803
 
304803
 static char *amd_strdup(char *);
304803
+static void amd_set_value(char **, char *);
304803
 static void local_init_vars(void);
304803
 static void local_free_vars(void);
304803
 
304803
@@ -289,19 +290,22 @@ option_assignment: MAP_OPTION OPTION_ASS
304803
 			}
304803
 
304803
 			if (!strcmp($1, "fs"))
304803
-				entry.fs = fs_opt_val;
304803
+				amd_set_value(&entry.fs, fs_opt_val);
304803
 			else if (!strcmp($1, "sublink")) {
304803
-				entry.sublink = fs_opt_val;
304803
+				amd_set_value(&entry.sublink, fs_opt_val);
304803
 			} else if (!strcmp($1, "pref")) {
304803
 				if (strcmp(fs_opt_val, "null"))
304803
-					entry.pref = fs_opt_val;
304803
+					amd_set_value(&entry.pref, fs_opt_val);
304803
 				else {
304803
-					entry.pref = amd_strdup("");
304803
-					if (!entry.pref) {
304803
+					char *empty;
304803
+
304803
+					empty = amd_strdup("");
304803
+					if (!empty) {
304803
 						amd_notify($3);
304803
 						free(fs_opt_val);
304803
 						YYABORT;
304803
 					}
304803
+					amd_set_value(&entry.pref, empty);
304803
 					free(fs_opt_val);
304803
 				}
304803
 			} else {
304803
@@ -314,11 +318,14 @@ option_assignment: MAP_OPTION OPTION_ASS
304803
 	| MAP_OPTION OPTION_ASSIGN
304803
 	{
304803
 		if (!strcmp($1, "fs")) {
304803
-			entry.fs = amd_strdup("");
304803
-			if (!entry.fs) {
304803
+			char *empty;
304803
+
304803
+			empty = amd_strdup("");
304803
+			if (!empty) {
304803
 				amd_notify($1);
304803
 				YYABORT;
304803
 			}
304803
+			amd_set_value(&entry.fs, empty);
304803
 		} else {
304803
 			amd_notify($1);
304803
 			YYABORT;
304803
@@ -335,11 +342,11 @@ option_assignment: MAP_OPTION OPTION_ASS
304803
 		}
304803
 
304803
 		if (!strcmp($1, "rhost"))
304803
-			entry.rhost = fs_opt_val;
304803
+			amd_set_value(&entry.rhost, fs_opt_val);
304803
 		else if (!strcmp($1, "rfs"))
304803
-			entry.rfs = fs_opt_val;
304803
+			amd_set_value(&entry.rfs, fs_opt_val);
304803
 		else if (!strcmp($1, "dev"))
304803
-			entry.dev = fs_opt_val;
304803
+			amd_set_value(&entry.dev, fs_opt_val);
304803
 		else if (!strcmp($1, "mount") ||
304803
 			 !strcmp($1, "unmount") ||
304803
 			 !strcmp($1, "umount")) {
304803
@@ -369,11 +376,11 @@ option_assignment: MAP_OPTION OPTION_ASS
304803
 		}
304803
 
304803
 		if (!strcmp($1, "rhost"))
304803
-			entry.rhost = empty;
304803
+			amd_set_value(&entry.rhost, empty);
304803
 		else if (!strcmp($1, "rfs"))
304803
-			entry.rfs = empty;
304803
+			amd_set_value(&entry.rfs, empty);
304803
 		else if (!strcmp($1, "dev"))
304803
-			entry.dev = empty;
304803
+			amd_set_value(&entry.dev, empty);
304803
 		else {
304803
 			amd_notify($1);
304803
 			free(empty);
304803
@@ -468,36 +475,27 @@ static int match_map_option_fs_type(char
304803
 		return 0;
304803
 	}
304803
 
304803
-	if (!strcmp(fs_type, "auto")) {
304803
+	if (!strcmp(fs_type, "auto"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_AUTO;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "nfs") ||
304803
-		   !strcmp(fs_type, "nfs4")) {
304803
+	else if (!strcmp(fs_type, "nfs") ||
304803
+		 !strcmp(fs_type, "nfs4"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_NFS;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "nfsl")) {
304803
+	else if (!strcmp(fs_type, "nfsl"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_NFSL;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "link")) {
304803
+	else if (!strcmp(fs_type, "link"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_LINK;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "linkx")) {
304803
+	else if (!strcmp(fs_type, "linkx"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_LINKX;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "host")) {
304803
+	else if (!strcmp(fs_type, "host"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_HOST;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "lofs")) {
304803
+	else if (!strcmp(fs_type, "lofs"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_LOFS;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "xfs")) {
304803
+	else if (!strcmp(fs_type, "xfs"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_XFS;
304803
-		entry.type = fs_type;
304803
-	} else if (!strcmp(fs_type, "ext2") ||
304803
+	else if (!strcmp(fs_type, "ext2") ||
304803
 		   !strcmp(fs_type, "ext3") ||
304803
-		   !strcmp(fs_type, "ext4")) {
304803
+		   !strcmp(fs_type, "ext4"))
304803
 		entry.flags |= AMD_MOUNT_TYPE_EXT;
304803
-		entry.type = fs_type;
304803
 	} else if (!strcmp(fs_type, "ufs")) {
304803
 		entry.flags |= AMD_MOUNT_TYPE_UFS;
304803
 		entry.type = conf_amd_get_linux_ufs_mount_type();
304803
@@ -508,6 +506,7 @@ static int match_map_option_fs_type(char
304803
 			return 0;
304803
 		}
304803
 		free(fs_type);
304803
+		fs_type = NULL;
304803
 	} else if (!strcmp(fs_type, "cdfs")) {
304803
 		entry.flags |= AMD_MOUNT_TYPE_CDFS;
304803
 		entry.type = amd_strdup("iso9660");
304803
@@ -518,6 +517,7 @@ static int match_map_option_fs_type(char
304803
 			return 0;
304803
 		}
304803
 		free(fs_type);
304803
+		fs_type = NULL;
304803
 	} else if (!strcmp(fs_type, "jfs") ||
304803
 		   !strcmp(fs_type, "nfsx") ||
304803
 		   !strcmp(fs_type, "program") ||
304803
@@ -534,12 +534,16 @@ static int match_map_option_fs_type(char
304803
 				 fs_type);
304803
 		amd_msg(msg_buf);
304803
 		free(fs_type);
304803
+		fs_type = NULL;
304803
 	} else {
304803
 		amd_notify(fs_type);
304803
 		free(fs_type);
304803
 		return 0;
304803
 	}
304803
 
304803
+	if (fs_type)
304803
+		amd_set_value(&entry.type, fs_type);
304803
+
304803
 	return 1;
304803
 }
304803
 
304803
@@ -558,15 +562,18 @@ static int match_map_option_map_type(cha
304803
 	    !strcmp(map_type, "nisplus") ||
304803
 	    !strcmp(map_type, "ldap") ||
304803
 	    !strcmp(map_type, "hesiod")) {
304803
-		entry.map_type = map_type;
304803
+		amd_set_value(&entry.map_type, map_type);
304803
 	} else if (!strcmp(map_type, "exec")) {
304803
 		/* autofs uses "program" for "exec" map type */
304803
-		entry.map_type = amd_strdup("program");
304803
-		if (!entry.map_type) {
304803
+		char * tmp;
304803
+
304803
+		tmp = amd_strdup("program");
304803
+		if (!tmp) {
304803
 			amd_notify(type);
304803
 			free(map_type);
304803
 			return 0;
304803
 		}
304803
+		amd_set_value(&entry.map_type, tmp);
304803
 		free(map_type);
304803
 	} else if (!strcmp(map_type, "passwd")) {
304803
 		sprintf(msg_buf, "map type %s is "
304803
@@ -621,17 +628,17 @@ static int match_mnt_option_options(char
304803
 		tmp = amd_strdup(options);
304803
 		if (!tmp)
304803
 			return 0;
304803
-		entry.opts = tmp;
304803
+		amd_set_value(&entry.opts, tmp);
304803
 	} else if (!strcmp(mnt_option, "addopts")) {
304803
 		tmp = amd_strdup(options);
304803
 		if (!tmp)
304803
 			return 0;
304803
-		entry.addopts = tmp;
304803
+		amd_set_value(&entry.addopts, tmp);
304803
 	} else if (!strcmp(mnt_option, "remopts")) {
304803
 		tmp = amd_strdup(options);
304803
 		if (!tmp)
304803
 			return 0;
304803
-		entry.remopts = tmp;
304803
+		amd_set_value(&entry.remopts, tmp);
304803
 	} else
304803
 		return 0;
304803
 
304803
@@ -715,6 +722,13 @@ done:
304803
 	return tmp;
304803
 }
304803
 
304803
+static void amd_set_value(char **field, char *value)
304803
+{
304803
+	if (*field)
304803
+		free(*field);
304803
+	*field = value;
304803
+}
304803
+
304803
 static int amd_error(const char *s)
304803
 {
304803
 	if (strcmp(s, "syntax"))