Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close connection when an abnormally large number of frames are queued #4369

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
close connection when an abnormally large number of frames are queued
Under normal circumstances, we should be able to send out control frames
right away, so we don't expect any queue to build up. To defend against
resource exhaustion attacks, we limit the control frame queue to 8192
elements.
  • Loading branch information
marten-seemann committed Mar 17, 2024
commit ce7e5ebd653c34ab61d4d5888787a7c0078ef4cb
3 changes: 3 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ func (s *connection) run() error {

runLoop:
for {
if s.framer.QueuedTooManyControlFrames() {
s.closeLocal(&qerr.TransportError{ErrorCode: InternalError})
}
// Close immediately if requested
select {
case closeErr = <-s.closeChan:
Expand Down
28 changes: 24 additions & 4 deletions framer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ type framer interface {
AppendStreamFrames([]ackhandler.StreamFrame, protocol.ByteCount, protocol.Version) ([]ackhandler.StreamFrame, protocol.ByteCount)

Handle0RTTRejection() error

// QueuedTooManyControlFrames says if the control frame queue exceeded its maximum queue length.
// This is a hack.
// It is easier to implement than propagating an error return value in QueueControlFrame.
// The correct solution would be to queue frames with their respective structs.
// See https://github.com/quic-go/quic-go/issues/4271 for the queueing of stream-related control frames.
QueuedTooManyControlFrames() bool
}

const maxPathResponses = 256
const (
maxPathResponses = 256
maxControlFrames = 16 << 10
)

type framerI struct {
mutex sync.Mutex
Expand All @@ -33,9 +43,10 @@ type framerI struct {
activeStreams map[protocol.StreamID]struct{}
streamQueue ringbuffer.RingBuffer[protocol.StreamID]

controlFrameMutex sync.Mutex
controlFrames []wire.Frame
pathResponses []*wire.PathResponseFrame
controlFrameMutex sync.Mutex
controlFrames []wire.Frame
pathResponses []*wire.PathResponseFrame
queuedTooManyControlFrames bool
}

var _ framer = &framerI{}
Expand Down Expand Up @@ -73,6 +84,11 @@ func (f *framerI) QueueControlFrame(frame wire.Frame) {
f.pathResponses = append(f.pathResponses, pr)
return
}
// This is a hack.
if len(f.controlFrames) >= maxControlFrames {
f.queuedTooManyControlFrames = true
return
}
f.controlFrames = append(f.controlFrames, frame)
}

Expand Down Expand Up @@ -105,6 +121,10 @@ func (f *framerI) AppendControlFrames(frames []ackhandler.Frame, maxLen protocol
return frames, length
}

func (f *framerI) QueuedTooManyControlFrames() bool {
return f.queuedTooManyControlFrames
}

func (f *framerI) AddActiveStream(id protocol.StreamID) {
f.mutex.Lock()
if _, ok := f.activeStreams[id]; !ok {
Expand Down
17 changes: 17 additions & 0 deletions framer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ var _ = Describe("Framer", func() {
Expect(fs).To(HaveLen(2))
Expect(length).To(Equal(ping.Length(version) + ncid.Length(version)))
})

It("detects when too many frames are queued", func() {
for i := 0; i < maxControlFrames-1; i++ {
framer.QueueControlFrame(&wire.PingFrame{})
framer.QueueControlFrame(&wire.PingFrame{})
Expect(framer.QueuedTooManyControlFrames()).To(BeFalse())
frames, _ := framer.AppendControlFrames([]ackhandler.Frame{}, 1, protocol.Version1)
Expect(frames).To(HaveLen(1))
Expect(framer.(*framerI).controlFrames).To(HaveLen(i + 1))
}
framer.QueueControlFrame(&wire.PingFrame{})
Expect(framer.QueuedTooManyControlFrames()).To(BeFalse())
Expect(framer.(*framerI).controlFrames).To(HaveLen(maxControlFrames))
framer.QueueControlFrame(&wire.PingFrame{})
Expect(framer.QueuedTooManyControlFrames()).To(BeTrue())
Expect(framer.(*framerI).controlFrames).To(HaveLen(maxControlFrames))
})
})

Context("handling PATH_RESPONSE frames", func() {
Expand Down
Loading