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

29d2b9
autofs-5.1.7 - fix inconsistent locking in parse_mount()
29d2b9
29d2b9
From: Ian Kent <raven@themaw.net>
29d2b9
29d2b9
Some map entry cache locking inconsistencies have crept in.
29d2b9
29d2b9
In parse_mount() of the sun format parser the cache read lock is too
29d2b9
heavily used and has too broad a scope. This has lead to some operations
29d2b9
that should hold the write lock being called with only the read lock.
29d2b9
29d2b9
Signed-off-by: Ian Kent <raven@themaw.net>
29d2b9
---
29d2b9
 CHANGELOG           |    1 +
29d2b9
 lib/mounts.c        |    9 ++++++++-
29d2b9
 modules/parse_sun.c |   53 ++++++++++++++++++++++++++++++++-------------------
29d2b9
 3 files changed, 42 insertions(+), 21 deletions(-)
29d2b9
29d2b9
diff --git a/CHANGELOG b/CHANGELOG
29d2b9
index c60a9ed3..d25b19c8 100644
29d2b9
--- a/CHANGELOG
29d2b9
+++ b/CHANGELOG
29d2b9
@@ -18,6 +18,7 @@
29d2b9
 - fix inconsistent locking in umount_subtree_mounts().
29d2b9
 - fix return from umount_subtree_mounts() on offset list delete.
29d2b9
 - pass mapent_cache to update_offset_entry().
29d2b9
+- fix inconsistent locking in parse_mount().
29d2b9
 
29d2b9
 25/01/2021 autofs-5.1.7
29d2b9
 - make bind mounts propagation slave by default.
