| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797 |
- package nebula
- import (
- "bytes"
- "errors"
- "log/slog"
- "math"
- "net/netip"
- "testing"
- "time"
- "github.com/gaissmai/bart"
- "github.com/slackhq/nebula/cert"
- "github.com/slackhq/nebula/config"
- "github.com/slackhq/nebula/firewall"
- "github.com/slackhq/nebula/iputil"
- "github.com/slackhq/nebula/test"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- func TestNewFirewall(t *testing.T) {
- l := test.NewLogger()
- c := &dummyCert{}
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- conntrack := fw.Conntrack
- assert.NotNil(t, conntrack)
- assert.NotNil(t, conntrack.Conns)
- assert.NotNil(t, conntrack.TimerWheel)
- assert.NotNil(t, fw.InRules)
- assert.NotNil(t, fw.OutRules)
- assert.Equal(t, time.Second, fw.TCPTimeout)
- assert.Equal(t, time.Minute, fw.UDPTimeout)
- assert.Equal(t, time.Hour, fw.DefaultTimeout)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Second, time.Hour, time.Minute, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Hour, time.Second, time.Minute, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Hour, time.Minute, time.Second, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Minute, time.Hour, time.Second, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Minute, time.Second, time.Hour, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- }
- func TestFirewall_AddRule(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- c := &dummyCert{}
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- assert.NotNil(t, fw.InRules)
- assert.NotNil(t, fw.OutRules)
- ti, err := netip.ParsePrefix("1.2.3.4/32")
- require.NoError(t, err)
- ti6, err := netip.ParsePrefix("fd12::34/128")
- require.NoError(t, err)
- require.NoError(t, fw.AddRule(true, iputil.IPProtocolTCP, 1, 1, []string{}, "", "", "", "", ""))
- // An empty rule is any
- assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
- assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
- assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "", ""))
- assert.Nil(t, fw.InRules.UDP[1].Any.Any)
- assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0].Groups, "g1")
- assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, iputil.IPProtocolICMP, 1, 1, []string{}, "h1", "", "", "", ""))
- //no matter what port is given for icmp, it should end up as "any"
- assert.Nil(t, fw.InRules.ICMP[firewall.PortAny].Any.Any)
- assert.Empty(t, fw.InRules.ICMP[firewall.PortAny].Any.Groups)
- assert.Contains(t, fw.InRules.ICMP[firewall.PortAny].Any.Hosts, "h1")
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
- _, ok := fw.OutRules.AnyProto[1].Any.CIDR.Get(ti)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti6.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
- _, ok = fw.OutRules.AnyProto[1].Any.CIDR.Get(ti6)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti.String(), "", ""))
- assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
- ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti6.String(), "", ""))
- assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
- ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti6)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "ca-name", ""))
- assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "", "ca-sha"))
- assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", "", "", "", ""))
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- anyIp, err := netip.ParsePrefix("0.0.0.0/0")
- require.NoError(t, err)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
- table, ok := fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
- assert.True(t, table.Any)
- table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
- assert.False(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- anyIp6, err := netip.ParsePrefix("::/0")
- require.NoError(t, err)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp6.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
- table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
- assert.True(t, table.Any)
- table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
- assert.False(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "any", "", "", ""))
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp.String(), "", ""))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp6.String(), "", ""))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", "any", "", ""))
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- // Test error conditions
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", "", "", "", ""))
- require.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", "", "", "", ""))
- }
- func TestFirewall_Drop(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- c := dummyCert{
- name: "host1",
- networks: []netip.Prefix{netip.MustParsePrefix("1.2.3.4/24")},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &cert.CachedCertificate{
- Certificate: &c,
- InvertedGroups: map[string]struct{}{"default-group": {}},
- },
- },
- vpnAddrs: []netip.Addr{netip.MustParseAddr("1.2.3.4")},
- }
- h.buildNetworks(myVpnNetworksTable, &c)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Drop outbound
- assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // Allow outbound because conntrack
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- // test remote mismatch
- oldRemote := p.RemoteAddr
- p.RemoteAddr = netip.MustParseAddr("1.2.3.10")
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
- p.RemoteAddr = oldRemote
- // ensure signer doesn't get in the way of group checks
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caSha doesn't drop on match
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // ensure ca name doesn't get in the way of group checks
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caName doesn't drop on match
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func TestFirewall_DropV6(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("fd12::34"),
- RemoteAddr: netip.MustParseAddr("fd12::34"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- c := dummyCert{
- name: "host1",
- networks: []netip.Prefix{netip.MustParsePrefix("fd12::34/120")},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &cert.CachedCertificate{
- Certificate: &c,
- InvertedGroups: map[string]struct{}{"default-group": {}},
- },
- },
- vpnAddrs: []netip.Addr{netip.MustParseAddr("fd12::34")},
- }
- h.buildNetworks(myVpnNetworksTable, &c)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Drop outbound
- assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // Allow outbound because conntrack
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- // test remote mismatch
- oldRemote := p.RemoteAddr
- p.RemoteAddr = netip.MustParseAddr("fd12::56")
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
- p.RemoteAddr = oldRemote
- // ensure signer doesn't get in the way of group checks
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caSha doesn't drop on match
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // ensure ca name doesn't get in the way of group checks
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caName doesn't drop on match
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func BenchmarkFirewallTable_match(b *testing.B) {
- f := &Firewall{}
- ft := FirewallTable{
- TCP: firewallPort{},
- }
- pfix := netip.MustParsePrefix("172.1.1.1/32")
- _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix.String(), "", "", "")
- _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix.String(), "", "")
- pfix6 := netip.MustParsePrefix("fd11::11/128")
- _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix6.String(), "", "", "")
- _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix6.String(), "", "")
- cp := cert.NewCAPool()
- b.Run("fail on proto", func(b *testing.B) {
- // This benchmark is showing us the cost of failing to match the protocol
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolUDP}, true, c, cp))
- }
- })
- b.Run("pass proto, fail on port", func(b *testing.B) {
- // This benchmark is showing us the cost of matching a specific protocol but failing to match the port
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 1}, true, c, cp))
- }
- })
- b.Run("pass proto, port, fail on local CIDR", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- ip := netip.MustParsePrefix("9.254.254.254/32")
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
- }
- })
- b.Run("pass proto, port, fail on local CIDRv6", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- ip := netip.MustParsePrefix("fd99::99/128")
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
- }
- })
- b.Run("pass proto, port, any local CIDR, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
- }
- })
- b.Run("pass proto, port, any local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
- }
- })
- b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
- }
- })
- b.Run("pass proto, port, specific local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
- }
- })
- b.Run("pass on group on any local cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- },
- InvertedGroups: map[string]struct{}{"good-group": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
- }
- })
- b.Run("pass on group on specific local cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- },
- InvertedGroups: map[string]struct{}{"good-group": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
- }
- })
- b.Run("pass on group on specific local cidr6", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- },
- InvertedGroups: map[string]struct{}{"good-group": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
- }
- })
- b.Run("pass on name", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "good-host",
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp)
- }
- })
- }
- func TestFirewall_Drop2(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- },
- InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}},
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- c1 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- },
- InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}},
- }
- h1 := HostInfo{
- vpnAddrs: []netip.Addr{network.Addr()},
- ConnectionState: &ConnectionState{
- peerCert: &c1,
- },
- }
- h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // h1/c1 lacks the proper groups
- require.ErrorIs(t, fw.Drop(p, true, &h1, cp, nil), ErrNoMatchingRule)
- // c has the proper groups
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func TestFirewall_Drop3(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 1,
- RemotePort: 1,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host-owner",
- networks: []netip.Prefix{network},
- },
- }
- c1 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- issuer: "signer-sha-bad",
- },
- }
- h1 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c1,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
- c2 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host2",
- networks: []netip.Prefix{network},
- issuer: "signer-sha",
- },
- }
- h2 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c2,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h2.buildNetworks(myVpnNetworksTable, c2.Certificate)
- c3 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host3",
- networks: []netip.Prefix{network},
- issuer: "signer-sha-bad",
- },
- }
- h3 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c3,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h3.buildNetworks(myVpnNetworksTable, c3.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", "", "", "", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", "signer-sha"))
- cp := cert.NewCAPool()
- // c1 should pass because host match
- require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
- // c2 should pass because ca sha match
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h2, cp, nil))
- // c3 should fail because no match
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(p, true, &h3, cp, nil), ErrNoMatchingRule)
- // Test a remote address match
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "1.2.3.4/24", "", "", ""))
- require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
- }
- func TestFirewall_Drop3V6(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("fd12::34"),
- RemoteAddr: netip.MustParseAddr("fd12::34"),
- LocalPort: 1,
- RemotePort: 1,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("fd12::34/120")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host-owner",
- networks: []netip.Prefix{network},
- },
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- // Test a remote address match
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- cp := cert.NewCAPool()
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "fd12::34/120", "", "", ""))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func TestFirewall_DropConntrackReload(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- },
- InvertedGroups: map[string]struct{}{"default-group": {}},
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Drop outbound
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // Allow outbound because conntrack
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- oldFw := fw
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", "", "", "", ""))
- fw.Conntrack = oldFw.Conntrack
- fw.rulesVersion = oldFw.rulesVersion + 1
- // Allow outbound because conntrack and new rules allow port 10
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- oldFw = fw
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", "", "", "", ""))
- fw.Conntrack = oldFw.Conntrack
- fw.rulesVersion = oldFw.rulesVersion + 1
- // Drop outbound because conntrack doesn't match new ruleset
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
- }
- func TestFirewall_ICMPPortBehavior(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- },
- InvertedGroups: map[string]struct{}{"default-group": {}},
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- cp := cert.NewCAPool()
- templ := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- Protocol: iputil.IPProtocolICMP,
- Fragment: false,
- }
- t.Run("ICMP allowed", func(t *testing.T) {
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, iputil.IPProtocolICMP, 0, 0, []string{"any"}, "", "", "", "", ""))
- t.Run("zero ports", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0
- p.RemotePort = 0
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- })
- t.Run("nonzero ports", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0xabcd
- p.RemotePort = 0x1234
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- })
- })
- t.Run("Any proto, some ports allowed", func(t *testing.T) {
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 80, 444, []string{"any"}, "", "", "", "", ""))
- t.Run("zero ports, still blocked", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0
- p.RemotePort = 0
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule)
- //now also allow outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- t.Run("nonzero ports, still blocked", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0xabcd
- p.RemotePort = 0x1234
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule)
- //now also allow outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- t.Run("nonzero, matching ports, still blocked", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 80
- p.RemotePort = 80
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule)
- //now also allow outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- })
- t.Run("Any proto, any port", func(t *testing.T) {
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- t.Run("zero ports, allowed", func(t *testing.T) {
- resetConntrack(fw)
- p := templ.Copy()
- p.LocalPort = 0
- p.RemotePort = 0
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- })
- t.Run("nonzero ports, allowed", func(t *testing.T) {
- resetConntrack(fw)
- p := templ.Copy()
- p.LocalPort = 0xabcd
- p.RemotePort = 0x1234
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- //different ID is blocked
- p.RemotePort++
- require.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- })
- }
- func TestFirewall_DropIPSpoofing(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host-owner",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
- },
- }
- c1 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
- unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
- },
- }
- h1 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c1,
- },
- vpnAddrs: []netip.Addr{c1.Certificate.Networks()[0].Addr()},
- }
- h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Packet spoofed by `c1`. Note that the remote addr is not a valid one.
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("192.0.2.1"),
- RemoteAddr: netip.MustParseAddr("192.0.2.3"),
- LocalPort: 1,
- RemotePort: 1,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- assert.Equal(t, fw.Drop(p, true, &h1, cp, nil), ErrInvalidRemoteIP)
- }
- func TestFirewall_ConntrackSourceSpoofingAcrossPeers(t *testing.T) {
- l := test.NewLoggerWithOutput(&bytes.Buffer{})
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
- owner := &dummyCert{
- name: "owner",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
- }
- victim := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "victim",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
- },
- }
- victimHI := HostInfo{
- ConnectionState: &ConnectionState{peerCert: victim},
- vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")},
- }
- victimHI.buildNetworks(myVpnNetworksTable, victim.Certificate)
- attacker := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "attacker",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.3/24")},
- },
- }
- attackerHI := HostInfo{
- ConnectionState: &ConnectionState{peerCert: attacker},
- vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.3")},
- }
- attackerHI.buildNetworks(myVpnNetworksTable, attacker.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
- // Allow any inbound traffic that passes the cert / source-IP checks.
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- flow := firewall.Packet{
- LocalAddr: netip.MustParseAddr("192.0.2.1"),
- RemoteAddr: netip.MustParseAddr("192.0.2.2"),
- LocalPort: 443,
- RemotePort: 55000,
- Protocol: iputil.IPProtocolUDP,
- }
- require.NoError(t, fw.Drop(flow, true, &victimHI, cp, nil),
- "victim's own traffic from its own overlay IP must be allowed")
- unseen := flow
- unseen.RemotePort = 55001
- assert.Equal(t, ErrInvalidRemoteIP, fw.Drop(unseen, true, &attackerHI, cp, nil),
- "sanity: attacker forging victim's source IP must be rejected when no conntrack entry exists")
- got := fw.Drop(flow, true, &attackerHI, cp, nil)
- t.Logf("attacker replaying victim's 4-tuple: Drop returned %v (nil == packet ALLOWED == spoof succeeded)", got)
- assert.Equal(t, ErrInvalidRemoteIP, got,
- "SECURITY: attacker spoofed victim's overlay source IP (192.0.2.2) by reusing an existing conntrack 4-tuple; Drop returned %v instead of rejecting", got)
- }
- // BenchmarkFirewallDropConntrackHit measures Drop on an already-established flow
- // (a conntrack hit). This is the fast path that the source-IP<->cert binding
- // reordering adds work to, so it quantifies the cost of moving the address checks
- // ahead of the conntrack lookup. Cases:
- // - simple: peer cert has one address, no unsafe networks (h.networks == nil),
- // so the remote-address check is a single netip.Addr compare.
- // - complex: peer cert has unsafe networks (h.networks populated), so the
- // remote-address check is a BART lookup.
- // - noCache/localCache: whether a per-batch ConntrackCache is supplied, which in
- // the original code let the fast path skip straight past the address checks.
- func BenchmarkFirewallDropConntrackHit(b *testing.B) {
- l := test.NewLoggerWithOutput(&bytes.Buffer{})
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
- owner := &dummyCert{
- name: "owner",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
- }
- simpleCert := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "simple",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
- },
- }
- simpleHost := &HostInfo{
- ConnectionState: &ConnectionState{peerCert: simpleCert},
- vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")},
- }
- simpleHost.buildNetworks(myVpnNetworksTable, simpleCert.Certificate)
- complexCert := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "complex",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
- unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
- },
- }
- complexHost := &HostInfo{
- ConnectionState: &ConnectionState{peerCert: complexCert},
- vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")},
- }
- complexHost.buildNetworks(myVpnNetworksTable, complexCert.Certificate)
- cp := cert.NewCAPool()
- flow := firewall.Packet{
- LocalAddr: netip.MustParseAddr("192.0.2.1"),
- RemoteAddr: netip.MustParseAddr("192.0.2.2"),
- LocalPort: 443,
- RemotePort: 55000,
- Protocol: iputil.IPProtocolUDP,
- }
- cases := []struct {
- name string
- host *HostInfo
- useCache bool
- }{
- {"simple/noCache", simpleHost, false},
- {"simple/localCache", simpleHost, true},
- {"complex/noCache", complexHost, false},
- {"complex/localCache", complexHost, true},
- }
- for _, tc := range cases {
- b.Run(tc.name, func(b *testing.B) {
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
- require.NoError(b, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- // Establish the conntrack entry so every benchmarked Drop is a hit.
- require.NoError(b, fw.Drop(flow, true, tc.host, cp, nil))
- var cache firewall.ConntrackCache
- if tc.useCache {
- cache = firewall.ConntrackCache{}
- }
- b.ReportAllocs()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- if err := fw.Drop(flow, true, tc.host, cp, cache); err != nil {
- b.Fatal(err)
- }
- }
- })
- }
- }
- func BenchmarkLookup(b *testing.B) {
- ml := func(m map[string]struct{}, a [][]string) {
- for n := 0; n < b.N; n++ {
- for _, sg := range a {
- found := false
- for _, g := range sg {
- if _, ok := m[g]; !ok {
- found = false
- break
- }
- found = true
- }
- if found {
- return
- }
- }
- }
- }
- b.Run("array to map best", func(b *testing.B) {
- m := map[string]struct{}{
- "1ne": {},
- "2wo": {},
- "3hr": {},
- "4ou": {},
- "5iv": {},
- "6ix": {},
- }
- a := [][]string{
- {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "six"},
- }
- for n := 0; n < b.N; n++ {
- ml(m, a)
- }
- })
- b.Run("array to map worst", func(b *testing.B) {
- m := map[string]struct{}{
- "one": {},
- "two": {},
- "thr": {},
- "fou": {},
- "fiv": {},
- "six": {},
- }
- a := [][]string{
- {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "six"},
- }
- for n := 0; n < b.N; n++ {
- ml(m, a)
- }
- })
- }
- func Test_parsePort(t *testing.T) {
- _, _, err := parsePort("")
- require.EqualError(t, err, "was not a number; ``")
- _, _, err = parsePort(" ")
- require.EqualError(t, err, "was not a number; ` `")
- _, _, err = parsePort("-")
- require.EqualError(t, err, "appears to be a range but could not be parsed; `-`")
- _, _, err = parsePort(" - ")
- require.EqualError(t, err, "appears to be a range but could not be parsed; ` - `")
- _, _, err = parsePort("a-b")
- require.EqualError(t, err, "beginning range was not a number; `a`")
- _, _, err = parsePort("1-b")
- require.EqualError(t, err, "ending range was not a number; `b`")
- s, e, err := parsePort(" 1 - 2 ")
- assert.Equal(t, int32(1), s)
- assert.Equal(t, int32(2), e)
- require.NoError(t, err)
- s, e, err = parsePort("0-1")
- assert.Equal(t, int32(0), s)
- assert.Equal(t, int32(0), e)
- require.NoError(t, err)
- s, e, err = parsePort("9919")
- assert.Equal(t, int32(9919), s)
- assert.Equal(t, int32(9919), e)
- require.NoError(t, err)
- s, e, err = parsePort("any")
- assert.Equal(t, int32(0), s)
- assert.Equal(t, int32(0), e)
- require.NoError(t, err)
- }
- // Test_parsePort_invalid covers inputs that must error. The named bug is
- // that int32(strconv.Atoi("4294967296")) truncates to 0 == firewall.PortAny,
- // silently turning a typo into a match-all-ports rule; the rest are
- // representative syntax/range probes.
- func Test_parsePort_invalid(t *testing.T) {
- tests := []struct {
- name string
- input string
- wantErrContains string
- }{
- // Numeric overflow (the named bug + boundary).
- {"named bug: 2^32 truncates to PortAny", "4294967296", "out of range"},
- {"just above max real port", "65536", "out of range"},
- // Negatives route through the range branch and hit the empty-half
- // guard; included as defense in depth so a future refactor cannot
- // accidentally reach the int32 cast.
- {"negative", "-1", "could not be parsed"},
- // Syntax probes.
- {"NUL between digits", "4\x002", "was not a number"},
- {"hex notation", "0x10", "was not a number"},
- {"scientific notation", "1e3", "was not a number"},
- {"leading whitespace", " 42", "was not a number"},
- {"fullwidth digits", "42", "was not a number"},
- // Range branch.
- {"range upper out of range", "1-65536", "ending range out of range"},
- {"range lower out of range", "65536-65537", "beginning range out of range"},
- {"range with negative upper", "1--1", "ending range was not a number"},
- }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- _, _, err := parsePort(tc.input)
- require.Error(t, err, "input %q must error", tc.input)
- require.ErrorContains(t, err, tc.wantErrContains)
- })
- }
- }
- // Test_parsePort_valid_boundaries locks in success cases at 0, 1, and 65535
- // so a future refactor cannot regress the boundaries.
- func Test_parsePort_valid_boundaries(t *testing.T) {
- tests := []struct {
- name string
- input string
- wantStart int32
- wantEnd int32
- }{
- {"zero is PortAny", "0", 0, 0},
- {"min real port", "1", 1, 1},
- {"max real port", "65535", 65535, 65535},
- {"range zero to max forces end to zero", "0-65535", 0, 0},
- {"range max to max", "65535-65535", 65535, 65535},
- {"range one to max", "1-65535", 1, 65535},
- {"range with whitespace inside", " 1 - 2 ", 1, 2},
- }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- s, e, err := parsePort(tc.input)
- require.NoError(t, err)
- assert.Equal(t, tc.wantStart, s, "start port")
- assert.Equal(t, tc.wantEnd, e, "end port")
- })
- }
- }
- func TestNewFirewallFromConfig(t *testing.T) {
- l := test.NewLogger()
- // Test a bad rule definition
- c := &dummyCert{}
- cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil, "aes")
- require.NoError(t, err)
- conf := config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"outbound": "asdf"}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
- // Test both port and code
- conf = config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "code": "2"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; only one of port or code should be provided")
- // Test missing host, group, cidr, ca_name and ca_sha
- conf = config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; at least one of host, group, cidr, local_cidr, ca_name, or ca_sha must be provided")
- // Test code/port error
- conf = config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "a", "host": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`")
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "a", "host": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
- // Test proto error
- conf = config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "host": "testh"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
- // Test cidr parse error
- conf = config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "cidr": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
- // Test local_cidr parse error
- conf = config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "local_cidr": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
- // Test both group and groups
- conf = config.NewC(test.NewLogger())
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
- }
- func TestAddFirewallRulesFromConfig(t *testing.T) {
- l := test.NewLogger()
- // Test adding tcp rule
- conf := config.NewC(test.NewLogger())
- mf := &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "tcp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding udp rule
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "udp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding icmp rule
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "icmp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding icmp rule no port
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"proto": "icmp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding any rule
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding rule with cidr
- cidr := netip.MustParsePrefix("10.0.0.0/8")
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr.String(), localIp: ""}, mf.lastCall)
- // Test adding rule with local_cidr
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr.String()}, mf.lastCall)
- // Test adding rule with cidr ipv6
- cidr6 := netip.MustParsePrefix("fd00::/8")
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr6.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr6.String(), localIp: ""}, mf.lastCall)
- // Test adding rule with any cidr
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "any"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "any", localIp: ""}, mf.lastCall)
- // Test adding rule with junk cidr
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "junk/junk"}}}
- require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP")
- // Test adding rule with local_cidr ipv6
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr6.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr6.String()}, mf.lastCall)
- // Test adding rule with any local_cidr
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "any"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, localIp: "any"}, mf.lastCall)
- // Test adding rule with junk local_cidr
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "junk/junk"}}}
- require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP")
- // Test adding rule with ca_sha
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caSha: "12312313123"}, mf.lastCall)
- // Test adding rule with ca_name
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_name": "root01"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caName: "root01"}, mf.lastCall)
- // Test single group
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
- // Test single groups
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
- // Test multiple AND groups
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: "", localIp: ""}, mf.lastCall)
- // Test group and groups values that used to crash or become a group that matches nothing: an empty array,
- // a null, an array, or a map is rejected
- badGroups := []struct {
- key string
- value any
- err string
- }{
- {"group", []any{}, "group should contain a single value, an empty array was provided"},
- {"group", []any{nil}, "group should contain a single value, a null was provided"},
- {"group", []any{[]any{"a"}}, "group should contain a single value, an array was provided"},
- {"group", map[string]any{"a": "b"}, "group should contain a single value, a map was provided"},
- {"groups", []any{nil}, "groups entry #0 should contain a single value, a null was provided"},
- {"groups", []any{"a", []any{"b"}}, "groups entry #1 should contain a single value, an array was provided"},
- {"groups", []any{map[string]any{"a": "b"}}, "groups entry #0 should contain a single value, a map was provided"},
- {"groups", map[string]any{"a": "b"}, "groups should contain an array or a single value, a map was provided"},
- {"group", 1.1, "group should contain a single value, 1.1 was read as a float64, quote it to use it as a group name"},
- {"group", []any{1.1}, "group should contain a single value, 1.1 was read as a float64, quote it to use it as a group name"},
- {"groups", []any{"a", 1.1}, "groups entry #1 should contain a single value, 1.1 was read as a float64, quote it to use it as a group name"},
- {"groups", time.Date(2001, 12, 14, 0, 0, 0, 0, time.UTC), "groups should contain an array or a single value, 2001-12-14 00:00:00 +0000 UTC was read as a time.Time, quote it to use it as a group name"},
- }
- for _, tc := range badGroups {
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", tc.key: tc.value}}}
- require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; "+tc.err, "%s: %v", tc.key, tc.value)
- }
- // Test int and bool groups entries are read as strings
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": []any{1, true}}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, []string{"1", "true"}, mf.lastCall.groups)
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": 1}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, []string{"1"}, mf.lastCall.groups)
- // Test a null group or groups is the same as not providing it
- for _, key := range []string{"group", "groups"} {
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", key: nil, "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Empty(t, mf.lastCall.groups)
- assert.Equal(t, "a", mf.lastCall.host)
- }
- // Test Add error
- conf = config.NewC(test.NewLogger())
- mf = &mockFirewall{}
- mf.nextCallReturn = errors.New("test error")
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
- require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
- }
- func TestFirewall_convertRule(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- // Ensure group array of 1 is converted and a warning is printed
- c := map[string]any{
- "group": []any{"group1"},
- }
- r, err := convertRule(l, c, "test", 1)
- assert.Contains(t, ob.String(), "group was an array with a single value, converting to simple value")
- assert.Contains(t, ob.String(), "table=test")
- assert.Contains(t, ob.String(), "rule=1")
- require.NoError(t, err)
- assert.Equal(t, []string{"group1"}, r.Groups)
- // Ensure group array of > 1 is errord
- ob.Reset()
- c = map[string]any{
- "group": []any{"group1", "group2"},
- }
- r, err = convertRule(l, c, "test", 1)
- assert.Empty(t, ob.String())
- require.Error(t, err, "group should contain a single value, an array with more than one entry was provided")
- // Make sure a well formed group is alright
- ob.Reset()
- c = map[string]any{
- "group": "group1",
- }
- r, err = convertRule(l, c, "test", 1)
- require.NoError(t, err)
- assert.Equal(t, []string{"group1"}, r.Groups)
- }
- func TestFirewall_convertRuleSanity(t *testing.T) {
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- noWarningPlease := []map[string]any{
- {"group": "group1"},
- {"groups": []any{"group2"}},
- {"host": "bob"},
- {"cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "host": "bob"},
- {"cidr": "1.1.1.1/1", "host": "bob"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
- }
- for _, c := range noWarningPlease {
- r, err := convertRule(l, c, "test", 1)
- require.NoError(t, err)
- require.NoError(t, r.sanity(), "should not generate a sanity warning, %+v", c)
- }
- yesWarningPlease := []map[string]any{
- {"group": "group1"},
- {"groups": []any{"group2"}},
- {"cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "host": "bob"},
- {"cidr": "1.1.1.1/1", "host": "bob"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
- }
- for _, c := range yesWarningPlease {
- c["host"] = "any"
- r, err := convertRule(l, c, "test", 1)
- require.NoError(t, err)
- err = r.sanity()
- require.Error(t, err, "I wanted a warning: %+v", c)
- }
- //reset the list
- yesWarningPlease = []map[string]any{
- {"group": "group1"},
- {"groups": []any{"group2"}},
- {"cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "host": "bob"},
- {"cidr": "1.1.1.1/1", "host": "bob"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
- }
- for _, c := range yesWarningPlease {
- r, err := convertRule(l, c, "test", 1)
- require.NoError(t, err)
- r.Groups = append(r.Groups, "any")
- err = r.sanity()
- require.Error(t, err, "I wanted a warning: %+v", c)
- }
- }
- type testcase struct {
- h *HostInfo
- p firewall.Packet
- c cert.Certificate
- err error
- }
- func (c *testcase) Test(t *testing.T, fw *Firewall) {
- t.Helper()
- cp := cert.NewCAPool()
- resetConntrack(fw)
- err := fw.Drop(c.p, true, c.h, cp, nil)
- if c.err == nil {
- require.NoError(t, err, "failed to not drop remote address %s", c.p.RemoteAddr)
- } else {
- require.ErrorIs(t, c.err, err, "failed to drop remote address %s", c.p.RemoteAddr)
- }
- }
- func buildTestCase(setup testsetup, err error, theirPrefixes ...netip.Prefix) testcase {
- c1 := dummyCert{
- name: "host1",
- networks: theirPrefixes,
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &cert.CachedCertificate{
- Certificate: &c1,
- InvertedGroups: map[string]struct{}{"default-group": {}},
- },
- },
- vpnAddrs: make([]netip.Addr, len(theirPrefixes)),
- }
- for i := range theirPrefixes {
- h.vpnAddrs[i] = theirPrefixes[i].Addr()
- }
- h.buildNetworks(setup.myVpnNetworksTable, &c1)
- p := firewall.Packet{
- LocalAddr: setup.c.Networks()[0].Addr(), //todo?
- RemoteAddr: theirPrefixes[0].Addr(),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: iputil.IPProtocolUDP,
- Fragment: false,
- }
- return testcase{
- h: &h,
- p: p,
- c: &c1,
- err: err,
- }
- }
- type testsetup struct {
- c dummyCert
- myVpnNetworksTable *bart.Lite
- fw *Firewall
- }
- func newSetup(t *testing.T, l *slog.Logger, myPrefixes ...netip.Prefix) testsetup {
- c := dummyCert{
- name: "me",
- networks: myPrefixes,
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- return newSetupFromCert(t, l, c)
- }
- func newSetupFromCert(t *testing.T, l *slog.Logger, c dummyCert) testsetup {
- myVpnNetworksTable := new(bart.Lite)
- for _, prefix := range c.Networks() {
- myVpnNetworksTable.Insert(prefix)
- }
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- return testsetup{
- c: c,
- fw: fw,
- myVpnNetworksTable: myVpnNetworksTable,
- }
- }
- func TestFirewall_Drop_EnforceIPMatch(t *testing.T) {
- t.Parallel()
- ob := &bytes.Buffer{}
- l := test.NewLoggerWithOutput(ob)
- myPrefix := netip.MustParsePrefix("1.1.1.1/8")
- // for now, it's okay that these are all "incoming", the logic this test tries to check doesn't care about in/out
- t.Run("allow inbound all matching", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, nil, netip.MustParsePrefix("1.2.3.4/24"))
- tc.Test(t, setup.fw)
- })
- t.Run("allow inbound local matching", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrInvalidLocalIP, netip.MustParsePrefix("1.2.3.4/24"))
- tc.p.LocalAddr = netip.MustParseAddr("1.2.3.8")
- tc.Test(t, setup.fw)
- })
- t.Run("block inbound remote mismatched", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrInvalidRemoteIP, netip.MustParsePrefix("1.2.3.4/24"))
- tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
- tc.Test(t, setup.fw)
- })
- t.Run("Block a vpn peer packet", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrPeerRejected, netip.MustParsePrefix("2.2.2.2/24"))
- tc.Test(t, setup.fw)
- })
- twoPrefixes := []netip.Prefix{
- netip.MustParsePrefix("1.2.3.4/24"), netip.MustParsePrefix("2.2.2.2/24"),
- }
- t.Run("allow inbound one matching", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, nil, twoPrefixes...)
- tc.Test(t, setup.fw)
- })
- t.Run("block inbound multimismatch", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrInvalidRemoteIP, twoPrefixes...)
- tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
- tc.Test(t, setup.fw)
- })
- t.Run("allow inbound 2nd one matching", func(t *testing.T) {
- t.Parallel()
- setup2 := newSetup(t, l, netip.MustParsePrefix("2.2.2.1/24"))
- tc := buildTestCase(setup2, nil, twoPrefixes...)
- tc.p.RemoteAddr = twoPrefixes[1].Addr()
- tc.Test(t, setup2.fw)
- })
- t.Run("allow inbound unsafe route", func(t *testing.T) {
- t.Parallel()
- unsafePrefix := netip.MustParsePrefix("192.168.0.0/24")
- c := dummyCert{
- name: "me",
- networks: []netip.Prefix{myPrefix},
- unsafeNetworks: []netip.Prefix{unsafePrefix},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- unsafeSetup := newSetupFromCert(t, l, c)
- tc := buildTestCase(unsafeSetup, nil, twoPrefixes...)
- tc.p.LocalAddr = netip.MustParseAddr("192.168.0.3")
- tc.err = ErrNoMatchingRule
- tc.Test(t, unsafeSetup.fw) //should hit firewall and bounce off
- require.NoError(t, unsafeSetup.fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", unsafePrefix.String(), "", ""))
- tc.err = nil
- tc.Test(t, unsafeSetup.fw) //should pass
- })
- }
- type addRuleCall struct {
- incoming bool
- proto uint8
- startPort int32
- endPort int32
- groups []string
- host string
- ip string
- localIp string
- caName string
- caSha string
- }
- type mockFirewall struct {
- lastCall addRuleCall
- nextCallReturn error
- }
- func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip, localIp, caName string, caSha string) error {
- mf.lastCall = addRuleCall{
- incoming: incoming,
- proto: proto,
- startPort: startPort,
- endPort: endPort,
- groups: groups,
- host: host,
- ip: ip,
- localIp: localIp,
- caName: caName,
- caSha: caSha,
- }
- err := mf.nextCallReturn
- mf.nextCallReturn = nil
- return err
- }
- func resetConntrack(fw *Firewall) {
- fw.Conntrack.Lock()
- fw.Conntrack.Conns = map[firewall.Packet]*conn{}
- fw.Conntrack.Unlock()
- }
|