ICMP SerializeTo Strips Payload Portion
- Dominant language
- Go
- Stars
- 6.8k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
When serializing an ICMP layer the current function allocates 8 bytes of space which only included the id, seq, and type code when it should be reserving at least 8 bytes and also putting the payload into the serialization.
I am fairly new to Go so the submitted solution may not be as clean as it could be.
Rather I think it should reserve what it needs to by adding the required 8 bytes and the length of the payload. Then add the payload to the byte array.
If the payload is not correctly serialized many types of ICMP messages can be dropped or not properly responded to.
```
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv4) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
bytes, err := b.PrependBytes(len(i.Payload)+8)
if err != nil {
return err
}
i.TypeCode.SerializeTo(bytes)
binary.BigEndian.PutUint16(bytes[4:], i.Id)
binary.BigEndian.PutUint16(bytes[6:], i.Seq)
startIndex := 8
for _, element := range i.Payload {
bytes[startIndex] = byte(uint16(element))
startIndex+= 1
}
if opts.ComputeChecksums {
bytes[2] = 0
bytes[3] = 0
i.Checksum = tcpipChecksum(b.Bytes(), 0)
}
binary.BigEndian.PutUint16(bytes[2:], i.Checksum)
return nil
}
```
Contributor guide
Assessment
This issue has not been assessed yet.