Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(egress): atomically replace gateway DNS rules

leo pein926983fdb1ae
diff --git a/components/egress/pkg/iptables/gateway_dns.go b/components/egress/pkg/iptables/gateway_dns.go
index e68da14c3..dac74d750 100644
--- a/components/egress/pkg/iptables/gateway_dns.go
+++ b/components/egress/pkg/iptables/gateway_dns.go
@@ -24,6 +24,7 @@ import (
"context"
"fmt"
"net/netip"
+ "sort"
"strings"
"github.com/alibaba/opensandbox/egress/pkg/log"
@@ -31,55 +32,72 @@ import (
const gatewayDNSNftTable = "opensandbox_gateway_dns"
-func gatewayDNSRedirectScript(gateway netip.Addr, port int) string {
+// gatewayDNSRedirectScript builds one transactional replacement of the
+// shared table. Empty gateways means remove the table; addresses are
+// normalized and deduplicated before rendering so mapped IPv4 values cannot
+// create a second spelling of the same gateway.
+func gatewayDNSRedirectScript(gateways []netip.Addr, port int) (string, error) {
+ if port <= 0 {
+ return "", fmt.Errorf("gateway DNS redirect: invalid port %d", port)
+ }
+ unique := make(map[netip.Addr]struct{}, len(gateways))
+ for _, gateway := range gateways {
+ if !gateway.IsValid() {
+ return "", fmt.Errorf("gateway DNS redirect: invalid gateway %s", gateway)
+ }
+ unique[gateway.Unmap()] = struct{}{}
+ }
+ normalized := make([]netip.Addr, 0, len(unique))
+ for gateway := range unique {
+ normalized = append(normalized, gateway)
+ }
+ sort.Slice(normalized, func(i, j int) bool { return normalized[i].Compare(normalized[j]) < 0 })
+
var b strings.Builder
+ fmt.Fprintf(&b, "delete table inet %s\n", gatewayDNSNftTable)
+ if len(normalized) == 0 {
+ return b.String(), nil
+ }
fmt.Fprintf(&b, "add table inet %s\n", gatewayDNSNftTable)
fmt.Fprintf(&b, "add chain inet %s gw { type nat hook prerouting priority dstnat; }\n", gatewayDNSNftTable)
- addr := gateway.String()
- udp := fmt.Sprintf("udp dport 53 redirect to :%d", port)
- tcp := fmt.Sprintf("tcp dport 53 redirect to :%d", port)
- if gateway.Is4() {
- fmt.Fprintf(&b, "add rule inet %s gw ip daddr %s %s\n", gatewayDNSNftTable, addr, udp)
- fmt.Fprintf(&b, "add rule inet %s gw ip daddr %s %s\n", gatewayDNSNftTable, addr, tcp)
- } else {
- fmt.Fprintf(&b, "add rule inet %s gw ip6 daddr %s %s\n", gatewayDNSNftTable, addr, udp)
- fmt.Fprintf(&b, "add rule inet %s gw ip6 daddr %s %s\n", gatewayDNSNftTable, addr, tcp)
+ for _, gateway := range normalized {
+ family, addr := "ip6", gateway.String()
+ if gateway.Is4() {
+ family = "ip"
+ }
+ fmt.Fprintf(&b, "add rule inet %s gw %s daddr %s udp dport 53 redirect to :%d\n", gatewayDNSNftTable, family, addr, port)
+ fmt.Fprintf(&b, "add rule inet %s gw %s daddr %s tcp dport 53 redirect to :%d\n", gatewayDNSNftTable, family, addr, port)
}
- return b.String()
+ return b.String(), nil
}
-// SetupGatewayDNSRedirect installs (idempotent per gateway) the prerouting
-// REDIRECT so DNS addressed to the sandbox gateway:53 reaches the local DNS
-// proxy. Installing for an already-present gateway is a no-op.
-func SetupGatewayDNSRedirect(gateway netip.Addr, port int) error {
- if !gateway.IsValid() || port <= 0 {
- return fmt.Errorf("gateway DNS redirect: invalid gateway %s or port %d", gateway, port)
- }
- r := defaultRedirectRunner()
- if out, err := r.runNft(context.Background(), "list table inet "+gatewayDNSNftTable); err == nil &&
- strings.Contains(string(out), gateway.String()) {
- return nil // already installed for this gateway
- }
- if _, err := r.runNft(context.Background(), gatewayDNSRedirectScript(gateway, port)); err != nil {
- return fmt.Errorf("gateway DNS redirect install: %w", err)
- }
- log.Infof("gateway DNS redirect installed (gateway %s -> :%d)", gateway, port)
- return nil
+// ReplaceGatewayDNSRedirects atomically replaces the shared prerouting
+// redirect table with exactly the supplied gateway set. Callers own the
+// subject-to-gateway map and must retain it for a retry when this returns an
+// error; an ambiguous command result does not prove the old table is live.
+func ReplaceGatewayDNSRedirects(gateways []netip.Addr, port int) error {
+ return replaceGatewayDNSRedirects(defaultRedirectRunner(), gateways, port)
}
-// RemoveGatewayDNSRedirect removes the whole gateway redirect table
-// (idempotent; a missing table is not an error). Callers must refcount per
-// gateway: several subjects may share one gateway.
-func RemoveGatewayDNSRedirect() error {
- script := fmt.Sprintf("delete table inet %s\n", gatewayDNSNftTable)
- r := defaultRedirectRunner()
- if _, err := r.runNft(context.Background(), script); err != nil {
- msg := strings.ToLower(err.Error())
- if strings.Contains(msg, "no such file") || strings.Contains(msg, "does not exist") {
+func replaceGatewayDNSRedirects(r redirectRunner, gateways []netip.Addr, port int) error {
+ script, err := gatewayDNSRedirectScript(gateways, port)
+ if err != nil {
+ return err
+ }
+ out, err := r.runNft(context.Background(), script)
+ if err != nil {
+ if !isNftMissingTableErrorFor(out, err, gatewayDNSNftTable) {
+ return fmt.Errorf("gateway DNS redirect replace: %w (output: %s)", err, strings.TrimSpace(string(out)))
+ }
+ if len(gateways) == 0 {
return nil
}
- return fmt.Errorf("gateway DNS redirect remove: %w", err)
+ retryScript := removeNftDeleteTableLineFor(script, gatewayDNSNftTable)
+ retryOut, retryErr := r.runNft(context.Background(), retryScript)
+ if retryErr != nil {
+ return fmt.Errorf("gateway DNS redirect replace retry: %w (output: %s)", retryErr, strings.TrimSpace(string(retryOut)))
+ }
}
- log.Infof("gateway DNS redirect removed")
+ log.Infof("gateway DNS redirects replaced (%d gateway input(s) -> :%d)", len(gateways), port)
return nil
}