E6b: cut a derived rule's comment to nftables' 128 bytes with a count

nftables refuses a rule comment past 128 bytes, and with it the whole
table, so a port shared by enough modules took every opening down. The
comment is a label: past the bound its module list is cut with a count of
the rest; the whole list stands in the table's lines above the rule.

Builds: design 47 H2 §813 (4); H-A-357, H-A-384 (1)
This commit is contained in:
2026-11-22 00:11:18 +01:00
parent f5dcea2f10
commit fea6836f9e
2 changed files with 24 additions and 3 deletions
+14 -3
View File
@@ -154,12 +154,23 @@ var scopeWords = map[string]string{
mcat.FromLocalNetwork: "local-network", mcat.FromLocalNetwork: "local-network",
} }
// commentMost is the most bytes nftables takes in a rule's comment; a longer one refuses the whole table.
const commentMost = 128
// derivedComment is the one comment of a derived accept rule, `derived <modules joined by ,> <protocol>/<port> scope // 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: // <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. // the table is loaded whole, never reconciled rule by rule. The comment is a label: past commentMost the module list is
// cut with a count of the rest, `derived a,b,+4 tcp/443 scope any`, and the whole list stands in the table's lines
// above the rule (H-A-384 (1)).
func derivedComment(rule mcat.Rule) string { func derivedComment(rule mcat.Rule) string {
return fmt.Sprintf(" comment %q", fmt.Sprintf("derived %s %s/%d scope %s", label := func(names []string) string {
strings.Join(rule.Because, ","), rule.Protocol, rule.Port, scopeWords[rule.From])) return fmt.Sprintf("derived %s %s/%d scope %s", strings.Join(names, ","), rule.Protocol, rule.Port, scopeWords[rule.From])
}
text := label(rule.Because)
for kept := len(rule.Because) - 1; len(text) > commentMost && kept >= 0; kept-- {
text = label(append(slices.Clone(rule.Because[:kept]), fmt.Sprintf("+%d", len(rule.Because)-kept)))
}
return fmt.Sprintf(" comment %q", text)
} }
// widest drops a rule that another already covers. // widest drops a rule that another already covers.
@@ -84,6 +84,9 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) {
{Port: 1883, Protocol: "tcp", From: mcat.FromLocalNetwork, Because: []string{"mosquitto"}}, {Port: 1883, Protocol: "tcp", From: mcat.FromLocalNetwork, Because: []string{"mosquitto"}},
{Port: 5432, Protocol: "tcp", From: mcat.FromMesh, Because: []string{"postgres"}}, {Port: 5432, Protocol: "tcp", From: mcat.FromMesh, Because: []string{"postgres"}},
{Port: 443, Protocol: "tcp", From: mcat.FromEverywhere, Because: []string{"caddy", "forge"}}, {Port: 443, Protocol: "tcp", From: mcat.FromEverywhere, Because: []string{"caddy", "forge"}},
// A shared port whose module list would pass nftables' 128 bytes: the label is cut with a count (H-A-384 (1)).
{Port: 8443, Protocol: "tcp", From: mcat.FromEverywhere, Because: []string{"mesh-delivery-pipeline", "mesh-delivery-builder",
"mesh-delivery-assigner", "mesh-delivery-indexer", "mesh-delivery-gate", "mesh-delivery-teller"}},
} }
local := CountedLocalNetworks(judged(t)) local := CountedLocalNetworks(judged(t))
out := AsNftablesWithLocal(rules, []string{"10.10.0.1", "10.10.0.4"}, false, nil, out := AsNftablesWithLocal(rules, []string{"10.10.0.1", "10.10.0.4"}, false, nil,
@@ -102,11 +105,18 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) {
`tcp dport 5432 accept comment "derived postgres tcp/5432 scope private-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\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", "\t\tct original proto-dst 443 accept comment \"derived caddy,forge tcp/443 scope any\"\n",
`tcp dport 8443 accept comment "derived mesh-delivery-pipeline,mesh-delivery-builder,mesh-delivery-assigner,mesh-delivery-indexer,+2 tcp/8443 scope any"`,
"\t\t# mesh-delivery-teller\n",
} { } {
if !strings.Contains(out, want) { if !strings.Contains(out, want) {
t.Errorf("the rule set lacks %q:\n%s", want, out) t.Errorf("the rule set lacks %q:\n%s", want, out)
} }
} }
for _, line := range strings.Split(out, "\n") {
if _, comment, ok := strings.Cut(line, ` comment "`); ok && len(strings.TrimSuffix(comment, `"`)) > commentMost {
t.Errorf("a comment passes nftables' %d bytes: %s", commentMost, line)
}
}
if strings.Contains(out, "203.0.113") { if strings.Contains(out, "203.0.113") {
t.Errorf("the public subnet reached the rule set:\n%s", out) t.Errorf("the public subnet reached the rule set:\n%s", out)
} }