@@ -10,6 +10,7 @@ package nfdump
10
10
11
11
import (
12
12
"encoding/binary"
13
+ "fmt"
13
14
"net"
14
15
"unsafe"
15
16
)
@@ -45,15 +46,15 @@ type FlowRecordV3 struct {
45
46
}
46
47
47
48
// Extract next flow record from []byte stream
48
- func NewRecord (record []byte ) * FlowRecordV3 {
49
+ func NewRecord (record []byte ) ( * FlowRecordV3 , error ) {
49
50
50
51
offset := 0
51
52
recordType := binary .LittleEndian .Uint16 (record [offset : offset + 2 ])
52
53
recordSize := binary .LittleEndian .Uint16 (record [offset + 2 : offset + 4 ])
53
54
numElements := binary .LittleEndian .Uint16 (record [offset + 4 : offset + 6 ])
54
55
55
56
if recordType != V3Record {
56
- return nil
57
+ return nil , fmt . Errorf ( "Not a v3 record" )
57
58
}
58
59
59
60
flowRecord := new (FlowRecordV3 )
@@ -64,8 +65,15 @@ func NewRecord(record []byte) *FlowRecordV3 {
64
65
flowRecord .recordHeader = (* recordHeaderV3 )(unsafe .Pointer (& raw [0 ]))
65
66
offset = 12
66
67
for i := 0 ; i < int (numElements ); i ++ {
68
+ // fmt.Printf(" . next Element at offset: %d\n", offset)
69
+ if (offset + 4 ) > int (recordSize ) {
70
+ return nil , fmt .Errorf ("Record header boundary check error" )
71
+ }
67
72
elementType := binary .LittleEndian .Uint16 (raw [offset : offset + 2 ])
68
73
elementSize := binary .LittleEndian .Uint16 (raw [offset + 2 : offset + 4 ])
74
+ if (offset + int (elementSize )) > int (recordSize ) {
75
+ return nil , fmt .Errorf ("Record body boundary check error" )
76
+ }
69
77
// fmt.Printf(" . Element type: %d, length: %d\n", elementType, elementSize)
70
78
exOffset := offset + 4
71
79
if elementType < MAXEXTENSIONS {
@@ -96,7 +104,7 @@ func NewRecord(record []byte) *FlowRecordV3 {
96
104
flowRecord .packetInterval = 1
97
105
flowRecord .spaceInterval = 0
98
106
99
- return flowRecord
107
+ return flowRecord , nil
100
108
}
101
109
102
110
// Return generic extension
0 commit comments