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

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