firewall_test.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. package nebula
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "log/slog"
  7. "math"
  8. "net/netip"
  9. "testing"
  10. "time"
  11. "github.com/gaissmai/bart"
  12. "github.com/slackhq/nebula/cert"
  13. "github.com/slackhq/nebula/config"
  14. "github.com/slackhq/nebula/firewall"
  15. "github.com/slackhq/nebula/iputil"
  16. "github.com/slackhq/nebula/test"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. )
  20. func TestNewFirewall(t *testing.T) {
  21. l := test.NewLogger()
  22. c := &dummyCert{}
  23. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  24. assert.NotNil(t, fw.Conntrack)
  25. assert.NotNil(t, fw.InRules)
  26. assert.NotNil(t, fw.OutRules)
  27. assert.Equal(t, time.Second, fw.TCPTimeout)
  28. assert.Equal(t, time.Minute, fw.UDPTimeout)
  29. assert.Equal(t, time.Hour, fw.DefaultTimeout)
  30. }
  31. func TestFirewall_AddRule(t *testing.T) {
  32. ob := &bytes.Buffer{}
  33. l := test.NewLoggerWithOutput(ob)
  34. c := &dummyCert{}
  35. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  36. assert.NotNil(t, fw.InRules)
  37. assert.NotNil(t, fw.OutRules)
  38. ti, err := netip.ParsePrefix("1.2.3.4/32")
  39. require.NoError(t, err)
  40. ti6, err := netip.ParsePrefix("fd12::34/128")
  41. require.NoError(t, err)
  42. require.NoError(t, fw.AddRule(true, iputil.IPProtocolTCP, 1, 1, []string{}, "", "", "", "", ""))
  43. // An empty rule is any
  44. assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
  45. assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
  46. assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
  47. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  48. require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "", ""))
  49. assert.Nil(t, fw.InRules.UDP[1].Any.Any)
  50. assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0].Groups, "g1")
  51. assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
  52. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  53. require.NoError(t, fw.AddRule(true, iputil.IPProtocolICMP, 1, 1, []string{}, "h1", "", "", "", ""))
  54. //no matter what port is given for icmp, it should end up as "any"
  55. assert.Nil(t, fw.InRules.ICMP[firewall.PortAny].Any.Any)
  56. assert.Empty(t, fw.InRules.ICMP[firewall.PortAny].Any.Groups)
  57. assert.Contains(t, fw.InRules.ICMP[firewall.PortAny].Any.Hosts, "h1")
  58. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  59. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti.String(), "", "", ""))
  60. assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
  61. _, ok := fw.OutRules.AnyProto[1].Any.CIDR.Get(ti)
  62. assert.True(t, ok)
  63. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  64. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti6.String(), "", "", ""))
  65. assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
  66. _, ok = fw.OutRules.AnyProto[1].Any.CIDR.Get(ti6)
  67. assert.True(t, ok)
  68. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  69. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti.String(), "", ""))
  70. assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
  71. ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti)
  72. assert.True(t, ok)
  73. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  74. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti6.String(), "", ""))
  75. assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
  76. ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti6)
  77. assert.True(t, ok)
  78. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  79. require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "ca-name", ""))
  80. assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
  81. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  82. require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "", "ca-sha"))
  83. assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
  84. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  85. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", "", "", "", ""))
  86. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  87. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  88. anyIp, err := netip.ParsePrefix("0.0.0.0/0")
  89. require.NoError(t, err)
  90. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp.String(), "", "", ""))
  91. assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
  92. table, ok := fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
  93. assert.True(t, table.Any)
  94. table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
  95. assert.False(t, ok)
  96. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  97. anyIp6, err := netip.ParsePrefix("::/0")
  98. require.NoError(t, err)
  99. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp6.String(), "", "", ""))
  100. assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
  101. table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
  102. assert.True(t, table.Any)
  103. table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
  104. assert.False(t, ok)
  105. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  106. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "any", "", "", ""))
  107. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  108. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  109. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp.String(), "", ""))
  110. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  111. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
  112. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
  113. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  114. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp6.String(), "", ""))
  115. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  116. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
  117. assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
  118. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  119. require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", "any", "", ""))
  120. assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
  121. // Test error conditions
  122. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
  123. require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", "", "", "", ""))
  124. require.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", "", "", "", ""))
  125. }
  126. func TestFirewall_Drop(t *testing.T) {
  127. ob := &bytes.Buffer{}
  128. l := test.NewLoggerWithOutput(ob)
  129. myVpnNetworksTable := new(bart.Lite)
  130. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  131. p := firewall.Packet{
  132. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  133. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  134. LocalPort: 10,
  135. RemotePort: 90,
  136. Protocol: iputil.IPProtocolUDP,
  137. Fragment: false,
  138. }
  139. c := dummyCert{
  140. name: "host1",
  141. networks: []netip.Prefix{netip.MustParsePrefix("1.2.3.4/24")},
  142. groups: []string{"default-group"},
  143. issuer: "signer-shasum",
  144. }
  145. h := HostInfo{
  146. ConnectionState: &ConnectionState{
  147. peerCert: &cert.CachedCertificate{
  148. Certificate: &c,
  149. InvertedGroups: map[string]struct{}{"default-group": {}},
  150. },
  151. },
  152. vpnAddrs: []netip.Addr{netip.MustParseAddr("1.2.3.4")},
  153. }
  154. h.buildNetworks(myVpnNetworksTable, &c)
  155. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  156. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  157. cp := cert.NewCAPool()
  158. // Drop outbound
  159. assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp))
  160. // Allow inbound
  161. resetConntrack(fw)
  162. require.NoError(t, fw.Drop(p, true, &h, cp))
  163. // Allow outbound because conntrack
  164. require.NoError(t, fw.Drop(p, false, &h, cp))
  165. // test remote mismatch
  166. oldRemote := p.RemoteAddr
  167. p.RemoteAddr = netip.MustParseAddr("1.2.3.10")
  168. assert.Equal(t, fw.Drop(p, false, &h, cp), ErrInvalidRemoteIP)
  169. p.RemoteAddr = oldRemote
  170. // ensure signer doesn't get in the way of group checks
  171. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  172. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
  173. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
  174. assert.Equal(t, fw.Drop(p, true, &h, cp), ErrNoMatchingRule)
  175. // test caSha doesn't drop on match
  176. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  177. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
  178. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
  179. require.NoError(t, fw.Drop(p, true, &h, cp))
  180. // ensure ca name doesn't get in the way of group checks
  181. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  182. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  183. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
  184. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
  185. assert.Equal(t, fw.Drop(p, true, &h, cp), ErrNoMatchingRule)
  186. // test caName doesn't drop on match
  187. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  188. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  189. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
  190. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
  191. require.NoError(t, fw.Drop(p, true, &h, cp))
  192. }
  193. func TestFirewall_DropV6(t *testing.T) {
  194. ob := &bytes.Buffer{}
  195. l := test.NewLoggerWithOutput(ob)
  196. myVpnNetworksTable := new(bart.Lite)
  197. myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
  198. p := firewall.Packet{
  199. LocalAddr: netip.MustParseAddr("fd12::34"),
  200. RemoteAddr: netip.MustParseAddr("fd12::34"),
  201. LocalPort: 10,
  202. RemotePort: 90,
  203. Protocol: iputil.IPProtocolUDP,
  204. Fragment: false,
  205. }
  206. c := dummyCert{
  207. name: "host1",
  208. networks: []netip.Prefix{netip.MustParsePrefix("fd12::34/120")},
  209. groups: []string{"default-group"},
  210. issuer: "signer-shasum",
  211. }
  212. h := HostInfo{
  213. ConnectionState: &ConnectionState{
  214. peerCert: &cert.CachedCertificate{
  215. Certificate: &c,
  216. InvertedGroups: map[string]struct{}{"default-group": {}},
  217. },
  218. },
  219. vpnAddrs: []netip.Addr{netip.MustParseAddr("fd12::34")},
  220. }
  221. h.buildNetworks(myVpnNetworksTable, &c)
  222. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  223. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  224. cp := cert.NewCAPool()
  225. // Drop outbound
  226. assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp))
  227. // Allow inbound
  228. resetConntrack(fw)
  229. require.NoError(t, fw.Drop(p, true, &h, cp))
  230. // Allow outbound because conntrack
  231. require.NoError(t, fw.Drop(p, false, &h, cp))
  232. // test remote mismatch
  233. oldRemote := p.RemoteAddr
  234. p.RemoteAddr = netip.MustParseAddr("fd12::56")
  235. assert.Equal(t, fw.Drop(p, false, &h, cp), ErrInvalidRemoteIP)
  236. p.RemoteAddr = oldRemote
  237. // ensure signer doesn't get in the way of group checks
  238. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  239. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
  240. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
  241. assert.Equal(t, fw.Drop(p, true, &h, cp), ErrNoMatchingRule)
  242. // test caSha doesn't drop on match
  243. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  244. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
  245. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
  246. require.NoError(t, fw.Drop(p, true, &h, cp))
  247. // ensure ca name doesn't get in the way of group checks
  248. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  249. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  250. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
  251. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
  252. assert.Equal(t, fw.Drop(p, true, &h, cp), ErrNoMatchingRule)
  253. // test caName doesn't drop on match
  254. cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
  255. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  256. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
  257. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
  258. require.NoError(t, fw.Drop(p, true, &h, cp))
  259. }
  260. func BenchmarkFirewallTable_match(b *testing.B) {
  261. f := &Firewall{}
  262. ft := FirewallTable{
  263. TCP: firewallPort{},
  264. }
  265. pfix := netip.MustParsePrefix("172.1.1.1/32")
  266. _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix.String(), "", "", "")
  267. _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix.String(), "", "")
  268. pfix6 := netip.MustParsePrefix("fd11::11/128")
  269. _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix6.String(), "", "", "")
  270. _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix6.String(), "", "")
  271. cp := cert.NewCAPool()
  272. b.Run("fail on proto", func(b *testing.B) {
  273. // This benchmark is showing us the cost of failing to match the protocol
  274. c := &cert.CachedCertificate{
  275. Certificate: &dummyCert{},
  276. }
  277. for n := 0; n < b.N; n++ {
  278. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolUDP}, true, c, cp))
  279. }
  280. })
  281. b.Run("pass proto, fail on port", func(b *testing.B) {
  282. // This benchmark is showing us the cost of matching a specific protocol but failing to match the port
  283. c := &cert.CachedCertificate{
  284. Certificate: &dummyCert{},
  285. }
  286. for n := 0; n < b.N; n++ {
  287. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 1}, true, c, cp))
  288. }
  289. })
  290. b.Run("pass proto, port, fail on local CIDR", func(b *testing.B) {
  291. c := &cert.CachedCertificate{
  292. Certificate: &dummyCert{},
  293. }
  294. ip := netip.MustParsePrefix("9.254.254.254/32")
  295. for n := 0; n < b.N; n++ {
  296. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
  297. }
  298. })
  299. b.Run("pass proto, port, fail on local CIDRv6", func(b *testing.B) {
  300. c := &cert.CachedCertificate{
  301. Certificate: &dummyCert{},
  302. }
  303. ip := netip.MustParsePrefix("fd99::99/128")
  304. for n := 0; n < b.N; n++ {
  305. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
  306. }
  307. })
  308. b.Run("pass proto, port, any local CIDR, fail all group, name, and cidr", func(b *testing.B) {
  309. c := &cert.CachedCertificate{
  310. Certificate: &dummyCert{
  311. name: "nope",
  312. networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
  313. },
  314. InvertedGroups: map[string]struct{}{"nope": {}},
  315. }
  316. for n := 0; n < b.N; n++ {
  317. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
  318. }
  319. })
  320. b.Run("pass proto, port, any local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
  321. c := &cert.CachedCertificate{
  322. Certificate: &dummyCert{
  323. name: "nope",
  324. networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
  325. },
  326. InvertedGroups: map[string]struct{}{"nope": {}},
  327. }
  328. for n := 0; n < b.N; n++ {
  329. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
  330. }
  331. })
  332. b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) {
  333. c := &cert.CachedCertificate{
  334. Certificate: &dummyCert{
  335. name: "nope",
  336. networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
  337. },
  338. InvertedGroups: map[string]struct{}{"nope": {}},
  339. }
  340. for n := 0; n < b.N; n++ {
  341. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
  342. }
  343. })
  344. b.Run("pass proto, port, specific local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
  345. c := &cert.CachedCertificate{
  346. Certificate: &dummyCert{
  347. name: "nope",
  348. networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
  349. },
  350. InvertedGroups: map[string]struct{}{"nope": {}},
  351. }
  352. for n := 0; n < b.N; n++ {
  353. assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
  354. }
  355. })
  356. b.Run("pass on group on any local cidr", func(b *testing.B) {
  357. c := &cert.CachedCertificate{
  358. Certificate: &dummyCert{
  359. name: "nope",
  360. },
  361. InvertedGroups: map[string]struct{}{"good-group": {}},
  362. }
  363. for n := 0; n < b.N; n++ {
  364. assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
  365. }
  366. })
  367. b.Run("pass on group on specific local cidr", func(b *testing.B) {
  368. c := &cert.CachedCertificate{
  369. Certificate: &dummyCert{
  370. name: "nope",
  371. },
  372. InvertedGroups: map[string]struct{}{"good-group": {}},
  373. }
  374. for n := 0; n < b.N; n++ {
  375. assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
  376. }
  377. })
  378. b.Run("pass on group on specific local cidr6", func(b *testing.B) {
  379. c := &cert.CachedCertificate{
  380. Certificate: &dummyCert{
  381. name: "nope",
  382. },
  383. InvertedGroups: map[string]struct{}{"good-group": {}},
  384. }
  385. for n := 0; n < b.N; n++ {
  386. assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
  387. }
  388. })
  389. b.Run("pass on name", func(b *testing.B) {
  390. c := &cert.CachedCertificate{
  391. Certificate: &dummyCert{
  392. name: "good-host",
  393. },
  394. InvertedGroups: map[string]struct{}{"nope": {}},
  395. }
  396. for n := 0; n < b.N; n++ {
  397. ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp)
  398. }
  399. })
  400. }
  401. func TestFirewall_Drop2(t *testing.T) {
  402. ob := &bytes.Buffer{}
  403. l := test.NewLoggerWithOutput(ob)
  404. myVpnNetworksTable := new(bart.Lite)
  405. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  406. p := firewall.Packet{
  407. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  408. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  409. LocalPort: 10,
  410. RemotePort: 90,
  411. Protocol: iputil.IPProtocolUDP,
  412. Fragment: false,
  413. }
  414. network := netip.MustParsePrefix("1.2.3.4/24")
  415. c := cert.CachedCertificate{
  416. Certificate: &dummyCert{
  417. name: "host1",
  418. networks: []netip.Prefix{network},
  419. },
  420. InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}},
  421. }
  422. h := HostInfo{
  423. ConnectionState: &ConnectionState{
  424. peerCert: &c,
  425. },
  426. vpnAddrs: []netip.Addr{network.Addr()},
  427. }
  428. h.buildNetworks(myVpnNetworksTable, c.Certificate)
  429. c1 := cert.CachedCertificate{
  430. Certificate: &dummyCert{
  431. name: "host1",
  432. networks: []netip.Prefix{network},
  433. },
  434. InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}},
  435. }
  436. h1 := HostInfo{
  437. vpnAddrs: []netip.Addr{network.Addr()},
  438. ConnectionState: &ConnectionState{
  439. peerCert: &c1,
  440. },
  441. }
  442. h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
  443. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  444. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", "", "", "", ""))
  445. cp := cert.NewCAPool()
  446. // h1/c1 lacks the proper groups
  447. require.ErrorIs(t, fw.Drop(p, true, &h1, cp), ErrNoMatchingRule)
  448. // c has the proper groups
  449. resetConntrack(fw)
  450. require.NoError(t, fw.Drop(p, true, &h, cp))
  451. }
  452. func TestFirewall_Drop3(t *testing.T) {
  453. ob := &bytes.Buffer{}
  454. l := test.NewLoggerWithOutput(ob)
  455. myVpnNetworksTable := new(bart.Lite)
  456. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  457. p := firewall.Packet{
  458. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  459. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  460. LocalPort: 1,
  461. RemotePort: 1,
  462. Protocol: iputil.IPProtocolUDP,
  463. Fragment: false,
  464. }
  465. network := netip.MustParsePrefix("1.2.3.4/24")
  466. c := cert.CachedCertificate{
  467. Certificate: &dummyCert{
  468. name: "host-owner",
  469. networks: []netip.Prefix{network},
  470. },
  471. }
  472. c1 := cert.CachedCertificate{
  473. Certificate: &dummyCert{
  474. name: "host1",
  475. networks: []netip.Prefix{network},
  476. issuer: "signer-sha-bad",
  477. },
  478. }
  479. h1 := HostInfo{
  480. ConnectionState: &ConnectionState{
  481. peerCert: &c1,
  482. },
  483. vpnAddrs: []netip.Addr{network.Addr()},
  484. }
  485. h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
  486. c2 := cert.CachedCertificate{
  487. Certificate: &dummyCert{
  488. name: "host2",
  489. networks: []netip.Prefix{network},
  490. issuer: "signer-sha",
  491. },
  492. }
  493. h2 := HostInfo{
  494. ConnectionState: &ConnectionState{
  495. peerCert: &c2,
  496. },
  497. vpnAddrs: []netip.Addr{network.Addr()},
  498. }
  499. h2.buildNetworks(myVpnNetworksTable, c2.Certificate)
  500. c3 := cert.CachedCertificate{
  501. Certificate: &dummyCert{
  502. name: "host3",
  503. networks: []netip.Prefix{network},
  504. issuer: "signer-sha-bad",
  505. },
  506. }
  507. h3 := HostInfo{
  508. ConnectionState: &ConnectionState{
  509. peerCert: &c3,
  510. },
  511. vpnAddrs: []netip.Addr{network.Addr()},
  512. }
  513. h3.buildNetworks(myVpnNetworksTable, c3.Certificate)
  514. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  515. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", "", "", "", ""))
  516. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", "signer-sha"))
  517. cp := cert.NewCAPool()
  518. // c1 should pass because host match
  519. require.NoError(t, fw.Drop(p, true, &h1, cp))
  520. // c2 should pass because ca sha match
  521. resetConntrack(fw)
  522. require.NoError(t, fw.Drop(p, true, &h2, cp))
  523. // c3 should fail because no match
  524. resetConntrack(fw)
  525. assert.Equal(t, fw.Drop(p, true, &h3, cp), ErrNoMatchingRule)
  526. // Test a remote address match
  527. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  528. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "1.2.3.4/24", "", "", ""))
  529. require.NoError(t, fw.Drop(p, true, &h1, cp))
  530. }
  531. func TestFirewall_Drop3V6(t *testing.T) {
  532. ob := &bytes.Buffer{}
  533. l := test.NewLoggerWithOutput(ob)
  534. myVpnNetworksTable := new(bart.Lite)
  535. myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
  536. p := firewall.Packet{
  537. LocalAddr: netip.MustParseAddr("fd12::34"),
  538. RemoteAddr: netip.MustParseAddr("fd12::34"),
  539. LocalPort: 1,
  540. RemotePort: 1,
  541. Protocol: iputil.IPProtocolUDP,
  542. Fragment: false,
  543. }
  544. network := netip.MustParsePrefix("fd12::34/120")
  545. c := cert.CachedCertificate{
  546. Certificate: &dummyCert{
  547. name: "host-owner",
  548. networks: []netip.Prefix{network},
  549. },
  550. }
  551. h := HostInfo{
  552. ConnectionState: &ConnectionState{
  553. peerCert: &c,
  554. },
  555. vpnAddrs: []netip.Addr{network.Addr()},
  556. }
  557. h.buildNetworks(myVpnNetworksTable, c.Certificate)
  558. // Test a remote address match
  559. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  560. cp := cert.NewCAPool()
  561. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "fd12::34/120", "", "", ""))
  562. require.NoError(t, fw.Drop(p, true, &h, cp))
  563. }
  564. func TestFirewall_DropConntrackReload(t *testing.T) {
  565. ob := &bytes.Buffer{}
  566. l := test.NewLoggerWithOutput(ob)
  567. myVpnNetworksTable := new(bart.Lite)
  568. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  569. p := firewall.Packet{
  570. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  571. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  572. LocalPort: 10,
  573. RemotePort: 90,
  574. Protocol: iputil.IPProtocolUDP,
  575. Fragment: false,
  576. }
  577. network := netip.MustParsePrefix("1.2.3.4/24")
  578. c := cert.CachedCertificate{
  579. Certificate: &dummyCert{
  580. name: "host1",
  581. networks: []netip.Prefix{network},
  582. groups: []string{"default-group"},
  583. issuer: "signer-shasum",
  584. },
  585. InvertedGroups: map[string]struct{}{"default-group": {}},
  586. }
  587. h := HostInfo{
  588. ConnectionState: &ConnectionState{
  589. peerCert: &c,
  590. },
  591. vpnAddrs: []netip.Addr{network.Addr()},
  592. }
  593. h.buildNetworks(myVpnNetworksTable, c.Certificate)
  594. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  595. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  596. cp := cert.NewCAPool()
  597. // Drop outbound
  598. assert.Equal(t, fw.Drop(p, false, &h, cp), ErrNoMatchingRule)
  599. // Allow inbound
  600. resetConntrack(fw)
  601. require.NoError(t, fw.Drop(p, true, &h, cp))
  602. // Allow outbound because conntrack
  603. require.NoError(t, fw.Drop(p, false, &h, cp))
  604. oldFw := fw
  605. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  606. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", "", "", "", ""))
  607. fw.Conntrack = oldFw.Conntrack
  608. fw.rulesVersion = oldFw.rulesVersion + 1
  609. // Allow outbound because conntrack and new rules allow port 10
  610. require.NoError(t, fw.Drop(p, false, &h, cp))
  611. oldFw = fw
  612. fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  613. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", "", "", "", ""))
  614. fw.Conntrack = oldFw.Conntrack
  615. fw.rulesVersion = oldFw.rulesVersion + 1
  616. // Drop outbound because conntrack doesn't match new ruleset
  617. assert.Equal(t, fw.Drop(p, false, &h, cp), ErrNoMatchingRule)
  618. }
  619. func TestFirewall_ICMPPortBehavior(t *testing.T) {
  620. ob := &bytes.Buffer{}
  621. l := test.NewLoggerWithOutput(ob)
  622. myVpnNetworksTable := new(bart.Lite)
  623. myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
  624. network := netip.MustParsePrefix("1.2.3.4/24")
  625. c := cert.CachedCertificate{
  626. Certificate: &dummyCert{
  627. name: "host1",
  628. networks: []netip.Prefix{network},
  629. groups: []string{"default-group"},
  630. issuer: "signer-shasum",
  631. },
  632. InvertedGroups: map[string]struct{}{"default-group": {}},
  633. }
  634. h := HostInfo{
  635. ConnectionState: &ConnectionState{
  636. peerCert: &c,
  637. },
  638. vpnAddrs: []netip.Addr{network.Addr()},
  639. }
  640. h.buildNetworks(myVpnNetworksTable, c.Certificate)
  641. cp := cert.NewCAPool()
  642. templ := firewall.Packet{
  643. LocalAddr: netip.MustParseAddr("1.2.3.4"),
  644. RemoteAddr: netip.MustParseAddr("1.2.3.4"),
  645. Protocol: iputil.IPProtocolICMP,
  646. Fragment: false,
  647. }
  648. t.Run("ICMP allowed", func(t *testing.T) {
  649. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  650. require.NoError(t, fw.AddRule(true, iputil.IPProtocolICMP, 0, 0, []string{"any"}, "", "", "", "", ""))
  651. t.Run("zero ports", func(t *testing.T) {
  652. p := templ.Copy()
  653. p.LocalPort = 0
  654. p.RemotePort = 0
  655. // Drop outbound
  656. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  657. // Allow inbound
  658. resetConntrack(fw)
  659. require.NoError(t, fw.Drop(*p, true, &h, cp))
  660. //now also allow outbound
  661. require.NoError(t, fw.Drop(*p, false, &h, cp))
  662. })
  663. t.Run("nonzero ports", func(t *testing.T) {
  664. p := templ.Copy()
  665. p.LocalPort = 0xabcd
  666. p.RemotePort = 0x1234
  667. // Drop outbound
  668. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  669. // Allow inbound
  670. resetConntrack(fw)
  671. require.NoError(t, fw.Drop(*p, true, &h, cp))
  672. //now also allow outbound
  673. require.NoError(t, fw.Drop(*p, false, &h, cp))
  674. })
  675. })
  676. t.Run("Any proto, some ports allowed", func(t *testing.T) {
  677. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  678. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 80, 444, []string{"any"}, "", "", "", "", ""))
  679. t.Run("zero ports, still blocked", func(t *testing.T) {
  680. p := templ.Copy()
  681. p.LocalPort = 0
  682. p.RemotePort = 0
  683. // Drop outbound
  684. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  685. // Allow inbound
  686. resetConntrack(fw)
  687. assert.Equal(t, fw.Drop(*p, true, &h, cp), ErrNoMatchingRule)
  688. //now also allow outbound
  689. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  690. })
  691. t.Run("nonzero ports, still blocked", func(t *testing.T) {
  692. p := templ.Copy()
  693. p.LocalPort = 0xabcd
  694. p.RemotePort = 0x1234
  695. // Drop outbound
  696. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  697. // Allow inbound
  698. resetConntrack(fw)
  699. assert.Equal(t, fw.Drop(*p, true, &h, cp), ErrNoMatchingRule)
  700. //now also allow outbound
  701. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  702. })
  703. t.Run("nonzero, matching ports, still blocked", func(t *testing.T) {
  704. p := templ.Copy()
  705. p.LocalPort = 80
  706. p.RemotePort = 80
  707. // Drop outbound
  708. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  709. // Allow inbound
  710. resetConntrack(fw)
  711. assert.Equal(t, fw.Drop(*p, true, &h, cp), ErrNoMatchingRule)
  712. //now also allow outbound
  713. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  714. })
  715. })
  716. t.Run("Any proto, any port", func(t *testing.T) {
  717. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  718. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  719. t.Run("zero ports, allowed", func(t *testing.T) {
  720. resetConntrack(fw)
  721. p := templ.Copy()
  722. p.LocalPort = 0
  723. p.RemotePort = 0
  724. // Drop outbound
  725. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  726. // Allow inbound
  727. resetConntrack(fw)
  728. require.NoError(t, fw.Drop(*p, true, &h, cp))
  729. //now also allow outbound
  730. require.NoError(t, fw.Drop(*p, false, &h, cp))
  731. })
  732. t.Run("nonzero ports, allowed", func(t *testing.T) {
  733. resetConntrack(fw)
  734. p := templ.Copy()
  735. p.LocalPort = 0xabcd
  736. p.RemotePort = 0x1234
  737. // Drop outbound
  738. assert.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  739. // Allow inbound
  740. resetConntrack(fw)
  741. require.NoError(t, fw.Drop(*p, true, &h, cp))
  742. //now also allow outbound
  743. require.NoError(t, fw.Drop(*p, false, &h, cp))
  744. //different ID is blocked
  745. p.RemotePort++
  746. require.Equal(t, fw.Drop(*p, false, &h, cp), ErrNoMatchingRule)
  747. })
  748. })
  749. }
  750. func TestFirewall_DropIPSpoofing(t *testing.T) {
  751. ob := &bytes.Buffer{}
  752. l := test.NewLoggerWithOutput(ob)
  753. myVpnNetworksTable := new(bart.Lite)
  754. myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
  755. c := cert.CachedCertificate{
  756. Certificate: &dummyCert{
  757. name: "host-owner",
  758. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
  759. },
  760. }
  761. c1 := cert.CachedCertificate{
  762. Certificate: &dummyCert{
  763. name: "host",
  764. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
  765. unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
  766. },
  767. }
  768. h1 := HostInfo{
  769. ConnectionState: &ConnectionState{
  770. peerCert: &c1,
  771. },
  772. vpnAddrs: []netip.Addr{c1.Certificate.Networks()[0].Addr()},
  773. }
  774. h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
  775. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
  776. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", ""))
  777. cp := cert.NewCAPool()
  778. // Packet spoofed by `c1`. Note that the remote addr is not a valid one.
  779. p := firewall.Packet{
  780. LocalAddr: netip.MustParseAddr("192.0.2.1"),
  781. RemoteAddr: netip.MustParseAddr("192.0.2.3"),
  782. LocalPort: 1,
  783. RemotePort: 1,
  784. Protocol: iputil.IPProtocolUDP,
  785. Fragment: false,
  786. }
  787. assert.Equal(t, fw.Drop(p, true, &h1, cp), ErrInvalidRemoteIP)
  788. }
  789. func TestFirewall_ConntrackSourceSpoofingAcrossPeers(t *testing.T) {
  790. l := test.NewLoggerWithOutput(&bytes.Buffer{})
  791. myVpnNetworksTable := new(bart.Lite)
  792. myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
  793. owner := &dummyCert{
  794. name: "owner",
  795. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
  796. }
  797. victim := &cert.CachedCertificate{
  798. Certificate: &dummyCert{
  799. name: "victim",
  800. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
  801. },
  802. }
  803. victimHI := HostInfo{
  804. ConnectionState: &ConnectionState{peerCert: victim},
  805. vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")},
  806. }
  807. victimHI.buildNetworks(myVpnNetworksTable, victim.Certificate)
  808. attacker := &cert.CachedCertificate{
  809. Certificate: &dummyCert{
  810. name: "attacker",
  811. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.3/24")},
  812. },
  813. }
  814. attackerHI := HostInfo{
  815. ConnectionState: &ConnectionState{peerCert: attacker},
  816. vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.3")},
  817. }
  818. attackerHI.buildNetworks(myVpnNetworksTable, attacker.Certificate)
  819. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
  820. // Allow any inbound traffic that passes the cert / source-IP checks.
  821. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  822. cp := cert.NewCAPool()
  823. flow := firewall.Packet{
  824. LocalAddr: netip.MustParseAddr("192.0.2.1"),
  825. RemoteAddr: netip.MustParseAddr("192.0.2.2"),
  826. LocalPort: 443,
  827. RemotePort: 55000,
  828. Protocol: iputil.IPProtocolUDP,
  829. }
  830. require.NoError(t, fw.Drop(flow, true, &victimHI, cp),
  831. "victim's own traffic from its own overlay IP must be allowed")
  832. unseen := flow
  833. unseen.RemotePort = 55001
  834. assert.Equal(t, ErrInvalidRemoteIP, fw.Drop(unseen, true, &attackerHI, cp),
  835. "sanity: attacker forging victim's source IP must be rejected when no conntrack entry exists")
  836. got := fw.Drop(flow, true, &attackerHI, cp)
  837. t.Logf("attacker replaying victim's 4-tuple: Drop returned %v (nil == packet ALLOWED == spoof succeeded)", got)
  838. assert.Equal(t, ErrInvalidRemoteIP, got,
  839. "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)
  840. }
  841. func TestFirewallConntrack_sweep(t *testing.T) {
  842. ct := newFirewallConntrack()
  843. live := firewall.Packet{LocalPort: 1}
  844. dead := firewall.Packet{LocalPort: 2}
  845. ct.store(live, newConn(11, true, 0))
  846. ct.store(dead, newConn(10, true, 0))
  847. assert.Equal(t, int64(2), ct.count.Load())
  848. ct.sweep(10)
  849. _, ok := ct.conns.Load(live)
  850. assert.True(t, ok)
  851. _, ok = ct.conns.Load(dead)
  852. assert.False(t, ok)
  853. assert.Equal(t, int64(1), ct.count.Load())
  854. }
  855. func TestFirewallConntrack_storeAndDeleteCount(t *testing.T) {
  856. ct := newFirewallConntrack()
  857. fp := firewall.Packet{LocalPort: 1}
  858. first := newConn(10, true, 0)
  859. ct.store(fp, first)
  860. // Replacing an entry doesn't add to the count
  861. second := newConn(20, true, 1)
  862. ct.store(fp, second)
  863. assert.Equal(t, int64(1), ct.count.Load())
  864. // Deleting a stale entry is a no-op, the newer one stays
  865. ct.delete(fp, first)
  866. v, ok := ct.conns.Load(fp)
  867. assert.True(t, ok)
  868. assert.Same(t, second, v)
  869. assert.Equal(t, int64(1), ct.count.Load())
  870. ct.delete(fp, second)
  871. _, ok = ct.conns.Load(fp)
  872. assert.False(t, ok)
  873. assert.Equal(t, int64(0), ct.count.Load())
  874. ct.store(fp, first)
  875. ct.store(firewall.Packet{LocalPort: 2}, newConn(10, true, 0))
  876. ct.clear()
  877. assert.Equal(t, int64(0), ct.count.Load())
  878. }
  879. func TestFirewallConntrack_Run(t *testing.T) {
  880. ct := newFirewallConntrack()
  881. ct.epoch = time.Now().Add(-time.Hour)
  882. ct.store(firewall.Packet{LocalPort: 1}, newConn(10, true, 0))
  883. ctx, cancel := context.WithCancel(context.Background())
  884. done := make(chan struct{})
  885. go func() {
  886. ct.Run(ctx)
  887. close(done)
  888. }()
  889. // The first tick sets the clock to an hour in and sweeps the long expired entry
  890. require.Eventually(t, func() bool { return ct.count.Load() == 0 }, 5*time.Second, 10*time.Millisecond)
  891. assert.GreaterOrEqual(t, ct.now.Load(), uint32(3600))
  892. cancel()
  893. <-done
  894. }
  895. // BenchmarkFirewallDropConntrackHit measures Drop on an already-established flow
  896. // (a conntrack hit). This is the fast path that the source-IP<->cert binding
  897. // reordering adds work to, so it quantifies the cost of moving the address checks
  898. // ahead of the conntrack lookup. Cases:
  899. // - simple: peer cert has one address, no unsafe networks (h.networks == nil),
  900. // so the remote-address check is a single netip.Addr compare.
  901. // - complex: peer cert has unsafe networks (h.networks populated), so the
  902. // remote-address check is a BART lookup.
  903. func BenchmarkFirewallDropConntrackHit(b *testing.B) {
  904. l := test.NewLoggerWithOutput(&bytes.Buffer{})
  905. myVpnNetworksTable := new(bart.Lite)
  906. myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
  907. owner := &dummyCert{
  908. name: "owner",
  909. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
  910. }
  911. simpleCert := &cert.CachedCertificate{
  912. Certificate: &dummyCert{
  913. name: "simple",
  914. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
  915. },
  916. }
  917. simpleHost := &HostInfo{
  918. ConnectionState: &ConnectionState{peerCert: simpleCert},
  919. vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")},
  920. }
  921. simpleHost.buildNetworks(myVpnNetworksTable, simpleCert.Certificate)
  922. complexCert := &cert.CachedCertificate{
  923. Certificate: &dummyCert{
  924. name: "complex",
  925. networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
  926. unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
  927. },
  928. }
  929. complexHost := &HostInfo{
  930. ConnectionState: &ConnectionState{peerCert: complexCert},
  931. vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2")},
  932. }
  933. complexHost.buildNetworks(myVpnNetworksTable, complexCert.Certificate)
  934. cp := cert.NewCAPool()
  935. flow := firewall.Packet{
  936. LocalAddr: netip.MustParseAddr("192.0.2.1"),
  937. RemoteAddr: netip.MustParseAddr("192.0.2.2"),
  938. LocalPort: 443,
  939. RemotePort: 55000,
  940. Protocol: iputil.IPProtocolUDP,
  941. }
  942. cases := []struct {
  943. name string
  944. host *HostInfo
  945. }{
  946. {"simple", simpleHost},
  947. {"complex", complexHost},
  948. }
  949. for _, tc := range cases {
  950. b.Run(tc.name, func(b *testing.B) {
  951. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
  952. require.NoError(b, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  953. // Establish the conntrack entry so every benchmarked Drop is a hit.
  954. require.NoError(b, fw.Drop(flow, true, tc.host, cp))
  955. b.ReportAllocs()
  956. b.ResetTimer()
  957. for i := 0; i < b.N; i++ {
  958. if err := fw.Drop(flow, true, tc.host, cp); err != nil {
  959. b.Fatal(err)
  960. }
  961. }
  962. })
  963. }
  964. }
  965. func BenchmarkLookup(b *testing.B) {
  966. ml := func(m map[string]struct{}, a [][]string) {
  967. for n := 0; n < b.N; n++ {
  968. for _, sg := range a {
  969. found := false
  970. for _, g := range sg {
  971. if _, ok := m[g]; !ok {
  972. found = false
  973. break
  974. }
  975. found = true
  976. }
  977. if found {
  978. return
  979. }
  980. }
  981. }
  982. }
  983. b.Run("array to map best", func(b *testing.B) {
  984. m := map[string]struct{}{
  985. "1ne": {},
  986. "2wo": {},
  987. "3hr": {},
  988. "4ou": {},
  989. "5iv": {},
  990. "6ix": {},
  991. }
  992. a := [][]string{
  993. {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
  994. {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
  995. {"one", "two", "3hr", "4ou", "5iv", "6ix"},
  996. {"one", "two", "thr", "4ou", "5iv", "6ix"},
  997. {"one", "two", "thr", "fou", "5iv", "6ix"},
  998. {"one", "two", "thr", "fou", "fiv", "6ix"},
  999. {"one", "two", "thr", "fou", "fiv", "six"},
  1000. }
  1001. for n := 0; n < b.N; n++ {
  1002. ml(m, a)
  1003. }
  1004. })
  1005. b.Run("array to map worst", func(b *testing.B) {
  1006. m := map[string]struct{}{
  1007. "one": {},
  1008. "two": {},
  1009. "thr": {},
  1010. "fou": {},
  1011. "fiv": {},
  1012. "six": {},
  1013. }
  1014. a := [][]string{
  1015. {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
  1016. {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
  1017. {"one", "two", "3hr", "4ou", "5iv", "6ix"},
  1018. {"one", "two", "thr", "4ou", "5iv", "6ix"},
  1019. {"one", "two", "thr", "fou", "5iv", "6ix"},
  1020. {"one", "two", "thr", "fou", "fiv", "6ix"},
  1021. {"one", "two", "thr", "fou", "fiv", "six"},
  1022. }
  1023. for n := 0; n < b.N; n++ {
  1024. ml(m, a)
  1025. }
  1026. })
  1027. }
  1028. func Test_parsePort(t *testing.T) {
  1029. _, _, err := parsePort("")
  1030. require.EqualError(t, err, "was not a number; ``")
  1031. _, _, err = parsePort(" ")
  1032. require.EqualError(t, err, "was not a number; ` `")
  1033. _, _, err = parsePort("-")
  1034. require.EqualError(t, err, "appears to be a range but could not be parsed; `-`")
  1035. _, _, err = parsePort(" - ")
  1036. require.EqualError(t, err, "appears to be a range but could not be parsed; ` - `")
  1037. _, _, err = parsePort("a-b")
  1038. require.EqualError(t, err, "beginning range was not a number; `a`")
  1039. _, _, err = parsePort("1-b")
  1040. require.EqualError(t, err, "ending range was not a number; `b`")
  1041. s, e, err := parsePort(" 1 - 2 ")
  1042. assert.Equal(t, int32(1), s)
  1043. assert.Equal(t, int32(2), e)
  1044. require.NoError(t, err)
  1045. s, e, err = parsePort("0-1")
  1046. assert.Equal(t, int32(0), s)
  1047. assert.Equal(t, int32(0), e)
  1048. require.NoError(t, err)
  1049. s, e, err = parsePort("9919")
  1050. assert.Equal(t, int32(9919), s)
  1051. assert.Equal(t, int32(9919), e)
  1052. require.NoError(t, err)
  1053. s, e, err = parsePort("any")
  1054. assert.Equal(t, int32(0), s)
  1055. assert.Equal(t, int32(0), e)
  1056. require.NoError(t, err)
  1057. }
  1058. // Test_parsePort_invalid covers inputs that must error. The named bug is
  1059. // that int32(strconv.Atoi("4294967296")) truncates to 0 == firewall.PortAny,
  1060. // silently turning a typo into a match-all-ports rule; the rest are
  1061. // representative syntax/range probes.
  1062. func Test_parsePort_invalid(t *testing.T) {
  1063. tests := []struct {
  1064. name string
  1065. input string
  1066. wantErrContains string
  1067. }{
  1068. // Numeric overflow (the named bug + boundary).
  1069. {"named bug: 2^32 truncates to PortAny", "4294967296", "out of range"},
  1070. {"just above max real port", "65536", "out of range"},
  1071. // Negatives route through the range branch and hit the empty-half
  1072. // guard; included as defense in depth so a future refactor cannot
  1073. // accidentally reach the int32 cast.
  1074. {"negative", "-1", "could not be parsed"},
  1075. // Syntax probes.
  1076. {"NUL between digits", "4\x002", "was not a number"},
  1077. {"hex notation", "0x10", "was not a number"},
  1078. {"scientific notation", "1e3", "was not a number"},
  1079. {"leading whitespace", " 42", "was not a number"},
  1080. {"fullwidth digits", "42", "was not a number"},
  1081. // Range branch.
  1082. {"range upper out of range", "1-65536", "ending range out of range"},
  1083. {"range lower out of range", "65536-65537", "beginning range out of range"},
  1084. {"range with negative upper", "1--1", "ending range was not a number"},
  1085. }
  1086. for _, tc := range tests {
  1087. t.Run(tc.name, func(t *testing.T) {
  1088. _, _, err := parsePort(tc.input)
  1089. require.Error(t, err, "input %q must error", tc.input)
  1090. require.ErrorContains(t, err, tc.wantErrContains)
  1091. })
  1092. }
  1093. }
  1094. // Test_parsePort_valid_boundaries locks in success cases at 0, 1, and 65535
  1095. // so a future refactor cannot regress the boundaries.
  1096. func Test_parsePort_valid_boundaries(t *testing.T) {
  1097. tests := []struct {
  1098. name string
  1099. input string
  1100. wantStart int32
  1101. wantEnd int32
  1102. }{
  1103. {"zero is PortAny", "0", 0, 0},
  1104. {"min real port", "1", 1, 1},
  1105. {"max real port", "65535", 65535, 65535},
  1106. {"range zero to max forces end to zero", "0-65535", 0, 0},
  1107. {"range max to max", "65535-65535", 65535, 65535},
  1108. {"range one to max", "1-65535", 1, 65535},
  1109. {"range with whitespace inside", " 1 - 2 ", 1, 2},
  1110. }
  1111. for _, tc := range tests {
  1112. t.Run(tc.name, func(t *testing.T) {
  1113. s, e, err := parsePort(tc.input)
  1114. require.NoError(t, err)
  1115. assert.Equal(t, tc.wantStart, s, "start port")
  1116. assert.Equal(t, tc.wantEnd, e, "end port")
  1117. })
  1118. }
  1119. }
  1120. func TestNewFirewallFromConfig(t *testing.T) {
  1121. l := test.NewLogger()
  1122. // Test a bad rule definition
  1123. c := &dummyCert{}
  1124. cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil, "aes")
  1125. require.NoError(t, err)
  1126. conf := config.NewC(test.NewLogger())
  1127. conf.Settings["firewall"] = map[string]any{"outbound": "asdf"}
  1128. _, err = NewFirewallFromConfig(l, cs, conf)
  1129. require.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
  1130. // Test both port and code
  1131. conf = config.NewC(test.NewLogger())
  1132. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "code": "2"}}}
  1133. _, err = NewFirewallFromConfig(l, cs, conf)
  1134. require.EqualError(t, err, "firewall.outbound rule #0; only one of port or code should be provided")
  1135. // Test missing host, group, cidr, ca_name and ca_sha
  1136. conf = config.NewC(test.NewLogger())
  1137. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{}}}
  1138. _, err = NewFirewallFromConfig(l, cs, conf)
  1139. 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")
  1140. // Test code/port error
  1141. conf = config.NewC(test.NewLogger())
  1142. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "a", "host": "testh", "proto": "any"}}}
  1143. _, err = NewFirewallFromConfig(l, cs, conf)
  1144. require.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`")
  1145. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "a", "host": "testh", "proto": "any"}}}
  1146. _, err = NewFirewallFromConfig(l, cs, conf)
  1147. require.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
  1148. // Test proto error
  1149. conf = config.NewC(test.NewLogger())
  1150. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "host": "testh"}}}
  1151. _, err = NewFirewallFromConfig(l, cs, conf)
  1152. require.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
  1153. // Test cidr parse error
  1154. conf = config.NewC(test.NewLogger())
  1155. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "cidr": "testh", "proto": "any"}}}
  1156. _, err = NewFirewallFromConfig(l, cs, conf)
  1157. require.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
  1158. // Test local_cidr parse error
  1159. conf = config.NewC(test.NewLogger())
  1160. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "local_cidr": "testh", "proto": "any"}}}
  1161. _, err = NewFirewallFromConfig(l, cs, conf)
  1162. require.EqualError(t, err, "firewall.outbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
  1163. // Test both group and groups
  1164. conf = config.NewC(test.NewLogger())
  1165. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
  1166. _, err = NewFirewallFromConfig(l, cs, conf)
  1167. require.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
  1168. }
  1169. func TestAddFirewallRulesFromConfig(t *testing.T) {
  1170. l := test.NewLogger()
  1171. // Test adding tcp rule
  1172. conf := config.NewC(test.NewLogger())
  1173. mf := &mockFirewall{}
  1174. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "tcp", "host": "a"}}}
  1175. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  1176. assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  1177. // Test adding udp rule
  1178. conf = config.NewC(test.NewLogger())
  1179. mf = &mockFirewall{}
  1180. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "udp", "host": "a"}}}
  1181. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  1182. assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  1183. // Test adding icmp rule
  1184. conf = config.NewC(test.NewLogger())
  1185. mf = &mockFirewall{}
  1186. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "icmp", "host": "a"}}}
  1187. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  1188. assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  1189. // Test adding icmp rule no port
  1190. conf = config.NewC(test.NewLogger())
  1191. mf = &mockFirewall{}
  1192. conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"proto": "icmp", "host": "a"}}}
  1193. require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
  1194. assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  1195. // Test adding any rule
  1196. conf = config.NewC(test.NewLogger())
  1197. mf = &mockFirewall{}
  1198. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
  1199. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1200. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
  1201. // Test adding rule with cidr
  1202. cidr := netip.MustParsePrefix("10.0.0.0/8")
  1203. conf = config.NewC(test.NewLogger())
  1204. mf = &mockFirewall{}
  1205. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr.String()}}}
  1206. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1207. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr.String(), localIp: ""}, mf.lastCall)
  1208. // Test adding rule with local_cidr
  1209. conf = config.NewC(test.NewLogger())
  1210. mf = &mockFirewall{}
  1211. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr.String()}}}
  1212. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1213. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr.String()}, mf.lastCall)
  1214. // Test adding rule with cidr ipv6
  1215. cidr6 := netip.MustParsePrefix("fd00::/8")
  1216. conf = config.NewC(test.NewLogger())
  1217. mf = &mockFirewall{}
  1218. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr6.String()}}}
  1219. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1220. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr6.String(), localIp: ""}, mf.lastCall)
  1221. // Test adding rule with any cidr
  1222. conf = config.NewC(test.NewLogger())
  1223. mf = &mockFirewall{}
  1224. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "any"}}}
  1225. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1226. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "any", localIp: ""}, mf.lastCall)
  1227. // Test adding rule with junk cidr
  1228. conf = config.NewC(test.NewLogger())
  1229. mf = &mockFirewall{}
  1230. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "junk/junk"}}}
  1231. 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")
  1232. // Test adding rule with local_cidr ipv6
  1233. conf = config.NewC(test.NewLogger())
  1234. mf = &mockFirewall{}
  1235. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr6.String()}}}
  1236. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1237. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr6.String()}, mf.lastCall)
  1238. // Test adding rule with any local_cidr
  1239. conf = config.NewC(test.NewLogger())
  1240. mf = &mockFirewall{}
  1241. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "any"}}}
  1242. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1243. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, localIp: "any"}, mf.lastCall)
  1244. // Test adding rule with junk local_cidr
  1245. conf = config.NewC(test.NewLogger())
  1246. mf = &mockFirewall{}
  1247. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "junk/junk"}}}
  1248. 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")
  1249. // Test adding rule with ca_sha
  1250. conf = config.NewC(test.NewLogger())
  1251. mf = &mockFirewall{}
  1252. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
  1253. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1254. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caSha: "12312313123"}, mf.lastCall)
  1255. // Test adding rule with ca_name
  1256. conf = config.NewC(test.NewLogger())
  1257. mf = &mockFirewall{}
  1258. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_name": "root01"}}}
  1259. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1260. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caName: "root01"}, mf.lastCall)
  1261. // Test single group
  1262. conf = config.NewC(test.NewLogger())
  1263. mf = &mockFirewall{}
  1264. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a"}}}
  1265. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1266. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
  1267. // Test single groups
  1268. conf = config.NewC(test.NewLogger())
  1269. mf = &mockFirewall{}
  1270. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": "a"}}}
  1271. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1272. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
  1273. // Test multiple AND groups
  1274. conf = config.NewC(test.NewLogger())
  1275. mf = &mockFirewall{}
  1276. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
  1277. require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
  1278. assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: "", localIp: ""}, mf.lastCall)
  1279. // Test Add error
  1280. conf = config.NewC(test.NewLogger())
  1281. mf = &mockFirewall{}
  1282. mf.nextCallReturn = errors.New("test error")
  1283. conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
  1284. require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
  1285. }
  1286. func TestFirewall_convertRule(t *testing.T) {
  1287. ob := &bytes.Buffer{}
  1288. l := test.NewLoggerWithOutput(ob)
  1289. // Ensure group array of 1 is converted and a warning is printed
  1290. c := map[string]any{
  1291. "group": []any{"group1"},
  1292. }
  1293. r, err := convertRule(l, c, "test", 1)
  1294. assert.Contains(t, ob.String(), "group was an array with a single value, converting to simple value")
  1295. assert.Contains(t, ob.String(), "table=test")
  1296. assert.Contains(t, ob.String(), "rule=1")
  1297. require.NoError(t, err)
  1298. assert.Equal(t, []string{"group1"}, r.Groups)
  1299. // Ensure group array of > 1 is errord
  1300. ob.Reset()
  1301. c = map[string]any{
  1302. "group": []any{"group1", "group2"},
  1303. }
  1304. r, err = convertRule(l, c, "test", 1)
  1305. assert.Empty(t, ob.String())
  1306. require.Error(t, err, "group should contain a single value, an array with more than one entry was provided")
  1307. // Make sure a well formed group is alright
  1308. ob.Reset()
  1309. c = map[string]any{
  1310. "group": "group1",
  1311. }
  1312. r, err = convertRule(l, c, "test", 1)
  1313. require.NoError(t, err)
  1314. assert.Equal(t, []string{"group1"}, r.Groups)
  1315. }
  1316. func TestFirewall_convertRuleSanity(t *testing.T) {
  1317. ob := &bytes.Buffer{}
  1318. l := test.NewLoggerWithOutput(ob)
  1319. noWarningPlease := []map[string]any{
  1320. {"group": "group1"},
  1321. {"groups": []any{"group2"}},
  1322. {"host": "bob"},
  1323. {"cidr": "1.1.1.1/1"},
  1324. {"groups": []any{"group2"}, "host": "bob"},
  1325. {"cidr": "1.1.1.1/1", "host": "bob"},
  1326. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
  1327. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
  1328. }
  1329. for _, c := range noWarningPlease {
  1330. r, err := convertRule(l, c, "test", 1)
  1331. require.NoError(t, err)
  1332. require.NoError(t, r.sanity(), "should not generate a sanity warning, %+v", c)
  1333. }
  1334. yesWarningPlease := []map[string]any{
  1335. {"group": "group1"},
  1336. {"groups": []any{"group2"}},
  1337. {"cidr": "1.1.1.1/1"},
  1338. {"groups": []any{"group2"}, "host": "bob"},
  1339. {"cidr": "1.1.1.1/1", "host": "bob"},
  1340. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
  1341. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
  1342. }
  1343. for _, c := range yesWarningPlease {
  1344. c["host"] = "any"
  1345. r, err := convertRule(l, c, "test", 1)
  1346. require.NoError(t, err)
  1347. err = r.sanity()
  1348. require.Error(t, err, "I wanted a warning: %+v", c)
  1349. }
  1350. //reset the list
  1351. yesWarningPlease = []map[string]any{
  1352. {"group": "group1"},
  1353. {"groups": []any{"group2"}},
  1354. {"cidr": "1.1.1.1/1"},
  1355. {"groups": []any{"group2"}, "host": "bob"},
  1356. {"cidr": "1.1.1.1/1", "host": "bob"},
  1357. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
  1358. {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
  1359. }
  1360. for _, c := range yesWarningPlease {
  1361. r, err := convertRule(l, c, "test", 1)
  1362. require.NoError(t, err)
  1363. r.Groups = append(r.Groups, "any")
  1364. err = r.sanity()
  1365. require.Error(t, err, "I wanted a warning: %+v", c)
  1366. }
  1367. }
  1368. type testcase struct {
  1369. h *HostInfo
  1370. p firewall.Packet
  1371. c cert.Certificate
  1372. err error
  1373. }
  1374. func (c *testcase) Test(t *testing.T, fw *Firewall) {
  1375. t.Helper()
  1376. cp := cert.NewCAPool()
  1377. resetConntrack(fw)
  1378. err := fw.Drop(c.p, true, c.h, cp)
  1379. if c.err == nil {
  1380. require.NoError(t, err, "failed to not drop remote address %s", c.p.RemoteAddr)
  1381. } else {
  1382. require.ErrorIs(t, c.err, err, "failed to drop remote address %s", c.p.RemoteAddr)
  1383. }
  1384. }
  1385. func buildTestCase(setup testsetup, err error, theirPrefixes ...netip.Prefix) testcase {
  1386. c1 := dummyCert{
  1387. name: "host1",
  1388. networks: theirPrefixes,
  1389. groups: []string{"default-group"},
  1390. issuer: "signer-shasum",
  1391. }
  1392. h := HostInfo{
  1393. ConnectionState: &ConnectionState{
  1394. peerCert: &cert.CachedCertificate{
  1395. Certificate: &c1,
  1396. InvertedGroups: map[string]struct{}{"default-group": {}},
  1397. },
  1398. },
  1399. vpnAddrs: make([]netip.Addr, len(theirPrefixes)),
  1400. }
  1401. for i := range theirPrefixes {
  1402. h.vpnAddrs[i] = theirPrefixes[i].Addr()
  1403. }
  1404. h.buildNetworks(setup.myVpnNetworksTable, &c1)
  1405. p := firewall.Packet{
  1406. LocalAddr: setup.c.Networks()[0].Addr(), //todo?
  1407. RemoteAddr: theirPrefixes[0].Addr(),
  1408. LocalPort: 10,
  1409. RemotePort: 90,
  1410. Protocol: iputil.IPProtocolUDP,
  1411. Fragment: false,
  1412. }
  1413. return testcase{
  1414. h: &h,
  1415. p: p,
  1416. c: &c1,
  1417. err: err,
  1418. }
  1419. }
  1420. type testsetup struct {
  1421. c dummyCert
  1422. myVpnNetworksTable *bart.Lite
  1423. fw *Firewall
  1424. }
  1425. func newSetup(t *testing.T, l *slog.Logger, myPrefixes ...netip.Prefix) testsetup {
  1426. c := dummyCert{
  1427. name: "me",
  1428. networks: myPrefixes,
  1429. groups: []string{"default-group"},
  1430. issuer: "signer-shasum",
  1431. }
  1432. return newSetupFromCert(t, l, c)
  1433. }
  1434. func newSetupFromCert(t *testing.T, l *slog.Logger, c dummyCert) testsetup {
  1435. myVpnNetworksTable := new(bart.Lite)
  1436. for _, prefix := range c.Networks() {
  1437. myVpnNetworksTable.Insert(prefix)
  1438. }
  1439. fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
  1440. require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
  1441. return testsetup{
  1442. c: c,
  1443. fw: fw,
  1444. myVpnNetworksTable: myVpnNetworksTable,
  1445. }
  1446. }
  1447. func TestFirewall_Drop_EnforceIPMatch(t *testing.T) {
  1448. t.Parallel()
  1449. ob := &bytes.Buffer{}
  1450. l := test.NewLoggerWithOutput(ob)
  1451. myPrefix := netip.MustParsePrefix("1.1.1.1/8")
  1452. // for now, it's okay that these are all "incoming", the logic this test tries to check doesn't care about in/out
  1453. t.Run("allow inbound all matching", func(t *testing.T) {
  1454. t.Parallel()
  1455. setup := newSetup(t, l, myPrefix)
  1456. tc := buildTestCase(setup, nil, netip.MustParsePrefix("1.2.3.4/24"))
  1457. tc.Test(t, setup.fw)
  1458. })
  1459. t.Run("allow inbound local matching", func(t *testing.T) {
  1460. t.Parallel()
  1461. setup := newSetup(t, l, myPrefix)
  1462. tc := buildTestCase(setup, ErrInvalidLocalIP, netip.MustParsePrefix("1.2.3.4/24"))
  1463. tc.p.LocalAddr = netip.MustParseAddr("1.2.3.8")
  1464. tc.Test(t, setup.fw)
  1465. })
  1466. t.Run("block inbound remote mismatched", func(t *testing.T) {
  1467. t.Parallel()
  1468. setup := newSetup(t, l, myPrefix)
  1469. tc := buildTestCase(setup, ErrInvalidRemoteIP, netip.MustParsePrefix("1.2.3.4/24"))
  1470. tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
  1471. tc.Test(t, setup.fw)
  1472. })
  1473. t.Run("Block a vpn peer packet", func(t *testing.T) {
  1474. t.Parallel()
  1475. setup := newSetup(t, l, myPrefix)
  1476. tc := buildTestCase(setup, ErrPeerRejected, netip.MustParsePrefix("2.2.2.2/24"))
  1477. tc.Test(t, setup.fw)
  1478. })
  1479. twoPrefixes := []netip.Prefix{
  1480. netip.MustParsePrefix("1.2.3.4/24"), netip.MustParsePrefix("2.2.2.2/24"),
  1481. }
  1482. t.Run("allow inbound one matching", func(t *testing.T) {
  1483. t.Parallel()
  1484. setup := newSetup(t, l, myPrefix)
  1485. tc := buildTestCase(setup, nil, twoPrefixes...)
  1486. tc.Test(t, setup.fw)
  1487. })
  1488. t.Run("block inbound multimismatch", func(t *testing.T) {
  1489. t.Parallel()
  1490. setup := newSetup(t, l, myPrefix)
  1491. tc := buildTestCase(setup, ErrInvalidRemoteIP, twoPrefixes...)
  1492. tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
  1493. tc.Test(t, setup.fw)
  1494. })
  1495. t.Run("allow inbound 2nd one matching", func(t *testing.T) {
  1496. t.Parallel()
  1497. setup2 := newSetup(t, l, netip.MustParsePrefix("2.2.2.1/24"))
  1498. tc := buildTestCase(setup2, nil, twoPrefixes...)
  1499. tc.p.RemoteAddr = twoPrefixes[1].Addr()
  1500. tc.Test(t, setup2.fw)
  1501. })
  1502. t.Run("allow inbound unsafe route", func(t *testing.T) {
  1503. t.Parallel()
  1504. unsafePrefix := netip.MustParsePrefix("192.168.0.0/24")
  1505. c := dummyCert{
  1506. name: "me",
  1507. networks: []netip.Prefix{myPrefix},
  1508. unsafeNetworks: []netip.Prefix{unsafePrefix},
  1509. groups: []string{"default-group"},
  1510. issuer: "signer-shasum",
  1511. }
  1512. unsafeSetup := newSetupFromCert(t, l, c)
  1513. tc := buildTestCase(unsafeSetup, nil, twoPrefixes...)
  1514. tc.p.LocalAddr = netip.MustParseAddr("192.168.0.3")
  1515. tc.err = ErrNoMatchingRule
  1516. tc.Test(t, unsafeSetup.fw) //should hit firewall and bounce off
  1517. require.NoError(t, unsafeSetup.fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", unsafePrefix.String(), "", ""))
  1518. tc.err = nil
  1519. tc.Test(t, unsafeSetup.fw) //should pass
  1520. })
  1521. }
  1522. type addRuleCall struct {
  1523. incoming bool
  1524. proto uint8
  1525. startPort int32
  1526. endPort int32
  1527. groups []string
  1528. host string
  1529. ip string
  1530. localIp string
  1531. caName string
  1532. caSha string
  1533. }
  1534. type mockFirewall struct {
  1535. lastCall addRuleCall
  1536. nextCallReturn error
  1537. }
  1538. func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip, localIp, caName string, caSha string) error {
  1539. mf.lastCall = addRuleCall{
  1540. incoming: incoming,
  1541. proto: proto,
  1542. startPort: startPort,
  1543. endPort: endPort,
  1544. groups: groups,
  1545. host: host,
  1546. ip: ip,
  1547. localIp: localIp,
  1548. caName: caName,
  1549. caSha: caSha,
  1550. }
  1551. err := mf.nextCallReturn
  1552. mf.nextCallReturn = nil
  1553. return err
  1554. }
  1555. func resetConntrack(fw *Firewall) {
  1556. fw.Conntrack.clear()
  1557. }