Skip to content

Commit 46c7708

Browse files
committed
Add overflow handling for timestamps and ports
If the input data is too large for the target type log an error and set the target data to 0. The added checking should remove the need for te #nosec G115 comment but is seems the nested dnstap struct is causing issues for gosec, see the linked issue comment for details.
1 parent b0b3dbb commit 46c7708

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

pkg/runner/runner.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -1750,13 +1750,25 @@ func (edm *dnstapMinimiser) newSession(dt *dnstap.Dnstap, msg *dns.Msg, isQuery
17501750
sd := &sessionData{}
17511751

17521752
if dt.Message.QueryPort != nil {
1753-
qp := int32(*dt.Message.QueryPort) // #nosec G115 -- QueryPort is defined as 16-bit number and is used in parquet field with type=INT32, convertedType=UINT_16
1754-
sd.SourcePort = &qp
1753+
if *dt.Message.QueryPort > math.MaxInt32 {
1754+
edm.log.Error("dt.Message.QueryPort is too large for int32, setting port 0")
1755+
var qp int32
1756+
sd.SourcePort = &qp
1757+
} else {
1758+
qp := int32(*dt.Message.QueryPort) // #nosec G115 -- QueryPort is defined as 16-bit number and is used in parquet field with type=INT32, convertedType=UINT_16, https://github.com/securego/gosec/issues/1212#issuecomment-2739574884
1759+
sd.SourcePort = &qp
1760+
}
17551761
}
17561762

17571763
if dt.Message.ResponsePort != nil {
1758-
rp := int32(*dt.Message.ResponsePort) // #nosec G115 -- ResponsePort is defined as 16-bit number and is used in parquet field with type=INT32, convertedType=UINT_16
1759-
sd.DestPort = &rp
1764+
if *dt.Message.ResponsePort > math.MaxInt32 {
1765+
edm.log.Error("dt.Message.ResponsePort is too large for int32, setting port 0")
1766+
var rp int32
1767+
sd.DestPort = &rp
1768+
} else {
1769+
rp := int32(*dt.Message.ResponsePort) // #nosec G115 -- ResponsePort is defined as 16-bit number and is used in parquet field with type=INT32, convertedType=UINT_16, https://github.com/securego/gosec/issues/1212#issuecomment-2739574884
1770+
sd.DestPort = &rp
1771+
}
17601772
}
17611773

17621774
edm.setSessionLabels(dns.SplitDomainName(msg.Question[0].Name), labelLimit, sd)
@@ -2050,14 +2062,24 @@ func (edm *dnstapMinimiser) parsePacket(dt *dnstap.Dnstap, isQuery bool) (*dns.M
20502062
edm.log.Error("unable to unpack query message", "error", err, "query_address", queryAddress, "response_address", responseAddress)
20512063
msg = nil
20522064
}
2053-
t = time.Unix(int64(*dt.Message.QueryTimeSec), int64(*dt.Message.QueryTimeNsec)).UTC() // #nosec G115 -- Overflowing the int64 would result in interesting timestamps but not much else
2065+
if *dt.Message.QueryTimeSec > math.MaxInt64 {
2066+
edm.log.Error("dt.Message.QueryTimeSec is too large for int64, setting time to 0")
2067+
*dt.Message.QueryTimeSec = 0
2068+
*dt.Message.QueryTimeNsec = 0
2069+
}
2070+
t = time.Unix(int64(*dt.Message.QueryTimeSec), int64(*dt.Message.QueryTimeNsec)).UTC() // #nosec G115 -- Will be zeroed out above if too large, https://github.com/securego/gosec/issues/1212#issuecomment-2739574884
20542071
} else {
20552072
err = msg.Unpack(dt.Message.ResponseMessage)
20562073
if err != nil {
20572074
edm.log.Error("unable to unpack response message", "error", err, "query_address", queryAddress, "response_address", responseAddress)
20582075
msg = nil
20592076
}
2060-
t = time.Unix(int64(*dt.Message.ResponseTimeSec), int64(*dt.Message.ResponseTimeNsec)).UTC() // #nosec G115 -- Overflowing the int64 would result in interesting timestamps but not much else
2077+
if *dt.Message.ResponseTimeSec > math.MaxInt64 {
2078+
edm.log.Error("dt.Message.ResponseTimeSec is too large for int64, setting time to 0")
2079+
*dt.Message.ResponseTimeSec = 0
2080+
*dt.Message.ResponseTimeNsec = 0
2081+
}
2082+
t = time.Unix(int64(*dt.Message.ResponseTimeSec), int64(*dt.Message.ResponseTimeNsec)).UTC() // #nosec G115 -- Will be zeroed out above if too large, https://github.com/securego/gosec/issues/1212#issuecomment-2739574884
20612083
}
20622084

20632085
return msg, t

0 commit comments

Comments
 (0)