Blame SOURCES/xz-5.2.4-cve-2022-1271.patch

b5445f
From 69d1b3fc29677af8ade8dc15dba83f0589cb63d6 Mon Sep 17 00:00:00 2001
b5445f
From: Lasse Collin <lasse.collin@tukaani.org>
b5445f
Date: Tue, 29 Mar 2022 19:19:12 +0300
b5445f
Subject: [PATCH] xzgrep: Fix escaping of malicious filenames (ZDI-CAN-16587).
b5445f
b5445f
Malicious filenames can make xzgrep to write to arbitrary files
b5445f
or (with a GNU sed extension) lead to arbitrary code execution.
b5445f
b5445f
xzgrep from XZ Utils versions up to and including 5.2.5 are
b5445f
affected. 5.3.1alpha and 5.3.2alpha are affected as well.
b5445f
This patch works for all of them.
b5445f
b5445f
This bug was inherited from gzip's zgrep. gzip 1.12 includes
b5445f
a fix for zgrep.
b5445f
b5445f
The issue with the old sed script is that with multiple newlines,
b5445f
the N-command will read the second line of input, then the
b5445f
s-commands will be skipped because it's not the end of the
b5445f
file yet, then a new sed cycle starts and the pattern space
b5445f
is printed and emptied. So only the last line or two get escaped.
b5445f
b5445f
One way to fix this would be to read all lines into the pattern
b5445f
space first. However, the included fix is even simpler: All lines
b5445f
except the last line get a backslash appended at the end. To ensure
b5445f
that shell command substitution doesn't eat a possible trailing
b5445f
newline, a colon is appended to the filename before escaping.
b5445f
The colon is later used to separate the filename from the grep
b5445f
output so it is fine to add it here instead of a few lines later.
b5445f
b5445f
The old code also wasn't POSIX compliant as it used \n in the
b5445f
replacement section of the s-command. Using \<newline> is the
b5445f
POSIX compatible method.
b5445f
b5445f
LC_ALL=C was added to the two critical sed commands. POSIX sed
b5445f
manual recommends it when using sed to manipulate pathnames
b5445f
because in other locales invalid multibyte sequences might
b5445f
cause issues with some sed implementations. In case of GNU sed,
b5445f
these particular sed scripts wouldn't have such problems but some
b5445f
other scripts could have, see:
b5445f
b5445f
    info '(sed)Locale Considerations'
b5445f
b5445f
This vulnerability was discovered by:
b5445f
cleemy desu wayo working with Trend Micro Zero Day Initiative
b5445f
b5445f
Thanks to Jim Meyering and Paul Eggert discussing the different
b5445f
ways to fix this and for coordinating the patch release schedule
b5445f
with gzip.
b5445f
---
b5445f
 src/scripts/xzgrep.in | 20 ++++++++++++--------
b5445f
 1 file changed, 12 insertions(+), 8 deletions(-)
b5445f
b5445f
diff --git a/src/scripts/xzgrep.in b/src/scripts/xzgrep.in
b5445f
index b180936..e5186ba 100644
b5445f
--- a/src/scripts/xzgrep.in
b5445f
+++ b/src/scripts/xzgrep.in
b5445f
@@ -180,22 +180,26 @@ for i; do
b5445f
          { test $# -eq 1 || test $no_filename -eq 1; }; then
b5445f
       eval "$grep"
b5445f
     else
b5445f
+      # Append a colon so that the last character will never be a newline
b5445f
+      # which would otherwise get lost in shell command substitution.
b5445f
+      i="$i:"
b5445f
+
b5445f
+      # Escape & \ | and newlines only if such characters are present
b5445f
+      # (speed optimization).
b5445f
       case $i in
b5445f
       (*'
b5445f
 '* | *'&'* | *'\'* | *'|'*)
b5445f
-        i=$(printf '%s\n' "$i" |
b5445f
-            sed '
b5445f
-              $!N
b5445f
-              $s/[&\|]/\\&/g
b5445f
-              $s/\n/\\n/g
b5445f
-            ');;
b5445f
+        i=$(printf '%s\n' "$i" | LC_ALL=C sed 's/[&\|]/\\&/;; $!s/$/\\/');;
b5445f
       esac
b5445f
-      sed_script="s|^|$i:|"
b5445f
+
b5445f
+      # $i already ends with a colon so don't add it here.
b5445f
+      sed_script="s|^|$i|"
b5445f
 
b5445f
       # Fail if grep or sed fails.
b5445f
       r=$(
b5445f
         exec 4>&1
b5445f
-        (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
b5445f
+        (eval "$grep" 4>&-; echo $? >&4) 3>&- |
b5445f
+            LC_ALL=C sed "$sed_script" >&3 4>&-
b5445f
       ) || r=2
b5445f
       exit $r
b5445f
     fi >&3 5>&-
b5445f
-- 
b5445f
2.35.1
b5445f