Blame SOURCES/0005-Fix-CVE-2022-4883-compression-commands-depend-on-PAT.patch

63fff0
From 66854ee1d187095186ae718979baf771c177002a Mon Sep 17 00:00:00 2001
63fff0
From: Alan Coopersmith <alan.coopersmith@oracle.com>
63fff0
Date: Fri, 6 Jan 2023 12:50:48 -0800
63fff0
Subject: [PATCH libXpm 5/5] Fix CVE-2022-4883: compression commands depend on
63fff0
 $PATH
63fff0
63fff0
By default, on all platforms except MinGW, libXpm will detect if a
63fff0
filename ends in .Z or .gz, and will when reading such a file fork off
63fff0
an uncompress or gunzip command to read from via a pipe, and when
63fff0
writing such a file will fork off a compress or gzip command to write
63fff0
to via a pipe.
63fff0
63fff0
In libXpm 3.5.14 or older these are run via execlp(), relying on $PATH
63fff0
to find the commands.  If libXpm is called from a program running with
63fff0
raised privileges, such as via setuid, then a malicious user could set
63fff0
$PATH to include programs of their choosing to be run with those
63fff0
privileges.
63fff0
63fff0
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
63fff0
---
63fff0
 README       | 12 ++++++++++++
63fff0
 configure.ac | 14 ++++++++++++++
63fff0
 src/RdFToI.c | 17 ++++++++++++++---
63fff0
 src/WrFFrI.c |  4 ++--
63fff0
 4 files changed, 42 insertions(+), 5 deletions(-)
63fff0
63fff0
diff --git a/README b/README
63fff0
index f532bef..c7d6dbf 100644
63fff0
--- a/README
63fff0
+++ b/README
63fff0
@@ -38,3 +38,15 @@ if it can't find the file it was asked to open.  It relies on the
63fff0
 --enable-open-zfile feature to open the file, and is enabled by default
63fff0
 when --enable-open-zfile is enabled, and can be disabled by passing the
63fff0
 --disable-stat-zfile flag to the configure script.
63fff0
+
63fff0
+All of these commands will be executed with whatever userid & privileges the
63fff0
+function is called with, relying on the caller to ensure the correct euid,
63fff0
+egid, etc. are set before calling.
63fff0
+
63fff0
+To reduce risk, the paths to these commands are now set at configure time to
63fff0
+the first version found in the PATH used to run configure, and do not depend
63fff0
+on the PATH environment variable set at runtime.
63fff0
+
63fff0
+To specify paths to be used for these commands instead of searching $PATH, pass
63fff0
+the XPM_PATH_COMPRESS, XPM_PATH_UNCOMPRESS, XPM_PATH_GZIP, and XPM_PATH_GUNZIP
63fff0
+variables to the configure command.
63fff0
diff --git a/configure.ac b/configure.ac
63fff0
index 4a8d6de..c1da348 100644
63fff0
--- a/configure.ac
63fff0
+++ b/configure.ac
63fff0
@@ -48,6 +48,14 @@ if test "x$USE_GETTEXT" = "xyes" ; then
63fff0
 fi
63fff0
 AM_CONDITIONAL(USE_GETTEXT, test "x$USE_GETTEXT" = "xyes")
63fff0
 
