Skip to content
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
7 changes: 6 additions & 1 deletion actions/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ func serializeTamperedPacket(
return nil, err
}

return gopacket.NewPacket(buffer.Bytes(), layers[0].LayerType(), gopacket.Default), nil
// NoCopy: the serialize buffer was allocated here and is not reused, so the
// packet may alias it instead of taking a second copy of every byte. Lazy:
// the caller of a tampered packet usually just serializes it back out, so
// eagerly re-decoding every layer is work for nobody.
return gopacket.NewPacket(buffer.Bytes(), layers[0].LayerType(),
gopacket.DecodeOptions{Lazy: true, NoCopy: true}), nil
}
5 changes: 3 additions & 2 deletions actions/tamper_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,9 @@ func tamperIPv4(ip *layers.IPv4, field IPv4Field, values tamperValues) error {
if err := ip.SerializeTo(sb, gopacket.SerializeOptions{}); err != nil {
return err
}
ip.Contents = make([]byte, len(sb.Bytes()))
copy(ip.Contents, sb.Bytes())
// Reuse the existing header buffer rather than allocating a new one per
// packet, as the TCP path above already does.
ip.Contents = append(ip.Contents[:0], sb.Bytes()...)

return nil
}
Expand Down