ee1fd2
diff --git a/ChangeLog b/ChangeLog
ee1fd2
index d46caaa0d..41d6e99d9 100644
ee1fd2
--- a/ChangeLog
ee1fd2
+++ b/ChangeLog
ee1fd2
@@ -1,3 +1,8 @@
ee1fd2
+2020-12-08  16:24  Christos Zoulas <christos@zoulas.com>
ee1fd2
+
ee1fd2
+	* fix multithreaded decompression file descriptor issue
ee1fd2
+	  by using close-on-exec (Denys Vlasenko)
ee1fd2
+
ee1fd2
 2020-06-14  20:02  Christos Zoulas <christos@zoulas.com>
ee1fd2
ee1fd2
	* release 5.39
ee1fd2
diff --git a/configure.ac b/configure.ac
ee1fd2
index 64c9f42e3..521dc12db 100644
ee1fd2
--- a/configure.ac
ee1fd2
+++ b/configure.ac
ee1fd2
@@ -166,7 +166,7 @@ else
ee1fd2
 fi])
ee1fd2
ee1fd2
 dnl Checks for functions
ee1fd2
-AC_CHECK_FUNCS(strndup mkstemp mkostemp utimes utime wcwidth strtof newlocale uselocale freelocale memmem)
ee1fd2
+AC_CHECK_FUNCS(strndup mkstemp mkostemp utimes utime wcwidth strtof newlocale uselocale freelocale memmem pipe2)
ee1fd2
ee1fd2
 dnl Provide implementation of some required functions if necessary
ee1fd2
 AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat getline ctime_r asctime_r localtime_r gmtime_r pread strcasestr fmtcheck dprintf)
ee1fd2
diff --git a/src/compress.c b/src/compress.c
ee1fd2
index cbc2ce19b..9f65e4fa1 100644
ee1fd2
--- a/src/compress.c
ee1fd2
+++ b/src/compress.c
ee1fd2
@@ -35,7 +35,7 @@
ee1fd2
 #include "file.h"
ee1fd2
ee1fd2
 #ifndef lint
ee1fd2
-FILE_RCSID("@(#)$File: compress.c,v 1.127 2020/05/31 00:11:06 christos Exp $")
ee1fd2
+FILE_RCSID("@(#)$File: compress.c,v 1.129 2020/12/08 21:26:00 christos Exp $")
ee1fd2
 #endif
ee1fd2
ee1fd2
 #include "magic.h"
