|
| 1 | +From b050f79897597e999396c27aab8e57a01d88db23 Mon Sep 17 00:00:00 2001 |
| 2 | +From: AllSpark <allspark@microsoft.com> |
| 3 | +Date: Mon, 8 Dec 2025 09:54:26 +0000 |
| 4 | +Subject: [PATCH] vendor/logrus: Fix potential DoS in Writer() by scanning |
| 5 | + input in 64KB chunks and trimming newlines. Add comments and finalizer. Based |
| 6 | + on upstream patches. |
| 7 | + |
| 8 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 9 | +Upstream-reference: AI Backport of https://github.com/sirupsen/logrus/pull/1376.patch |
| 10 | +--- |
| 11 | + vendor/github.com/Sirupsen/logrus/writer.go | 34 ++++++++++++++++++++- |
| 12 | + 1 file changed, 33 insertions(+), 1 deletion(-) |
| 13 | + |
| 14 | +diff --git a/vendor/github.com/Sirupsen/logrus/writer.go b/vendor/github.com/Sirupsen/logrus/writer.go |
| 15 | +index f74d2aa..4bff841 100644 |
| 16 | +--- a/vendor/github.com/Sirupsen/logrus/writer.go |
| 17 | ++++ b/vendor/github.com/Sirupsen/logrus/writer.go |
| 18 | +@@ -4,16 +4,20 @@ import ( |
| 19 | + "bufio" |
| 20 | + "io" |
| 21 | + "runtime" |
| 22 | ++ "strings" |
| 23 | + ) |
| 24 | + |
| 25 | ++// Writer returns an io.Writer that writes to the logger at the info log level |
| 26 | + func (logger *Logger) Writer() *io.PipeWriter { |
| 27 | + return logger.WriterLevel(InfoLevel) |
| 28 | + } |
| 29 | + |
| 30 | ++// WriterLevel returns an io.Writer that writes to the logger at the given log level |
| 31 | + func (logger *Logger) WriterLevel(level Level) *io.PipeWriter { |
| 32 | + reader, writer := io.Pipe() |
| 33 | + |
| 34 | + var printFunc func(args ...interface{}) |
| 35 | ++ // Determine which log function to use based on the specified log level |
| 36 | + switch level { |
| 37 | + case DebugLevel: |
| 38 | + printFunc = logger.Debug |
| 39 | +@@ -31,23 +35,51 @@ func (logger *Logger) WriterLevel(level Level) *io.PipeWriter { |
| 40 | + printFunc = logger.Print |
| 41 | + } |
| 42 | + |
| 43 | ++ // Start a new goroutine to scan the input and write it to the logger using the specified print function. |
| 44 | ++ // It splits the input into chunks of up to 64KB to avoid buffer overflows. |
| 45 | + go logger.writerScanner(reader, printFunc) |
| 46 | ++ |
| 47 | ++ // Set a finalizer function to close the writer when it is garbage collected |
| 48 | + runtime.SetFinalizer(writer, writerFinalizer) |
| 49 | + |
| 50 | + return writer |
| 51 | + } |
| 52 | + |
| 53 | ++// writerScanner scans the input from the reader and writes it to the logger |
| 54 | + func (logger *Logger) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) { |
| 55 | + scanner := bufio.NewScanner(reader) |
| 56 | ++ |
| 57 | ++ // Set the buffer size to the maximum token size to avoid buffer overflows |
| 58 | ++ scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize) |
| 59 | ++ |
| 60 | ++ // Define a split function to split the input into chunks of up to 64KB |
| 61 | ++ chunkSize := 64 * 1024 // 64KB |
| 62 | ++ splitFunc := func(data []byte, atEOF bool) (int, []byte, error) { |
| 63 | ++ if len(data) > chunkSize { |
| 64 | ++ return chunkSize, data[:chunkSize], nil |
| 65 | ++ } |
| 66 | ++ |
| 67 | ++ return len(data), data, nil |
| 68 | ++ } |
| 69 | ++ |
| 70 | ++ //Use the custom split function to split the input |
| 71 | ++ scanner.Split(splitFunc) |
| 72 | ++ |
| 73 | ++ // Scan the input and write it to the logger using the specified print function |
| 74 | + for scanner.Scan() { |
| 75 | +- printFunc(scanner.Text()) |
| 76 | ++ printFunc(strings.TrimRight(scanner.Text(), "\r\n")) |
| 77 | + } |
| 78 | ++ |
| 79 | ++ // If there was an error while scanning the input, log an error |
| 80 | + if err := scanner.Err(); err != nil { |
| 81 | + logger.Errorf("Error while reading from Writer: %s", err) |
| 82 | + } |
| 83 | ++ |
| 84 | ++ // Close the reader when we are done |
| 85 | + reader.Close() |
| 86 | + } |
| 87 | + |
| 88 | ++// WriterFinalizer is a finalizer function that closes then given writer when it is garbage collected |
| 89 | + func writerFinalizer(writer *io.PipeWriter) { |
| 90 | + writer.Close() |
| 91 | + } |
| 92 | +-- |
| 93 | +2.45.4 |
| 94 | + |
0 commit comments