dns: add length check to avoid panic

This commit is contained in:
nadoo committed 2020-05-05 01:30:57 +08:00
1 parent 665d722d2c
commit e3c57ba369
6 files changed
+20 -55

No files matched your search

+7 -5
View File
@@ -163,7 +163,9 @@ func (c *Client) exchange(qname string, reqBytes []byte, preferTCP bool) (
defer rc.Close()
// TODO: support timeout setting for different upstream server
rc.SetDeadline(time.Now().Add(time.Duration(c.config.Timeout) * time.Second))
if c.config.Timeout > 0 {
rc.SetDeadline(time.Now().Add(time.Duration(c.config.Timeout) * time.Second))
}
switch network {
case "tcp":
@@ -219,14 +221,14 @@ func (c *Client) exchangeUDP(rc net.Conn, reqBytes []byte) ([]byte, error) {
return nil, err
}
reqBytes = make([]byte, 2+UDPMaxLen)
n, err := rc.Read(reqBytes[2:])
respBytes := make([]byte, 2+UDPMaxLen)
n, err := rc.Read(respBytes[2:])
if err != nil {
return nil, err
}
binary.BigEndian.PutUint16(reqBytes[:2], uint16(n))
binary.BigEndian.PutUint16(respBytes[:2], uint16(n))
return reqBytes[:2+n], nil
return respBytes[:2+n], nil
}
// SetServers sets upstream dns servers for the given domain.
+4
View File
@@ -403,6 +403,10 @@ func MarshalDomainTo(w io.Writer, domain string) (int, error) {
// UnmarshalDomain gets domain from bytes.
func (m *Message) UnmarshalDomain(b []byte) (string, int, error) {
if len(b) < 2 {
return "", 0, errors.New("UnmarshalDomain: not enough size")
}
var idx, size int
var labels = []string{}