ee1fd2
@@ -844,8 +844,23 @@ uncompressbuf(int fd, size_t bytes_max, size_t method, const unsigned char *old,
ee1fd2
	for (i = 0; i < __arraycount(fdp); i++)
ee1fd2
		fdp[i][0] = fdp[i][1] = -1;
ee1fd2
ee1fd2
-	if ((fd == -1 && pipe(fdp[STDIN_FILENO]) == -1) ||
ee1fd2
-	    pipe(fdp[STDOUT_FILENO]) == -1 || pipe(fdp[STDERR_FILENO]) == -1) {
ee1fd2
+	/*
ee1fd2
+	 * There are multithreaded users who run magic_file()
ee1fd2
+	 * from dozens of threads. If two parallel magic_file() calls
ee1fd2
+	 * analyze two large compressed files, both will spawn
ee1fd2
+	 * an uncompressing child here, which writes out uncompressed data.
ee1fd2
+	 * We read some portion, then close the pipe, then waitpid() the child.
ee1fd2
+	 * If uncompressed data is larger, child shound get EPIPE and exit.
ee1fd2
+	 * However, with *parallel* calls OTHER child may unintentionally
ee1fd2
+	 * inherit pipe fds, thus keeping pipe open and making writes in
ee1fd2
+	 * our child block instead of failing with EPIPE!
ee1fd2
+	 * (For the bug to occur, two threads must mutually inherit their pipes,
ee1fd2
+	 * and both must have large outputs. Thus it happens not that often).
ee1fd2
+	 * To avoid this, be sure to create pipes with O_CLOEXEC.
ee1fd2
+	 */
ee1fd2
+	if ((fd == -1 && file_pipe_closexec(fdp[STDIN_FILENO]) == -1) ||
ee1fd2
+	    file_pipe_closexec(fdp[STDOUT_FILENO]) == -1 ||
ee1fd2
+	    file_pipe_closexec(fdp[STDERR_FILENO]) == -1) {
ee1fd2
		closep(fdp[STDIN_FILENO]);
ee1fd2
		closep(fdp[STDOUT_FILENO]);
ee1fd2
		return makeerror(newch, n, "Cannot create pipe, %s",
ee1fd2
@@ -876,16 +891,20 @@ uncompressbuf(int fd, size_t bytes_max, size_t method, const unsigned char *old,
ee1fd2
			if (fdp[STDIN_FILENO][1] > 2)
ee1fd2
				(void) close(fdp[STDIN_FILENO][1]);
ee1fd2
		}
ee1fd2
+		file_clear_closexec(STDIN_FILENO);
ee1fd2
+
ee1fd2
 ///FIXME: if one of the fdp[i][j] is 0 or 1, this can bomb spectacularly
ee1fd2
		if (copydesc(STDOUT_FILENO, fdp[STDOUT_FILENO][1]))
ee1fd2
			(void) close(fdp[STDOUT_FILENO][1]);
ee1fd2
		if (fdp[STDOUT_FILENO][0] > 2)
ee1fd2
			(void) close(fdp[STDOUT_FILENO][0]);
ee1fd2
+		file_clear_closexec(STDOUT_FILENO);
ee1fd2
ee1fd2
		if (copydesc(STDERR_FILENO, fdp[STDERR_FILENO][1]))
ee1fd2
			(void) close(fdp[STDERR_FILENO][1]);
ee1fd2
		if (fdp[STDERR_FILENO][0] > 2)
ee1fd2
			(void) close(fdp[STDERR_FILENO][0]);
ee1fd2
+		file_clear_closexec(STDERR_FILENO);
ee1fd2
ee1fd2
		(void)execvp(compr[method].argv[0],
ee1fd2
		    RCAST(char *const *, RCAST(intptr_t, compr[method].argv)));
ee1fd2
diff --git a/src/file.h b/src/file.h
ee1fd2
index f00e8010b..6c3900479 100644
ee1fd2
--- a/src/file.h
ee1fd2
+++ b/src/file.h
ee1fd2
@@ -27,7 +27,7 @@
ee1fd2
  */
ee1fd2
 /*
ee1fd2
  * file.h - definitions for file(1) program
ee1fd2
- * @(#)$File: file.h,v 1.220 2020/06/08 17:38:27 christos Exp $
ee1fd2
+ * @(#)$File: file.h,v 1.223 2020/12/08 21:26:00 christos Exp $
ee1fd2
  */
ee1fd2
ee1fd2
 #ifndef __file_h__
ee1fd2
@@ -143,6 +143,14 @@
ee1fd2
 #define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
ee1fd2
 #endif
ee1fd2
ee1fd2
+#ifndef O_CLOEXEC
ee1fd2
+# define O_CLOEXEC 0
ee1fd2
+#endif
ee1fd2
+
ee1fd2
+#ifndef FD_CLOEXEC
ee1fd2
+# define FD_CLOEXEC 1
ee1fd2
+#endif
ee1fd2
+
ee1fd2
 #define FILE_BADSIZE CAST(size_t, ~0ul)
ee1fd2
 #define MAXDESC	64		/* max len of text description/MIME type */
ee1fd2
 #define MAXMIME	80		/* max len of text MIME type */
ee1fd2
@@ -540,6 +548,8 @@ protected char * file_printable(char *, size_t, const char *, size_t);
ee1fd2
 protected int file_os2_apptype(struct magic_set *, const char *, const void *,
ee1fd2
     size_t);
ee1fd2
 #endif /* __EMX__ */
ee1fd2
+protected int file_pipe_closexec(int *);
ee1fd2
+protected int file_clear_closexec(int);
ee1fd2
ee1fd2
 protected void buffer_init(struct buffer *, int, const struct stat *,
ee1fd2
     const void *, size_t);
ee1fd2
diff --git a/src/funcs.c b/src/funcs.c
ee1fd2
index ecbfa28c5..bcf9ddaae 100644
ee1fd2
--- a/src/funcs.c
ee1fd2
+++ b/src/funcs.c
ee1fd2
@@ -27,7 +27,7 @@
ee1fd2
 #include "file.h"
ee1fd2
ee1fd2
 #ifndef	lint
ee1fd2
-FILE_RCSID("@(#)$File: funcs.c,v 1.115 2020/02/20 15:50:20 christos Exp $")
ee1fd2
+FILE_RCSID("@(#)$File: funcs.c,v 1.118 2020/12/08 21:26:00 christos Exp $")
ee1fd2
 #endif	/* lint */
ee1fd2
ee1fd2
 #include "magic.h"
ee1fd2
@@ -36,6 +36,9 @@ FILE_RCSID("@(#)$File: funcs.c,v 1.117 2020/06/25 16:52:48 christos Exp $")
ee1fd2
 #include <stdlib.h>
ee1fd2
 #include <string.h>
ee1fd2
 #include <ctype.h>
ee1fd2
+#ifdef HAVE_UNISTD_H
ee1fd2
+#include <unistd.h>	/* for pipe2() */
ee1fd2
+#endif
ee1fd2
 #if defined(HAVE_WCHAR_H)
ee1fd2
 #include <wchar.h>
ee1fd2
 #endif
ee1fd2
@@ -784,3 +787,22 @@ file_print_guid(char *str, size_t len, const uint64_t *guid)
ee1fd2
	    g->data4[2], g->data4[3], g->data4[4], g->data4[5],
ee1fd2
	    g->data4[6], g->data4[7]);
ee1fd2
 }
ee1fd2
+
ee1fd2
+protected int
ee1fd2
+file_pipe_closexec(int *fds)
ee1fd2
+{
ee1fd2
+#ifdef HAVE_PIPE2
ee1fd2
+	return pipe2(fds, O_CLOEXEC);
ee1fd2
+#else
ee1fd2
+	if (pipe(fds) == -1)
ee1fd2
+		return -1;
ee1fd2
+	(void)fcntl(fds[0], F_SETFD, FD_CLOEXEC);
ee1fd2
+	(void)fcntl(fds[1], F_SETFD, FD_CLOEXEC);
ee1fd2
+	return 0;
ee1fd2
+#endif
ee1fd2
+}
ee1fd2
+
ee1fd2
+protected int
ee1fd2
+file_clear_closexec(int fd) {
ee1fd2
+	return fcntl(fd, F_SETFD, 0);
ee1fd2
+}
ee1fd2
diff --git a/src/magic.c b/src/magic.c
ee1fd2
index 17a7077d8..89f4e16c0 100644
ee1fd2
--- a/src/magic.c
ee1fd2
+++ b/src/magic.c
ee1fd2
@@ -33,7 +33,7 @@
ee1fd2
 #include "file.h"
ee1fd2
ee1fd2
 #ifndef	lint
ee1fd2
-FILE_RCSID("@(#)$File: magic.c,v 1.112 2020/06/08 19:44:10 christos Exp $")
ee1fd2
+FILE_RCSID("@(#)$File: magic.c,v 1.113 2020/12/08 21:26:00 christos Exp $")
ee1fd2
 #endif	/* lint */
ee1fd2
ee1fd2
 #include "magic.h"
ee1fd2
@@ -436,7 +436,7 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
ee1fd2
		_setmode(STDIN_FILENO, O_BINARY);
ee1fd2
 #endif
ee1fd2
	if (inname != NULL) {
ee1fd2
-		int flags = O_RDONLY|O_BINARY|O_NONBLOCK;
ee1fd2
+		int flags = O_RDONLY|O_BINARY|O_NONBLOCK|O_CLOEXEC;
ee1fd2
		errno = 0;
ee1fd2
		if ((fd = open(inname, flags)) < 0) {
ee1fd2
			okstat = stat(inname, &sb) == 0;
ee1fd2
@@ -460,6 +460,9 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
ee1fd2
			rv = 0;
ee1fd2
			goto done;
ee1fd2
		}
ee1fd2
+#if O_CLOEXEC == 0
ee1fd2
+		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
ee1fd2
+#endif
ee1fd2
	}
ee1fd2
ee1fd2
	if (fd != -1) {