29d2b9
diff --git a/lib/mounts.c b/lib/mounts.c
29d2b9
index 5ebfe5fd..0fcd4087 100644
29d2b9
--- a/lib/mounts.c
29d2b9
+++ b/lib/mounts.c
29d2b9
@@ -2491,6 +2491,12 @@ static int do_mount_autofs_offset(struct autofs_point *ap,
29d2b9
 		else {
29d2b9
 			debug(ap->logopt, "ignoring \"nohide\" trigger %s",
29d2b9
 			      oe->key);
29d2b9
+			/*
29d2b9
+			 * Ok, so we shouldn't modify the mapent but
29d2b9
+			 * mount requests are blocked at a point above
29d2b9
+			 * this and expire only uses the mapent key or
29d2b9
+			 * holds the cache write lock.
29d2b9
+			 */
29d2b9
 			free(oe->mapent);
29d2b9
 			oe->mapent = NULL;
29d2b9
 		}
29d2b9
@@ -2634,7 +2640,8 @@ static int do_umount_offset(struct autofs_point *ap, struct mapent *oe, const ch
29d2b9
 			/*
29d2b9
 			 * Ok, so we shouldn't modify the mapent but
29d2b9
 			 * mount requests are blocked at a point above
29d2b9
-			 * this and expire only uses the mapent key.
29d2b9
+			 * this and expire only uses the mapent key or
29d2b9
+			 * holds the cache write lock.
29d2b9
 			 */
29d2b9
 			if (oe->mapent) {
29d2b9
 				free(oe->mapent);
29d2b9
diff --git a/modules/parse_sun.c b/modules/parse_sun.c
29d2b9
index 95251bee..a6630a76 100644
29d2b9
--- a/modules/parse_sun.c
29d2b9
+++ b/modules/parse_sun.c
29d2b9
@@ -851,10 +851,12 @@ update_offset_entry(struct autofs_point *ap,
29d2b9
 			strcpy(m_mapent, loc);
29d2b9
 	}
29d2b9
 
29d2b9
+	cache_writelock(mc);
29d2b9
 	ret = cache_update_offset(mc, name, m_key, m_mapent, age);
29d2b9
 
29d2b9
 	if (!cache_set_offset_parent(mc, m_key))
29d2b9
 		error(ap->logopt, "failed to set offset parent");
29d2b9
+	cache_unlock(mc);
29d2b9
 
29d2b9
 	if (ret == CHE_DUPLICATE) {
29d2b9
 		warn(ap->logopt, MODPREFIX
29d2b9
@@ -1128,14 +1130,22 @@ static void cleanup_multi_triggers(struct autofs_point *ap,
29d2b9
 	return;
29d2b9
 }
29d2b9
 
29d2b9
-static int mount_subtree(struct autofs_point *ap, struct mapent *me,
29d2b9
+static int mount_subtree(struct autofs_point *ap, struct mapent_cache *mc,
29d2b9
 			 const char *name, char *loc, char *options, void *ctxt)
29d2b9
 {
29d2b9
+	struct mapent *me;
29d2b9
 	struct mapent *ro;
29d2b9
 	char *mm_root, *mm_base, *mm_key;
29d2b9
 	unsigned int mm_root_len;
29d2b9
 	int start, ret = 0, rv;
29d2b9
 
29d2b9
+	cache_readlock(mc);
29d2b9
+	me = cache_lookup_distinct(mc, name);
29d2b9
+	if (!me) {
29d2b9
+		cache_unlock(mc);
29d2b9
+		return 0;
29d2b9
+	}
29d2b9
+
29d2b9
 	rv = 0;
29d2b9
 
29d2b9
 	mm_key = me->multi->key;
29d2b9
@@ -1180,9 +1190,12 @@ static int mount_subtree(struct autofs_point *ap, struct mapent *me,
29d2b9
 			rv = parse_mapent(ro->mapent,
29d2b9
 				options, &myoptions, &ro_loc, ap->logopt);
29d2b9
 			if (!rv) {
29d2b9
+				cache_unlock(mc);
29d2b9
 				warn(ap->logopt,
29d2b9
 				      MODPREFIX "failed to parse root offset");
29d2b9
-				cache_delete_offset_list(me->mc, name);
29d2b9
+				cache_writelock(mc);
29d2b9
+				cache_delete_offset_list(mc, name);
29d2b9
+				cache_unlock(mc);
29d2b9
 				return 1;
29d2b9
 			}
29d2b9
 			ro_len = 0;
29d2b9
@@ -1199,9 +1212,10 @@ static int mount_subtree(struct autofs_point *ap, struct mapent *me,
29d2b9
 		if ((ro && rv == 0) || rv <= 0) {
29d2b9
 			ret = mount_multi_triggers(ap, me, mm_root, start, mm_base);
29d2b9
 			if (ret == -1) {
29d2b9
+				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
29d2b9
+				cache_unlock(mc);
29d2b9
 				error(ap->logopt, MODPREFIX
29d2b9
 					 "failed to mount offset triggers");
29d2b9
-				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
29d2b9
 				return 1;
29d2b9
 			}
29d2b9
 		}
29d2b9
@@ -1217,9 +1231,10 @@ static int mount_subtree(struct autofs_point *ap, struct mapent *me,
29d2b9
 		if (rv == 0) {
29d2b9
 			ret = mount_multi_triggers(ap, me->multi, name, start, mm_base);
29d2b9
 			if (ret == -1) {
29d2b9
+				cleanup_multi_triggers(ap, me, name, start, mm_base);
29d2b9
+				cache_unlock(mc);
29d2b9
 				error(ap->logopt, MODPREFIX
29d2b9
 					 "failed to mount offset triggers");
29d2b9
-				cleanup_multi_triggers(ap, me, name, start, mm_base);
29d2b9
 				return 1;
29d2b9
 			}
29d2b9
 		} else if (rv < 0) {
29d2b9
@@ -1227,8 +1242,11 @@ static int mount_subtree(struct autofs_point *ap, struct mapent *me,
29d2b9
 			unsigned int mm_root_base_len = mm_root_len + strlen(mm_base) + 1;
29d2b9
 	
29d2b9
 			if (mm_root_base_len > PATH_MAX) {
29d2b9
+				cache_unlock(mc);
29d2b9
 				warn(ap->logopt, MODPREFIX "path too long");
29d2b9
-				cache_delete_offset_list(me->mc, name);
29d2b9
+				cache_writelock(mc);
29d2b9
+				cache_delete_offset_list(mc, name);
29d2b9
+				cache_unlock(mc);
29d2b9
 				return 1;
29d2b9
 			}
29d2b9
 
29d2b9
@@ -1237,13 +1255,15 @@ static int mount_subtree(struct autofs_point *ap, struct mapent *me,
29d2b9
 
29d2b9
 			ret = mount_multi_triggers(ap, me->multi, mm_root_base, start, mm_base);
29d2b9
 			if (ret == -1) {
29d2b9
+				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
29d2b9
+				cache_unlock(mc);
29d2b9
 				error(ap->logopt, MODPREFIX
29d2b9
 					 "failed to mount offset triggers");
29d2b9
-				cleanup_multi_triggers(ap, me, mm_root, start, mm_base);
29d2b9
 				return 1;
29d2b9
 			}
29d2b9
 		}
29d2b9
 	}
29d2b9
+	cache_unlock(mc);
29d2b9
 
29d2b9
 	/* Mount for base of tree failed */
29d2b9
 	if (rv > 0)
29d2b9
@@ -1484,7 +1504,6 @@ dont_expand:
29d2b9
 			return 1;
29d2b9
 		}
29d2b9
 
29d2b9
-		cache_multi_writelock(me);
29d2b9
 		/* So we know we're the multi-mount root */
29d2b9
 		if (!me->multi)
29d2b9
 			me->multi = me;
29d2b9
@@ -1509,14 +1528,13 @@ dont_expand:
29d2b9
 			if (source->flags & MAP_FLAG_FORMAT_AMD) {
29d2b9
 				free(options);
29d2b9
 				free(pmapent);
29d2b9
-				cache_multi_unlock(me);
29d2b9
 				cache_unlock(mc);
29d2b9
 				pthread_setcancelstate(cur_state, NULL);
29d2b9
 				return 0;
29d2b9
 			}
29d2b9
 		}
29d2b9
-
29d2b9
 		age = me->age;
29d2b9
+		cache_unlock(mc);
29d2b9
 
29d2b9
 		/* It's a multi-mount; deal with it */
29d2b9
 		do {
29d2b9
@@ -1537,8 +1555,8 @@ dont_expand:
29d2b9
 
29d2b9
 			if (!path) {
29d2b9
 				warn(ap->logopt, MODPREFIX "null path or out of memory");
29d2b9
+				cache_writelock(mc);
29d2b9
 				cache_delete_offset_list(mc, name);
29d2b9
-				cache_multi_unlock(me);
29d2b9
 				cache_unlock(mc);
29d2b9
 				free(options);
29d2b9
 				free(pmapent);
29d2b9
@@ -1554,8 +1572,8 @@ dont_expand:
29d2b9
 
29d2b9
 			l = parse_mapent(p, options, &myoptions, &loc, ap->logopt);
29d2b9
 			if (!l) {
29d2b9
+				cache_writelock(mc);
29d2b9
 				cache_delete_offset_list(mc, name);
29d2b9
-				cache_multi_unlock(me);
29d2b9
 				cache_unlock(mc);
29d2b9
 				free(path);
29d2b9
 				free(options);
29d2b9
@@ -1573,8 +1591,8 @@ dont_expand:
29d2b9
 
29d2b9
 			if (status != CHE_OK) {
29d2b9
 				warn(ap->logopt, MODPREFIX "error adding multi-mount");
29d2b9
+				cache_writelock(mc);
29d2b9
 				cache_delete_offset_list(mc, name);
29d2b9
-				cache_multi_unlock(me);
29d2b9
 				cache_unlock(mc);
29d2b9
 				free(path);
29d2b9
 				free(options);
29d2b9
@@ -1592,10 +1610,7 @@ dont_expand:
29d2b9
 			free(myoptions);
29d2b9
 		} while (*p == '/' || (*p == '"' && *(p + 1) == '/'));
29d2b9
 
29d2b9
-		rv = mount_subtree(ap, me, name, NULL, options, ctxt);
29d2b9
-
29d2b9
-		cache_multi_unlock(me);
29d2b9
-		cache_unlock(mc);
29d2b9
+		rv = mount_subtree(ap, mc, name, NULL, options, ctxt);
29d2b9
 
29d2b9
 		free(options);
29d2b9
 		free(pmapent);
29d2b9
@@ -1616,6 +1631,7 @@ dont_expand:
29d2b9
 		cache_readlock(mc);
29d2b9
 		if (*name == '/' &&
29d2b9
 		   (me = cache_lookup_distinct(mc, name)) && me->multi) {
29d2b9
+			cache_unlock(mc);
29d2b9
 			loc = strdup(p);
29d2b9
 			if (!loc) {
29d2b9
 				free(options);
29d2b9
@@ -1624,10 +1640,7 @@ dont_expand:
29d2b9
 				warn(ap->logopt, MODPREFIX "out of memory");
29d2b9
 				return 1;
29d2b9
 			}
29d2b9
-			cache_multi_writelock(me);
29d2b9
-			rv = mount_subtree(ap, me, name, loc, options, ctxt);
29d2b9
-			cache_multi_unlock(me);
29d2b9
-			cache_unlock(mc);
29d2b9
+			rv = mount_subtree(ap, mc, name, loc, options, ctxt);
29d2b9
 			free(loc);
29d2b9
 			free(options);
29d2b9
 			free(pmapent);