government

commit acb25bc2dcb663eb679fb19123bf0471f5c3c349

tree

parent:
7fc9f65a585bd720d0768ecfb4755a050cfd6106

nmyk <nick@nmyk.io>

2026-11-14T17:12:08-05:00

implement strict policy graph check

diff --git a/DESIGN.md b/DESIGN.md
index fadae80f2ad29f0c9a68cfa1d8263eb3ed0a2b66..a6c9586e9407bd1b9bcdd58ad44c56f97b014f2f 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -314,7 +314,7 @@ 2. For each cert at depth *i*: `CheckSignatureFrom(cert at depth i−1)` passes (else `E-TAMPERED`); the check time (`-at`, default now) is inside its validity (else `E-NOT-IN-FORCE` or `E-LAPSED`).
 3. Certs at depths 0..n−1 are CAs allowed to sign certs (basicConstraints CA:TRUE and keyCertSign); no cert but the anchor is self-issued, since that would change `policy_mapping` counting (else `E-CHAIN`).
 4. No unknown critical extensions; no `policyConstraints` or `inhibitAnyPolicy`, rejected in v1 to keep the counters trivial (else `E-CHAIN`).
 5. With `-crl`: verify each CRL and reject any path cert it revokes (`E-REPEALED`, `E-STALE-REPEALS`).
-6. Run policy processing with the inputs in "The policy tree as AST", then the strict check.
+6. Run policy processing with the inputs in "The policy tree as AST", then the strict check (else `E-POLICY`; a graph past the `-max-nodes` budget is `E-TOO-LARGE`).
 
 ### CLI
 
diff --git a/policy/error.go b/policy/error.go
index 7e1ab3cada1bbc783a2fd8a327af8384d744c720..cfeef55814fb1cad83dfcd03da6c52acf4b7f073 100644
--- a/policy/error.go
+++ b/policy/error.go
@@ -6,6 +6,9 @@ // ErrPolicy: policy processing rejected the chain, so the certs do not
 // describe a program.
 const ErrPolicy = "E-POLICY"
 
