Blame SOURCES/autofs-5.1.3-move-open_xxxx-functions-to-spawn_c.patch

603f99
autofs-5.1.3 - move open_xxxx() functions to spawn.c
603f99
603f99
From: Ian Kent <raven@themaw.net>
603f99
603f99
In a bug report by John Salmon has described a race between the
603f99
autofs open_xxxx() functions and fork(2) that can cause file handles
603f99
that don't have close on exec set to leak into subprocesses.
603f99
603f99
Basically, in some cases there can be a finite time between performing
603f99
the open and setting the descriptor close on exec and it's this window
603f99
that leads to the problem.
603f99
603f99
The description went on to talk about a case where autofs can hang when
603f99
this happens. That is when the leaked decriptor causes poll(2) to not
603f99
return in another process because it is waiting for the decriptor to
603f99
close (on mount completion) but another execed process has the cloned
603f99
descriptor open.
603f99
603f99
Serializing access to the open_xxxx() functions wrt. to fork(2) calls
603f99
should be suficient to resolve this leakage.
603f99
603f99
This patch starts this by moving the open_xxxx() out of the automount.h
603f99
include file and into spawn.c.
603f99
603f99
Signed-off-by: Ian Kent <raven@themaw.net>
603f99
---
603f99
 CHANGELOG           |    1 
603f99
 daemon/automount.c  |    3 -
603f99
 daemon/spawn.c      |  125 +++++++++++++++++++++++++++++++++++++++++++++++++
603f99
 include/automount.h |  132 ++--------------------------------------------------
603f99
 4 files changed, 133 insertions(+), 128 deletions(-)
603f99
603f99
--- autofs-5.0.7.orig/CHANGELOG
603f99
+++ autofs-5.0.7/CHANGELOG
603f99
@@ -287,6 +287,7 @@
603f99
 - fix incorrect check in validate_program_options().
603f99
 - update configure to check for pipe2(2).
603f99
 - fix open calls not using open_xxxx() calls.
603f99
+- move open_xxxx() functions to spawn.c.
603f99
 
603f99
 25/07/2012 autofs-5.0.7
603f99
 =======================
603f99
--- autofs-5.0.7.orig/daemon/spawn.c
603f99
+++ autofs-5.0.7/daemon/spawn.c
603f99
@@ -49,6 +49,131 @@ void dump_core(void)
603f99
 }
603f99
 
