autofs-5.1.9 - fix locking when setting master map reading

From: Ian Kent <raven@themaw.net>

Serialization clearing the master map reading flag isn't done so
it's possible there could be multiple concurrent map loads which
could cause problems.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG          |    1 +
 daemon/automount.c |   21 ++++++++++++++-------
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index aa9fa182a..97416f80d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -60,6 +60,7 @@
 - dont assume non null tree node in tree_free().
 - add missing cache_unlock() in umount_subtree_mounts().
 - move open close on exec functions to autofs library.
+- fix locking when setting master map reading.
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/automount.c b/daemon/automount.c
index ce97892d0..ee43c823d 100644
--- a/daemon/automount.c
+++ b/daemon/automount.c
@@ -1239,16 +1239,19 @@ static void *do_read_master(void *arg)
 		pthread_exit(NULL);
 	}
 
-	status = pthread_mutex_unlock(&mrc.mutex);
-	if (status)
-		fatal(status);
-
 	if (!defaults_read_config(1)) {
 		error(logopt, "failed to read configuration, exiting");
 		master->reading = 0;
+		status = pthread_mutex_unlock(&mrc.mutex);
+		if (status)
+			fatal(status);
 		pthread_exit(NULL);
 	}
 
+	status = pthread_mutex_unlock(&mrc.mutex);
+	if (status)
+		fatal(status);
+
 	info(logopt, "re-reading master map %s", master->name);
 
 	master->readall = 1;
@@ -1256,7 +1259,14 @@ static void *do_read_master(void *arg)
 	status = master_read_master(master, age);
 
 	master->readall = 0;
+
+	status = pthread_mutex_lock(&mrc.mutex);
+	if (status)
+		fatal(status);
 	master->reading = 0;
+	status = pthread_mutex_unlock(&mrc.mutex);
+	if (status)
+		fatal(status);
 
 	return NULL;
 }
@@ -1274,17 +1284,14 @@ static int do_hup_signal(struct master *master)
 
 	nfs_mount_uses_string_options = check_nfs_mount_version(&vers, &check);
 
-	master_mutex_lock();
 	/* Already doing a map read or shutdown or no mounts */
 	if (master->reading) {
 		status = pthread_mutex_unlock(&mrc.mutex);
 		if (status)
 			fatal(status);
-		master_mutex_unlock();
 		return 1;
 	}
 	master->reading = 1;
-	master_mutex_unlock();
 
 	status = pthread_create(&thid, &th_attr_detached, do_read_master, NULL);
 	if (status) {
