Skip to content

Commit

Permalink
Support inspecting fragmented DNS message for FQDN policy
Browse files Browse the repository at this point in the history
A DNS message sent over TCP will be fragmented if its length is greater
than MTU, in which case FQDN policy wouldn't work as expected because
the used DNS library expects the entire message. However, it's been
observed that DNS message was fragmented frequently in tests, which may
be applicable to production environments as well. On the other hand, we
don't really need the entire message to support FQDN policy, and the
first fragment normally already contains the information we need, the
question and answer sections.

This patch forks and modifies the message Unpack function to be able to
handle fragmented messages.

Signed-off-by: Quan Tian <qtian@vmware.com>
  • Loading branch information
tnqn committed Mar 21, 2023
1 parent a66f078 commit 17d239f
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 37 deletions.
53 changes: 33 additions & 20 deletions pkg/agent/controller/networkpolicy/fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"antrea.io/antrea/pkg/agent/types"
binding "antrea.io/antrea/pkg/ovs/openflow"
utilsets "antrea.io/antrea/pkg/util/sets"
dnsutil "antrea.io/antrea/third_party/dns"
)

const (
Expand Down Expand Up @@ -746,15 +747,41 @@ func (f *fqdnController) HandlePacketIn(pktIn *ofctrl.PacketIn) error {
func (f *fqdnController) handlePacketIn(pktIn *ofctrl.PacketIn) error {
klog.V(4).InfoS("Received a packetIn for DNS response")
waitCh := make(chan error, 1)
handleDNSData := func(dnsData []byte) {
handleUDP := func(udp *protocol.UDP) {
dnsMsg := dns.Msg{}
if err := dnsMsg.Unpack(dnsData); err != nil {
if err := dnsMsg.Unpack(udp.Data); err != nil {
// A non-DNS response packet or a fragmented DNS response is received. Forward it to the Pod.
waitCh <- nil
return
}
f.onDNSResponseMsg(&dnsMsg, time.Now(), waitCh)
}
handleTCP := func(tcpPkt *protocol.TCP) {
dnsData, dataLength, err := binding.GetTCPDNSData(tcpPkt)
if err != nil {
// The packet doesn't contain a valid DNS length field and data. Forward it to the Pod.
klog.V(4).InfoS("Unable to get DNS data from the packet, skipping it", "err", err)
waitCh <- nil
return
}
dnsMsg := dns.Msg{}
if dataLength > len(dnsData) {
// This is likely the first packet containing the length field and partial message.
klog.InfoS("Received a fragmented DNS response, partially unpacking it", "lengthField", dataLength, "actualLength", len(dnsData))
if err := dnsutil.UnpackDNSMsgPartially(dnsData, &dnsMsg); err != nil {
klog.InfoS("Unable to unpack the DNS response partially, skipping it", "err", err)
waitCh <- nil
return
}
} else if err := dnsMsg.Unpack(dnsData); err != nil {
// This is likely a non-DNS response packet or a non-first-DNS response packet containing partial message.
// Set verbose level to 2 as normally we are not interested in it.
klog.V(2).InfoS("Unable to unpack the DNS response, skipping it", "err", err)
waitCh <- nil
return
}
f.onDNSResponseMsg(&dnsMsg, time.Now(), waitCh)
}
go func() {
ethernetPkt, err := getEthernetPacket(pktIn)
if err != nil {
Expand All @@ -767,43 +794,29 @@ func (f *fqdnController) handlePacketIn(pktIn *ofctrl.PacketIn) error {
proto := ipPkt.Protocol
switch proto {
case protocol.Type_UDP:
udpPkt := ipPkt.Data.(*protocol.UDP)
handleDNSData(udpPkt.Data)
handleUDP(ipPkt.Data.(*protocol.UDP))
case protocol.Type_TCP:
tcpPkt, err := binding.GetTCPPacketFromIPMessage(ipPkt)
if err != nil {
// Can't parse the packet. Forward it to the Pod.
waitCh <- nil
return
}
dnsData, err := binding.GetTCPDNSData(tcpPkt)
if err != nil {
// A non-DNS response packet is received or a fragmented DNS response is received. Forward it to the Pod.
waitCh <- nil
return
}
handleDNSData(dnsData)
handleTCP(tcpPkt)
}
case *protocol.IPv6:
proto := ipPkt.NextHeader
switch proto {
case protocol.Type_UDP:
udpPkt := ipPkt.Data.(*protocol.UDP)
handleDNSData(udpPkt.Data)
handleUDP(ipPkt.Data.(*protocol.UDP))
case protocol.Type_TCP:
tcpPkt, err := binding.GetTCPPacketFromIPMessage(ipPkt)
if err != nil {
// Can't parse the packet. Forward it to the Pod.
waitCh <- nil
return
}
dnsData, err := binding.GetTCPDNSData(tcpPkt)
if err != nil {
// A non-DNS response packet is received or a fragmented DNS response is received. Forward it to the Pod.
waitCh <- nil
return
}
handleDNSData(dnsData)
handleTCP(tcpPkt)
}
}
}()
Expand Down
12 changes: 3 additions & 9 deletions pkg/ovs/openflow/ofctrl_packetin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"errors"
"fmt"

"k8s.io/klog/v2"

"antrea.io/libOpenflow/protocol"
"antrea.io/libOpenflow/util"
"antrea.io/ofnet/ofctrl"
Expand Down Expand Up @@ -64,7 +62,7 @@ func GetTCPPacketFromIPMessage(ipPkt util.Message) (tcpPkt *protocol.TCP, err er
return tcpPkt, nil
}

func GetTCPDNSData(tcpPkt *protocol.TCP) (data []byte, err error) {
func GetTCPDNSData(tcpPkt *protocol.TCP) (data []byte, length int, err error) {
// TCP.HdrLen is 4-octet unit indicating the length of TCP header including options.
tcpOptionsLen := (tcpPkt.HdrLen - tcpStandardHdrLen) * 4
// Move two more octet.
Expand All @@ -74,15 +72,11 @@ func GetTCPDNSData(tcpPkt *protocol.TCP) (data []byte, err error) {
// same time (e.g., in a single "write" system call) to make it more
// likely that all the data will be transmitted in a single TCP segment.
if int(tcpOptionsLen+2) > len(tcpPkt.Data) {
return nil, fmt.Errorf("no DNS data in TCP data")
return nil, 0, fmt.Errorf("no DNS data in TCP data")
}
dnsDataLen := binary.BigEndian.Uint16(tcpPkt.Data[tcpOptionsLen : tcpOptionsLen+2])
dnsData := tcpPkt.Data[tcpOptionsLen+2:]
if int(dnsDataLen) > len(dnsData) {
klog.Info("There is a non-DNS response or a fragmented DNS response in TCP payload")
return nil, fmt.Errorf("there is a non-DNS response or a fragmented DNS response in TCP payload")
}
return dnsData, nil
return dnsData, int(dnsDataLen), nil
}

func GetUDPHeaderData(ipPkt util.Message) (udpSrcPort, udpDstPort uint16, err error) {
Expand Down
20 changes: 12 additions & 8 deletions pkg/ovs/openflow/ofctrl_packetin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,10 @@ func TestParsePacketIn(t *testing.T) {

func TestGetTCPDNSData(t *testing.T) {
type args struct {
tcp protocol.TCP
expectErr error
expectData []byte
tcp protocol.TCP
expectErr error
expectData []byte
expectLength int
}
tests := []struct {
name string
Expand All @@ -257,8 +258,9 @@ func TestGetTCPDNSData(t *testing.T) {
HdrLen: 6,
Data: []byte{1, 2, 3, 4, 0, 2, 5},
},
expectErr: fmt.Errorf("there is a non-DNS response or a fragmented DNS response in TCP payload"),
expectData: nil,
expectErr: nil,
expectData: []byte{5},
expectLength: 2,
},
},
{
Expand All @@ -268,17 +270,19 @@ func TestGetTCPDNSData(t *testing.T) {
HdrLen: 6,
Data: []byte{1, 2, 3, 4, 0, 1, 5},
},
expectErr: nil,
expectData: []byte{5},
expectErr: nil,
expectData: []byte{5},
expectLength: 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tcp := tt.args.tcp
tcpData, err := GetTCPDNSData(&tcp)
tcpData, dataLength, err := GetTCPDNSData(&tcp)
assert.Equal(t, tt.args.expectErr, err)
assert.Equal(t, tt.args.expectData, tcpData)
assert.Equal(t, tt.args.expectLength, dataLength)
})
}
}
194 changes: 194 additions & 0 deletions third_party/dns/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright 2023 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This package is copied and modified from https://github.com/miekg/dns because the original function Unpack cannot
// unpack fragmented DNS message.

package dns

import (
"encoding/binary"
"fmt"

godns "github.com/miekg/dns"
)

const (
// Header.Bits
_QR = 1 << 15 // query/response (response=1)
_AA = 1 << 10 // authoritative
_TC = 1 << 9 // truncated
_RD = 1 << 8 // recursion desired
_RA = 1 << 7 // recursion available
_Z = 1 << 6 // Z
_AD = 1 << 5 // authenticated data
_CD = 1 << 4 // checking disabled
)

// UnpackDNSMsgPartially is modified from https://github.com/miekg/dns/blob/6ad6301ae27dca6d7822baf1b05ff9c9e4ba56f4/msg.go#L883.
// It can unpack a DNS response with partial data only. More specifically, it unpacks the message header, the question
// section, and the answer section, while ignores the authority section and the additional section.
// It's used to get the question and answer sections when a DNS response is carried by a TCP packet but is fragmented.
func UnpackDNSMsgPartially(msg []byte, dns *godns.Msg) error {
dh, off, err := unpackMsgHdr(msg, 0)
if err != nil {
return err
}

setHdr(dns, dh)
return unpackPartially(dns, dh, msg, off)
}

func unpackMsgHdr(msg []byte, off int) (godns.Header, int, error) {
var (
dh godns.Header
err error
)
dh.Id, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
}
dh.Bits, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
}
dh.Qdcount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
}
dh.Ancount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
}
dh.Nscount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
}
dh.Arcount, off, err = unpackUint16(msg, off)
if err != nil {
return dh, off, err
}
return dh, off, nil
}

func unpackUint16(msg []byte, off int) (i uint16, off1 int, err error) {
if off+2 > len(msg) {
return 0, len(msg), fmt.Errorf("overflow unpacking uint16")
}
return binary.BigEndian.Uint16(msg[off:]), off + 2, nil
}

func setHdr(dns *godns.Msg, dh godns.Header) {
dns.Id = dh.Id
dns.Response = dh.Bits&_QR != 0
dns.Opcode = int(dh.Bits>>11) & 0xF
dns.Authoritative = dh.Bits&_AA != 0
dns.Truncated = dh.Bits&_TC != 0
dns.RecursionDesired = dh.Bits&_RD != 0
dns.RecursionAvailable = dh.Bits&_RA != 0
dns.Zero = dh.Bits&_Z != 0 // _Z covers the zero bit, which should be zero; not sure why we set it to the opposite.
dns.AuthenticatedData = dh.Bits&_AD != 0
dns.CheckingDisabled = dh.Bits&_CD != 0
dns.Rcode = int(dh.Bits & 0xF)
}

// unpackPartially is modified from https://github.com/miekg/dns/blob/6ad6301ae27dca6d7822baf1b05ff9c9e4ba56f4/msg.go#L826.
// It unpacks the message header, the question section, and the answer section, while ignores the authority section and
// the additional section.
func unpackPartially(dns *godns.Msg, dh godns.Header, msg []byte, off int) (err error) {
// If we are at the end of the message we should return *just* the
// header. This can still be useful to the caller. 9.9.9.9 sends these
// when responding with REFUSED for instance.
if off == len(msg) {
// reset sections before returning
dns.Question, dns.Answer, dns.Ns, dns.Extra = nil, nil, nil, nil
return nil
}

// Qdcount, Ancount, Nscount, Arcount can't be trusted, as they are
// attacker controlled. This means we can't use them to pre-allocate
// slices.
dns.Question = nil
for i := 0; i < int(dh.Qdcount); i++ {
off1 := off
var q godns.Question
q, off, err = unpackQuestion(msg, off)
if err != nil {
return err
}
if off1 == off { // Offset does not increase anymore, dh.Qdcount is a lie!
dh.Qdcount = uint16(i)
break
}
dns.Question = append(dns.Question, q)
}

dns.Answer, off, err = unpackRRslice(int(dh.Ancount), msg, off)
// The header counts might have been wrong so we need to update it
dh.Ancount = uint16(len(dns.Answer))
// Skip unpacking the authority section and the additional section.
return err

}

func unpackQuestion(msg []byte, off int) (godns.Question, int, error) {
var (
q godns.Question
err error
)
q.Name, off, err = godns.UnpackDomainName(msg, off)
if err != nil {
return q, off, err
}
if off == len(msg) {
return q, off, nil
}
q.Qtype, off, err = unpackUint16(msg, off)
if err != nil {
return q, off, err
}
if off == len(msg) {
return q, off, nil
}
q.Qclass, off, err = unpackUint16(msg, off)
if off == len(msg) {
return q, off, nil
}
return q, off, err
}

// unpackRRslice unpacks msg[off:] into an []RR.
// If we cannot unpack the whole array, then it will return nil
func unpackRRslice(l int, msg []byte, off int) (dst1 []godns.RR, off1 int, err error) {
var r godns.RR
// Don't pre-allocate, l may be under attacker control
var dst []godns.RR
for i := 0; i < l; i++ {
off1 := off
r, off, err = godns.UnpackRR(msg, off)
if err != nil {
off = len(msg)
break
}
// If offset does not increase anymore, l is a lie
if off1 == off {
break
}
dst = append(dst, r)
}
if err != nil && off == len(msg) {
dst = nil
}
return dst, off, err
}
Loading

0 comments on commit 17d239f

Please sign in to comment.