603f99
 /*
603f99
+ * Use CLOEXEC flag for open(), pipe(), fopen() (read-only case) and
603f99
+ * socket() if possible.
603f99
+ */
603f99
+static int cloexec_works = 0;
603f99
+
603f99
+static void check_cloexec(int fd)
603f99
+{
603f99
+	if (cloexec_works == 0) {
603f99
+		int fl = fcntl(fd, F_GETFD);
603f99
+		if (fl != -1)
603f99
+			cloexec_works = (fl & FD_CLOEXEC) ? 1 : -1;
603f99
+	}
603f99
+	if (cloexec_works > 0)
603f99
+		return;
603f99
+	fcntl(fd, F_SETFD, FD_CLOEXEC);
603f99
+	return;
603f99
+}
603f99
+
603f99
+int open_fd(const char *path, int flags)
603f99
+{
603f99
+	int fd;
603f99
+
603f99
+#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
+	if (cloexec_works != -1)
603f99
+		flags |= O_CLOEXEC;
603f99
+#endif
603f99
+	fd = open(path, flags);
603f99
+	if (fd == -1)
603f99
+		return -1;
603f99
+	check_cloexec(fd);
603f99
+	return fd;
603f99
+}
603f99
+
603f99
+int open_fd_mode(const char *path, int flags, int mode)
603f99
+{
603f99
+	int fd;
603f99
+
603f99
+#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
+	if (cloexec_works != -1)
603f99
+		flags |= O_CLOEXEC;
603f99
+#endif
603f99
+	fd = open(path, flags, mode);
603f99
+	if (fd == -1)
603f99
+		return -1;
603f99
+	check_cloexec(fd);
603f99
+	return fd;
603f99
+}
603f99
+
603f99
+int open_pipe(int pipefd[2])
603f99
+{
603f99
+	int ret;
603f99
+
603f99
+#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC) && defined(HAVE_PIPE2)
603f99
+	if (cloexec_works != -1) {
603f99
+		ret = pipe2(pipefd, O_CLOEXEC);
603f99
+		if (ret != -1)
603f99
+			return 0;
603f99
+		if (errno != EINVAL)
603f99
+			return -1;
603f99
+	}
603f99
+#endif
603f99
+	ret = pipe(pipefd);
603f99
+	if (ret == -1)
603f99
+		return -1;
603f99
+	check_cloexec(pipefd[0]);
603f99
+	check_cloexec(pipefd[1]);
603f99
+	return 0;
603f99
+}
603f99
+
603f99
+int open_sock(int domain, int type, int protocol)
603f99
+{
603f99
+	int fd;
603f99
+
603f99
+#ifdef SOCK_CLOEXEC
603f99
+	if (cloexec_works != -1)
603f99
+		type |= SOCK_CLOEXEC;
603f99
+#endif
603f99
+	fd = socket(domain, type, protocol);
603f99
+	if (fd == -1)
603f99
+		return -1;
603f99
+	check_cloexec(fd);
603f99
+	return fd;
603f99
+}
603f99
+
603f99
+FILE *open_fopen_r(const char *path)
603f99
+{
603f99
+	FILE *f;
603f99
+
603f99
+#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
+	if (cloexec_works != -1) {
603f99
+		f = fopen(path, "re");
603f99
+		if (f != NULL) {
603f99
+			check_cloexec(fileno(f));
603f99
+			return f;
603f99
+		}
603f99
+	}
603f99
+#endif
603f99
+	f = fopen(path, "r");
603f99
+	if (f == NULL)
603f99
+		return NULL;
603f99
+	check_cloexec(fileno(f));
603f99
+	return f;
603f99
+}
603f99
+
603f99
+FILE *open_setmntent_r(const char *table)
603f99
+{
603f99
+	FILE *tab;
603f99
+
603f99
+#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
+	if (cloexec_works != -1) {
603f99
+		tab = setmntent(table, "re");
603f99
+		if (tab != NULL) {
603f99
+			check_cloexec(fileno(tab));
603f99
+			return tab;
603f99
+		}
603f99
+	}
603f99
+#endif
603f99
+	tab = fopen(table, "r");
603f99
+	if (tab == NULL)
603f99
+		return NULL;
603f99
+	check_cloexec(fileno(tab));
603f99
+	return tab;
603f99
+}
603f99
+
603f99
+/*
603f99
  * Used by subprocesses which exec to avoid carrying over the main
603f99
  * daemon's signalling environment
603f99
  */
603f99
--- autofs-5.0.7.orig/include/automount.h
603f99
+++ autofs-5.0.7/include/automount.h
603f99
@@ -8,6 +8,7 @@
603f99
 #ifndef AUTOMOUNT_H
603f99
 #define AUTOMOUNT_H
603f99
 
603f99
+#include <stdio.h>
603f99
 #include <paths.h>
603f99
 #include <limits.h>
603f99
 #include <time.h>
