fix: zero question dns packet causes panic

This commit is contained in:
mzz2017 2023-01-31 19:54:11 +08:00
parent 460ef450a0
commit c1e196c1c6
3 changed files with 20 additions and 10 deletions

View File

@ -64,6 +64,9 @@ func (c *ControlPlane) BatchUpdateDomainRouting(cache *dnsCache) error {
}
func (c *ControlPlane) LookupDnsRespCache(msg *dnsmessage.Message) (resp []byte) {
if len(msg.Questions) == 0 {
return nil
}
q := msg.Questions[0]
if msg.Response {
return nil
@ -101,7 +104,7 @@ func (c *ControlPlane) DnsRespHandler(data []byte) (newData []byte, err error) {
}
// Check healthy.
if !msg.Response || msg.RCode != dnsmessage.RCodeSuccess {
if !msg.Response || msg.RCode != dnsmessage.RCodeSuccess || len(msg.Questions) == 0 {
return data, nil
}
// Check req type.

View File

@ -960,7 +960,7 @@ static int routing(__u32 flag[6], void *l4_hdr, __be32 saddr[4],
good_subrule = true;
}
} else if (match_set->type == MatchType_ProcessName) {
if ((equal_ipv6_format(match_set->pname, _pname))){
if ((equal_ipv6_format(match_set->pname, _pname))) {
good_subrule = true;
}
} else if (match_set->type == MatchType_Final) {
@ -1990,6 +1990,7 @@ static int __always_inline update_map_elem_by_sk(struct sock *sk) {
struct pid_pname val;
__builtin_memset(&val, 0, sizeof(struct pid_pname));
val.pid = bpf_get_current_pid_tgid() >> 32;
// struct task_struct *t = (void *)bpf_get_current_task();
if ((ret = bpf_get_current_comm(val.pname, sizeof(val.pname)))) {
return ret;
}

View File

@ -9,6 +9,7 @@ import (
"encoding/binary"
"fmt"
"github.com/mzz2017/softwind/pool"
"github.com/sirupsen/logrus"
"github.com/v2rayA/dae/common/consts"
"golang.org/x/net/dns/dnsmessage"
"net"
@ -117,10 +118,12 @@ func (c *ControlPlane) handlePkt(data []byte, lConn *net.UDPConn, lAddrPort neti
if err = sendPktWithHdr(resp, dest, lConn, lAddrPort); err != nil {
return fmt.Errorf("failed to write cached DNS resp: %w", err)
}
q := dnsMessage.Questions[0]
c.log.Debugf("UDP(DNS) %v <-[%v]-> Cache: %v %v",
lAddrPort.String(), outbound.Name, q.Name, q.Type,
)
if c.log.IsLevelEnabled(logrus.DebugLevel) && len(dnsMessage.Questions) > 0 {
q := dnsMessage.Questions[0]
c.log.Debugf("UDP(DNS) %v <-[%v]-> Cache: %v %v",
lAddrPort.String(), outbound.Name, q.Name, q.Type,
)
}
return nil
} else {
c.log.Debugf("Modify dns target %v to upstream: %v", addrHdr.Dest.String(), c.dnsUpstream.String())
@ -128,10 +131,13 @@ func (c *ControlPlane) handlePkt(data []byte, lConn *net.UDPConn, lAddrPort neti
// NOTICE: Routing was calculated in advance by the eBPF program.
dummyFrom = &addrHdr.Dest
dest = c.dnsUpstream
q := dnsMessage.Questions[0]
c.log.Debugf("UDP(DNS) %v <-[%v]-> %v: %v %v",
lAddrPort.String(), outbound.Name, dest.String(), q.Name, q.Type,
)
if c.log.IsLevelEnabled(logrus.DebugLevel) && len(dnsMessage.Questions) > 0 {
q := dnsMessage.Questions[0]
c.log.Debugf("UDP(DNS) %v <-[%v]-> %v: %v %v",
lAddrPort.String(), outbound.Name, dest.String(), q.Name, q.Type,
)
}
}
} else {
// TODO: Set-up ip to domain mapping and show domain if possible.