Skip to content

Commit ce8216a

Browse files
authored
filebeat/input/udp - Fix panic in linux input metrics (#35068)
A panic occurred while parsing /proc/net/udp and reaching the final empty line. This occurred on Linux when the desired socket was not found in the list. Fixes #35064
1 parent ea1293f commit ce8216a

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
106106
- Fix the ignore_inactive option being ignored in Filebeat's filestream input {pull}34770[34770]
107107
- Fix TestMultiEventForEOFRetryHandlerInput unit test of CometD input {pull}34903[34903]
108108
- Add input instance id to request trace filename for httpjson and cel inputs {pull}35024[35024]
109+
- Fix panic in UDP input on Linux when collecting socket metrics from OS. {issue}35064[35064]
109110

110111
*Heartbeat*
111112

filebeat/input/udp/input.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (m *inputMetrics) poll(addr []string, each time.Duration, log *logp.Logger)
239239
for {
240240
select {
241241
case <-t.C:
242-
rx, drops, err := procNetUDP(addr)
242+
rx, drops, err := procNetUDP("/proc/net/udp", addr)
243243
if err != nil {
244244
log.Warnf("failed to get udp stats from /proc: %v", err)
245245
continue
@@ -257,8 +257,8 @@ func (m *inputMetrics) poll(addr []string, each time.Duration, log *logp.Logger)
257257
// for the socket on the provided address formatted in hex, xxxxxxxx:xxxx.
258258
// This function is only useful on linux due to its dependence on the /proc
259259
// filesystem, but is kept in this file for simplicity.
260-
func procNetUDP(addr []string) (rx, drops int64, err error) {
261-
b, err := os.ReadFile("/proc/net/udp")
260+
func procNetUDP(path string, addr []string) (rx, drops int64, err error) {
261+
b, err := os.ReadFile(path)
262262
if err != nil {
263263
return 0, 0, err
264264
}
@@ -268,7 +268,7 @@ func procNetUDP(addr []string) (rx, drops int64, err error) {
268268
}
269269
for _, l := range lines[1:] {
270270
f := bytes.Fields(l)
271-
if contains(f[1], addr) {
271+
if len(f) > 12 && contains(f[1], addr) {
272272
_, r, ok := bytes.Cut(f[4], []byte(":"))
273273
if !ok {
274274
return 0, 0, errors.New("no rx_queue field " + string(f[4]))

filebeat/input/udp/input_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package udp
19+
20+
import (
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestProcNetUDP(t *testing.T) {
27+
t.Run("with_match", func(t *testing.T) {
28+
rx, drops, err := procNetUDP("testdata/proc_net_udp.txt", []string{"2508640A:1BBE"})
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
assert.EqualValues(t, 1, rx)
33+
assert.EqualValues(t, 2, drops)
34+
})
35+
36+
t.Run("without_match", func(t *testing.T) {
37+
_, _, err := procNetUDP("testdata/proc_net_udp.txt", []string{"FOO:BAR", "BAR:BAZ"})
38+
if assert.Error(t, err) {
39+
assert.Contains(t, err.Error(), "entry not found")
40+
}
41+
})
42+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
2+
420: 3500007F:0035 00000000:0000 07 00000000:00000000 00:00000000 00000000 101 0 14260 2 0000000000000000 0
3+
435: 2508640A:0044 2208640A:0043 01 00000000:00000000 00:00000000 00000000 0 0 23710 2 0000000000000000 0
4+
476: 2508640A:206D 00000000:0000 07 00000000:00000000 00:00000000 00000000 998 0 25078 2 0000000000000000 0
5+
478: 00000000:006F 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 1564 2 0000000000000000 0
6+
490: 010011AC:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 114 0 104819785 2 0000000000000000 0
7+
490: 2508640A:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 23875 2 0000000000000000 0
8+
490: 0100007F:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 23873 2 0000000000000000 0
9+
490: 00000000:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 23869 2 0000000000000000 0
10+
660: 2508640A:6925 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 105796790 2 0000000000000000 0
11+
676: 2508640A:B135 2208640A:0035 01 00000000:00000000 00:00000000 00000000 0 0 104819411 2 0000000000000000 0
12+
751: 2508640A:5980 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 110038458 2 0000000000000000 0
13+
775: 0100007F:2198 00000000:0000 07 00000000:00000000 00:00000000 00000000 998 0 25079 2 0000000000000000 0
14+
1041: 2508640A:7AA2 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 105793171 2 0000000000000000 0
15+
1324: 2508640A:1BBD 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 104833818 2 0000000000000000 0
16+
1325: 2508640A:1BBE 00000000:0000 07 00000000:00000001 00:00000000 00000000 0 0 104836465 2 0000000000000000 2
17+
1326: 2508640A:1BBF 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 104833801 2 0000000000000000 0
18+
1327: 2508640A:1BC0 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 104833775 2 0000000000000000 0
19+
1570: 2508640A:6CB3 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 104819249 2 0000000000000000 0
20+
1694: 2508640A:D52F 2208640A:0035 01 00000000:00000000 00:00000000 00000000 0 0 112991224 2 0000000000000000 0
21+
1938: 2508640A:5E23 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 104833716 2 0000000000000000 0

0 commit comments

Comments
 (0)