Skip to content

Commit e1ff2c5

Browse files
committed
Add ability to override layer type, and fix DNS issue.
1 parent b14ccee commit e1ff2c5

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

layers/dns.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ loop:
561561
*/
562562

563563
offsetp := int(binary.BigEndian.Uint16(data[index:index+2]) & 0x3fff)
564+
if offsetp > len(data) {
565+
return nil, 0, errors.New("dns offset pointer too high")
566+
}
564567
// This looks a little tricky, but actually isn't. Because of how
565568
// decodeName is written, calling it appends the decoded name to the
566569
// current buffer. We already have the start of the buffer, then, so

layers/dns_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,35 @@ func TestDNSMalformedPacket(t *testing.T) {
365365
t.Errorf("unexpected error message: %v", err)
366366
}
367367
}
368+
369+
// testDNSMalformedPacket2 is the packet:
370+
// 15:14:42.056054 IP 10.77.0.245.53 > 10.1.0.45.38769: 12625 zoneInit YXRRSet- [49833q],[|domain]
371+
// 0x0000: 0055 22af c637 0022 55ac deac 0800 4500 .U"..7."U.....E.
372+
// 0x0010: 0079 3767 4000 3911 f49d 0a4d 00f5 0a01 [email protected]....
373+
// 0x0020: 002d 0035 9771 0065 6377 3151 f057 c2a9 .-.5.q.ecw1Q.W..
374+
// 0x0030: fc6e e86a beb0 f7d4 8599 373e b5f8 9db2 .n.j......7>....
375+
// 0x0040: a399 21a1 9762 def1 def4 f5ab 5675 023e ..!..b......Vu.>
376+
// 0x0050: c9ca 304f 178a c2ad f2fc 677a 0e4c b892 ..0O......gz.L..
377+
// 0x0060: ab71 09bb 1ea4 f7c4 fe47 7a39 868b 29a0 .q.......Gz9..).
378+
// 0x0070: 62c4 d184 5b4e 8817 4cc0 d1d0 d430 11d3 b...[N..L....0..
379+
// 0x0080: d147 543f afc7 1a .GT?...
380+
var testDNSMalformedPacket2 = []byte{
381+
0x00, 0x55, 0x22, 0xaf, 0xc6, 0x37, 0x00, 0x22, 0x55, 0xac, 0xde, 0xac, 0x08, 0x00, 0x45, 0x00,
382+
0x00, 0x79, 0x37, 0x67, 0x40, 0x00, 0x39, 0x11, 0xf4, 0x9d, 0x0a, 0x4d, 0x00, 0xf5, 0x0a, 0x01,
383+
0x00, 0x2d, 0x00, 0x35, 0x97, 0x71, 0x00, 0x65, 0x63, 0x77, 0x31, 0x51, 0xf0, 0x57, 0xc2, 0xa9,
384+
0xfc, 0x6e, 0xe8, 0x6a, 0xbe, 0xb0, 0xf7, 0xd4, 0x85, 0x99, 0x37, 0x3e, 0xb5, 0xf8, 0x9d, 0xb2,
385+
0xa3, 0x99, 0x21, 0xa1, 0x97, 0x62, 0xde, 0xf1, 0xde, 0xf4, 0xf5, 0xab, 0x56, 0x75, 0x02, 0x3e,
386+
0xc9, 0xca, 0x30, 0x4f, 0x17, 0x8a, 0xc2, 0xad, 0xf2, 0xfc, 0x67, 0x7a, 0x0e, 0x4c, 0xb8, 0x92,
387+
0xab, 0x71, 0x09, 0xbb, 0x1e, 0xa4, 0xf7, 0xc4, 0xfe, 0x47, 0x7a, 0x39, 0x86, 0x8b, 0x29, 0xa0,
388+
0x62, 0xc4, 0xd1, 0x84, 0x5b, 0x4e, 0x88, 0x17, 0x4c, 0xc0, 0xd1, 0xd0, 0xd4, 0x30, 0x11, 0xd3,
389+
0xd1, 0x47, 0x54, 0x3f, 0xaf, 0xc7, 0x1a,
390+
}
391+
392+
func TestDNSMalformedPacket2(t *testing.T) {
393+
p := gopacket.NewPacket(testDNSMalformedPacket2, LinkTypeEthernet, testDecodeOptions)
394+
if errLayer := p.ErrorLayer(); errLayer == nil {
395+
t.Error("No error layer on invalid DNS name")
396+
} else if err := errLayer.Error(); !strings.Contains(err.Error(), "offset pointer too high") {
397+
t.Errorf("unexpected error message: %v", err)
398+
}
399+
}

layertype.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,24 @@ func RegisterLayerType(num int, meta LayerTypeMetadata) LayerType {
5555
if ltMeta[num].inUse {
5656
panic("Layer type already exists")
5757
}
58+
} else {
59+
if ltMetaMap[LayerType(num)].inUse {
60+
panic("Layer type already exists")
61+
}
62+
}
63+
return OverrideLayerType(num, meta)
64+
}
65+
66+
// OverrideLayerType acts like RegisterLayerType, except that if the layer type
67+
// has already been registered, it overrides the metadata with the passed-in
68+
// metadata intead of panicing.
69+
func OverrideLayerType(num int, meta LayerTypeMetadata) LayerType {
70+
if 0 <= num && num < maxLayerType {
5871
ltMeta[num] = layerTypeMetadata{
5972
inUse: true,
6073
LayerTypeMetadata: meta,
6174
}
6275
} else {
63-
if ltMetaMap[LayerType(num)].inUse {
64-
panic("Layer type already exists")
65-
}
6676
ltMetaMap[LayerType(num)] = layerTypeMetadata{
6777
inUse: true,
6878
LayerTypeMetadata: meta,

0 commit comments

Comments
 (0)