dns: check length in UnmarshalDomain to avoid panic

This commit is contained in:
nadoo committed 2020-08-21 23:54:18 +08:00
1 parent a118ec5837
commit a42d3a68d0
6 files changed
+18 -11

No files matched your search

+2 -2
View File
@@ -14,7 +14,7 @@ import (
)
// HandleFunc function handles the dns TypeA or TypeAAAA answer.
type HandleFunc func(Domain, ip string) error
type HandleFunc func(domain, ip string) error
// Config for dns.
type Config struct {
@@ -55,7 +55,7 @@ func NewClient(proxy proxy.Proxy, config *Config) (*Client, error) {
}
// Exchange handles request message and returns response message.
// reqBytes = reqLen + reqMsg
// NOTE: reqBytes = reqLen + reqMsg.
func (c *Client) Exchange(reqBytes []byte, clientAddr string, preferTCP bool) ([]byte, error) {
req, err := UnmarshalMessage(reqBytes[2:])
if err != nil {
+4
View File
@@ -415,6 +415,10 @@ func (m *Message) UnmarshalDomain(b []byte) (string, int, error) {
break
} else if b[idx]&0xC0 == 0xC0 {
if len(b[idx:]) < 2 {
return "", 0, errors.New("UnmarshalDomain: not enough size for compressed domain")
}
offset := binary.BigEndian.Uint16(b[idx : idx+2])
label, err := m.UnmarshalDomainPoint(int(offset & 0x3FFF))
if err != nil {