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

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