From 307e95c835f866e1b0edea8b88dd8bc137d504c1 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 26 Sep 2024 08:32:39 -0700 Subject: [PATCH] Reverts elastic/beats#40684 (#41009) We're reverting because Elastic Agent CI has been failing and we've narrowed it down to the type assertion failing here and not checking `ok` right after: https://github.com/elastic/beats/blob/138e43cad7eda93c1414641682056b6c88efcf1d/winlogbeat/sys/strings_windows.go#L31-L32 Specifically, when integration tests for Elastic Agent run on its CI Windows hosts, we are seeing this failure in the log: ``` panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x0 pc=0x284f4bf] goroutine 1 [running]: golang.org/x/text/encoding/charmap.(*Charmap).ID(0x0) /go/pkg/mod/golang.org/x/text@v0.18.0/encoding/charmap/charmap.go:111 +0x1f github.com/elastic/beats/v7/winlogbeat/sys.init.0() /go/src/github.com/elastic/beats/winlogbeat/sys/strings_windows.go:32 +0x10c ``` --- CHANGELOG.next.asciidoc | 2 - winlogbeat/sys/strings.go | 24 ---------- winlogbeat/sys/strings_test.go | 6 --- winlogbeat/sys/strings_windows.go | 45 ------------------- winlogbeat/sys/wineventlog/syscall_windows.go | 20 +-------- winlogbeat/sys/zsyscall_windows.go | 42 ----------------- winlogbeat/sys/zsyscall_windows_test.go | 42 ----------------- 7 files changed, 1 insertion(+), 180 deletions(-) delete mode 100644 winlogbeat/sys/strings_windows.go delete mode 100644 winlogbeat/sys/zsyscall_windows.go delete mode 100644 winlogbeat/sys/zsyscall_windows_test.go diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 33df59fcce3..6001cb3e528 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -363,8 +363,6 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff] *Winlogbeat* -- Add handling for missing `EvtVarType`s in experimental api. {issue}19337[19337] {pull}40684[40684] - *Functionbeat* diff --git a/winlogbeat/sys/strings.go b/winlogbeat/sys/strings.go index 34325c32f9d..d542277f17b 100644 --- a/winlogbeat/sys/strings.go +++ b/winlogbeat/sys/strings.go @@ -44,27 +44,3 @@ func RemoveWindowsLineEndings(s string) string { s = strings.Replace(s, "\r\n", "\n", -1) return strings.TrimRight(s, "\n") } - -// BinaryToString converts a binary field which is encoded in hexadecimal -// to its string representation. This is equivalent to hex.EncodeToString -// but its output is in uppercase to be equivalent to the windows -// XML formatting of this fields. -func BinaryToString(bin []byte) string { - if len(bin) == 0 { - return "" - } - - const hexTable = "0123456789ABCDEF" - - size := len(bin) * 2 - buffer := make([]byte, size) - - j := 0 - for _, v := range bin { - buffer[j] = hexTable[v>>4] - buffer[j+1] = hexTable[v&0x0f] - j += 2 - } - - return string(buffer) -} diff --git a/winlogbeat/sys/strings_test.go b/winlogbeat/sys/strings_test.go index 53f2d8ae632..0771b7b3cff 100644 --- a/winlogbeat/sys/strings_test.go +++ b/winlogbeat/sys/strings_test.go @@ -36,12 +36,6 @@ func TestUTF16BytesToString(t *testing.T) { assert.Equal(t, input, output) } -func TestMakeDisplayableBinaryString(t *testing.T) { - input := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF} - output := BinaryToString(input) - assert.Equal(t, "0123456789ABCDEF", output) -} - func BenchmarkUTF16BytesToString(b *testing.B) { utf16Bytes := common.StringToUTF16Bytes("A logon was attempted using explicit credentials.") diff --git a/winlogbeat/sys/strings_windows.go b/winlogbeat/sys/strings_windows.go deleted file mode 100644 index c99417594e2..00000000000 --- a/winlogbeat/sys/strings_windows.go +++ /dev/null @@ -1,45 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package sys - -import ( - "golang.org/x/sys/windows" - "golang.org/x/text/encoding" - "golang.org/x/text/encoding/charmap" -) - -var ansiDecoder *encoding.Decoder - -func init() { - ansiCP := windows.GetACP() - for _, enc := range charmap.All { - cm, ok := enc.(*charmap.Charmap) - cmID, _ := cm.ID() - if !ok || uint32(cmID) != ansiCP { - continue - } - ansiDecoder = cm.NewDecoder() - return - } - ansiDecoder = charmap.Windows1250.NewDecoder() -} - -func ANSIBytesToString(enc []byte) (string, error) { - out, err := ansiDecoder.Bytes(enc) - return string(out), err -} diff --git a/winlogbeat/sys/wineventlog/syscall_windows.go b/winlogbeat/sys/wineventlog/syscall_windows.go index 2dde1329e6b..6e03a1969cf 100644 --- a/winlogbeat/sys/wineventlog/syscall_windows.go +++ b/winlogbeat/sys/wineventlog/syscall_windows.go @@ -442,16 +442,11 @@ func (v EvtVariant) Data(buf []byte) (interface{}, error) { switch typ { case EvtVarTypeNull: return nil, nil - case EvtVarTypeString, EvtVarTypeEvtXml: + case EvtVarTypeString: addr := unsafe.Pointer(&buf[0]) offset := v.ValueAsUintPtr() - uintptr(addr) s, err := sys.UTF16BytesToString(buf[offset:]) return s, err - case EvtVarTypeAnsiString: - addr := unsafe.Pointer(&buf[0]) - offset := v.ValueAsUintPtr() - uintptr(addr) - s, err := sys.ANSIBytesToString(buf[offset:]) - return s, err case EvtVarTypeSByte: return int8(v.ValueAsUint8()), nil case EvtVarTypeByte: @@ -481,28 +476,15 @@ func (v EvtVariant) Data(buf []byte) (interface{}, error) { return false, nil } return true, nil - case EvtVarTypeBinary: - addr := unsafe.Pointer(&buf[0]) - offset := v.ValueAsUintPtr() - uintptr(addr) - return sys.BinaryToString(buf[offset:]), nil case EvtVarTypeGuid: addr := unsafe.Pointer(&buf[0]) offset := v.ValueAsUintPtr() - uintptr(addr) guid := (*windows.GUID)(unsafe.Pointer(&buf[offset])) copy := *guid return copy, nil - case EvtVarTypeSizeT: - return v.ValueAsUintPtr(), nil case EvtVarTypeFileTime: ft := (*windows.Filetime)(unsafe.Pointer(&v.Value)) return time.Unix(0, ft.Nanoseconds()).UTC(), nil - case EvtVarTypeSysTime: - st := (*windows.Systemtime)(unsafe.Pointer(&v.Value)) - var ft windows.Filetime - if err := sys.SystemTimeToFileTime(st, &ft); err != nil { - return nil, err - } - return time.Unix(0, ft.Nanoseconds()).UTC(), nil case EvtVarTypeSid: addr := unsafe.Pointer(&buf[0]) offset := v.ValueAsUintPtr() - uintptr(addr) diff --git a/winlogbeat/sys/zsyscall_windows.go b/winlogbeat/sys/zsyscall_windows.go deleted file mode 100644 index 00625824776..00000000000 --- a/winlogbeat/sys/zsyscall_windows.go +++ /dev/null @@ -1,42 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package sys - -import ( - "fmt" - "syscall" - "unsafe" - - "golang.org/x/sys/windows" -) - -var _ unsafe.Pointer - -var ( - modkernel = windows.NewLazySystemDLL("Kernel32.dll") - - procSystemTimeToFileTime = modkernel.NewProc("SystemTimeToFileTime") -) - -func SystemTimeToFileTime(systemTime *windows.Systemtime, fileTime *windows.Filetime) error { - r1, _, err := syscall.SyscallN(procSystemTimeToFileTime.Addr(), uintptr(unsafe.Pointer(systemTime)), uintptr(unsafe.Pointer(fileTime))) - if r1 == 0 { - return fmt.Errorf("error converting system time to file time: %w", err) - } - return nil -} diff --git a/winlogbeat/sys/zsyscall_windows_test.go b/winlogbeat/sys/zsyscall_windows_test.go deleted file mode 100644 index b7bcbe0009a..00000000000 --- a/winlogbeat/sys/zsyscall_windows_test.go +++ /dev/null @@ -1,42 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package sys - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" - "golang.org/x/sys/windows" -) - -func TestSystemTimeToFileTime(t *testing.T) { - ts := time.Date( - 2024, time.Month(9), 3, - 0, 0, 0, 0, time.UTC).UnixNano() - st := windows.Systemtime{ - Year: 2024, - Month: 9, - Day: 3, - } - var ft windows.Filetime - if err := SystemTimeToFileTime(&st, &ft); err != nil { - t.Fatal(err) - } - assert.Equal(t, ts, ft.Nanoseconds()) -}