Skip to content

Commit

Permalink
[7.17](backport #32519) winlogbeat: fix testing on windows 11 and re-…
Browse files Browse the repository at this point in the history
…enable (#33074)

This change is derived from the changes listed but is only a partial backport.
Tests for windows 11 have not been enabled, and linting fixes are not applied.
Only buffer size error in the call to _EvtFormatMessage and short-circuiting
EventMetadataIterator.Next when no handle is available are included in the
patch.

* winlogbeat: fix testing on windows 11 and re-enable (#32519)

* fix invalid write
* remove lint and provide errno for failure
* squelch error when template not available
* enable windows-11 testing

(cherry picked from commit fe37716)

# Conflicts:
#	winlogbeat/Jenkinsfile.yml
#	winlogbeat/sys/wineventlog/publisher_metadata.go
#	winlogbeat/sys/wineventlog/syscall_windows.go
#	x-pack/winlogbeat/Jenkinsfile.yml

* fix conflicts and disable windows 11 testing

* revert error handling changes

* remove irrelevant changelog lines

Co-authored-by: Dan Kortschak <[email protected]>
Co-authored-by: Dan Kortschak <[email protected]>
  • Loading branch information
3 people authored Sep 14, 2022
1 parent e4c3db2 commit e3cfd0e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Winlogbeat*

- Fix fatal invalid memory write on Windows 11. {issue}32469[32469] {pull}32519[32519]
- Fix handling of event formatting when no metadata is available on Windows 11. {issue}32468[32468] {pull}32519[32519]
- Reduce severity of message salvage failure logging. {pull}32697[32697]

*Functionbeat*
Expand Down
6 changes: 5 additions & 1 deletion winlogbeat/sys/wineventlog/format_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ func evtFormatMessage(metadataHandle EvtHandle, eventHandle EvtHandle, messageID
// Get a buffer from the pool and adjust its length.
bb := sys.NewPooledByteBuffer()
defer bb.Free()
// The documentation for EventFormatMessage specifies that the buffer is
// requested "in characters", and the buffer itself is LPWSTR, meaning the
// characters are WCHAR so double the value.
// https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtformatmessage
bb.Reserve(int(bufferUsed * 2))

err = _EvtFormatMessage(metadataHandle, eventHandle, messageID, valuesCount, valuesPtr, messageFlag, uint32(bb.Len()), bb.PtrAt(0), &bufferUsed)
err = _EvtFormatMessage(metadataHandle, eventHandle, messageID, valuesCount, valuesPtr, messageFlag, bufferUsed, bb.PtrAt(0), &bufferUsed)
switch err { //nolint:errorlint // This is an errno or nil.
case nil: // OK

Expand Down
4 changes: 4 additions & 0 deletions winlogbeat/sys/wineventlog/publisher_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ func (itr *EventMetadataIterator) Close() error {
// no more items or an error occurred. You should call Err() to check for an
// error.
func (itr *EventMetadataIterator) Next() bool {
if itr.eventMetadataEnumHandle == 0 {
// This is only the case when we could not find the event metadata file.
return false
}
// Close existing handle.
itr.currentEvent.Close()

Expand Down
2 changes: 1 addition & 1 deletion winlogbeat/sys/wineventlog/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func (v EvtVariant) Data(buf []byte) (interface{}, error) {
case EvtVarTypeEvtHandle:
return EvtHandle(v.ValueAsUintPtr()), nil
default:
return nil, errors.Errorf("unhandled type: %d", typ)
return nil, fmt.Errorf("unhandled type: %d", typ)
}
}

Expand Down
1 change: 0 additions & 1 deletion winlogbeat/sys/wineventlog/wineventlog_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ func renderXML(eventHandle EvtHandle, flag EvtRenderFlag, renderBuf []byte, out
}

if int(bufferUsed) > len(renderBuf) {
//nolint:stylecheck // These are proper nouns.
return fmt.Errorf("Windows EvtRender reported that wrote %d bytes "+
"to the buffer, but the buffer can only hold %d bytes",
bufferUsed, len(renderBuf))
Expand Down

0 comments on commit e3cfd0e

Please sign in to comment.