|
| 1 | +From a78e87c21580bd4264706f21e2cac6369757ee73 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Chris <straight.chris@gmail.com> |
| 3 | +Date: Fri, 10 Mar 2023 13:45:41 -0800 |
| 4 | +Subject: [PATCH 1/2] This commit fixes a potential denial of service |
| 5 | + vulnerability in logrus.Writer() that could be triggered by logging text |
| 6 | + longer than 64kb without newlines. Previously, the bufio.Scanner used by |
| 7 | + Writer() would hang indefinitely when reading such text without newlines, |
| 8 | + causing the application to become unresponsive. |
| 9 | + |
| 10 | +Upstream Reference : https://github.com/sirupsen/logrus/commit/766cfece3701d0b1737681ffb5e6e40b628b664d.patch |
| 11 | +--- |
| 12 | + vendor/github.com/sirupsen/logrus/writer.go | 33 ++++++++++++++++++++- |
| 13 | + 1 file changed, 32 insertions(+), 1 deletion(-) |
| 14 | + |
| 15 | +diff --git a/vendor/github.com/sirupsen/logrus/writer.go b/vendor/github.com/sirupsen/logrus/writer.go |
| 16 | +index 72e8e3a1..36032d06 100644 |
| 17 | +--- a/vendor/github.com/sirupsen/logrus/writer.go |
| 18 | ++++ b/vendor/github.com/sirupsen/logrus/writer.go |
| 19 | +@@ -4,6 +4,7 @@ import ( |
| 20 | + "bufio" |
| 21 | + "io" |
| 22 | + "runtime" |
| 23 | ++ "strings" |
| 24 | + ) |
| 25 | + |
| 26 | + // Writer at INFO level. See WriterLevel for details. |
| 27 | +@@ -20,15 +21,18 @@ func (logger *Logger) WriterLevel(level Level) *io.PipeWriter { |
| 28 | + return NewEntry(logger).WriterLevel(level) |
| 29 | + } |
| 30 | + |
| 31 | ++// Writer returns an io.Writer that writes to the logger at the info log level |
| 32 | + func (entry *Entry) Writer() *io.PipeWriter { |
| 33 | + return entry.WriterLevel(InfoLevel) |
| 34 | + } |
| 35 | + |
| 36 | ++// WriterLevel returns an io.Writer that writes to the logger at the given log level |
| 37 | + func (entry *Entry) WriterLevel(level Level) *io.PipeWriter { |
| 38 | + reader, writer := io.Pipe() |
| 39 | + |
| 40 | + var printFunc func(args ...interface{}) |
| 41 | + |
| 42 | ++ // Determine which log function to use based on the specified log level |
| 43 | + switch level { |
| 44 | + case TraceLevel: |
| 45 | + printFunc = entry.Trace |
| 46 | +@@ -48,23 +52,50 @@ func (entry *Entry) WriterLevel(level Level) *io.PipeWriter { |
| 47 | + printFunc = entry.Print |
| 48 | + } |
| 49 | + |
| 50 | ++ // Start a new goroutine to scan the input and write it to the logger using the specified print function. |
| 51 | ++ // It splits the input into chunks of up to 64KB to avoid buffer overflows. |
| 52 | + go entry.writerScanner(reader, printFunc) |
| 53 | ++ |
| 54 | ++ // Set a finalizer function to close the writer when it is garbage collected |
| 55 | + runtime.SetFinalizer(writer, writerFinalizer) |
| 56 | + |
| 57 | + return writer |
| 58 | + } |
| 59 | + |
| 60 | ++// writerScanner scans the input from the reader and writes it to the logger |
| 61 | + func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) { |
| 62 | + scanner := bufio.NewScanner(reader) |
| 63 | ++ |
| 64 | ++ // Set the buffer size to the maximum token size to avoid buffer overflows |
| 65 | ++ scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize) |
| 66 | ++ |
| 67 | ++ // Define a split function to split the input into chunks of up to 64KB |
| 68 | ++ chunkSize := 64 * 1024 // 64KB |
| 69 | ++ splitFunc := func(data []byte, atEOF bool) (int, []byte, error) { |
| 70 | ++ if len(data) > chunkSize { |
| 71 | ++ return chunkSize, data[:chunkSize], nil |
| 72 | ++ } |
| 73 | ++ return 0, nil, nil |
| 74 | ++ } |
| 75 | ++ |
| 76 | ++ //Use the custom split function to split the input |
| 77 | ++ scanner.Split(splitFunc) |
| 78 | ++ |
| 79 | ++ // Scan the input and write it to the logger using the specified print function |
| 80 | + for scanner.Scan() { |
| 81 | +- printFunc(scanner.Text()) |
| 82 | ++ printFunc(strings.TrimRight(scanner.Text(), "\r\n")) |
| 83 | + } |
| 84 | ++ |
| 85 | ++ // If there was an error while scanning the input, log an error |
| 86 | + if err := scanner.Err(); err != nil { |
| 87 | + entry.Errorf("Error while reading from Writer: %s", err) |
| 88 | + } |
| 89 | ++ |
| 90 | ++ // Close the reader when we are done |
| 91 | + reader.Close() |
| 92 | + } |
| 93 | + |
| 94 | ++// WriterFinalizer is a finalizer function that closes then given writer when it is garbage collected |
| 95 | + func writerFinalizer(writer *io.PipeWriter) { |
| 96 | + writer.Close() |
| 97 | + } |
| 98 | +-- |
| 99 | +2.45.4 |
| 100 | + |
| 101 | + |
| 102 | +From 2d51c47131bb31ebe41a9fa85552987bb9225a09 Mon Sep 17 00:00:00 2001 |
| 103 | +From: Chris <straight.chris@gmail.com> |
| 104 | +Date: Fri, 10 Mar 2023 13:45:41 -0800 |
| 105 | +Subject: [PATCH 2/2] Scan text in 64KB chunks |
| 106 | + |
| 107 | +This commit fixes a potential denial of service |
| 108 | +vulnerability in logrus.Writer() that could be |
| 109 | +triggered by logging text longer than 64KB |
| 110 | +without newlines. Previously, the bufio.Scanner |
| 111 | +used by Writer() would hang indefinitely when |
| 112 | +reading such text without newlines, causing the |
| 113 | +application to become unresponsive. |
| 114 | + |
| 115 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 116 | +Upstream-reference: https://github.com/sirupsen/logrus/pull/1376.patch |
| 117 | +--- |
| 118 | + vendor/github.com/sirupsen/logrus/writer.go | 3 ++- |
| 119 | + 1 file changed, 2 insertions(+), 1 deletion(-) |
| 120 | + |
| 121 | +diff --git a/vendor/github.com/sirupsen/logrus/writer.go b/vendor/github.com/sirupsen/logrus/writer.go |
| 122 | +index 36032d06..7e7703c7 100644 |
| 123 | +--- a/vendor/github.com/sirupsen/logrus/writer.go |
| 124 | ++++ b/vendor/github.com/sirupsen/logrus/writer.go |
| 125 | +@@ -75,7 +75,8 @@ func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ... |
| 126 | + if len(data) > chunkSize { |
| 127 | + return chunkSize, data[:chunkSize], nil |
| 128 | + } |
| 129 | +- return 0, nil, nil |
| 130 | ++ |
| 131 | ++ return len(data), data, nil |
| 132 | + } |
| 133 | + |
| 134 | + //Use the custom split function to split the input |
| 135 | +-- |
| 136 | +2.45.4 |
| 137 | + |
| 138 | +From d40e25cd45ed9c6b2b66e6b97573a0413e4c23bd Mon Sep 17 00:00:00 2001 |
| 139 | +From: Paul Holzinger <pholzing@redhat.com> |
| 140 | +Date: Wed, 17 May 2023 15:39:49 +0200 |
| 141 | +Subject: [PATCH] fix panic in Writer |
| 142 | + |
| 143 | +Commit 766cfece introduced this bug by defining an incorrect split |
| 144 | +function. First it breaks the old behavior because it never splits at |
| 145 | +newlines now. Second, it causes a panic because it never tells the |
| 146 | +scanner to stop. See the bufio.ScanLines function, something like: |
| 147 | +``` |
| 148 | +if atEOF && len(data) == 0 { |
| 149 | + return 0, nil, nil |
| 150 | +} |
| 151 | +``` |
| 152 | +is needed to do that. |
| 153 | + |
| 154 | +This commit fixes it by restoring the old behavior and calling |
| 155 | +bufio.ScanLines but also keep the 64KB check in place to avoid buffering |
| 156 | +for to long. |
| 157 | + |
| 158 | +Two tests are added to ensure it is working as expected. |
| 159 | + |
| 160 | +Fixes #1383 |
| 161 | +Upstream Reference Patch: https://github.com/sirupsen/logrus/commit/d40e25cd45ed9c6b2b66e6b97573a0413e4c23bd.patch |
| 162 | + |
| 163 | +Signed-off-by: Paul Holzinger <pholzing@redhat.com> |
| 164 | +--- |
| 165 | + vendor/github.com/sirupsen/logrus/writer.go | 8 ++++---- |
| 166 | + 1 file changed, 4 insertions(+), 4 deletions(-) |
| 167 | + |
| 168 | +diff --git a/vendor/github.com/sirupsen/logrus/writer.go b/vendor/github.com/sirupsen/logrus/writer.go |
| 169 | +index 7e7703c7..074fd4b8 100644 |
| 170 | +--- a/vendor/github.com/sirupsen/logrus/writer.go |
| 171 | ++++ b/vendor/github.com/sirupsen/logrus/writer.go |
| 172 | +@@ -70,16 +70,16 @@ func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ... |
| 173 | + scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize) |
| 174 | + |
| 175 | + // Define a split function to split the input into chunks of up to 64KB |
| 176 | +- chunkSize := 64 * 1024 // 64KB |
| 177 | ++ chunkSize := bufio.MaxScanTokenSize // 64KB |
| 178 | + splitFunc := func(data []byte, atEOF bool) (int, []byte, error) { |
| 179 | +- if len(data) > chunkSize { |
| 180 | ++ if len(data) >= chunkSize { |
| 181 | + return chunkSize, data[:chunkSize], nil |
| 182 | + } |
| 183 | + |
| 184 | +- return len(data), data, nil |
| 185 | ++ return bufio.ScanLines(data, atEOF) |
| 186 | + } |
| 187 | + |
| 188 | +- //Use the custom split function to split the input |
| 189 | ++ // Use the custom split function to split the input |
| 190 | + scanner.Split(splitFunc) |
| 191 | + |
| 192 | + // Scan the input and write it to the logger using the specified print function |
| 193 | +-- |
| 194 | +2.45.4 |
| 195 | + |
0 commit comments