Skip to content

Commit

Permalink
check kafka pkt length before parsing clientid
Browse files Browse the repository at this point in the history
  • Loading branch information
esara committed Jun 6, 2024
1 parent 294a8d9 commit f2639b8
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/internal/ebpf/common/kafka_detect_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (k Operation) String() string {
}
}

const KafaMinLength = 14
const KafkaMinLength = 14

// ProcessKafkaRequest processes a TCP packet and returns error if the packet is not a valid Kafka request.
// Otherwise, return kafka.Info with the processed data.
Expand All @@ -57,7 +57,7 @@ func ProcessPossibleKafkaEvent(pkt []byte, rpkt []byte) (*KafkaInfo, error) {

func ProcessKafkaRequest(pkt []byte) (*KafkaInfo, error) {

Check failure on line 58 in pkg/internal/ebpf/common/kafka_detect_transform.go

View workflow job for this annotation

GitHub Actions / test (1.22)

calculated cyclomatic complexity for function ProcessKafkaRequest is 13, max is 12 (cyclop)
k := &KafkaInfo{}
if len(pkt) < KafaMinLength {
if len(pkt) < KafkaMinLength {
return k, errors.New("packet too short")
}

Expand All @@ -73,8 +73,8 @@ func ProcessKafkaRequest(pkt []byte) (*KafkaInfo, error) {
return k, errors.New("invalid Kafka request header")
}

offset := KafaMinLength
if header.ClientIDSize > 0 {
offset := KafkaMinLength
if header.ClientIDSize > 0 && len(pkt) > KafkaMinLength+int(header.ClientIDSize) {
clientID := pkt[offset : offset+int(header.ClientIDSize)]
if !isValidClientID(clientID, int(header.ClientIDSize)) {
return k, errors.New("invalid client ID")
Expand Down Expand Up @@ -109,7 +109,7 @@ func ProcessKafkaRequest(pkt []byte) (*KafkaInfo, error) {
}

func isValidKafkaHeader(header *Header) bool {
if header.MessageSize < int32(KafaMinLength) || header.APIVersion < 0 {
if header.MessageSize < int32(KafkaMinLength) || header.APIVersion < 0 {
return false
}
switch Operation(header.APIKey) {
Expand Down

0 comments on commit f2639b8

Please sign in to comment.