Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | // 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 | |
| 6 | package layers |
| 7 | |
| 8 | import ( |
| 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) |
| 19 | func 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 | } |