7da94a
From dc9740df61e575e8c3148b7bd3c147a81ea00c7c Mon Sep 17 00:00:00 2001
7da94a
From: Lasse Collin <lasse.collin@tukaani.org>
7da94a
Date: Mon, 4 Apr 2022 23:52:49 -0700
7da94a
Subject: zgrep: avoid exploit via multi-newline file names
7da94a
7da94a
* zgrep.in: The issue with the old code is that with multiple
7da94a
newlines, the N-command will read the second line of input,
7da94a
then the s-commands will be skipped because it's not the end
7da94a
of the file yet, then a new sed cycle starts and the pattern
7da94a
space is printed and emptied. So only the last line or two get
7da94a
escaped. This patch makes sed read all lines into the pattern
7da94a
space and then do the escaping.
7da94a
7da94a
This vulnerability was discovered by:
7da94a
cleemy desu wayo working with Trend Micro Zero Day Initiative
7da94a
---
7da94a
 zgrep.in | 10 +++++++---
7da94a
 1 file changed, 7 insertions(+), 3 deletions(-)
7da94a
7da94a
diff --git a/zgrep.in b/zgrep.in
7da94a
index 345dae3..bdf7da2 100644
7da94a
--- a/zgrep.in
7da94a
+++ b/zgrep.in
7da94a
@@ -222,9 +222,13 @@ do
7da94a
 '* | *'&'* | *'\'* | *'|'*)
7da94a
         i=$(printf '%s\n' "$i" |
7da94a
             sed '
7da94a
-              $!N
7da94a
-              $s/[&\|]/\\&/g
7da94a
-              $s/\n/\\n/g
7da94a
+              :start
7da94a
+              $!{
7da94a
+                N
7da94a
+                b start
7da94a
+              }
7da94a
+              s/[&\|]/\\&/g
7da94a
+              s/\n/\\n/g
7da94a
             ');;
7da94a
       esac
7da94a
       sed_script="s|^|$i:|"
7da94a
-- 
7da94a
cgit v1.1
7da94a