Skip to content

Commit 72eaf98

Browse files
committed
dns/dnsmessage: correctly validate SVCB record parameter order
Fix a bug which caused us to not properly validate the order of parameters in SVCB/HTTPS records. Fixes golang/go#79796 Change-Id: I9d3c43bde06f3d25786a5b89e45613e76a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/786346 LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> Reviewed-by: Nicholas Husin <[email protected]>
1 parent 82e7868 commit 72eaf98

3 files changed

Lines changed: 92 additions & 22 deletions

File tree

‎dns/dnsmessage/message_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,30 @@ func TestInvalidMessages(t *testing.T) {
19401940
0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x5d,
19411941
},
19421942
},
1943+
{
1944+
name: "SVCB out-of-order keys",
1945+
in: []byte{
1946+
0x12, 0x34, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
1947+
0x00, 0x00, 0x41, 0x00, 0x01,
1948+
0x00, 0x00, 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
1949+
0x00, 0x0b,
1950+
0x00, 0x01, 0x00,
1951+
0x00, 0x02, 0x00, 0x00,
1952+
0x00, 0x01, 0x00, 0x00,
1953+
},
1954+
},
1955+
{
1956+
name: "SVCB duplicate keys",
1957+
in: []byte{
1958+
0x12, 0x34, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
1959+
0x00, 0x00, 0x41, 0x00, 0x01,
1960+
0x00, 0x00, 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
1961+
0x00, 0x0b,
1962+
0x00, 0x01, 0x00,
1963+
0x00, 0x01, 0x00, 0x00,
1964+
0x00, 0x01, 0x00, 0x00,
1965+
},
1966+
},
19431967
} {
19441968
t.Run(test.name, func(t *testing.T) {
19451969
var p Parser

‎dns/dnsmessage/svcb.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ func (r *SVCBResource) pack(msg []byte, _ map[string]uint16, _ int) ([]byte, err
168168
if err != nil {
169169
return oldMsg, &nestedError{"SVCBResource.Target", err}
170170
}
171-
var previousKey SVCParamKey
172171
for i, param := range r.Params {
173-
if i > 0 && param.Key <= previousKey {
172+
if i > 0 && param.Key <= r.Params[i-1].Key {
174173
return oldMsg, &nestedError{"SVCBResource.Params", errParamOutOfOrder}
175174
}
176175
if len(param.Value) > (1<<16)-1 {
@@ -220,6 +219,7 @@ func unpackSVCBResource(msg []byte, off int, length uint16) (SVCBResource, error
220219
if off+int(size) > bodyEnd {
221220
return SVCBResource{}, errResourceLen
222221
}
222+
previousKey = key
223223
totalValueLen += size
224224
off += int(size)
225225
n++

‎dns/dnsmessage/svcb_test.go‎

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -366,28 +366,74 @@ func TestSVCBWireFormat(t *testing.T) {
366366
testRecord(bytes, parsed)
367367
}
368368

369-
func TestSVCBPackLongValue(t *testing.T) {
370-
b := NewBuilder(nil, Header{})
371-
b.StartQuestions()
372-
b.StartAnswers()
373-
374-
res := SVCBResource{
375-
Target: MustNewName("example.com."),
376-
Params: []SVCParam{
377-
{
378-
Key: SVCParamMandatory,
379-
Value: make([]byte, math.MaxUint16+1),
369+
func TestSVCBPackErrors(t *testing.T) {
370+
for _, test := range []struct {
371+
name string
372+
r SVCBResource
373+
}{
374+
{
375+
name: "long value",
376+
r: SVCBResource{
377+
Target: MustNewName("example.com."),
378+
Params: []SVCParam{
379+
{
380+
Key: SVCParamMandatory,
381+
Value: make([]byte, math.MaxUint16+1),
382+
},
383+
},
380384
},
381385
},
382-
}
383-
384-
err := b.SVCBResource(ResourceHeader{Name: MustNewName("example.com.")}, res)
385-
if err == nil || err.Error() != "ResourceBody: SVCBResource.Params: value too long (>65535 bytes)" {
386-
t.Fatalf(`b.SVCBResource() = %v; want = "ResourceBody: SVCBResource.Params: value too long (>65535 bytes)"`, err)
387-
}
386+
{
387+
name: "out-of-order keys",
388+
r: SVCBResource{
389+
Target: MustNewName("example.com."),
390+
Params: []SVCParam{
391+
{
392+
Key: SVCParamPort, // 3
393+
Value: []byte("443"),
394+
},
395+
{
396+
Key: SVCParamALPN, // 1
397+
Value: []byte("h2"),
398+
},
399+
},
400+
},
401+
},
402+
{
403+
name: "duplicate keys",
404+
r: SVCBResource{
405+
Target: MustNewName("example.com."),
406+
Params: []SVCParam{
407+
{
408+
Key: SVCParamALPN,
409+
Value: []byte("h3"),
410+
},
411+
{
412+
Key: SVCParamALPN,
413+
Value: []byte("h2"),
414+
},
415+
},
416+
},
417+
},
418+
} {
419+
t.Run(test.name, func(t *testing.T) {
420+
b := NewBuilder(nil, Header{})
421+
b.StartQuestions()
422+
b.StartAnswers()
423+
if err := b.SVCBResource(ResourceHeader{
424+
Name: MustNewName("example.com."),
425+
}, test.r); err == nil {
426+
t.Errorf("b.SVCBResource() succeeded; want error")
427+
}
388428

389-
err = b.HTTPSResource(ResourceHeader{Name: MustNewName("example.com.")}, HTTPSResource{res})
390-
if err == nil || err.Error() != "ResourceBody: SVCBResource.Params: value too long (>65535 bytes)" {
391-
t.Fatalf(`b.HTTPSResource() = %v; want = "ResourceBody: SVCBResource.Params: value too long (>65535 bytes)"`, err)
429+
b = NewBuilder(nil, Header{})
430+
b.StartQuestions()
431+
b.StartAnswers()
432+
if err := b.HTTPSResource(ResourceHeader{
433+
Name: MustNewName("example.com."),
434+
}, HTTPSResource{test.r}); err == nil {
435+
t.Errorf("b.HTTPSResource() succeeded; want error")
436+
}
437+
})
392438
}
393439
}

0 commit comments

Comments
 (0)