+// ErrTooLarge: building the graph would exceed the node budget.
+const ErrTooLarge = "E-TOO-LARGE"
+
 type Error struct {
 	Code string
 	Msg  string
diff --git a/policy/graph.go b/policy/graph.go
index 564abb968281f6a9bde6c4408ac1a91969c78d14..3c3e92128cf296cc13d3f138a5fe4536b6eb50bc 100644
--- a/policy/graph.go
+++ b/policy/graph.go
@@ -46,6 +46,35 @@ // Graph is the valid policy graph after the whole path. Levels[0] holds the
 // root, an anyPolicy node; Levels[i] holds the nodes made by cert i.
 type Graph struct {
 	Levels [][]*Node
+	// budget is how many more nodes may be made.
+	budget int
+}
+
+// DefaultMaxNodes caps the graph's size. Policy processing has caused real
+// denial-of-service bugs (Go's CVE-2026-32281 among them): a small file
+// can ask for a very large graph, so the budget is checked as nodes are
+// made, not after.
+const DefaultMaxNodes = 1_000_000
+
+type Options struct {
+	// MaxNodes is the most nodes the graph may hold, counting the
+	// validator's copies. 0 means DefaultMaxNodes.
+	MaxNodes int
+}
+
+func (o Options) maxNodes() int {
+	if o.MaxNodes == 0 {
+		return DefaultMaxNodes
+	}
+	return o.MaxNodes
+}
+
+func (g *Graph) spend(depth int) error {
+	if g.budget == 0 {
+		return errorf(ErrTooLarge, "the policy graph needs more than its node budget at depth %d (see -max-nodes)", depth)
+	}
+	g.budget--
+	return nil
 }
 
 func (g *Graph) Depth() int { return len(g.Levels) - 1 }
@@ -58,9 +87,9 @@ // The inputs are fixed by the design: user-initial-policy-set {anyPolicy},
 // explicit policy required, anyPolicy and mappings not inhibited. Ratify
 // already rejected policyConstraints and inhibitAnyPolicy, so the RFC's
 // three counters never reach zero and are left out.
-func Process(certs []*x509.Certificate) (*Graph, error) {
+func Process(certs []*x509.Certificate, opts Options) (*Graph, error) {
 	root := &Node{Policy: AnyPolicy, Expected: []x509.OID{AnyPolicy}}
-	g := &Graph{Levels: [][]*Node{{root}}}
+	g := &Graph{Levels: [][]*Node{{root}}, budget: opts.maxNodes()}
 	n := len(certs) - 1
 	for i := 1; i <= n; i++ {
 		if err := g.addCert(i, certs[i]); err != nil {
@@ -87,13 +116,17 @@ 	}
 	parents := g.Levels[i-1]
 	var level []*Node
 	byPolicy := map[string]*Node{}
-	add := func(p x509.OID, from []*Node) {
+	add := func(p x509.OID, from []*Node) error {
+		if err := g.spend(i); err != nil {
+			return err
+		}
 		n := &Node{Policy: p, Depth: i, Parents: from, Expected: []x509.OID{p}}
 		for _, parent := range from {
 			parent.Children = append(parent.Children, n)
 		}
 		level = append(level, n)
 		byPolicy[key(p)] = n
+		return nil
 	}
 	expecting := func(p x509.OID) []*Node {
 		var from []*Node
@@ -126,7 +159,9 @@ 				}
 			}
 		}
 		if len(from) > 0 {
-			add(p, from)
+			if err := add(p, from); err != nil {
+				return err
+			}
 		}
 	}
 
@@ -138,7 +173,9 @@ 	if assertsAny {
 		for _, parent := range parents {
 			for _, p := range parent.Expected {
 				if byPolicy[key(p)] == nil {
-					add(p, expecting(p))
+					if err := add(p, expecting(p)); err != nil {
+						return err
+					}
 				}
 			}
 		}
@@ -227,6 +264,9 @@ 		// cert 1, so this only matters for chains built by other tools.
 		for _, anyNode := range level {
 			if !anyNode.Policy.Equal(AnyPolicy) {
 				continue
+			}
+			if err := g.spend(i); err != nil {
+				return err
 			}
 			n := &Node{Policy: p, Depth: i, Parents: slices.Clone(anyNode.Parents), Expected: mapped[key(p)]}
 			for _, parent := range n.Parents {
diff --git a/policy/graph_test.go b/policy/graph_test.go
index f1e1966414de83d9f814bf528792d5d89bef5dfc..e2970c181219bcae18f88610587c570326f57106 100644
--- a/policy/graph_test.go
+++ b/policy/graph_test.go
@@ -49,7 +49,7 @@ }
 
 func mustProcess(t *testing.T, certs []*x509.Certificate) *Graph {
 	t.Helper()
-	g, err := Process(certs)
+	g, err := Process(certs, Options{})
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -58,7 +58,7 @@ }
 
 func wantPolicyError(t *testing.T, name string, certs []*x509.Certificate, msg string) {
 	t.Helper()
-	_, err := Process(certs)
+	_, err := Process(certs, Options{})
 	var e *Error
 	if !errors.As(err, &e) || e.Code != ErrPolicy || !strings.Contains(e.Msg, msg) {
 		t.Errorf("%s: error = %v, want %s containing %q", name, err, ErrPolicy, msg)
diff --git a/policy/layers.go b/policy/layers.go
new file mode 100644
index 0000000000000000000000000000000000000000..b0af1c232956022acd1223a3a82feb7b332e5f80
--- /dev/null
+++ b/policy/layers.go
@@ -0,0 +1,36 @@
+package policy
+
+import (
+	"fmt"
+
+	"government/ast"
+)
+
+// Layers turns the graph into the decoder's input: every node below the
+// root, decoded from its OID, with its depth and edges. The validator's
+// copies stay in as nodes of their own; ast.Unlayer recognizes and skips
+// them.
+func (g *Graph) Layers() (ast.Layers, error) {
+	n := g.Depth()
+	l := ast.Layers{Nodes: make([][]*ast.Node, n), Edges: make([][]ast.Edge, n)}
+	decoded := map[*Node]*ast.Node{}
+	for d := 1; d <= n; d++ {
+		for _, gn := range g.Levels[d] {
+			ns, id, text, err := ast.DecodeOID(gn.Policy)
+			if err != nil {
+				return ast.Layers{}, fmt.Errorf("depth %d: %w", d, err)
+			}
+			an := &ast.Node{ID: id, NS: ns, Text: text}
+			decoded[gn] = an
+			l.Nodes[d-1] = append(l.Nodes[d-1], an)
+		}
+	}
+	for d := 1; d < n; d++ {
+		for _, parent := range g.Levels[d] {
+			for _, child := range parent.Children {
+				l.Edges[d-1] = append(l.Edges[d-1], ast.Edge{Parent: decoded[parent], Child: decoded[child]})
+			}
+		}
+	}
+	return l, nil
+}
diff --git a/policy/strict.go b/policy/strict.go
new file mode 100644
index 0000000000000000000000000000000000000000..1531a689a9788e7b484e85969d28af408039e0b9
--- /dev/null
+++ b/policy/strict.go
@@ -0,0 +1,140 @@
+package policy
+
+import (
+	"crypto/x509"
+	"strings"
+
+	"government/ast"
+)
+
+// Program runs policy processing the GOVERNMENT way: it checks the shape
+// the compiler always writes, runs RFC processing with a node budget, then
+// checks that processing kept everything the certs asked for.
+//
+// RFC processing is forgiving by design: a policy that attaches nowhere,
+// or a mapping from a policy with no node, is silently dropped, because a
+// relying party only needs to know which policies survive. For a program,
+// every dropped policy is dropped code, so the strict check turns each one
+// into an error.
+func Program(certs []*x509.Certificate, opts Options) (*Graph, error) {
+	if err := checkShape(certs); err != nil {
+		return nil, err
+	}
+	g, err := Process(certs, opts)
+	if err != nil {
+		return nil, err
+	}
+	if err := g.checkKept(certs); err != nil {
+		return nil, err
+	}
+	if err := g.checkIDs(); err != nil {
+		return nil, err
+	}
+	return g, nil
+}
+
+// checkShape enforces what the compiler always writes: cert 1 lists the
+// top-level nodes and never anyPolicy, certs 2..n assert anyPolicy, and
+// cert n has no mappings, which RFC 5280 would never read.
+func checkShape(certs []*x509.Certificate) error {
+	n := len(certs) - 1
+	for i := 1; i <= n; i++ {
+		asserts := false
+		for _, p := range certs[i].Policies {
+			if p.Equal(AnyPolicy) {
+				asserts = true
+			}
+		}
+		switch {
+		case i == 1 && asserts:
+			return errorf(ErrPolicy, "cert 1 asserts anyPolicy; it must list the top-level forms only")
+		case i > 1 && !asserts:
+			return errorf(ErrPolicy, "cert %d does not assert anyPolicy, so nothing below depth %d can exist", i, i-1)
+		}
+	}
+	if len(certs[n].PolicyMappings) > 0 {
+		return errorf(ErrPolicy, "cert %d is the last and has mappings, which are never processed", n)
+	}
+	return nil
+}
+
+// checkKept checks that processing dropped nothing: every policy a cert
+// names is a node at that cert's depth, and every mapping became an edge.
+func (g *Graph) checkKept(certs []*x509.Certificate) error {
+	for i := 1; i <= g.Depth(); i++ {
+		for _, p := range certs[i].Policies {
+			if !p.Equal(AnyPolicy) && g.find(i, p) == nil {
+				return errorf(ErrPolicy, "orphaned policy %s at depth %d: it attaches to nothing", short(p), i)
+			}
+		}
+		if i == g.Depth() {
+			break
+		}
+		for _, m := range certs[i].PolicyMappings {
+			parent := g.find(i, m.IssuerDomainPolicy)
+			if parent == nil {
+				return errorf(ErrPolicy, "orphaned mapping %s -> %s in cert %d: %s is not a node at depth %d",
+					short(m.IssuerDomainPolicy), short(m.SubjectDomainPolicy), i, short(m.IssuerDomainPolicy), i)
+			}
+			if !hasChild(parent, m.SubjectDomainPolicy) {
+				return errorf(ErrPolicy, "orphaned mapping %s -> %s in cert %d: no such child at depth %d",
+					short(m.IssuerDomainPolicy), short(m.SubjectDomainPolicy), i, i+1)
+			}
+		}
+	}
+	return nil
+}
+
+// checkIDs checks that every node is a GOVERNMENT node and the program's
+// node ids are exactly 1..N. The compiler numbers every node, so a gap
+// means a subtree went missing between compiling and running, and a node
+// whose mappings were lost would otherwise decode as a shorter program.
+// One loss this cannot see is the subtree holding the highest ids, which
+// leaves no gap; signatures rule out tampering, so only a compiler bug can
+// cause it, and the round-trip tests are what catch that.
+func (g *Graph) checkIDs() error {
+	byID := map[uint64]string{}
+	var maxID uint64
+	for d := 1; d <= g.Depth(); d++ {
+		for _, n := range g.Levels[d] {
+			_, id, _, err := ast.DecodeOID(n.Policy)
+			if err != nil {
+				return errorf(ErrPolicy, "depth %d: %v", d, err)
+			}
+			if prev, ok := byID[id]; ok && prev != key(n.Policy) {
+				return errorf(ErrPolicy, "depth %d: two different nodes use id %d", d, id)
+			}
+			byID[id] = key(n.Policy)
+			maxID = max(maxID, id)
+		}
+	}
+	for id := uint64(1); id <= maxID; id++ {
+		if _, ok := byID[id]; !ok {
+			return errorf(ErrPolicy, "node id %d is missing (ids run to %d): a subtree was lost", id, maxID)
+		}
+	}
+	return nil
+}
+
+func (g *Graph) find(depth int, p x509.OID) *Node {
+	for _, n := range g.Levels[depth] {
+		if n.Policy.Equal(p) {
+			return n
+		}
+	}
+	return nil
+}
+
+func hasChild(n *Node, p x509.OID) bool {
+	for _, c := range n.Children {
+		if c.Policy.Equal(p) {
+			return true
+		}
+	}
+	return false
+}
+
+// short prints an OID with GOV in place of the root arc, as tools do.
+func short(oid x509.OID) string {
+	return strings.Replace(oid.String(), ast.Root, "GOV", 1)
+}
diff --git a/policy/strict_test.go b/policy/strict_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..e34c6b3dcc9bf854851fc2a55b54be7db9967df8
--- /dev/null
+++ b/policy/strict_test.go
@@ -0,0 +1,199 @@
+package policy
+
+import (
+	"crypto/x509"
+	"errors"
+	"os"
+	"path/filepath"
+	"reflect"
+	"strings"
+	"testing"
+
+	"government/ast"
+	"government/lisp2gov"
+	"government/ratify"
+	"government/sexpr"
+)
+
+// loadCerts compiles src and returns its certs in order. Policy processing
+// never looks at signatures, so tests may edit these certs freely.
+func loadCerts(t *testing.T, src string) []*x509.Certificate {
+	t.Helper()
+	out, err := lisp2gov.Compile(src, lisp2gov.Options{})
+	if err != nil {
+		t.Fatal(err)
+	}
+	certs, err := ratify.Ratify(out, ratify.Options{At: ratifyTime})
+	if err != nil {
+		t.Fatal(err)
+	}
+	return certs
+}
+
+// edited returns a copy of certs whose cert i is a copy that f may change.
+func edited(certs []*x509.Certificate, i int, f func(c *x509.Certificate)) []*x509.Certificate {
+	out := append([]*x509.Certificate(nil), certs...)
+	c := *certs[i]
+	c.Policies = append([]x509.OID(nil), c.Policies...)
+	c.PolicyMappings = append([]x509.PolicyMapping(nil), c.PolicyMappings...)
+	f(&c)
+	out[i] = &c
+	return out
+}
+
+// decode runs the rest of the way to a program: graph, layers, tree,
+// values.
+func decode(certs []*x509.Certificate) ([]sexpr.Value, error) {
+	g, err := Program(certs, Options{})
+	if err != nil {
+		return nil, err
+	}
+	layers, err := g.Layers()
+	if err != nil {
+		return nil, err
+	}
+	tree, err := ast.Unlayer(layers)
+	if err != nil {
+		return nil, err
+	}
+	forms, _, err := ast.FromTree(tree)
+	return forms, err
+}
+
+func TestTestdataDecodesToItsSource(t *testing.T) {
+	files, _ := filepath.Glob("../testdata/*.gov.lisp")
+	files = append(files, "../examples/sq.gov.lisp")
+	for _, file := range files {
+		src, err := os.ReadFile(file)
+		if err != nil {
+			t.Fatal(err)
+		}
+		want, err := sexpr.ReadAll(string(src))
+		if err != nil {
+			t.Fatal(err)
+		}
+		got, err := decode(loadCerts(t, string(src)))
+		if err != nil {
+			t.Errorf("%s: %v", file, err)
+			continue
+		}
+		if !reflect.DeepEqual(got, want) {
+			t.Errorf("%s: decoded program differs from its source", file)
+		}
+	}
+}
+
+func checkErr(t *testing.T, name string, err error, code, msg string) {
+	t.Helper()
+	var e *Error
+	if !errors.As(err, &e) || e.Code != code || !strings.Contains(e.Msg, msg) {
+		t.Errorf("%s: error = %v, want %s containing %q", name, err, code, msg)
+	}
+}
+
+func TestStrictShape(t *testing.T) {
+	certs := loadCerts(t, "(+ 1 (* 2 3))")
+	plusOID := certs[1].PolicyMappings[0].SubjectDomainPolicy  // +#2, at depth 2
+	timesOID := certs[2].PolicyMappings[0].SubjectDomainPolicy // *#5, at depth 3
+	tests := []struct {
+		name  string
+		certs []*x509.Certificate
+		msg   string
+	}{
+		{"anyPolicy in cert 1", edited(certs, 1, func(c *x509.Certificate) { c.Policies = append(c.Policies, AnyPolicy) }), "cert 1 asserts anyPolicy"},
+		{"no anyPolicy in cert 2", edited(certs, 2, func(c *x509.Certificate) { c.Policies = []x509.OID{plusOID} }), "cert 2 does not assert anyPolicy"},
+		{"mappings in the last cert", edited(certs, 3, func(c *x509.Certificate) { c.PolicyMappings = certs[2].PolicyMappings }), "cert 3 is the last and has mappings"},
+		// *#5 belongs at depth 3; named in cert 2, no depth-1 node expects
+		// it, so RFC processing would quietly drop it.
+		{"policy that attaches to nothing", edited(certs, 2, func(c *x509.Certificate) { c.Policies = append(c.Policies, timesOID) }), "orphaned policy GOV.0.5.23 at depth 2"},
+		{"mapping from a policy with no node", edited(certs, 2, func(c *x509.Certificate) {
+			c.PolicyMappings = append(c.PolicyMappings, x509.PolicyMapping{IssuerDomainPolicy: timesOID, SubjectDomainPolicy: certs[1].Policies[0]})
+		}), "orphaned mapping GOV.0.5.23 -> GOV.0.1.1 in cert 2: GOV.0.5.23 is not a node at depth 2"},
+		{"a policy outside GOV", edited(certs, 1, func(c *x509.Certificate) { c.Policies = append(c.Policies, mustOID("1.2.3")) }), "not under"},
+	}
+	for _, tt := range tests {
+		_, err := Program(tt.certs, Options{})
+		checkErr(t, tt.name, err, ErrPolicy, tt.msg)
+	}
+}
+
+// TestDeletingAMapping is the point of the strict check: losing a mapping
+// must be an error, not a shorter program. It drops each mapping of a
+// program in turn. The one loss nothing can see is the subtree holding the
+// highest ids, as long as its parent keeps another element; this test
+// lists those cases rather than hiding them.
+func TestDeletingAMapping(t *testing.T) {
+	src := "(define f (lambda (x) (* x (+ x 1)))) (f 2)"
+	certs := loadCerts(t, src)
+	tree, err := ast.ToTree(mustRead(t, src))
+	if err != nil {
+		t.Fatal(err)
+	}
+	subtree := map[uint64][2]uint64{} // id -> [first id, last id] of its subtree
+	var walk func(n *ast.Node) uint64
+	walk = func(n *ast.Node) uint64 {
+		last := n.ID
+		for _, c := range n.Children {
+			last = walk(c)
+		}
+		subtree[n.ID] = [2]uint64{n.ID, last}
+		return last
+	}
+	for _, f := range tree.Forms {
+		walk(f)
+	}
+	maxID := subtree[tree.Forms[len(tree.Forms)-1].ID][1]
+
+	var silent []string
+	for i := 1; i < len(certs)-1; i++ {
+		for j, m := range certs[i].PolicyMappings {
+			_, childID, _, _ := ast.DecodeOID(m.SubjectDomainPolicy)
+			parentKeepsAnElement := false
+			for k, other := range certs[i].PolicyMappings {
+				if k != j && other.IssuerDomainPolicy.Equal(m.IssuerDomainPolicy) {
+					parentKeepsAnElement = true
+				}
+			}
+			invisible := subtree[childID][1] == maxID && parentKeepsAnElement
+
+			dropped := edited(certs, i, func(c *x509.Certificate) {
+				c.PolicyMappings = append(c.PolicyMappings[:j:j], c.PolicyMappings[j+1:]...)
+			})
+			_, err := decode(dropped)
+			name := short(m.IssuerDomainPolicy) + " -> " + short(m.SubjectDomainPolicy)
+			switch {
+			case invisible && err != nil:
+				t.Errorf("%s: expected an undetectable loss, got %v", name, err)
+			case invisible:
+				silent = append(silent, name)
+			case err == nil:
+				t.Errorf("%s: dropping it gave a shorter program, not an error", name)
+			}
+		}
+	}
+	// The undetectable cases are the last element of each list on the
+	// program's rightmost edge whose list keeps another element: here only
+	// the 2 in (f 2), node 17.
+	if len(silent) != 1 || !strings.HasSuffix(silent[0], "GOV.2.17.50") {
+		t.Errorf("undetectable losses: %v, want only the mapping to 2#17", silent)
+	}
+}
+
+func TestNodeBudget(t *testing.T) {
+	certs := loadCerts(t, "(+ 1 (* 2 3))")
+	// The graph holds 9 nodes below the root: 1 + 3 + 5.
+	if _, err := Program(certs, Options{MaxNodes: 9}); err != nil {
+		t.Errorf("budget 9: %v", err)
+	}
+	_, err := Program(certs, Options{MaxNodes: 8})
+	checkErr(t, "budget 8", err, ErrTooLarge, "depth 3")
+}
+
+func mustRead(t *testing.T, src string) []sexpr.Value {
+	t.Helper()
+	forms, err := sexpr.ReadAll(src)
+	if err != nil {
+		t.Fatal(err)
+	}
+	return forms
+}