Blame SOURCES/autofs-5.1.6-fix-a-regression-with-map-instance-lookup.patch

135b98
autofs-5.1.6 - fix a regression with map instance lookup
135b98
135b98
From: Ian Kent <raven@themaw.net>
135b98
135b98
Commit b66deff4241d ("autofs-5.1.3 - fix possible map instance memory
135b98
leak") introduced a regression.
135b98
135b98
The change didn't fix the memory leak and also failed to fix the race
135b98
updating the map instance list that caused it.
135b98
135b98
To fix this get rid of the horible temporary map usage and update the
135b98
instance list in place. Doing this causes some additional allocations
135b98
and frees but is somewhat simpler overall and avoids the race.
135b98
135b98
Fixes: b66deff4241d ("autofs-5.1.3 - fix possible map instance memory leak")
135b98
Signed-off-by: Ian Kent <raven@themaw.net>
135b98
---
135b98
 CHANGELOG       |    1 
135b98
 daemon/lookup.c |  180 +++++++++++++++++++++++---------------------------------
135b98
 2 files changed, 76 insertions(+), 105 deletions(-)
135b98
135b98
--- autofs-5.1.4.orig/CHANGELOG
135b98
+++ autofs-5.1.4/CHANGELOG
135b98
@@ -76,6 +76,7 @@ xx/xx/2018 autofs-5.1.5
135b98
 - use local getmntent_r() in get_mnt_list().
135b98
 - use local getmntent_r() in tree_make_mnt_list().
135b98
 - fix missing initialization of autofs_point flags.
135b98
+- fix a regression with map instance lookup.
135b98
 
135b98
 19/12/2017 autofs-5.1.4
135b98
 - fix spec file url.
135b98
--- autofs-5.1.4.orig/daemon/lookup.c
135b98
+++ autofs-5.1.4/daemon/lookup.c
135b98
@@ -64,6 +64,10 @@ static char *find_map_path(struct autofs
135b98
 	char *search_path;
135b98
 	struct stat st;
135b98
 
135b98
+	/* Absolute path, just return a copy */
135b98
+	if (mname[0] == '/')
135b98
+		return strdup(mname);
135b98
+
135b98
 	/*
135b98
 	 * This is different to the way it is in amd.
135b98
 	 * autofs will always try to locate maps in AUTOFS_MAP_DIR
135b98
@@ -373,14 +377,27 @@ static int read_file_source_instance(str
135b98
 	char src_prog[] = "program";
135b98
 	struct stat st;
135b98
 	char *type, *format;
135b98
+	char *path;
135b98
+
135b98
+	if (map->argc < 1) {
135b98
+		error(ap->logopt, "invalid arguments for autofs_point");
135b98
+		return NSS_STATUS_UNKNOWN;
135b98
+	}
135b98
 
135b98
-	if (stat(map->argv[0], &st) == -1) {
135b98
-		warn(ap->logopt, "file map %s not found", map->argv[0]);
135b98
+	path = find_map_path(ap, map);
135b98
+	if (!path)
135b98
+		return NSS_STATUS_UNKNOWN;
135b98
+
135b98
+	if (stat(path, &st) == -1) {
135b98
+		warn(ap->logopt, "file map %s not found", path);
135b98
+		free(path);
135b98
 		return NSS_STATUS_NOTFOUND;
135b98
 	}
135b98
 
135b98
-	if (!S_ISREG(st.st_mode))
135b98
+	if (!S_ISREG(st.st_mode)) {
135b98
+		free(path);
135b98
 		return NSS_STATUS_NOTFOUND;
135b98
+	}
135b98
 
135b98
 	if (st.st_mode & __S_IEXEC)
135b98
 		type = src_prog;
135b98
@@ -391,9 +408,23 @@ static int read_file_source_instance(str
135b98
 
135b98
 	instance = master_find_source_instance(map, type, format, 0, NULL);
135b98
 	if (!instance) {
135b98
-		int argc = map->argc;
135b98
-		const char **argv = map->argv;
135b98
+		const char **argv;
135b98
+		int argc;
135b98
+
135b98
+		argc = map->argc;
135b98
+		argv = copy_argv(map->argc, map->argv);
135b98
+		if (!argv) {
135b98
+			error(ap->logopt, "failed to copy args");
135b98
+			free(path);
135b98
+			return NSS_STATUS_UNKNOWN;
135b98
+		}
135b98
+		if (argv[0])
135b98
+			free((char *) argv[0]);
135b98
+		argv[0] = path;
135b98
+		path = NULL;
135b98
+
135b98
 		instance = master_add_source_instance(map, type, format, age, argc, argv);
135b98
+		free_argv(argc, argv);
135b98
 		if (!instance)
135b98
 			return NSS_STATUS_UNAVAIL;
135b98
 		instance->recurse = map->recurse;
135b98
@@ -401,6 +432,9 @@ static int read_file_source_instance(str
135b98
 	}
135b98
 	instance->stale = map->stale;
135b98
 
135b98
+	if (path)
135b98
+		free(path);
135b98
+
135b98
 	return do_read_map(ap, instance, age);
135b98
 }
135b98
 
135b98
@@ -476,16 +510,11 @@ static int lookup_map_read_map(struct au
135b98
 static enum nsswitch_status read_map_source(struct nss_source *this,
135b98
 		struct autofs_point *ap, struct map_source *map, time_t age)
135b98
 {
135b98
-	enum nsswitch_status result;
135b98
-	struct map_source tmap;
135b98
-	char *path;
135b98
-
135b98
 	if (strcasecmp(this->source, "files")) {
135b98
 		return read_source_instance(ap, map, this->source, age);
135b98
 	}
135b98
 
135b98
 	/* 
135b98
-	 * autofs built-in map for nsswitch "files" is "file".
135b98
 	 * This is a special case as we need to append the
135b98
 	 * normal location to the map name.
135b98
 	 * note: It's invalid to specify a relative path.
135b98
@@ -496,50 +525,7 @@ static enum nsswitch_status read_map_sou
135b98
 		return NSS_STATUS_NOTFOUND;
135b98
 	}
135b98
 
135b98
-	this->source[4] = '\0';
135b98
-	tmap.flags = map->flags;
135b98
-	tmap.type = this->source;
135b98
-	tmap.format = map->format;
135b98
-	tmap.name = map->name;
135b98
-	tmap.lookup = map->lookup;
135b98
-	tmap.mc = map->mc;
135b98
-	tmap.instance = map->instance;
135b98
-	tmap.exp_timeout = map->exp_timeout;
135b98
-	tmap.recurse = map->recurse;
135b98
-	tmap.depth = map->depth;
135b98
-	tmap.stale = map->stale;
135b98
-	tmap.argc = 0;
135b98
-	tmap.argv = NULL;
135b98
-
135b98
-	path = find_map_path(ap, map);
135b98
-	if (!path)
135b98
-		return NSS_STATUS_UNKNOWN;
135b98
-
135b98
-	if (map->argc >= 1) {
135b98
-		tmap.argc = map->argc;
135b98
-		tmap.argv = copy_argv(map->argc, map->argv);
135b98
-		if (!tmap.argv) {
135b98
-			error(ap->logopt, "failed to copy args");
135b98
-			free(path);
135b98
-			return NSS_STATUS_UNKNOWN;
135b98
-		}
135b98
-		if (tmap.argv[0])
135b98
-			free((char *) tmap.argv[0]);
135b98
-		tmap.argv[0] = path;
135b98
-	} else {
135b98
-		error(ap->logopt, "invalid arguments for autofs_point");
135b98
-		free(path);
135b98
-		return NSS_STATUS_UNKNOWN;
135b98
-	}
135b98
-
135b98
-	pthread_cleanup_push(argv_cleanup, &tmap);
135b98
-	result = read_file_source_instance(ap, &tmap, age);
135b98
-	pthread_cleanup_pop(1);
135b98
-
135b98
-	if (!map->instance)
135b98
-		map->instance = tmap.instance;
135b98
-
135b98
-	return result;
135b98
+	return read_file_source_instance(ap, map, age);
135b98
 }
135b98
 
135b98
 int lookup_nss_read_map(struct autofs_point *ap, struct map_source *source, time_t age)
135b98
@@ -925,17 +911,30 @@ static int lookup_name_file_source_insta
135b98
 	time_t age = monotonic_time(NULL);
135b98
 	struct stat st;
135b98
 	char *type, *format;
135b98
+	char *path;
135b98
 
135b98
 	if (*name == '/' && map->flags & MAP_FLAG_FORMAT_AMD)
135b98
 		return lookup_amd_instance(ap, map, name, name_len);
135b98
 
135b98
-	if (stat(map->argv[0], &st) == -1) {
135b98
+	if (map->argc < 1) {
135b98
+		error(ap->logopt, "invalid arguments for autofs_point");
135b98
+		return NSS_STATUS_UNKNOWN;
135b98
+	}
135b98
+
135b98
+	path = find_map_path(ap, map);
135b98
+	if (!path)
135b98
+		return NSS_STATUS_UNKNOWN;
135b98
+
135b98
+	if (stat(path, &st) == -1) {
135b98
 		debug(ap->logopt, "file map not found");
135b98
+		free(path);
135b98
 		return NSS_STATUS_NOTFOUND;
135b98
 	}
135b98
 
135b98
-	if (!S_ISREG(st.st_mode))
135b98
+	if (!S_ISREG(st.st_mode)) {
135b98
+		free(path);
135b98
 		return NSS_STATUS_NOTFOUND;
135b98
+	}
135b98
 
135b98
 	if (st.st_mode & __S_IEXEC)
135b98
 		type = src_prog;
135b98
@@ -946,15 +945,32 @@ static int lookup_name_file_source_insta
135b98
 
135b98
 	instance = master_find_source_instance(map, type, format, 0, NULL);
135b98
 	if (!instance) {
135b98
-		int argc = map->argc;
135b98
-		const char **argv = map->argv;
135b98
+		const char **argv;
135b98
+		int argc;
135b98
+
135b98
+		argc = map->argc;
135b98
+		argv = copy_argv(map->argc, map->argv);
135b98
+		if (!argv) {
135b98
+			error(ap->logopt, "failed to copy args");
135b98
+			free(path);
135b98
+			return NSS_STATUS_UNKNOWN;
135b98
+		}
135b98
+		if (argv[0])
135b98
+			free((char *) argv[0]);
135b98
+		argv[0] = path;
135b98
+		path = NULL;
135b98
+
135b98
 		instance = master_add_source_instance(map, type, format, age, argc, argv);
135b98
+		free_argv(argc, argv);
135b98
 		if (!instance)
135b98
 			return NSS_STATUS_NOTFOUND;
135b98
 		instance->recurse = map->recurse;
135b98
 		instance->depth = map->depth;
135b98
 	}
135b98
 
135b98
+	if (path)
135b98
+		free(path);
135b98
+
135b98
 	return do_lookup_mount(ap, instance, name, name_len);
135b98
 }
135b98
 
135b98
@@ -1030,10 +1046,6 @@ static enum nsswitch_status lookup_map_n
135b98
 			struct autofs_point *ap, struct map_source *map,
135b98
 			const char *name, int name_len)
135b98
 {
135b98
-	enum nsswitch_status result;
135b98
-	struct map_source tmap;
135b98
-	char *path;
135b98
-
135b98
 	if (strcasecmp(this->source, "files"))
135b98
 		return lookup_name_source_instance(ap, map,
135b98
 					this->source, name, name_len);
135b98
@@ -1050,49 +1062,7 @@ static enum nsswitch_status lookup_map_n
135b98
 		return NSS_STATUS_NOTFOUND;
135b98
 	}
135b98
 
135b98
-	this->source[4] = '\0';
135b98
-	tmap.flags = map->flags;
135b98
-	tmap.type = this->source;
135b98
-	tmap.format = map->format;
135b98
-	tmap.name = map->name;
135b98
-	tmap.mc = map->mc;
135b98
-	tmap.instance = map->instance;
135b98
-	tmap.exp_timeout = map->exp_timeout;
135b98
-	tmap.recurse = map->recurse;
135b98
-	tmap.depth = map->depth;
135b98
-	tmap.argc = 0;
135b98
-	tmap.argv = NULL;
135b98
-
135b98
-	path = find_map_path(ap, map);
135b98
-	if (!path)
135b98
-		return NSS_STATUS_UNKNOWN;
135b98
-
135b98
-	if (map->argc >= 1) {
135b98
-		tmap.argc = map->argc;
135b98
-		tmap.argv = copy_argv(map->argc, map->argv);
135b98
-		if (!tmap.argv) {
135b98
-			error(ap->logopt, "failed to copy args");
135b98
-			free(path);
135b98
-			return NSS_STATUS_UNKNOWN;
135b98
-		}
135b98
-		if (tmap.argv[0])
135b98
-			free((char *) tmap.argv[0]);
135b98
-		tmap.argv[0] = path;
135b98
-	} else {
135b98
-		error(ap->logopt, "invalid arguments for autofs_point");
135b98
-		free(path);
135b98
-		return NSS_STATUS_UNKNOWN;
135b98
-	}
135b98
-
135b98
-	result = lookup_name_file_source_instance(ap, &tmap, name, name_len);
135b98
-
135b98
-	if (!map->instance)
135b98
-		map->instance = tmap.instance;
135b98
-
135b98
-	/* path is freed in free_argv */
135b98
-	free_argv(tmap.argc, tmap.argv);
135b98
-
135b98
-	return result;
135b98
+	return lookup_name_file_source_instance(ap, map, name, name_len);
135b98
 }
135b98
 
135b98
 static struct map_source *lookup_get_map_source(struct master_mapent *entry)