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

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