//go:build linux package publicguard import ( "bytes" "context" "errors" "fmt" "os" "os/exec" "path/filepath" "strconv" ) type linuxDriver struct { nftPath string run commandRunner } type commandRunner interface { Run(context.Context, string, []string, []byte) ([]byte, error) } type execRunner struct{} const publicGuardNFTOutputLimit = 2 << 20 type boundedOutput struct { buffer bytes.Buffer overflow bool } func (output *boundedOutput) Write(value []byte) (int, error) { remaining := publicGuardNFTOutputLimit - output.buffer.Len() if remaining > 0 { length := len(value) if length > remaining { length = remaining } _, _ = output.buffer.Write(value[:length]) } if len(value) > remaining { output.overflow = true } return len(value), nil } func (execRunner) Run(ctx context.Context, path string, arguments []string, input []byte) ([]byte, error) { command := exec.CommandContext(ctx, path, arguments...) command.Env = []string{"LC_ALL=C"} command.Stdin = bytes.NewReader(input) var output boundedOutput command.Stdout = &output command.Stderr = &output runErr := command.Run() if output.overflow { return nil, errors.New("nft output exceeded limit") } if runErr != nil { return nil, fmt.Errorf("%s failed: %w", filepath.Base(path), runErr) } return append([]byte(nil), output.buffer.Bytes()...), nil } func newPlatformDriver() hostDriver { return &linuxDriver{ nftPath: trustedBinary([]string{"/usr/sbin/nft", "/usr/bin/nft", "/sbin/nft", "/bin/nft"}), run: execRunner{}, } } func trustedBinary(candidates []string) string { for _, candidate := range candidates { info, err := os.Stat(candidate) if err == nil && info.Mode().IsRegular() && info.Mode()&0o111 != 0 { return candidate } } return "" } func (d *linuxDriver) Verify(ctx context.Context, bindings []Binding) error { if d == nil || d.run == nil || d.nftPath == "" { return errors.New("trusted nft executable is unavailable") } tables, err := d.run.Run(ctx, d.nftPath, []string{"-j", "list", "tables"}, nil) if err != nil { return fmt.Errorf("inspect nft tables: %w", err) } if !nftTableExists(tables) { return errors.New("public guard nft table is missing") } current, err := d.run.Run(ctx, d.nftPath, []string{"-j", "list", "table", nftFamily, nftTable}, nil) if err != nil { return fmt.Errorf("inspect public guard table: %w", err) } if !nftTableOwned(current) { return errors.New("reserved public guard nft table is foreign") } if !nftProjectionCurrent(current, bindings) { return errors.New("public guard nft projection is stale") } return nil } func (d *linuxDriver) Reconcile(ctx context.Context, bindings []Binding) error { if d == nil || d.run == nil || d.nftPath == "" { return errors.New("trusted nft executable is unavailable") } probeTable := "virt_cloud_pg_probe_" + strconv.Itoa(os.Getpid()) probe := "add table bridge " + probeTable + "\n" + "add chain bridge " + probeTable + " ingress { type filter hook prerouting priority -320; policy accept; }\n" + "add rule bridge " + probeTable + " ingress ether type ip6 icmpv6 type nd-neighbor-advert icmpv6 taddr != 2001:db8::1 icmpv6 taddr != fe80::1 drop\n" if _, err := d.run.Run(ctx, d.nftPath, []string{"--check", "-f", "-"}, []byte(probe)); err != nil { return fmt.Errorf("public guard nft syntax is unavailable: %w", err) } tables, err := d.run.Run(ctx, d.nftPath, []string{"-j", "list", "tables"}, nil) if err != nil { return fmt.Errorf("inspect nft tables: %w", err) } exists := nftTableExists(tables) if exists { current, inspectErr := d.run.Run(ctx, d.nftPath, []string{"-j", "list", "table", nftFamily, nftTable}, nil) if inspectErr != nil { return fmt.Errorf("inspect public guard table: %w", inspectErr) } if !nftTableOwned(current) { return errors.New("reserved public guard nft table is foreign") } } script := []byte(nftScript(bindings, exists)) if _, err := d.run.Run(ctx, d.nftPath, []string{"--check", "-f", "-"}, script); err != nil { return fmt.Errorf("validate public guard table replacement: %w", err) } if _, err := d.run.Run(ctx, d.nftPath, []string{"-f", "-"}, script); err != nil { return fmt.Errorf("apply public guard table: %w", err) } current, err := d.run.Run(ctx, d.nftPath, []string{"-j", "list", "table", nftFamily, nftTable}, nil) if err != nil { return fmt.Errorf("verify public guard table: %w", err) } if !nftTableOwned(current) || !nftProjectionCurrent(current, bindings) { return errors.New("verify public guard table: applied projection is incomplete") } return nil }