Skip to content

Commit

Permalink
fix: check IP/UDP header size before trying to access it
Browse files Browse the repository at this point in the history
This should fix the panic we've seen with the malformed packets on the
wire. Remaining buffer size should be checked before trying to access
the data, otherwise Go might panic on out of bounds slice operation.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira authored and hugelgupf committed Dec 14, 2021
1 parent 7d93572 commit 5297eed
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dhcpv4/nclient4/conn_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.12
// +build go1.12

package nclient4
Expand Down Expand Up @@ -98,12 +99,26 @@ func (upc *BroadcastRawUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
buf := uio.NewBigEndianBuffer(pkt)

// To read the header length, access data directly.
if !buf.Has(ipv4MinimumSize) {
continue
}

ipHdr := ipv4(buf.Data())

if !buf.Has(int(ipHdr.headerLength())) {
continue
}

ipHdr = ipv4(buf.Consume(int(ipHdr.headerLength())))

if ipHdr.transportProtocol() != udpProtocolNumber {
continue
}

if !buf.Has(udpHdrLen) {
continue
}

udpHdr := udp(buf.Consume(udpHdrLen))

addr := &net.UDPAddr{
Expand Down

0 comments on commit 5297eed

Please sign in to comment.