mcuadros / go-syslog

Syslog server library for go.
http://godoc.org/gopkg.in/mcuadros/go-syslog.v2
MIT License
523 stars 143 forks source link

Add bounds checks to rfc5424 parser #62

Closed libc closed 5 years ago

libc commented 5 years ago

Hello,

We're streaming logs via syslog from our CDN provider, and sometimes the server crashes with "out of range panic":

panic: runtime error: index out of range

goroutine 243 [running]:
gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424.(*Parser).parseTimestamp(0xc000704c00, 0x1, 0x0, 0x0, 0x0, 0x0)
        /go/pkg/mod/gopkg.in/mcuadros/go-syslog.v2@v2.2.1/internal/syslogparser/rfc5424/rfc5424.go:196 +0x39d
gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424.(*Parser).parseHeader(0xc000704c00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /go/pkg/mod/gopkg.in/mcuadros/go-syslog.v2@v2.2.1/internal/syslogparser/rfc5424/rfc5424.go:141 +0x107
gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser/rfc5424.(*Parser).Parse(0xc000704c00, 0xc000b58900, 0xc000704c00)
        /go/pkg/mod/gopkg.in/mcuadros/go-syslog.v2@v2.2.1/internal/syslogparser/rfc5424/rfc5424.go:85 +0x71
gopkg.in/mcuadros/go-syslog%2ev2.(*Server).parser(0xc00014e580, 0xc00080e5a8, 0x7, 0x8, 0xc0002a0be0, 0x10, 0x0, 0x0)
        /go/pkg/mod/gopkg.in/mcuadros/go-syslog.v2@v2.2.1/server.go:254 +0x8e
gopkg.in/mcuadros/go-syslog%2ev2.(*Server).scan(0xc00014e580, 0xc0003825c0, 0xc0002a0be0, 0x10, 0x0, 0x0)
        /go/pkg/mod/gopkg.in/mcuadros/go-syslog.v2@v2.2.1/server.go:242 +0xd8
created by gopkg.in/mcuadros/go-syslog%2ev2.(*Server).goScanConnection
        /go/pkg/mod/gopkg.in/mcuadros/go-syslog.v2@v2.2.1/server.go:227 +0x272

It could be caused by our network, not CDN, but in any case we would like to prevent crashing and gracefully error out.

I fixed the panic mentioned above with an explicit test TestParseTimestamp_Empty. Then I added a test that feeds truncated messages to the panic and prevented corresponding panics.

Please let me know if anything needs to be changed.