mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 03:56:52 +01:00
checkpt
This commit is contained in:
1 parent
e99e48cca7
commit
ed8ae57fee
4 files changed
+49
-64
No files matched your search
+3
-6
@@ -84,13 +84,10 @@ func addRulesFromConfig(l *slog.Logger, inbound bool, c *config.C, ra ruleAdder)
|
|||||||
}
|
}
|
||||||
proto = uint8(n)
|
proto = uint8(n)
|
||||||
}
|
}
|
||||||
// ICMPv6 rules are ICMP rules, see RulesBuilder.AddRule
|
|
||||||
if proto == iputil.IPProtocolICMPv6 {
|
|
||||||
proto = iputil.IPProtocolICMP
|
|
||||||
}
|
|
||||||
|
|
||||||
var startPort, endPort int32
|
var startPort, endPort int32
|
||||||
if proto == iputil.IPProtocolICMP {
|
if proto == iputil.IPProtocolICMP || proto == iputil.IPProtocolICMPv6 {
|
||||||
|
// RulesBuilder.AddRule coerces ICMP ports to `any` as well. This copy stays so that an ICMP rule's
|
||||||
|
// port is warned about rather than parsed, whatever it holds.
|
||||||
startPort = PortAny
|
startPort = PortAny
|
||||||
endPort = PortAny
|
endPort = PortAny
|
||||||
if sPort != "" {
|
if sPort != "" {
|
||||||
|
|||||||
+13
-5
@@ -170,13 +170,21 @@ func TestAddRulesFromConfig(t *testing.T) {
|
|||||||
assert.Equal(t, addRuleCall{incoming: false, proto: 47, startPort: PortAny, endPort: PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall, proto)
|
assert.Equal(t, addRuleCall{incoming: false, proto: 47, startPort: PortAny, endPort: PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall, proto)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test ICMP and ICMPv6, by name and by number, still ignore ports. An ICMPv6 rule is added as an ICMP rule.
|
// Test ICMP and ICMPv6, by name and by number, still ignore ports
|
||||||
for _, proto := range []any{"icmp", "icmpv6", iputil.IPProtocolICMP, iputil.IPProtocolICMPv6} {
|
for _, tc := range []struct {
|
||||||
|
given any
|
||||||
|
want uint8
|
||||||
|
}{
|
||||||
|
{"icmp", iputil.IPProtocolICMP},
|
||||||
|
{"icmpv6", iputil.IPProtocolICMPv6},
|
||||||
|
{iputil.IPProtocolICMP, iputil.IPProtocolICMP},
|
||||||
|
{iputil.IPProtocolICMPv6, iputil.IPProtocolICMPv6},
|
||||||
|
} {
|
||||||
conf = config.NewC(test.NewLogger())
|
conf = config.NewC(test.NewLogger())
|
||||||
mf = &mockFirewall{}
|
mf = &mockFirewall{}
|
||||||
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": proto, "host": "a"}}}
|
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": tc.given, "host": "a"}}}
|
||||||
require.NoError(t, addRulesFromConfig(l, false, conf, mf), proto)
|
require.NoError(t, addRulesFromConfig(l, false, conf, mf), tc.given)
|
||||||
assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolICMP, startPort: PortAny, endPort: PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall, proto)
|
assert.Equal(t, addRuleCall{incoming: false, proto: tc.want, startPort: PortAny, endPort: PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall, tc.given)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test protocols that aren't a known name or a number from 1 to 255
|
// Test protocols that aren't a known name or a number from 1 to 255
|
||||||
|
|||||||
+25
-33
@@ -4,40 +4,32 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash"
|
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"math"
|
"math"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/iputil"
|
"github.com/slackhq/nebula/iputil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RulesBuilder collects the inbound and outbound rules, Build turns them into a Table for each direction
|
// RulesBuilder collects the inbound and outbound rules, Build turns them into a Table for each direction
|
||||||
type RulesBuilder struct {
|
type RulesBuilder struct {
|
||||||
in, out tableRules
|
// in and out are each direction's rules in the order they were added
|
||||||
|
in, out []rule
|
||||||
|
|
||||||
// defaultLocalCIDRAny makes rules without a local cidr allow any local address, even with unsafe networks
|
// defaultLocalCIDRAny makes rules without a local cidr allow any local address, even with unsafe networks
|
||||||
defaultLocalCIDRAny bool
|
defaultLocalCIDRAny bool
|
||||||
|
|
||||||
// ruleSHA and ruleFNV hash every rule as it's added, see Rules
|
// ruleText describes every rule as it is added, one line each. Build hashes it; see Rules.
|
||||||
ruleSHA hash.Hash
|
ruleText strings.Builder
|
||||||
ruleFNV hash.Hash32
|
|
||||||
|
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// tableRules are one direction's rules in the order they were added, waiting to be built into a Table
|
|
||||||
type tableRules []rule
|
|
||||||
|
|
||||||
func NewRulesBuilder(l *slog.Logger) *RulesBuilder {
|
func NewRulesBuilder(l *slog.Logger) *RulesBuilder {
|
||||||
return &RulesBuilder{
|
return &RulesBuilder{l: l}
|
||||||
ruleSHA: sha256.New(),
|
|
||||||
ruleFNV: fnv.New32a(),
|
|
||||||
l: l,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddRule adds a rule for incoming or outgoing traffic, on proto or on every protocol when proto is ProtoAny.
|
// AddRule adds a rule for incoming or outgoing traffic, on proto or on every protocol when proto is ProtoAny.
|
||||||
@@ -57,19 +49,19 @@ func (b *RulesBuilder) AddRule(incoming bool, proto uint8, startPort int32, endP
|
|||||||
endPort = PortAny
|
endPort = PortAny
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := parseRule(startPort, endPort, groups, host, cidr, localCidr, caName, caSha)
|
r, err := parseRule(proto, startPort, endPort, groups, host, cidr, localCidr, caName, caSha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if incoming {
|
if incoming {
|
||||||
b.in.add(proto, r)
|
b.in = append(b.in, r)
|
||||||
} else {
|
} else {
|
||||||
b.out.add(proto, r)
|
b.out = append(b.out, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The rule hashes are of these lines, changing them changes every hash
|
// The rule hashes are of these lines, changing them changes every hash
|
||||||
fmt.Fprintf(io.MultiWriter(b.ruleSHA, b.ruleFNV),
|
fmt.Fprintf(&b.ruleText,
|
||||||
"incoming: %v, proto: %v, startPort: %v, endPort: %v, groups: %v, host: %v, ip: %v, localIp: %v, caName: %v, caSha: %s\n",
|
"incoming: %v, proto: %v, startPort: %v, endPort: %v, groups: %v, host: %v, ip: %v, localIp: %v, caName: %v, caSha: %s\n",
|
||||||
incoming, proto, startPort, endPort, groups, host, cidr, localCidr, caName, caSha,
|
incoming, proto, startPort, endPort, groups, host, cidr, localCidr, caName, caSha,
|
||||||
)
|
)
|
||||||
@@ -94,15 +86,20 @@ func (b *RulesBuilder) Build(vpnNetworks, unsafeNetworks []netip.Prefix) Rules {
|
|||||||
defaultLocal = append([]netip.Prefix{}, vpnNetworks...)
|
defaultLocal = append([]netip.Prefix{}, vpnNetworks...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
text := []byte(b.ruleText.String())
|
||||||
|
sha := sha256.Sum256(text)
|
||||||
|
h := fnv.New32a()
|
||||||
|
h.Write(text)
|
||||||
|
|
||||||
return Rules{
|
return Rules{
|
||||||
In: b.in.build(defaultLocal),
|
In: buildTable(b.in, defaultLocal),
|
||||||
Out: b.out.build(defaultLocal),
|
Out: buildTable(b.out, defaultLocal),
|
||||||
Hash: hex.EncodeToString(b.ruleSHA.Sum(nil)),
|
Hash: hex.EncodeToString(sha[:]),
|
||||||
HashFNV: b.ruleFNV.Sum32(),
|
HashFNV: h.Sum32(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRule(startPort, endPort int32, groups []string, host, cidr, localCidr, caName, caSha string) (rule, error) {
|
func parseRule(proto uint8, startPort, endPort int32, groups []string, host, cidr, localCidr, caName, caSha string) (rule, error) {
|
||||||
if startPort > endPort {
|
if startPort > endPort {
|
||||||
return rule{}, fmt.Errorf("start port was lower than end port")
|
return rule{}, fmt.Errorf("start port was lower than end port")
|
||||||
}
|
}
|
||||||
@@ -115,6 +112,7 @@ func parseRule(startPort, endPort int32, groups []string, host, cidr, localCidr,
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := rule{
|
r := rule{
|
||||||
|
proto: proto,
|
||||||
startPort: startPort,
|
startPort: startPort,
|
||||||
endPort: endPort,
|
endPort: endPort,
|
||||||
groups: groups,
|
groups: groups,
|
||||||
@@ -146,18 +144,12 @@ func parseRule(startPort, endPort int32, groups []string, host, cidr, localCidr,
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// add keeps r for proto, or for every protocol when proto is ProtoAny.
|
// buildTable turns one direction's rules into a Table. Rules without a local cidr allow defaultLocal,
|
||||||
func (tr *tableRules) add(proto uint8, r rule) {
|
|
||||||
r.proto = proto
|
|
||||||
*tr = append(*tr, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// build turns the rules into a Table. Rules without a local cidr allow defaultLocal,
|
|
||||||
// or any local address when defaultLocal is nil.
|
// or any local address when defaultLocal is nil.
|
||||||
func (tr tableRules) build(defaultLocal []netip.Prefix) *Table {
|
func buildTable(rules []rule, defaultLocal []netip.Prefix) *Table {
|
||||||
t := &Table{rules: make([]rule, len(tr))}
|
t := &Table{rules: make([]rule, len(rules))}
|
||||||
var hasOwn [256]bool
|
var hasOwn [256]bool
|
||||||
for id, r := range tr {
|
for id, r := range rules {
|
||||||
t.rules[id] = r.withDefaultLocal(defaultLocal)
|
t.rules[id] = r.withDefaultLocal(defaultLocal)
|
||||||
if r.proto != ProtoAny {
|
if r.proto != ProtoAny {
|
||||||
hasOwn[r.proto] = true
|
hasOwn[r.proto] = true
|
||||||
|
|||||||
+8
-20
@@ -361,10 +361,11 @@ func parseV6(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
|
|||||||
fp.Fragment = isFragment
|
fp.Fragment = isFragment
|
||||||
fp.FragAny = anyFragment
|
fp.FragAny = anyFragment
|
||||||
fp.IPHdrLen = offset
|
fp.IPHdrLen = offset
|
||||||
|
// fp is reused, so the ports are cleared for the packets that have none: fragments, protocols Nebula
|
||||||
|
// doesn't inspect, and ICMPv6 other than echo. Only the cases below set them.
|
||||||
|
fp.RemotePort, fp.LocalPort = 0, 0
|
||||||
if isFragment {
|
if isFragment {
|
||||||
// Non-first fragments carry no transport header, so we have no ports to read
|
// Non-first fragments carry no transport header, so we have no ports to read
|
||||||
fp.RemotePort = 0
|
|
||||||
fp.LocalPort = 0
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,15 +375,12 @@ func parseV6(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
|
|||||||
if dataLen < offset+4 {
|
if dataLen < offset+4 {
|
||||||
return ErrIPv6PacketTooShort
|
return ErrIPv6PacketTooShort
|
||||||
}
|
}
|
||||||
fp.LocalPort = 0 //incoming vs outgoing doesn't matter for icmpv6
|
|
||||||
switch data[offset] { //icmp type
|
switch data[offset] { //icmp type
|
||||||
case iputil.ICMPv6TypeEchoRequest, iputil.ICMPv6TypeEchoReply:
|
case iputil.ICMPv6TypeEchoRequest, iputil.ICMPv6TypeEchoReply:
|
||||||
if dataLen < offset+6 {
|
if dataLen < offset+6 {
|
||||||
return ErrIPv6PacketTooShort
|
return ErrIPv6PacketTooShort
|
||||||
}
|
}
|
||||||
fp.RemotePort = binary.BigEndian.Uint16(data[offset+4 : offset+6]) //identifier
|
fp.RemotePort = binary.BigEndian.Uint16(data[offset+4 : offset+6]) //identifier, orientation doesn't matter
|
||||||
default:
|
|
||||||
fp.RemotePort = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case iputil.HasPorts(proto):
|
case iputil.HasPorts(proto):
|
||||||
@@ -396,11 +394,6 @@ func parseV6(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
|
|||||||
fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2])
|
fp.LocalPort = binary.BigEndian.Uint16(data[offset : offset+2])
|
||||||
fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
|
fp.RemotePort = binary.BigEndian.Uint16(data[offset+2 : offset+4])
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
|
||||||
// don't set ports for protocols Nebula doesn't inspect
|
|
||||||
fp.RemotePort = 0
|
|
||||||
fp.LocalPort = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -453,16 +446,16 @@ func parseV4(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
|
|||||||
fp.RemoteAddr, _ = netip.AddrFromSlice(data[16:20])
|
fp.RemoteAddr, _ = netip.AddrFromSlice(data[16:20])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fp is reused, so the ports are cleared for the packets that have none: fragments, and protocols Nebula
|
||||||
|
// doesn't inspect, whose first 4 bytes (GRE, IPIP, AH, etc.) are not ports. Only the cases below set them.
|
||||||
|
fp.RemotePort, fp.LocalPort = 0, 0
|
||||||
if fp.Fragment {
|
if fp.Fragment {
|
||||||
fp.RemotePort = 0
|
|
||||||
fp.LocalPort = 0
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case fp.Protocol == iputil.IPProtocolICMP: //note that orientation doesn't matter on ICMP
|
case fp.Protocol == iputil.IPProtocolICMP: //note that orientation doesn't matter on ICMP
|
||||||
fp.RemotePort = binary.BigEndian.Uint16(data[ihl+4 : ihl+6]) //identifier
|
fp.RemotePort = binary.BigEndian.Uint16(data[ihl+4 : ihl+6]) //identifier, LocalPort stays 0 but could be the code, data[ihl+1]
|
||||||
fp.LocalPort = 0 //code would be uint16(data[ihl+1])
|
|
||||||
|
|
||||||
case iputil.HasPorts(fp.Protocol):
|
case iputil.HasPorts(fp.Protocol):
|
||||||
if incoming {
|
if incoming {
|
||||||
@@ -472,11 +465,6 @@ func parseV4(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
|
|||||||
fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
|
fp.LocalPort = binary.BigEndian.Uint16(data[ihl : ihl+2]) //src port
|
||||||
fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
|
fp.RemotePort = binary.BigEndian.Uint16(data[ihl+2 : ihl+4]) //dst port
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
|
||||||
// don't set ports for protocols Nebula doesn't inspect, the first 4 bytes of GRE, IPIP, AH, etc. are not ports
|
|
||||||
fp.RemotePort = 0
|
|
||||||
fp.LocalPort = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in new issue
Block a user