| 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 |
| } |