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

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