Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(events): fix dns events slice out of bounds issues #3548

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions pkg/events/derive/net_packet_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,16 @@ func eventToProtoDNS(event *trace.Event) (*netPair, *trace.ProtoDNS, error) {
func convertProtoDNSQuestionToDnsRequest(
questions []trace.ProtoDNSQuestion,
) []trace.DnsQueryData {
var requests []trace.DnsQueryData
requests := make([]trace.DnsQueryData, 0, len(questions))

for _, question := range questions {
requests = append(requests, trace.DnsQueryData{
Query: question.Name,
QueryType: question.Type,
QueryClass: question.Class,
})
requests = append(requests,
trace.DnsQueryData{
Query: question.Name,
QueryType: question.Type,
QueryClass: question.Class,
},
)
}

return requests
Expand All @@ -203,7 +205,7 @@ func convertProtoDNSResourceRecordToDnsResponse(
dnsQueryData trace.DnsQueryData,
dnsResourceRecord []trace.ProtoDNSResourceRecord,
) []trace.DnsResponseData {
var dnsAnswers []trace.DnsAnswer
dnsAnswers := make([]trace.DnsAnswer, 0, len(dnsResourceRecord))

for _, record := range dnsResourceRecord {
var dnsAnswer trace.DnsAnswer
Expand All @@ -229,6 +231,7 @@ func convertProtoDNSResourceRecordToDnsResponse(

dnsAnswer.Type = record.Type
dnsAnswer.Ttl = record.TTL

dnsAnswers = append(dnsAnswers, dnsAnswer)
}

Expand Down Expand Up @@ -262,31 +265,33 @@ func copyDNSToProtoDNS(l7 *layers.DNS, proto *trace.ProtoDNS) {
proto.ARCount = l7.ARCount

// process all existing questions (if any)
proto.Questions = make([]trace.ProtoDNSQuestion, len(l7.Questions))
for i, j := range l7.Questions {
proto.Questions[i] = trace.ProtoDNSQuestion{
Name: string(j.Name),
Type: j.Type.String(),
Class: j.Class.String(),
}
proto.Questions = make([]trace.ProtoDNSQuestion, 0, len(l7.Questions))
for _, j := range l7.Questions {
proto.Questions = append(proto.Questions,
trace.ProtoDNSQuestion{
Name: string(j.Name),
Type: j.Type.String(),
Class: j.Class.String(),
},
)
}

// process all existing answers (if any)
proto.Answers = make([]trace.ProtoDNSResourceRecord, len(l7.Answers))
for i, j := range l7.Answers {
proto.Answers[i] = newProtoDNSResourceRecord(j)
proto.Answers = make([]trace.ProtoDNSResourceRecord, 0, len(l7.Answers))
for _, j := range l7.Answers {
proto.Answers = append(proto.Answers, newProtoDNSResourceRecord(j))
}

// process all existing authorities (if any)
proto.Authorities = make([]trace.ProtoDNSResourceRecord, len(l7.Authorities))
for i, j := range l7.Authorities {
proto.Authorities[i] = newProtoDNSResourceRecord(j)
proto.Authorities = make([]trace.ProtoDNSResourceRecord, 0, len(l7.Authorities))
for _, j := range l7.Authorities {
proto.Authorities = append(proto.Authorities, newProtoDNSResourceRecord(j))
}

// process all existing additionals (if any)
proto.Additionals = make([]trace.ProtoDNSResourceRecord, len(l7.Additionals))
for i, j := range l7.Additionals {
proto.Additionals[i] = newProtoDNSResourceRecord(j)
proto.Additionals = make([]trace.ProtoDNSResourceRecord, 0, len(l7.Additionals))
for _, j := range l7.Additionals {
proto.Additionals = append(proto.Additionals, newProtoDNSResourceRecord(j))
}
}

Expand Down Expand Up @@ -341,13 +346,15 @@ func newProtoDNSResourceRecord(j layers.DNSResourceRecord) trace.ProtoDNSResourc
// helpers

func convertArrayOfDNSOPT(given []layers.DNSOPT) []trace.ProtoDNSOPT {
res := make([]trace.ProtoDNSOPT, len(given))

for i, j := range given {
res[i] = trace.ProtoDNSOPT{
Code: j.Code.String(),
Data: string(j.Data),
}
res := make([]trace.ProtoDNSOPT, 0, len(given))

for _, j := range given {
res = append(res,
trace.ProtoDNSOPT{
Code: j.Code.String(),
Data: string(j.Data),
},
)
}

return res
Expand Down
2 changes: 1 addition & 1 deletion pkg/events/derive/net_packet_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func boolToUint8(b bool) uint8 {
}

func convertArrayOfBytes(given [][]byte) []string {
var res []string
res := make([]string, 0, len(given))

for _, i := range given {
res = append(res, string(i))
Expand Down