carlos / platform Public

egressaddr: allow entries may be private prefixes (#1357)

ParseEntry reads a single address or a prefix; AllowedPrefix accepts a
prefix no wider than /16 or /48 that lies wholly inside one fence range
and overlaps no refused class. Normalize works on entries and stores a
full-length prefix as the bare address. Parse still refuses a range.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 8ced8809064aa278b1e71716065687ac95952f53 parent f1eb7b7
2 files changed, +298 −24
  • internal/egressaddr/egressaddr.go +157 −24
  • internal/egressaddr/egressaddr_test.go +141 −0
diff --git a/internal/egressaddr/egressaddr.go b/internal/egressaddr/egressaddr.go
index bc938751c..2061903d4 100644
--- a/internal/egressaddr/egressaddr.go
+++ b/internal/egressaddr/egressaddr.go
@@ -3,11 +3,18 @@
// rule set the console door applies when an operator grants an address and
// root applies again on the box before it writes the allow.
//
-// An allow re-opens one address the instance fence closes. So an allow must
-// be a single address, inside the fence (a public address is already
-// reachable), and never one of the classes no tenant may reach from any
-// box: loopback, unspecified, link-local, the metadata service, multicast,
-// the reserved ranges and the NAT64 prefix.
+// An allow re-opens an address, or a range of addresses, the instance fence
+// closes. An entry is a single address or a prefix (#1357). It must lie
+// wholly inside the fence (a public address is already reachable) and hold
+// none of the classes no tenant may reach from any box: loopback,
+// unspecified, link-local, the metadata service, multicast, the reserved
+// ranges and the NAT64 prefix. A prefix is allowed exactly when every
+// address in it would be allowed alone, checked on the prefix, not by
+// enumeration, and it is no wider than MinPrefixV4 or MinPrefixV6.
+//
+// A single address is stored as the bare address, so a list of singles
+// reads the same as before prefixes. Parse stays the single-address
+// reader for callers that need one address and refuses a prefix.
//
// FenceDeny is the fence itself, the IPAddressDeny= line the instance unit
// templates write. internal/host's TestInstanceFenceIsEgressaddrFenceDeny
@@ -28,9 +35,17 @@ import (
"strings"
)
-// MaxAllow is the most addresses one app may be granted.
+// MaxAllow is the most entries one app may be granted. A prefix counts as
+// one entry.
const MaxAllow = 8
+// The widest prefix an entry may be: /16 is the largest AWS VPC, /48 a ULA
+// site prefix (RFC 4193).
+const (
+ MinPrefixV4 = 16
+ MinPrefixV6 = 48
+)
+
// Refusal text. A person reads these at the door and in root's report.
const (
msgEmpty = "an address is empty"
@@ -47,6 +62,9 @@ const (
msgNAT64 = "%s is in the NAT64 prefix 64:ff9b::/96, which reaches any IPv4 address"
msgPublic = "%s is a public address and is already reachable. no allow is needed"
msgTooMany = "at most %d addresses per app (%d given)"
+ msgHostBits = "%q has host bits set. write it as %s"
+ msgWide = "%s is wider than /%d. allow at most a /%d"
+ msgNotPrivate = "%s is not wholly inside a private range"
)
// FenceDeny is the instance fence: the IPAddressDeny= line of the instance
@@ -61,6 +79,12 @@ var FenceDeny = mustPrefixes(
)
var (
+ // The classes a prefix may not overlap, as Allowed's net/netip tests
+ // read them.
+ loopback = mustPrefixes("127.0.0.0/8", "::1/128")
+ linkLocal = mustPrefixes("169.254.0.0/16", "fe80::/10")
+ multicast = mustPrefixes("224.0.0.0/4", "ff00::/8")
+
// The cloud metadata service's two addresses (as ResolverAllowed).
metadataV4 = netip.MustParseAddr("169.254.169.254")
metadataV6 = netip.MustParseAddr("fd00:ec2::254")
@@ -139,40 +163,149 @@ func Allowed(a netip.Addr) error {
return fmt.Errorf(msgPublic, a)
}
+// ParseEntry reads one entry as an operator writes it: surrounding space
+// trimmed, a single address (as Parse reads it) or a prefix a/n with no
+// host bits set, port or zone. A single address becomes its full-length
+// prefix. An IPv4-mapped IPv6 prefix is refused.
+func ParseEntry(s string) (netip.Prefix, error) {
+ s = strings.TrimSpace(s)
+ slash := strings.LastIndex(s, "/")
+ if slash < 0 {
+ a, err := Parse(s)
+ if err != nil {
+ return netip.Prefix{}, err
+ }
+ return netip.PrefixFrom(a, a.BitLen()), nil
+ }
+ if strings.Contains(s[slash:], ":") {
+ return netip.Prefix{}, fmt.Errorf(msgPort, s)
+ }
+ if strings.Contains(s, "%") {
+ return netip.Prefix{}, fmt.Errorf(msgZone, s)
+ }
+ p, err := netip.ParsePrefix(s)
+ if err != nil || p.Addr().Is4In6() {
+ return netip.Prefix{}, fmt.Errorf(msgNotAddr, s)
+ }
+ if m := p.Masked(); m != p {
+ return netip.Prefix{}, fmt.Errorf(msgHostBits, s, m)
+ }
+ return p, nil
+}
+
+// AllowedPrefix reports why p may not be granted, or nil if it may. A
+// full-length prefix is the single address and gets Allowed's answer. A
+// wider one must be no wider than the minimum length, lie wholly inside
+// one FenceDeny range, and overlap none of the refused classes, so every
+// address in it would pass Allowed.
+func AllowedPrefix(p netip.Prefix) error {
+ if !p.IsValid() || (p.Addr().Is4In6() && !p.IsSingleIP()) {
+ return fmt.Errorf(msgNotAddr, p.String())
+ }
+ if p.IsSingleIP() {
+ return Allowed(p.Addr())
+ }
+ if m := p.Masked(); m != p {
+ return fmt.Errorf(msgHostBits, p.String(), m)
+ }
+ minBits := MinPrefixV6
+ if p.Addr().Is4() {
+ minBits = MinPrefixV4
+ }
+ if p.Bits() < minBits {
+ return fmt.Errorf(msgWide, p, minBits, minBits)
+ }
+ inside := false
+ for _, f := range FenceDeny {
+ if f.Bits() <= p.Bits() && f.Contains(p.Addr()) {
+ inside = true
+ break
+ }
+ }
+ if !inside {
+ return fmt.Errorf(msgNotPrivate, p)
+ }
+ // Link-local before metadata: a link-local prefix holding the
+ // metadata address reads better as link-local.
+ switch {
+ case overlapsAny(p, loopback):
+ return fmt.Errorf(msgLoopback, p)
+ case overlapsAny(p, linkLocal):
+ return fmt.Errorf(msgLinkLocal, p)
+ case p.Contains(metadataV4), p.Contains(metadataV6):
+ return fmt.Errorf(msgMetadata, p)
+ case overlapsAny(p, multicast):
+ return fmt.Errorf(msgMulticast, p)
+ }
+ for _, r := range reserved {
+ if r.Overlaps(p) {
+ return fmt.Errorf(msgReserved, p, r)
+ }
+ }
+ if nat64.Overlaps(p) {
+ return fmt.Errorf(msgNAT64, p)
+ }
+ // Unspecified is inside 0.0.0.0/8 (reserved) for IPv4 and outside
+ // the fence for IPv6, so it is refused above; kept for the rule set.
+ if p.Contains(netip.IPv4Unspecified()) || p.Contains(netip.IPv6Unspecified()) {
+ return fmt.Errorf(msgUnspecified, p)
+ }
+ return nil
+}
+
+func overlapsAny(p netip.Prefix, ps []netip.Prefix) bool {
+ for _, q := range ps {
+ if q.Overlaps(p) {
+ return true
+ }
+ }
+ return false
+}
+
// Normalize turns an operator's list into the stored form: every
-// non-empty entry parsed and allowed, duplicates collapsed, sorted (IPv4
-// before IPv6), at most MaxAllow after the collapse, each in its
-// canonical text. An empty result is nil. The first refused entry is the
-// error.
+// non-empty entry parsed and allowed, exact duplicates collapsed (an entry
+// inside another is kept), sorted by address then prefix length (IPv4
+// before IPv6), at most MaxAllow after the collapse. A full-length prefix
+// is stored as the bare address, any other as its canonical prefix text.
+// An empty result is nil. The first refused entry is the error.
func Normalize(in []string) ([]string, error) {
- seen := map[netip.Addr]bool{}
- var addrs []netip.Addr
+ seen := map[netip.Prefix]bool{}
+ var entries []netip.Prefix
for _, s := range in {
if strings.TrimSpace(s) == "" {
continue
}
- a, err := Parse(s)
+ p, err := ParseEntry(s)
if err != nil {
return nil, err
}
- if err := Allowed(a); err != nil {
+ if err := AllowedPrefix(p); err != nil {
return nil, err
}
- if !seen[a] {
- seen[a] = true
- addrs = append(addrs, a)
+ if !seen[p] {
+ seen[p] = true
+ entries = append(entries, p)
}
}
- if len(addrs) > MaxAllow {
- return nil, fmt.Errorf(msgTooMany, MaxAllow, len(addrs))
+ if len(entries) > MaxAllow {
+ return nil, fmt.Errorf(msgTooMany, MaxAllow, len(entries))
}
- if len(addrs) == 0 {
+ if len(entries) == 0 {
return nil, nil
}
- slices.SortFunc(addrs, netip.Addr.Compare)
- out := make([]string, len(addrs))
- for i, a := range addrs {
- out[i] = a.String()
+ slices.SortFunc(entries, func(a, b netip.Prefix) int {
+ if c := a.Addr().Compare(b.Addr()); c != 0 {
+ return c
+ }
+ return a.Bits() - b.Bits()
+ })
+ out := make([]string, len(entries))
+ for i, p := range entries {
+ if p.IsSingleIP() {
+ out[i] = p.Addr().String()
+ } else {
+ out[i] = p.String()
+ }
}
return out, nil
}
diff --git a/internal/egressaddr/egressaddr_test.go b/internal/egressaddr/egressaddr_test.go
index b3ec2f03e..5073d0e87 100644
--- a/internal/egressaddr/egressaddr_test.go
+++ b/internal/egressaddr/egressaddr_test.go
@@ -127,3 +127,144 @@ func TestNormalize(t *testing.T) {
t.Errorf("Normalize with a public address error %v, want it refused", err)
}
}
+
+// acceptedEntries are entries an operator may grant, each with its
+// stored form.
+var acceptedEntries = map[string]string{
+ "10.0.64.0/20": "10.0.64.0/20",
+ "10.0.0.0/16": "10.0.0.0/16",
+ "172.16.0.0/16": "172.16.0.0/16",
+ "192.168.0.0/16": "192.168.0.0/16",
+ "100.64.0.0/16": "100.64.0.0/16",
+ "fd00:5::/64": "fd00:5::/64",
+ "fd00:5::/48": "fd00:5::/48",
+ "10.0.5.20/32": "10.0.5.20",
+ "fd00:5::20/128": "fd00:5::20",
+ "10.0.5.20": "10.0.5.20",
+ " fd00:5::20 ": "fd00:5::20",
+}
+
+func TestEntriesAccepted(t *testing.T) {
+ for in, want := range acceptedEntries {
+ got, err := Normalize([]string{in})
+ if err != nil {
+ t.Errorf("Normalize(%q): %v", in, err)
+ continue
+ }
+ if len(got) != 1 || got[0] != want {
+ t.Errorf("Normalize(%q) = %q, want [%s]", in, got, want)
+ }
+ }
+}
+
+func TestEntriesRefused(t *testing.T) {
+ for in, want := range map[string]string{
+ "10.0.0.0/8": "10.0.0.0/8 is wider than /16. allow at most a /16",
+ "10.0.0.0/15": "10.0.0.0/15 is wider than /16. allow at most a /16",
+ "fd00::/47": "fd00::/47 is wider than /48. allow at most a /48",
+ "10.0.64.5/20": `"10.0.64.5/20" has host bits set. write it as 10.0.64.0/20`,
+ "169.254.0.0/16": "169.254.0.0/16 is link-local",
+ "169.254.169.0/24": "169.254.169.0/24 is link-local",
+ "fd00:ec2::/48": "fd00:ec2::/48 is the cloud metadata service",
+ "64:ff9b::/96": "64:ff9b::/96 is in the NAT64 prefix 64:ff9b::/96, which reaches any IPv4 address",
+ "198.18.0.0/16": "198.18.0.0/16 is in 198.18.0.0/15, a reserved range",
+ "8.8.8.0/24": "8.8.8.0/24 is not wholly inside a private range",
+ "0.0.0.0/16": "0.0.0.0/16 is in 0.0.0.0/8, a reserved range",
+ "224.0.0.0/16": "224.0.0.0/16 is a multicast address",
+ "127.0.0.0/16": "127.0.0.0/16 is a loopback address",
+ "fe80::/64": "fe80::/64 is link-local",
+ "10.0.64.0/20:5432": `"10.0.64.0/20:5432" has a port. an allow opens an address, never a port`,
+ "::ffff:10.0.0.0/112": `"::ffff:10.0.0.0/112" is not an IP address`,
+ "10.0.0.0/16%eth0": "has a zone",
+ "10.0.0.0/33": "is not an IP address",
+ "10.0.0.0/": "is not an IP address",
+ "fd00:5::/129": "is not an IP address",
+ "172.16.0.0/12": "172.16.0.0/12 is wider than /16",
+ "fec0::/48": "fec0::/48 is in fec0::/10, a reserved range",
+ "2001:db8::/48": "2001:db8::/48 is not wholly inside a private range",
+ "8.8.8.8/32": "8.8.8.8 is a public address",
+ "169.254.169.254/32": "169.254.169.254 is the cloud metadata service",
+ } {
+ got, err := Normalize([]string{in})
+ if err == nil {
+ t.Errorf("Normalize(%q) = %q, want an error containing %q", in, got, want)
+ continue
+ }
+ if !strings.Contains(err.Error(), want) {
+ t.Errorf("Normalize(%q) error %q, want it to contain %q", in, err, want)
+ }
+ }
+}
+
+func TestParseEntryUnmapsAndKeepsParseStrict(t *testing.T) {
+ p, err := ParseEntry("::ffff:10.0.5.20")
+ if err != nil || p != netip.MustParsePrefix("10.0.5.20/32") {
+ t.Errorf("ParseEntry(::ffff:10.0.5.20) = %s, %v; want 10.0.5.20/32", p, err)
+ }
+ if _, err := ParseEntry(""); err == nil || err.Error() != "an address is empty" {
+ t.Errorf("ParseEntry(\"\") error %v, want empty", err)
+ }
+ // Parse stays the single-address reader: an old box refuses a range.
+ if _, err := Parse("10.0.64.0/20"); err == nil || !strings.Contains(err.Error(), "is a range") {
+ t.Errorf("Parse(10.0.64.0/20) error %v, want the range refusal", err)
+ }
+}
+
+func TestNormalizeSortsAndDedupsEntries(t *testing.T) {
+ got, err := Normalize([]string{"fd00:5::/64", "10.0.64.0", "10.0.64.0/20", "10.0.64.0/20", "10.0.64.0/32"})
+ if err != nil {
+ t.Fatalf("Normalize: %v", err)
+ }
+ if want := []string{"10.0.64.0/20", "10.0.64.0", "fd00:5::/64"}; !reflect.DeepEqual(got, want) {
+ t.Errorf("Normalize = %q, want %q", got, want)
+ }
+ got, err = Normalize([]string{"10.0.64.0", "10.0.64.0/20", "10.0.64.0/20"})
+ if err != nil {
+ t.Fatalf("Normalize: %v", err)
+ }
+ if want := []string{"10.0.64.0/20", "10.0.64.0"}; !reflect.DeepEqual(got, want) {
+ t.Errorf("Normalize = %q, want %q", got, want)
+ }
+}
+
+func TestNormalizeCountsPrefixesAsEntries(t *testing.T) {
+ var nine []string
+ for i := 1; i <= 9; i++ {
+ nine = append(nine, fmt.Sprintf("10.%d.0.0/16", i))
+ }
+ if _, err := Normalize(nine); err == nil || !strings.Contains(err.Error(), "at most 8 addresses per app (9 given)") {
+ t.Errorf("Normalize(nine prefixes) error %v, want the cap", err)
+ }
+ if got, err := Normalize(nine[:8]); err != nil || len(got) != MaxAllow {
+ t.Errorf("Normalize(eight prefixes) = %q, %v; want eight and no error", got, err)
+ }
+}
+
+// lastAddr is the highest address in p.
+func lastAddr(p netip.Prefix) netip.Addr {
+ b := p.Masked().Addr().AsSlice()
+ for i := p.Bits(); i < len(b)*8; i++ {
+ b[i/8] |= 0x80 >> (i % 8)
+ }
+ a, _ := netip.AddrFromSlice(b)
+ return a
+}
+
+// AllowedPrefix agrees with Allowed at the edges: an accepted prefix's
+// first and last addresses would each be accepted alone.
+func TestAcceptedPrefixEdgesPassAllowed(t *testing.T) {
+ for in := range acceptedEntries {
+ p, err := ParseEntry(in)
+ if err != nil {
+ t.Fatalf("ParseEntry(%q): %v", in, err)
+ }
+ if err := AllowedPrefix(p); err != nil {
+ t.Fatalf("AllowedPrefix(%s): %v", p, err)
+ }
+ for _, a := range []netip.Addr{p.Addr(), lastAddr(p)} {
+ if err := Allowed(a); err != nil {
+ t.Errorf("%s is accepted but its edge %s is not: %v", p, a, err)
+ }
+ }
+ }
+}