fix(sandbox): close IPv4-mapped and unspecified ranges in the Kubernetes scope

From a second review of the backend:
- Scope CIDRs written as IPv4-mapped IPv6 (::ffff:10.0.0.0/104) are read
  as the IPv4 range they map, so the private-range filter and the
  never-reachable excepts apply to them. netip treats the two families as
  distinct, so such an entry skipped both.
- The never-reachable list adds 0.0.0.0/8 and :: (they reach the proxy's
  own host), ::ffff:0:0/96 and link-local through NAT64
  (64:ff9b::a9fe:0/112).
- ARTEX_KUBE_CLUSTER_CIDRS and ARTEX_KUBE_PROXY_DENIED_CIDRS refuse /0
  (an except equal to its block is rejected by the API server) and
  IPv4-mapped prefixes (they would never match).
This commit is contained in:
Marco Azarmaneche 2026-12-03 03:16:02 +02:00
parent 1c262d3af6
commit d92a02faef
2 changed files with 27 additions and 6 deletions

View file

@ -137,8 +137,10 @@ var DefaultProxyDeniedCIDRs = []string{
}
// kubeNeverCIDRs are never reachable through a task's proxy, whatever its scope.
var kubeNeverCIDRs = []string{"127.0.0.0/8", "169.254.0.0/16", "168.63.129.16/32", "100.100.100.200/32",
"::1/128", "fe80::/10", "fd00:ec2::254/128"}
// It includes the unspecified addresses (0.0.0.0/8, ::), which reach the proxy's
// own host, IPv4-mapped IPv6, and link-local reached through NAT64.
var kubeNeverCIDRs = []string{"0.0.0.0/8", "127.0.0.0/8", "169.254.0.0/16", "168.63.129.16/32", "100.100.100.200/32",
"::/128", "::1/128", "::ffff:0:0/96", "fe80::/10", "fd00:ec2::254/128", "64:ff9b::a9fe:0/112"}
// kubeProbePublicAddr is dialled from the worker to prove it has no direct egress.
const kubeProbePublicAddr = "1.1.1.1/443"
@ -214,7 +216,7 @@ func NewKubernetesBackend(cfg KubeConfig) (*KubernetesBackend, error) {
}
never := mustParsePrefixes(kubeNeverCIDRs)
for _, c := range cfg.ClusterCIDRs {
p, err := netip.ParsePrefix(strings.TrimSpace(c))
p, err := configPrefix(c)
if err != nil {
return nil, fmt.Errorf("kubernetes backend: cluster CIDR %q: %v", c, err)
}
@ -233,7 +235,7 @@ func NewKubernetesBackend(cfg KubeConfig) (*KubernetesBackend, error) {
// IPv4 never-reachable ranges, skipping any already covered.
extra := slices.Clone(never)
for _, c := range cfg.ProxyDeniedCIDRs {
p, err := netip.ParsePrefix(strings.TrimSpace(c))
p, err := configPrefix(c)
if err != nil || !p.Addr().Is4() {
return nil, fmt.Errorf("kubernetes backend: denied CIDR %q is not an IPv4 CIDR", c)
}
@ -733,11 +735,29 @@ scope:
}
}
// configPrefix parses a configured CIDR. A /0 would make an except equal to its
// block, which the API server rejects; an IPv4-mapped prefix would never match.
func configPrefix(c string) (netip.Prefix, error) {
p, err := netip.ParsePrefix(strings.TrimSpace(c))
if err != nil {
return p, err
}
if p.Bits() == 0 || p.Addr().Is4In6() {
return p, fmt.Errorf("must not be /0 or IPv4-mapped")
}
return p.Masked(), nil
}
// scopePrefixes parses scope CIDRs and IPs (already checked by ValidateExecRequest).
// An IPv4-mapped prefix is read as the IPv4 range it maps, so the private-range
// filter and the never-reachable excepts apply to it.
func scopePrefixes(cidrs []string) []netip.Prefix {
var out []netip.Prefix
for _, c := range cidrs {
if p, err := netip.ParsePrefix(c); err == nil {
if p.Addr().Is4In6() && p.Bits() >= 96 {
p = netip.PrefixFrom(p.Addr().Unmap(), p.Bits()-96)
}
out = append(out, p.Masked())
} else if a, err := netip.ParseAddr(c); err == nil {
out = append(out, netip.PrefixFrom(a.Unmap(), a.Unmap().BitLen()))

View file

@ -178,7 +178,7 @@ func TestKubeScopeCannotOpenClusterRanges(t *testing.T) {
// Without ClusterCIDRs a private scope could cover pods, services or nodes:
// such entries are dropped, public ones kept.
req := execReq("true")
req.ScopeCIDRs = []string{"10.0.0.0/8", "0.0.0.0/0", "203.0.113.0/24", "172.20.1.1", "fd00::/8"}
req.ScopeCIDRs = []string{"10.0.0.0/8", "0.0.0.0/0", "203.0.113.0/24", "172.20.1.1", "fd00::/8", "::ffff:10.0.0.0/104"}
if _, err := b.Exec(context.Background(), "t1", req); err != nil {
t.Fatal(err)
}
@ -206,7 +206,8 @@ func TestKubeScopeCannotOpenClusterRanges(t *testing.T) {
t.Fatalf("scope blocks = %v", blocks)
}
never := envOf(dig(f.obj("pods", "artex-p-"+h), "spec", "containers", 0))["ARTEX_NEVER_CIDRS"]
for _, want := range []string{"169.254.0.0/16", "10.42.0.0/16", "10.43.0.0/16", "192.0.2.1/32", "fd00:ec2::254/128"} {
for _, want := range []string{"169.254.0.0/16", "10.42.0.0/16", "10.43.0.0/16", "192.0.2.1/32", "fd00:ec2::254/128",
"0.0.0.0/8", "::/128", "::ffff:0.0.0.0/96", "64:ff9b::a9fe:0/112"} {
if !strings.Contains(never, want) {
t.Errorf("ARTEX_NEVER_CIDRS %q lacks %s", never, want)
}