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

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