Blame SOURCES/autofs-5.1.7-fix-inconsistent-locking-in-parse_mount.patch

beb904
autofs-5.1.7 - fix inconsistent locking in parse_mount()
beb904
beb904
From: Ian Kent <raven@themaw.net>
beb904
beb904
Some map entry cache locking inconsistencies have crept in.
beb904
beb904
In parse_mount() of the sun format parser the cache read lock is too
beb904
heavily used and has too broad a scope. This has lead to some operations
beb904
that should hold the write lock being called with only the read lock.
beb904
beb904
Signed-off-by: Ian Kent <raven@themaw.net>
beb904
---
beb904
 CHANGELOG           |    1 
beb904
 lib/mounts.c        |    9 +++++++-
beb904
 modules/parse_sun.c |   53 ++++++++++++++++++++++++++++++++--------------------
beb904
 3 files changed, 42 insertions(+), 21 deletions(-)
beb904
beb904
--- autofs-5.1.4.orig/CHANGELOG
beb904
+++ autofs-5.1.4/CHANGELOG
beb904
@@ -17,6 +17,7 @@
beb904
 - fix inconsistent locking in umount_subtree_mounts().
beb904
 - fix return from umount_subtree_mounts() on offset list delete.
beb904
 - pass mapent_cache to update_offset_entry().
beb904
+- fix inconsistent locking in parse_mount().
beb904
 
beb904
 xx/xx/2018 autofs-5.1.5
beb904
 - fix flag file permission.