603f99
@@ -256,6 +257,12 @@ int spawnv(unsigned logopt, const char *
603f99
 int spawn_mount(unsigned logopt, ...);
603f99
 int spawn_bind_mount(unsigned logopt, ...);
603f99
 int spawn_umount(unsigned logopt, ...);
603f99
+int open_fd(const char *, int);
603f99
+int open_fd_mode(const char *, int, int);
603f99
+int open_pipe(int[2]);
603f99
+int open_sock(int, int, int);
603f99
+FILE *open_fopen_r(const char *);
603f99
+FILE *open_setmntent_r(const char *);
603f99
 void reset_signals(void);
603f99
 int do_mount(struct autofs_point *ap, const char *root, const char *name,
603f99
 	     int name_len, const char *what, const char *fstype,
603f99
@@ -600,130 +607,5 @@ int alarm_start_handler(void);
603f99
 int alarm_add(struct autofs_point *ap, time_t seconds);
603f99
 void alarm_delete(struct autofs_point *ap);
603f99
 
603f99
-/*
603f99
- * Use CLOEXEC flag for open(), pipe(), fopen() (read-only case) and
603f99
- * socket() if possible.
603f99
- */
603f99
-static int cloexec_works;
603f99
-
603f99
-static inline void check_cloexec(int fd)
603f99
-{
603f99
-	if (cloexec_works == 0) {
603f99
-		int fl = fcntl(fd, F_GETFD);
603f99
-		if (fl != -1)
603f99
-			cloexec_works = (fl & FD_CLOEXEC) ? 1 : -1;
603f99
-	}
603f99
-	if (cloexec_works > 0)
603f99
-		return;
603f99
-	fcntl(fd, F_SETFD, FD_CLOEXEC);
603f99
-	return;
603f99
-}
603f99
-
603f99
-static inline int open_fd(const char *path, int flags)
603f99
-{
603f99
-	int fd;
603f99
-
603f99
-#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
-	if (cloexec_works != -1)
603f99
-		flags |= O_CLOEXEC;
603f99
-#endif
603f99
-	fd = open(path, flags);
603f99
-	if (fd == -1)
603f99
-		return -1;
603f99
-	check_cloexec(fd);
603f99
-	return fd;
603f99
-}
603f99
-
603f99
-static inline int open_fd_mode(const char *path, int flags, int mode)
603f99
-{
603f99
-	int fd;
603f99
-
603f99
-#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
-	if (cloexec_works != -1)
603f99
-		flags |= O_CLOEXEC;
603f99
-#endif
603f99
-	fd = open(path, flags, mode);
603f99
-	if (fd == -1)
603f99
-		return -1;
603f99
-	check_cloexec(fd);
603f99
-	return fd;
603f99
-}
603f99
-
603f99
-static inline int open_pipe(int pipefd[2])
603f99
-{
603f99
-	int ret;
603f99
-
603f99
-#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC) && defined(HAVE_PIPE2)
603f99
-	if (cloexec_works != -1) {
603f99
-		ret = pipe2(pipefd, O_CLOEXEC);
603f99
-		if (ret != -1)
603f99
-			return 0;
603f99
-		if (errno != EINVAL)
603f99
-			return -1;
603f99
-	}
603f99
-#endif
603f99
-	ret = pipe(pipefd);
603f99
-	if (ret == -1)
603f99
-		return -1;
603f99
-	check_cloexec(pipefd[0]);
603f99
-	check_cloexec(pipefd[1]);
603f99
-	return 0;
603f99
-}
603f99
-
603f99
-static inline int open_sock(int domain, int type, int protocol)
603f99
-{
603f99
-	int fd;
603f99
-
603f99
-#ifdef SOCK_CLOEXEC
603f99
-	if (cloexec_works != -1)
603f99
-		type |= SOCK_CLOEXEC;
603f99
-#endif
603f99
-	fd = socket(domain, type, protocol);
603f99
-	if (fd == -1)
603f99
-		return -1;
603f99
-	check_cloexec(fd);
603f99
-	return fd;
603f99
-}
603f99
-
603f99
-static inline FILE *open_fopen_r(const char *path)
603f99
-{
603f99
-	FILE *f;
603f99
-
603f99
-#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
-	if (cloexec_works != -1) {
603f99
-		f = fopen(path, "re");
603f99
-		if (f != NULL) {
603f99
-			check_cloexec(fileno(f));
603f99
-			return f;
603f99
-		}
603f99
-	}
603f99
-#endif
603f99
-	f = fopen(path, "r");
603f99
-	if (f == NULL)
603f99
-		return NULL;
603f99
-	check_cloexec(fileno(f));
603f99
-	return f;
603f99
-}
603f99
-
603f99
-static inline FILE *open_setmntent_r(const char *table)
603f99
-{
603f99
-	FILE *tab;
603f99
-
603f99
-#if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
603f99
-	if (cloexec_works != -1) {
603f99
-		tab = setmntent(table, "re");
603f99
-		if (tab != NULL) {
603f99
-			check_cloexec(fileno(tab));
603f99
-			return tab;
603f99
-		}
603f99
-	}
603f99
-#endif
603f99
-	tab = fopen(table, "r");
603f99
-	if (tab == NULL)
603f99
-		return NULL;
603f99
-	check_cloexec(fileno(tab));
603f99
-	return tab;
603f99
-}
603f99
-
603f99
 #endif
603f99
 
603f99
--- autofs-5.0.7.orig/daemon/automount.c
603f99
+++ autofs-5.0.7/daemon/automount.c
603f99
@@ -75,9 +75,6 @@ static sigset_t block_sigs;
603f99
 /* Pre-calculated kernel packet length */
603f99
 static size_t kpkt_len;
603f99
 
603f99
-/* Does kernel know about SOCK_CLOEXEC and friends */
603f99
-static int cloexec_works = 0;
603f99
-
603f99
 /* Attributes for creating detached and joinable threads */
603f99
 pthread_attr_t th_attr;
603f99
 pthread_attr_t th_attr_detached;