Skip to content

limit size of gopacket.SerializeBuffer #33338

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 1 addition & 2 deletions pkg/networkpath/traceroute/icmp/tcp_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func NewICMPTCPParser() Parser {
icmpParser := &TCPParser{}
icmpParser.packetParser = gopacket.NewDecodingLayerParser(layers.LayerTypeICMPv4, &icmpParser.icmpLayer, &icmpParser.innerPayload)
icmpParser.innerPacketParser = gopacket.NewDecodingLayerParser(layers.LayerTypeIPv4, &icmpParser.innerIPLayer, &icmpParser.innerTCPLayer)
// TODO: can we ignore unsupported layers?
//icmpParser.packetParser.IgnoreUnsupported = true
icmpParser.packetParser.IgnoreUnsupported = true
return icmpParser
}

Expand Down
1 change: 0 additions & 1 deletion pkg/networkpath/traceroute/icmp/udp_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func NewICMPUDPParser() Parser {
icmpParser := &UDPParser{}
icmpParser.packetParser = gopacket.NewDecodingLayerParser(layers.LayerTypeICMPv4, &icmpParser.icmpLayer)
icmpParser.innerPacketParser = gopacket.NewDecodingLayerParser(layers.LayerTypeIPv4, &icmpParser.innerIPLayer, &icmpParser.innerUDPLayer)
// TODO: can we ignore unsupported layers?
icmpParser.packetParser.IgnoreUnsupported = true
return icmpParser
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/networkpath/traceroute/tcp/tcpv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type (

// NewTCPv4 initializes a new TCPv4 traceroute instance
func NewTCPv4(target net.IP, targetPort uint16, numPaths uint16, minTTL uint8, maxTTL uint8, delay time.Duration, timeout time.Duration) *TCPv4 {
buffer := gopacket.NewSerializeBuffer()
buffer := gopacket.NewSerializeBufferExpectedSize(40, 0)

return &TCPv4{
Target: target,
Expand Down Expand Up @@ -67,6 +67,8 @@ func (t *TCPv4) createRawTCPSyn(seqNum uint32, ttl int) (*ipv4.Header, []byte, e
}

func (t *TCPv4) createRawTCPSynBuffer(seqNum uint32, ttl int) (*ipv4.Header, []byte, int, error) {
// if this function is modified in a way that changes the size,
// update the NewSerializeBufferExpectedSize call in NewTCPv4
ipLayer := &layers.IPv4{
Version: 4,
Length: 20,
Expand Down
6 changes: 5 additions & 1 deletion pkg/networkpath/traceroute/udp/udpv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/icmp"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"golang.org/x/net/ipv4"
Expand All @@ -38,7 +39,7 @@ type (
// NewUDPv4 initializes a new UDPv4 traceroute instance
func NewUDPv4(target net.IP, targetPort uint16, numPaths uint16, minTTL uint8, maxTTL uint8, delay time.Duration, timeout time.Duration) *UDPv4 {
icmpParser := icmp.NewICMPUDPParser()
buffer := gopacket.NewSerializeBuffer()
buffer := gopacket.NewSerializeBufferExpectedSize(36, 0)

return &UDPv4{
Target: target,
Expand Down Expand Up @@ -66,6 +67,8 @@ func (u *UDPv4) Close() error {
//
// the nolint:unused is necessary because we don't yet use this outside the Windows implementation
func (u *UDPv4) createRawUDPBuffer(sourceIP net.IP, sourcePort uint16, destIP net.IP, destPort uint16, ttl int) (*ipv4.Header, []byte, uint16, int, error) { //nolint:unused
// if this function is modified in a way that changes the size,
// update the NewSerializeBufferExpectedSize call in NewUDPv4
ipLayer := &layers.IPv4{
Version: 4,
Length: 20,
Expand Down Expand Up @@ -108,6 +111,7 @@ func (u *UDPv4) createRawUDPBuffer(sourceIP net.IP, sourcePort uint16, destIP ne
return nil, nil, 0, 0, fmt.Errorf("failed to serialize packet: %w", err)
}
packet := u.buffer.Bytes()
log.Errorf("UDP Packet Length: %d", len(packet))

var ipHdr ipv4.Header
if err := ipHdr.Parse(packet[:20]); err != nil {
Expand Down
Loading