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

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