Skip to content

Commit 6b62f0a

Browse files
committed
Detect MTU overflow.
1 parent 0f7e8cc commit 6b62f0a

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ require (
2121
github.com/pion/interceptor v0.1.37
2222
github.com/pion/rtp v1.8.9
2323
github.com/pion/sdp/v3 v3.0.9
24-
github.com/pion/webrtc/v4 v4.0.4
24+
github.com/pion/srtp/v3 v3.0.4
25+
github.com/pion/webrtc/v4 v4.0.5
2526
github.com/pkg/errors v0.9.1
2627
github.com/prometheus/client_golang v1.20.5
2728
github.com/sirupsen/logrus v1.9.3
@@ -96,7 +97,6 @@ require (
9697
github.com/pion/randutil v0.1.0 // indirect
9798
github.com/pion/rtcp v1.2.14 // indirect
9899
github.com/pion/sctp v1.8.34 // indirect
99-
github.com/pion/srtp/v3 v3.0.4 // indirect
100100
github.com/pion/stun/v3 v3.0.0 // indirect
101101
github.com/pion/transport/v3 v3.0.7 // indirect
102102
github.com/pion/turn/v4 v4.0.0 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ github.com/pion/transport/v3 v3.0.7 h1:iRbMH05BzSNwhILHoBoAPxoB9xQgOaJk+591KC9P1
192192
github.com/pion/transport/v3 v3.0.7/go.mod h1:YleKiTZ4vqNxVwh77Z0zytYi7rXHl7j6uPLGhhz9rwo=
193193
github.com/pion/turn/v4 v4.0.0 h1:qxplo3Rxa9Yg1xXDxxH8xaqcyGUtbHYw4QSCvmFWvhM=
194194
github.com/pion/turn/v4 v4.0.0/go.mod h1:MuPDkm15nYSklKpN8vWJ9W2M0PlyQZqYt1McGuxG7mA=
195-
github.com/pion/webrtc/v4 v4.0.4 h1:X+gkoBLKDsR6FliKKQ/VXGBjnMR3yOPcyXEPt3z7Ep0=
196-
github.com/pion/webrtc/v4 v4.0.4/go.mod h1:LvP8Np5b/sM0uyJIcUPvJcCvhtjHxJwzh2H2PYzE6cQ=
195+
github.com/pion/webrtc/v4 v4.0.5 h1:8cVPojcv3cQTwVga2vF1rzCNvkiEimnYdCCG7yF317I=
196+
github.com/pion/webrtc/v4 v4.0.5/go.mod h1:LvP8Np5b/sM0uyJIcUPvJcCvhtjHxJwzh2H2PYzE6cQ=
197197
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
198198
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
199199
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

pkg/media/rtp/conn.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ import (
2323

2424
"github.com/frostbyte73/core"
2525
"github.com/pion/rtp"
26+
27+
"github.com/livekit/protocol/logger"
2628
)
2729

2830
var _ Writer = (*Conn)(nil)
2931

3032
type ConnConfig struct {
33+
Log logger.Logger
3134
MediaTimeoutInitial time.Duration
3235
MediaTimeout time.Duration
3336
TimeoutCallback func()
@@ -41,14 +44,18 @@ func NewConnWith(conn UDPConn, conf *ConnConfig) *Conn {
4144
if conf == nil {
4245
conf = &ConnConfig{}
4346
}
47+
if conf.Log == nil {
48+
conf.Log = logger.GetLogger()
49+
}
4450
if conf.MediaTimeoutInitial <= 0 {
4551
conf.MediaTimeoutInitial = 30 * time.Second
4652
}
4753
if conf.MediaTimeout <= 0 {
4854
conf.MediaTimeout = 15 * time.Second
4955
}
5056
c := &Conn{
51-
readBuf: make([]byte, 1500), // MTU
57+
log: conf.Log,
58+
readBuf: make([]byte, MTUSize+4), // larger buffer to detect overflow
5259
received: make(chan struct{}),
5360
conn: conn,
5461
timeout: conf.MediaTimeout,
@@ -69,6 +76,7 @@ type UDPConn interface {
6976
}
7077

7178
type Conn struct {
79+
log logger.Logger
7280
wmu sync.Mutex
7381
conn UDPConn
7482
closed core.Fuse
@@ -160,6 +168,10 @@ func (c *Conn) readLoop() {
160168
return
161169
}
162170
c.dest.Store(srcAddr)
171+
if n > MTUSize {
172+
c.log.Errorw("RTP packet is larger than MTU limit", nil)
173+
continue // ignore partial messages
174+
}
163175

164176
p = rtp.Packet{}
165177
if err := p.Unmarshal(buf[:n]); err != nil {

pkg/media/rtp/session.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ import (
2222

2323
"github.com/frostbyte73/core"
2424
"github.com/pion/rtp"
25+
26+
"github.com/livekit/protocol/logger"
2527
)
2628

27-
const enableZeroCopy = true
29+
const (
30+
enableZeroCopy = true
31+
MTUSize = 1500
32+
)
2833

2934
type Session interface {
3035
OpenWriteStream() (WriteStream, error)
@@ -42,21 +47,24 @@ type ReadStream interface {
4247
ReadRTP(h *rtp.Header, payload []byte) (int, error)
4348
}
4449

45-
func NewSession(conn net.Conn) Session {
50+
func NewSession(log logger.Logger, conn net.Conn) Session {
4651
return &session{
52+
log: log,
4753
conn: conn,
4854
w: &writeStream{conn: conn},
4955
bySSRC: make(map[uint32]*readStream),
56+
rbuf: make([]byte, MTUSize+4), // larger buffer to detect overflow
5057
}
5158
}
5259

5360
type session struct {
61+
log logger.Logger
5462
conn net.Conn
5563
closed core.Fuse
5664
w *writeStream
5765

5866
rmu sync.Mutex
59-
rbuf [1500]byte
67+
rbuf []byte
6068
bySSRC map[uint32]*readStream
6169
}
6270

@@ -72,6 +80,10 @@ func (s *session) AcceptStream() (ReadStream, uint32, error) {
7280
if err != nil {
7381
return nil, 0, err
7482
}
83+
if n > MTUSize {
84+
s.log.Errorw("RTP packet is larger than MTU limit", nil)
85+
continue // ignore partial messages
86+
}
7587
buf := s.rbuf[:n]
7688
var p rtp.Packet
7789
err = p.Unmarshal(buf)

pkg/media/srtp/srtp.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"net"
2121

2222
prtp "github.com/pion/rtp"
23-
"github.com/pion/srtp/v2"
23+
"github.com/pion/srtp/v3"
2424

25+
"github.com/livekit/protocol/logger"
2526
"github.com/livekit/sip/pkg/media/rtp"
2627
)
2728

@@ -96,16 +97,17 @@ type Profile struct {
9697
type Config = srtp.Config
9798
type SessionKeys = srtp.SessionKeys
9899

99-
func NewSession(conn net.Conn, conf *Config) (rtp.Session, error) {
100+
func NewSession(log logger.Logger, conn net.Conn, conf *Config) (rtp.Session, error) {
100101
s, err := srtp.NewSessionSRTP(conn, conf)
101102
if err != nil {
102103
return nil, err
103104
}
104-
return &session{s: s}, nil
105+
return &session{log: log, s: s}, nil
105106
}
106107

107108
type session struct {
108-
s *srtp.SessionSRTP
109+
log logger.Logger
110+
s *srtp.SessionSRTP
109111
}
110112

111113
func (s *session) OpenWriteStream() (rtp.WriteStream, error) {

pkg/sip/media_port.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ func (p *MediaPort) SetConfig(c *MediaConf) error {
251251
err error
252252
)
253253
if c.Crypto != nil {
254-
sess, err = srtp.NewSession(p.port, c.Crypto)
254+
sess, err = srtp.NewSession(p.log, p.port, c.Crypto)
255255
} else {
256-
sess = rtp.NewSession(p.port)
256+
sess = rtp.NewSession(p.log, p.port)
257257
}
258258
if err != nil {
259259
return err
@@ -295,7 +295,7 @@ func (p *MediaPort) rtpLoop(sess rtp.Session) {
295295
}
296296

297297
func (p *MediaPort) rtpReadLoop(r rtp.ReadStream) {
298-
buf := make([]byte, 1500)
298+
buf := make([]byte, rtp.MTUSize+4)
299299
var h rtp.Header
300300
for {
301301
h = rtp.Header{}
@@ -306,6 +306,10 @@ func (p *MediaPort) rtpReadLoop(r rtp.ReadStream) {
306306
p.log.Errorw("read RTP failed", err)
307307
return
308308
}
309+
if n > rtp.MTUSize {
310+
p.log.Errorw("RTP packet is larger than MTU limit", nil)
311+
continue // ignore partial messages
312+
}
309313

310314
ptr := p.hnd.Load()
311315
if ptr == nil {

0 commit comments

Comments
 (0)