firewall_test.go 64 KB

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