From 3cb144fcef26f3e31144988702d0687b145b7408 Mon Sep 17 00:00:00 2001 From: Lee Hinman <57081003+leehinman@users.noreply.github.com> Date: Mon, 15 Mar 2021 12:44:19 -0500 Subject: [PATCH] [Auditbeat] btmp offset check (#24515) * auditbeat btmp offset check Add check that saved offset is not larger than the current file size to prevent seeking past the end of file (cherry picked from commit b03a935282bec78b3160e74e66dfe29e2df8ee69) --- CHANGELOG.next.asciidoc | 1 + x-pack/auditbeat/module/system/login/utmp.go | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 9cac757e067..ecf58cbbffc 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -159,6 +159,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - system/socket: Fix dataset using 100% CPU and becoming unresponsive in some scenarios. {pull}19033[19033] {pull}19764[19764] - system/socket: Fixed tracking of long-running connections. {pull}19033[19033] - system/login: Fixed offset reset on inode reuse. {pull}24414[24414] +- system/login: Add additional offset check for utmp files. {pull}24515[24515] *Filebeat* diff --git a/x-pack/auditbeat/module/system/login/utmp.go b/x-pack/auditbeat/module/system/login/utmp.go index 24263d210cd..273229f051a 100644 --- a/x-pack/auditbeat/module/system/login/utmp.go +++ b/x-pack/auditbeat/module/system/login/utmp.go @@ -181,14 +181,14 @@ func (r *UtmpFileReader) readNewInFile(loginRecordC chan<- LoginRecord, errorC c size := utmpFile.Size oldSize := savedUtmpFile.Size - if size < oldSize { + if size < oldSize || utmpFile.Offset > size { // UTMP files are append-only and so this is weird. It might be a sign of // a highly unlikely inode reuse - or of something more nefarious. // Setting isKnownFile to false so we read the whole file from the beginning. isKnownFile = false - r.log.Warnf("Unexpectedly, the file %v is smaller than before (new: %v, old: %v) - reading whole file.", - utmpFile.Path, size, oldSize) + r.log.Warnf("saved size or offset illogical (new=%+v, saved=%+v) - reading whole file.", + utmpFile, savedUtmpFile) } if !isKnownFile && size == 0 { @@ -221,7 +221,7 @@ func (r *UtmpFileReader) readNewInFile(loginRecordC chan<- LoginRecord, errorC c // This will be the usual case, but we do not want to seek with the stored offset // if the saved size is smaller than the current one. - if size >= oldSize { + if size >= oldSize && utmpFile.Offset <= size { _, err = f.Seek(utmpFile.Offset, 0) if err != nil { errorC <- errors.Wrapf(err, "error setting offset %d for file %v", utmpFile.Offset, utmpFile.Path) @@ -230,7 +230,7 @@ func (r *UtmpFileReader) readNewInFile(loginRecordC chan<- LoginRecord, errorC c // If the saved size is smaller than the current one, or the previous Seek failed, // we retry one more time, this time resetting to the beginning of the file. - if size < oldSize || err != nil { + if size < oldSize || utmpFile.Offset > size || err != nil { _, err = f.Seek(0, 0) if err != nil { errorC <- errors.Wrapf(err, "error setting offset 0 for file %v", utmpFile.Path)