beb904
--- autofs-5.1.4.orig/lib/mounts.c
beb904
+++ autofs-5.1.4/lib/mounts.c
beb904
@@ -2485,6 +2485,12 @@ static int do_mount_autofs_offset(struct
beb904
 		else {
beb904
 			debug(ap->logopt, "ignoring \"nohide\" trigger %s",
beb904
 			      oe->key);
beb904
+			/*
beb904
+			 * Ok, so we shouldn't modify the mapent but
beb904
+			 * mount requests are blocked at a point above
beb904
+			 * this and expire only uses the mapent key or
beb904
+			 * holds the cache write lock.
beb904
+			 */
beb904
 			free(oe->mapent);
beb904
 			oe->mapent = NULL;
beb904
 		}
beb904
@@ -2628,7 +2634,8 @@ static int do_umount_offset(struct autof
beb904
 			/*
beb904
 			 * Ok, so we shouldn't modify the mapent but
beb904
 			 * mount requests are blocked at a point above
beb904
-			 * this and expire only uses the mapent key.
beb904
+			 * this and expire only uses the mapent key or
beb904
+			 * holds the cache write lock.
beb904
 			 */
beb904
 			if (oe->mapent) {
beb904
 				free(oe->mapent);
beb904
--- autofs-5.1.4.orig/modules/parse_sun.c
beb904
+++ autofs-5.1.4/modules/parse_sun.c
beb904
@@ -853,10 +853,12 @@ update_offset_entry(struct autofs_point
beb904
 			strcpy(m_mapent, loc);
beb904
 	}
beb904
 
beb904
+	cache_writelock(mc);
beb904
 	ret = cache_update_offset(mc, name, m_key, m_mapent, age);
beb904
 
beb904
 	if (!cache_set_offset_parent(mc, m_key))
beb904
 		error(ap->logopt, "failed to set offset parent");
beb904
+	cache_unlock(mc);
beb904
 
beb904
 	if (ret == CHE_DUPLICATE) {
beb904
 		warn(ap->logopt, MODPREFIX
beb904
@@ -1130,14 +1132,22 @@ static void cleanup_multi_triggers(struc
beb904
 	return;
beb904
 }
beb904
 
beb904
-static int mount_subtree(struct autofs_point *ap, struct mapent *me,
beb904
+static int mount_subtree(struct autofs_point *ap, struct mapent_cache *mc,
beb904
 			 const char *name, char *loc, char *options, void *ctxt)
beb904
 {
beb904
+	struct mapent *me;
beb904
 	struct mapent *ro;
beb904
 	char *mm_root, *mm_base, *mm_key;
beb904
 	unsigned int mm_root_len;
beb904
 	int start, ret = 0, rv;
beb904
 
beb904
+	cache_readlock(mc);
beb904
+	me = cache_lookup_distinct(mc, name);
beb904
+	if (!me) {
beb904
+		cache_unlock(mc);
beb904
+		return 0;
beb904
+	}
beb904
+
beb904
 	rv = 0;
beb904
 
beb904
 	mm_key = me->multi->key;
beb904
@@ -1182,9 +1192,12 @@ static int mount_subtree(struct autofs_p
beb904
 			rv = parse_mapent(ro->mapent,
beb904
 				options, &myoptions, &ro_loc, ap->logopt);
beb904
 			if (!rv) {
beb904
+				cache_unlock(mc);
beb904
 				warn(ap->logopt,
beb904
 				      MODPREFIX "failed to parse root offset");
beb904
-				cache_delete_offset_list(me->mc, name);
beb904
+				cache_writelock(mc);
beb904
+				cache_delete_offset_list(mc, name);
beb904
+				cache_unlock(mc);
beb904
 				return 1;
beb904
 			}
beb904
 			ro_len = 0;
beb904
@@ -1201,9 +1214,10 @@ static int mount_subtree(struct autofs_p
beb904
 		if ((ro && rv == 0) || rv <= 0) {
beb904
 			ret = mount_multi_triggers(ap, me, mm_root, start, mm_base);
beb904
 			if (ret == -1) {
beb904
+				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
beb904
+				cache_unlock(mc);
beb904
 				error(ap->logopt, MODPREFIX
beb904
 					 "failed to mount offset triggers");
beb904
-				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
beb904
 				return 1;
beb904
 			}
beb904
 		}
beb904
@@ -1219,9 +1233,10 @@ static int mount_subtree(struct autofs_p
beb904
 		if (rv == 0) {
beb904
 			ret = mount_multi_triggers(ap, me->multi, name, start, mm_base);
beb904
 			if (ret == -1) {
beb904
+				cleanup_multi_triggers(ap, me, name, start, mm_base);
beb904
+				cache_unlock(mc);
beb904
 				error(ap->logopt, MODPREFIX
beb904
 					 "failed to mount offset triggers");
beb904
-				cleanup_multi_triggers(ap, me, name, start, mm_base);
beb904
 				return 1;
beb904
 			}
beb904
 		} else if (rv < 0) {
beb904
@@ -1229,8 +1244,11 @@ static int mount_subtree(struct autofs_p
beb904
 			unsigned int mm_root_base_len = mm_root_len + strlen(mm_base) + 1;
beb904
 	
beb904
 			if (mm_root_base_len > PATH_MAX) {
beb904
+				cache_unlock(mc);
beb904
 				warn(ap->logopt, MODPREFIX "path too long");
beb904
-				cache_delete_offset_list(me->mc, name);
beb904
+				cache_writelock(mc);
beb904
+				cache_delete_offset_list(mc, name);
beb904
+				cache_unlock(mc);
beb904
 				return 1;
beb904
 			}
beb904
 
beb904
@@ -1239,13 +1257,15 @@ static int mount_subtree(struct autofs_p
beb904
 
beb904
 			ret = mount_multi_triggers(ap, me->multi, mm_root_base, start, mm_base);
beb904
 			if (ret == -1) {
beb904
+				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
beb904
+				cache_unlock(mc);
beb904
 				error(ap->logopt, MODPREFIX
beb904
 					 "failed to mount offset triggers");
beb904
-				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
beb904
 				return 1;
beb904
 			}
beb904
 		}
beb904
 	}
beb904
+	cache_unlock(mc);
beb904
 
beb904
 	/* Mount for base of tree failed */
beb904
 	if (rv > 0)
beb904
@@ -1486,7 +1506,6 @@ dont_expand:
beb904
 			return 1;
beb904
 		}
beb904
 
beb904
-		cache_multi_writelock(me);
beb904
 		/* So we know we're the multi-mount root */
beb904
 		if (!me->multi)
beb904
 			me->multi = me;
beb904
@@ -1511,14 +1530,13 @@ dont_expand:
beb904
 			if (source->flags & MAP_FLAG_FORMAT_AMD) {
beb904
 				free(options);
beb904
 				free(pmapent);
beb904
-				cache_multi_unlock(me);
beb904
 				cache_unlock(mc);
beb904
 				pthread_setcancelstate(cur_state, NULL);
beb904
 				return 0;
beb904
 			}
beb904
 		}
beb904
-
beb904
 		age = me->age;
beb904
+		cache_unlock(mc);
beb904
 
beb904
 		/* It's a multi-mount; deal with it */
beb904
 		do {
beb904
@@ -1539,8 +1557,8 @@ dont_expand:
beb904
 
beb904
 			if (!path) {
beb904
 				warn(ap->logopt, MODPREFIX "null path or out of memory");
beb904
+				cache_writelock(mc);
beb904
 				cache_delete_offset_list(mc, name);
beb904
-				cache_multi_unlock(me);
beb904
 				cache_unlock(mc);
beb904
 				free(options);
beb904
 				free(pmapent);
beb904
@@ -1556,8 +1574,8 @@ dont_expand:
beb904
 
beb904
 			l = parse_mapent(p, options, &myoptions, &loc, ap->logopt);
beb904
 			if (!l) {
beb904
+				cache_writelock(mc);
beb904
 				cache_delete_offset_list(mc, name);
beb904
-				cache_multi_unlock(me);
beb904
 				cache_unlock(mc);
beb904
 				free(path);
beb904
 				free(options);
beb904
@@ -1575,8 +1593,8 @@ dont_expand:
beb904
 
beb904
 			if (status != CHE_OK) {
beb904
 				warn(ap->logopt, MODPREFIX "error adding multi-mount");
beb904
+				cache_writelock(mc);
beb904
 				cache_delete_offset_list(mc, name);
beb904
-				cache_multi_unlock(me);
beb904
 				cache_unlock(mc);
beb904
 				free(path);
beb904
 				free(options);
beb904
@@ -1594,10 +1612,7 @@ dont_expand:
beb904
 			free(myoptions);
beb904
 		} while (*p == '/' || (*p == '"' && *(p + 1) == '/'));
beb904
 
beb904
-		rv = mount_subtree(ap, me, name, NULL, options, ctxt);
beb904
-
beb904
-		cache_multi_unlock(me);
beb904
-		cache_unlock(mc);
beb904
+		rv = mount_subtree(ap, mc, name, NULL, options, ctxt);
beb904
 
beb904
 		free(options);
beb904
 		free(pmapent);
beb904
@@ -1618,6 +1633,7 @@ dont_expand:
beb904
 		cache_readlock(mc);
beb904
 		if (*name == '/' &&
beb904
 		   (me = cache_lookup_distinct(mc, name)) && me->multi) {
beb904
+			cache_unlock(mc);
beb904
 			loc = strdup(p);
beb904
 			if (!loc) {
beb904
 				free(options);
beb904
@@ -1626,10 +1642,7 @@ dont_expand:
beb904
 				warn(ap->logopt, MODPREFIX "out of memory");
beb904
 				return 1;
beb904
 			}
beb904
-			cache_multi_writelock(me);
beb904
-			rv = mount_subtree(ap, me, name, loc, options, ctxt);
beb904
-			cache_multi_unlock(me);
beb904
-			cache_unlock(mc);
beb904
+			rv = mount_subtree(ap, mc, name, loc, options, ctxt);
beb904
 			free(loc);
beb904
 			free(options);
beb904
 			free(pmapent);