Repository: pion/dtls (go)

Issue:
Slice bounds out of range
<!--
  Come join our community on Slack https://pion.ly/slack for quicker support and more resources

  We also provide a Knowledge Base https://pion.ly/knowledge-base that covers Pion and general WebRTC
  
  We maintain a FAQ of common issues and questions https://pion.ly/knowledge-base/pion-basics/faq/

 Please answer these questions before submitting your issue. Thanks!
-->

### Your environment.
  - Version: v1.5.0
  - Browser: Firefox 68.0.1
  - Other Information - *stacktraces, related issues, suggestions how to fix, links for us to have context*
```
panic: runtime error: slice bounds out of range

goroutine 1490 [running]:
github.com/pion/dtls.unpackDatagram(0xc0006a0000, 0x2000, 0x2000, 0x2000, 0x2000, 0x0, 0x0, 0x0)
        /home/paul/go/pkg/mod/github.com/pion/dtls@v1.5.0/record_layer.go:84 +0x227
github.com/pion/dtls.(*Conn).inboundLoop(0xc000091b80)
        /home/paul/go/pkg/mod/github.com/pion/dtls@v1.5.0/conn.go:353 +0x150
created by github.com/pion/dtls.createConn
        /home/paul/go/pkg/mod/github.com/pion/dtls@v1.5.0/conn.go:170 +0x619
```

### What did you do?
It looks like it just occurs when a peer connects. I can't cause this to reproduce, but it does seem to happen once in a while. It could either be caused by a Firefox client or a client using the NPM wrtc library (latest ver.)

### What did you expect?
No panic.

### What happened?
Panics

Pull request description:
Add length check to unpackDatagram
Fixes #87

Patch:
diff --git a/record_layer.go b/record_layer.go
index f51e2af..7bd2932 100644
--- a/record_layer.go
+++ b/record_layer.go
@@ -81,6 +81,10 @@ func unpackDatagram(buf []byte) ([][]byte, error) {
 		}
 
 		pktLen := (recordLayerHeaderSize + int(binary.BigEndian.Uint16(buf[offset+11:])))
+		if offset+pktLen > len(buf) {
+			return nil, errLengthMismatch
+		}
+
 		out = append(out, buf[offset:offset+pktLen])
 		offset += pktLen
 	}


Test patch:
diff --git a/record_layer_test.go b/record_layer_test.go
index 049a6bd..f838aa8 100644
--- a/record_layer_test.go
+++ b/record_layer_test.go
@@ -30,12 +30,16 @@ func TestUDPDecode(t *testing.T) {
 				{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x01},
 			},
 		},
-
 		{
 			Name:      "Invalid packet length",
 			Data:      []byte{0x14, 0xfe},
 			WantError: errDTLSPacketInvalidLength,
 		},
+		{
+			Name:      "Packet declared invalid length",
+			Data:      []byte{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0xFF, 0x01},
+			WantError: errLengthMismatch,
+		},
 	} {
 		dtlsPkts, err := unpackDatagram(test.Data)
 		if err != test.WantError {