blob: 606e45d24cf7f5402bbecca393c9e5add868c72b [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301// Copyright 2019 The GoPacket Authors. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style license that can be found
4// in the LICENSE file in the root of the source tree.
5
6package layers
7
8import (
9 "encoding/binary"
10
11 "github.com/google/gopacket"
12)
13
14// FuzzLayer is a fuzz target for the layers package of gopacket
15// A fuzz target is a function processing a binary blob (byte slice)
16// The process here is to interpret this data as a packet, and print the layers contents.
17// The decoding options and the starting layer are encoded in the first bytes.
18// The function returns 1 if this is a valid packet (no error layer)
19func FuzzLayer(data []byte) int {
20 if len(data) < 3 {
21 return 0
22 }
23 // use the first two bytes to choose the top level layer
24 startLayer := binary.BigEndian.Uint16(data[:2])
25 var fuzzOpts = gopacket.DecodeOptions{
26 Lazy: data[2]&0x1 != 0,
27 NoCopy: data[2]&0x2 != 0,
28 SkipDecodeRecovery: data[2]&0x4 != 0,
29 DecodeStreamsAsDatagrams: data[2]&0x8 != 0,
30 }
31 p := gopacket.NewPacket(data[3:], gopacket.LayerType(startLayer), fuzzOpts)
32 for _, l := range p.Layers() {
33 gopacket.LayerString(l)
34 }
35 if p.ErrorLayer() != nil {
36 return 0
37 }
38 return 1
39}