|
| 1 | +package sandbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + |
| 9 | + "github.com/ossf/package-analysis/internal/log" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + ipBin = "/usr/sbin/ip" |
| 14 | + iptablesLoadBin = "/usr/sbin/iptables-restore" |
| 15 | + iptablesRules = "/usr/local/etc/iptables.rules" |
| 16 | + dummyInterface = "cnidummy0" |
| 17 | + |
| 18 | + // bridgeInterface is the name of the podman bridge defined in |
| 19 | + // tools/network/podman-analysis.conflist. This bridge is used by the |
| 20 | + // sandbox during analysis to separate the sandbox traffic from the host. |
| 21 | + bridgeInterface = "cni-analysis" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + // NetworkInterface is the name of a network interface that has access to |
| 26 | + // the sandbox network traffic. |
| 27 | + NetworkInterface = bridgeInterface |
| 28 | +) |
| 29 | + |
| 30 | +func loadIptablesRules() error { |
| 31 | + log.Debug("Loading iptable rules") |
| 32 | + |
| 33 | + // Open the iptables-restore configuration |
| 34 | + f, err := os.Open(iptablesRules) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + defer f.Close() |
| 39 | + |
| 40 | + logOut := log.Writer(log.InfoLevel) |
| 41 | + defer logOut.Close() |
| 42 | + logErr := log.Writer(log.WarnLevel) |
| 43 | + defer logErr.Close() |
| 44 | + |
| 45 | + cmd := exec.Command(iptablesLoadBin) |
| 46 | + cmd.Stdout = logOut |
| 47 | + cmd.Stderr = logErr |
| 48 | + stdin, err := cmd.StdinPipe() |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + defer stdin.Close() |
| 53 | + if err := cmd.Start(); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + // Send the iptables rules to the command via stdin |
| 57 | + if _, err := io.Copy(stdin, f); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + stdin.Close() |
| 61 | + return cmd.Wait() |
| 62 | +} |
| 63 | + |
| 64 | +// createBridgeNetwork ensures that NetworkInterface and the bridge network |
| 65 | +// exists prior to the sandbox. |
| 66 | +// |
| 67 | +// podman would create this bridge interface anyway, but doing it early allows |
| 68 | +// a packet capture to be started on the interface prior to the sandbox |
| 69 | +// starting. |
| 70 | +func createBridgeNetwork() error { |
| 71 | + log.Debug("Creating bridge network") |
| 72 | + |
| 73 | + // Create the bridge |
| 74 | + cmd := exec.Command(ipBin, "link", "add", "name", bridgeInterface, "type", "bridge") |
| 75 | + if err := cmd.Run(); err != nil { |
| 76 | + // If the error is not an ExitError, or the Exit Code is not 2, then abort. |
| 77 | + if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 2 { |
| 78 | + return fmt.Errorf("failed to add bridge interface: %w", err) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Bring the bridge up. |
| 83 | + cmd = exec.Command(ipBin, "link", "set", bridgeInterface, "up") |
| 84 | + if err := cmd.Run(); err != nil { |
| 85 | + return fmt.Errorf("failed to bring up bridge interface: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + // Add a dummy device so the bridge stays up |
| 89 | + cmd = exec.Command(ipBin, "link", "add", "dev", dummyInterface, "type", "dummy") |
| 90 | + if err := cmd.Run(); err != nil { |
| 91 | + // If the error is not an ExitError, or the Exit Code is not 2, then abort. |
| 92 | + if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 2 { |
| 93 | + return fmt.Errorf("failed to create dummy inteface: %w", err) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Add the dummy device to the bridge network |
| 98 | + cmd = exec.Command(ipBin, "link", "set", "dev", dummyInterface, "master", bridgeInterface) |
| 99 | + if err := cmd.Run(); err != nil { |
| 100 | + return fmt.Errorf("failed to add dummy interface to bridge: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + // Bring the dummy device up. |
| 104 | + cmd = exec.Command(ipBin, "link", "set", dummyInterface, "up") |
| 105 | + if err := cmd.Run(); err != nil { |
| 106 | + return fmt.Errorf("failed to bring up dummy interface: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +// InitEnv initializes the host for running sandboxes. |
| 113 | +// |
| 114 | +// It will ensure that the network interface exists, and any firewall |
| 115 | +// rules are configured. |
| 116 | +// |
| 117 | +// This function is idempotent and is safe to be called more than once. |
| 118 | +// |
| 119 | +// This function must be called after logging is complete, and may exit if |
| 120 | +// any of the commands fail. |
| 121 | +func InitEnv() { |
| 122 | + // Create the bridge network |
| 123 | + if err := createBridgeNetwork(); err != nil { |
| 124 | + log.Fatal("Failed to create bridge network", "error", err) |
| 125 | + } |
| 126 | + // Load iptables rules to further isolate the sandbox |
| 127 | + if err := loadIptablesRules(); err != nil { |
| 128 | + log.Fatal("Failed restoring iptables rules", "error", err) |
| 129 | + } |
| 130 | +} |
0 commit comments