Skip to content

Commit dec6603

Browse files
srrigopherbot
authored andcommitted
dns/dnsmessage: reject too large of names early during unpack
The existing implementation of the Name.unpack method does not check the length of the domain name until parsing is complete. This allows a malicious user to supply an unreasonably large name and wastle cycles parsing. This change moves an equivalent check into the loop during process to short-circuit if we've created too large of a name. For golang/go#77540 Change-Id: I4c4bf20c0342825a09cefd9b0b3c0bdce0c80137 Reviewed-on: https://go-review.googlesource.com/c/net/+/750260 Reviewed-by: Roland Shoemaker <roland@golang.org> Auto-Submit: Roland Shoemaker <roland@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
1 parent 8afa12f commit dec6603

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

dns/dnsmessage/message.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,7 +2085,11 @@ Loop:
20852085
return off, errInvalidName
20862086
}
20872087
}
2088-
2088+
// Reject names that are too long while unpacking
2089+
// See issue golang/go#77540
2090+
if len(name)+(endOff-currOff) >= nonEncodedNameMax {
2091+
return off, errNameTooLong
2092+
}
20892093
name = append(name, msg[currOff:endOff]...)
20902094
name = append(name, '.')
20912095
currOff = endOff
@@ -2111,9 +2115,6 @@ Loop:
21112115
if len(name) == 0 {
21122116
name = append(name, '.')
21132117
}
2114-
if len(name) > nonEncodedNameMax {
2115-
return off, errNameTooLong
2116-
}
21172118
n.Length = uint8(len(name))
21182119
if ptr == 0 {
21192120
newOff = currOff

dns/dnsmessage/message_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ func TestNameUnpackTooLongName(t *testing.T) {
292292
}{
293293
{name: prepName(255)},
294294
{name: prepName(256), err: errNameTooLong},
295+
// too large to be valid, return error during unpack.
296+
{name: prepName(300), err: errNameTooLong},
295297
}
296298

297299
for i, test := range tests {

0 commit comments

Comments
 (0)