63fff0
+dnl Helper macro to find absolute path to program and add a #define for it
63fff0
+AC_DEFUN([XPM_PATH_PROG],[
63fff0
+AC_PATH_PROG([$1], [$2], [])
63fff0
+AS_IF([test "x$$1" = "x"],
63fff0
+      [AC_MSG_ERROR([$2 not found, set $1 or use --disable-stat-zfile])])
63fff0
+AC_DEFINE_UNQUOTED([$1], ["$$1"], [Path to $2])
63fff0
+]) dnl End of AC_DEFUN([XPM_PATH_PROG]...
63fff0
+
63fff0
 # Optional feature: When a filename ending in .Z or .gz is requested,
63fff0
 # open a pipe to a newly forked compress/uncompress/gzip/gunzip command to
63fff0
 # handle it.
63fff0
@@ -63,6 +71,12 @@ AC_ARG_ENABLE(open-zfile,
63fff0
 AC_MSG_RESULT([$OPEN_ZFILE])
63fff0
 if test x$OPEN_ZFILE = xno ; then
63fff0
         AC_DEFINE(NO_ZPIPE, 1, [Define to 1 to disable decompression via pipes])
63fff0
+else
63fff0
+        XPM_PATH_PROG([XPM_PATH_COMPRESS], [compress])
63fff0
+        XPM_PATH_PROG([XPM_PATH_UNCOMPRESS], [uncompress])
63fff0
+        XPM_PATH_PROG([XPM_PATH_GZIP], [gzip])
63fff0
+        XPM_PATH_PROG([XPM_PATH_GUNZIP], [gunzip])
63fff0
+        AC_CHECK_FUNCS([closefrom close_range], [break])
63fff0
 fi
63fff0
 
63fff0
 # Optional feature: When ___.xpm is requested, also look for ___.xpm.Z & .gz
63fff0
diff --git a/src/RdFToI.c b/src/RdFToI.c
63fff0
index bd09611..a91d337 100644
63fff0
--- a/src/RdFToI.c
63fff0
+++ b/src/RdFToI.c
63fff0
@@ -43,6 +43,7 @@
63fff0
 #include <errno.h>
63fff0
 #include <sys/types.h>
63fff0
 #include <sys/wait.h>
63fff0
+#include <unistd.h>
63fff0
 #else
63fff0
 #ifdef FOR_MSW
63fff0
 #include <fcntl.h>
63fff0
@@ -161,7 +162,17 @@ xpmPipeThrough(
63fff0
 	    goto err;
63fff0
 	if ( 0 == pid )
63fff0
 	{
63fff0
-	    execlp(cmd, cmd, arg1, (char *)NULL);
63fff0
+#ifdef HAVE_CLOSEFROM
63fff0
+	    closefrom(3);
63fff0
+#elif defined(HAVE_CLOSE_RANGE)
63fff0
+# ifdef CLOSE_RANGE_UNSHARE
63fff0
+#  define close_range_flags CLOSE_RANGE_UNSHARE
63fff0
+# else
63fff0
+#  define close_range_flags 0
63fff0
+#endif
63fff0
+	    close_range(3, ~0U, close_range_flags);
63fff0
+#endif
63fff0
+	    execl(cmd, cmd, arg1, (char *)NULL);
63fff0
 	    perror(cmd);
63fff0
 	    goto err;
63fff0
 	}
63fff0
@@ -235,12 +246,12 @@ OpenReadFile(
63fff0
 	if ( ext && !strcmp(ext, ".Z") )
63fff0
 	{
63fff0
 	    mdata->type = XPMPIPE;
63fff0
-	    mdata->stream.file = xpmPipeThrough(fd, "uncompress", "-c", "r");
63fff0
+	    mdata->stream.file = xpmPipeThrough(fd, XPM_PATH_UNCOMPRESS, "-c", "r");
63fff0
 	}
63fff0
 	else if ( ext && !strcmp(ext, ".gz") )
63fff0
 	{
63fff0
 	    mdata->type = XPMPIPE;
63fff0
-	    mdata->stream.file = xpmPipeThrough(fd, "gunzip", "-qc", "r");
63fff0
+	    mdata->stream.file = xpmPipeThrough(fd, XPM_PATH_GUNZIP, "-qc", "r");
63fff0
 	}
63fff0
 	else
63fff0
 #endif /* z-files */
63fff0
diff --git a/src/WrFFrI.c b/src/WrFFrI.c
63fff0
index 067c96b..bc38f66 100644
63fff0
--- a/src/WrFFrI.c
63fff0
+++ b/src/WrFFrI.c
63fff0
@@ -336,10 +336,10 @@ OpenWriteFile(
63fff0
 #ifndef NO_ZPIPE
63fff0
 	len = strlen(filename);
63fff0
 	if (len > 2 && !strcmp(".Z", filename + (len - 2))) {
63fff0
-	    mdata->stream.file = xpmPipeThrough(fd, "compress", NULL, "w");
63fff0
+	    mdata->stream.file = xpmPipeThrough(fd, XPM_PATH_COMPRESS, NULL, "w");
63fff0
 	    mdata->type = XPMPIPE;
63fff0
 	} else if (len > 3 && !strcmp(".gz", filename + (len - 3))) {
63fff0
-	    mdata->stream.file = xpmPipeThrough(fd, "gzip", "-q", "w");
63fff0
+	    mdata->stream.file = xpmPipeThrough(fd, XPM_PATH_GZIP, "-q", "w");
63fff0
 	    mdata->type = XPMPIPE;
63fff0
 	} else
63fff0
 #endif
63fff0
-- 
63fff0
2.39.0
63fff0