E6: mark each derived accept rule's scope in its one comment

The holder of node-packet-filter reads a port's scope from rules, not
from the rule's shape; the older firewall's mesh-host mark stays.

Builds: design 47 H2 §813 (4); H-A-357
This commit is contained in:
2026-11-21 23:07:16 +01:00
parent 97ebe8e2a5
commit 6b3139218d
2 changed files with 44 additions and 19 deletions
+35 -18
View File
@@ -145,6 +145,23 @@ func Exposure(m mcat.Manifest, layers []mcat.Layer) (map[int]string, error) {
return out, nil
}
// scopeWords are the words a derived accept rule's comment names its source by (design 47 H2 §813 (4), H-A-357): the
// holder of node-packet-filter reads a port's scope from them, never from the rule's shape. A machine-only rule renders
// no rule, so it has none.
var scopeWords = map[string]string{
mcat.FromEverywhere: "any",
mcat.FromMesh: "private-network",
mcat.FromLocalNetwork: "local-network",
}
// derivedComment is the one comment of a derived accept rule, `derived <modules joined by ,> <protocol>/<port> scope
// <any|private-network|local-network>`, with the space before it (design 47 H2 §813 (4), H-A-357). It carries no mark:
// the table is loaded whole, never reconciled rule by rule.
func derivedComment(rule mcat.Rule) string {
return fmt.Sprintf(" comment %q", fmt.Sprintf("derived %s %s/%d scope %s",
strings.Join(rule.Because, ","), rule.Protocol, rule.Port, scopeWords[rule.From]))
}
// widest drops a rule that another already covers.
//
// A port open to anywhere is not additionally restricted by a second rule opening it to the mesh:
@@ -374,12 +391,12 @@ func AsNftablesAllowing(rules []mcat.Rule, mesh []string, outward bool, foundati
}
four, six := byFamily(mesh)
if len(four) > 0 {
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept\n",
strings.Join(four, ", "), rule.Protocol, rule.Port))
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept%s\n",
strings.Join(four, ", "), rule.Protocol, rule.Port, derivedComment(rule)))
}
if len(six) > 0 {
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept\n",
strings.Join(six, ", "), rule.Protocol, rule.Port))
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept%s\n",
strings.Join(six, ", "), rule.Protocol, rule.Port, derivedComment(rule)))
}
case mcat.FromLocalNetwork:
// The private network and the machine's own local networks, on this port alone (novox/hq
@@ -399,15 +416,15 @@ func AsNftablesAllowing(rules []mcat.Rule, mesh []string, outward bool, foundati
break
}
if len(four) > 0 {
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept\n",
strings.Join(four, ", "), rule.Protocol, rule.Port))
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } %s dport %d accept%s\n",
strings.Join(four, ", "), rule.Protocol, rule.Port, derivedComment(rule)))
}
if len(six) > 0 {
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept\n",
strings.Join(six, ", "), rule.Protocol, rule.Port))
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } %s dport %d accept%s\n",
strings.Join(six, ", "), rule.Protocol, rule.Port, derivedComment(rule)))
}
case mcat.FromEverywhere:
b.WriteString(fmt.Sprintf("\t\t%s dport %d accept\n", rule.Protocol, rule.Port))
b.WriteString(fmt.Sprintf("\t\t%s dport %d accept%s\n", rule.Protocol, rule.Port, derivedComment(rule)))
}
}
@@ -521,27 +538,27 @@ func AsNftablesAllowing(rules []mcat.Rule, mesh []string, outward bool, foundati
case mcat.FromMesh:
four, six := byFamily(mesh)
if len(four) > 0 {
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept\n",
strings.Join(four, ", "), rule.Port))
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept%s\n",
strings.Join(four, ", "), rule.Port, derivedComment(rule)))
}
if len(six) > 0 {
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept\n",
strings.Join(six, ", "), rule.Port))
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept%s\n",
strings.Join(six, ", "), rule.Port, derivedComment(rule)))
}
case mcat.FromLocalNetwork:
four, six := byFamily(mesh)
lfour, lsix := byFamily(local)
four, six = append(four, lfour...), append(six, lsix...)
if len(four) > 0 {
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept\n",
strings.Join(four, ", "), rule.Port))
b.WriteString(fmt.Sprintf("\t\tip saddr { %s } ct original proto-dst %d accept%s\n",
strings.Join(four, ", "), rule.Port, derivedComment(rule)))
}
if len(six) > 0 {
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept\n",
strings.Join(six, ", "), rule.Port))
b.WriteString(fmt.Sprintf("\t\tip6 saddr { %s } ct original proto-dst %d accept%s\n",
strings.Join(six, ", "), rule.Port, derivedComment(rule)))
}
case mcat.FromEverywhere:
b.WriteString(fmt.Sprintf("\t\tct original proto-dst %d accept\n", rule.Port))
b.WriteString(fmt.Sprintf("\t\tct original proto-dst %d accept%s\n", rule.Port, derivedComment(rule)))
}
}
@@ -69,6 +69,7 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) {
rules := []mcat.Rule{
{Port: 1883, Protocol: "tcp", From: mcat.FromLocalNetwork, Because: []string{"mosquitto"}},
{Port: 5432, Protocol: "tcp", From: mcat.FromMesh, Because: []string{"postgres"}},
{Port: 443, Protocol: "tcp", From: mcat.FromEverywhere, Because: []string{"caddy", "forge"}},
}
local := CountedLocalNetworks(judged(t))
out := AsNftablesWithLocal(rules, []string{"10.10.0.1", "10.10.0.4"}, false, nil,
@@ -80,6 +81,13 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) {
"ip saddr { 10.10.0.1, 10.10.0.4, 192.168.1.0/24 } ct original proto-dst 1883 accept",
// The mesh-only port is not widened by the machine having a local network.
"ip saddr { 10.10.0.1, 10.10.0.4 } tcp dport 5432 accept",
// Each derived accept rule names its modules, its listen and its scope in one comment (design 47 H2 §813 (4),
// H-A-357), in both chains.
`tcp dport 1883 accept comment "derived mosquitto tcp/1883 scope local-network"`,
`ct original proto-dst 1883 accept comment "derived mosquitto tcp/1883 scope local-network"`,
`tcp dport 5432 accept comment "derived postgres tcp/5432 scope private-network"`,
"\t\ttcp dport 443 accept comment \"derived caddy,forge tcp/443 scope any\"\n",
"\t\tct original proto-dst 443 accept comment \"derived caddy,forge tcp/443 scope any\"\n",
} {
if !strings.Contains(out, want) {
t.Errorf("the rule set lacks %q:\n%s", want, out)
@@ -92,7 +100,7 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) {
t.Errorf("1883 is open to anywhere:\n%s", out)
}
for _, line := range strings.Split(out, "\n") {
if strings.TrimSpace(line) == "tcp dport 1883 accept" {
if strings.HasPrefix(strings.TrimSpace(line), "tcp dport 1883 accept") {
t.Fatalf("1883 is open to anywhere:\n%s", out)
}
}