|
| 1 | +package squeeze |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "syscall" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + CLONE_NEWNET = 0x40000000 // Network namespace |
| 11 | + CLONE_NEWNS = 0x00020000 // Mount namespace |
| 12 | + CLONE_NEWUSER = 0x10000000 // User namespace |
| 13 | +) |
| 14 | + |
| 15 | +// IsolationConfig holds the configuration for running a process in isolated namespaces |
| 16 | +type IsolationConfig struct { |
| 17 | + ProxyAddr string // Address where the transparent HTTP proxy will listen |
| 18 | + AllowedPaths []string // Filesystem paths that will be visible in the mount namespace |
| 19 | + Command []string // Command and arguments to execute in isolation |
| 20 | + WorkingDir string // Working directory for the isolated process |
| 21 | +} |
| 22 | + |
| 23 | +// Option is a functional option for configuring IsolationConfig |
| 24 | +type Option func(*IsolationConfig) |
| 25 | + |
| 26 | +// WithProxy sets the address where the transparent HTTP proxy will listen. |
| 27 | +// All network traffic from the isolated process will be routed through this proxy. |
| 28 | +func WithProxy(addr string) Option { |
| 29 | + return func(c *IsolationConfig) { |
| 30 | + c.ProxyAddr = addr |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// WithAllowedPath adds a filesystem path that will be visible in the mount namespace. |
| 35 | +// This can be called multiple times to allow access to multiple paths. |
| 36 | +func WithAllowedPath(path string) Option { |
| 37 | + return func(c *IsolationConfig) { |
| 38 | + c.AllowedPaths = append(c.AllowedPaths, path) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// WithCommand sets the command and arguments to execute in the isolated environment. |
| 43 | +func WithCommand(cmd string, args ...string) Option { |
| 44 | + return func(c *IsolationConfig) { |
| 45 | + c.Command = append([]string{cmd}, args...) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// WithWorkingDir sets the working directory for the isolated process. |
| 50 | +func WithWorkingDir(dir string) Option { |
| 51 | + return func(c *IsolationConfig) { |
| 52 | + c.WorkingDir = dir |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// NewConfig creates a new IsolationConfig with the given options applied. |
| 57 | +// It returns a configuration with sensible defaults that can be customized |
| 58 | +// using the provided functional options. |
| 59 | +func NewConfig(options ...Option) *IsolationConfig { |
| 60 | + config := &IsolationConfig{ |
| 61 | + ProxyAddr: "127.0.0.1:0", // Let OS choose port |
| 62 | + WorkingDir: "/tmp", // Safe default working directory |
| 63 | + } |
| 64 | + |
| 65 | + for _, option := range options { |
| 66 | + option(config) |
| 67 | + } |
| 68 | + |
| 69 | + return config |
| 70 | +} |
| 71 | + |
| 72 | +// RunIsolated executes the configured command in isolated namespaces. |
| 73 | +// The parent process remains in the original namespaces while the child |
| 74 | +// runs in isolation with network, mount, and user namespace separation. |
| 75 | +func (c *IsolationConfig) RunIsolated() error { |
| 76 | + if len(c.Command) == 0 { |
| 77 | + return fmt.Errorf("no command specified") |
| 78 | + } |
| 79 | + |
| 80 | + // Fork a child process |
| 81 | + pid, err := syscall.ForkExec( |
| 82 | + "/proc/self/exe", // Re-execute ourselves |
| 83 | + []string{"squeeze-child"}, // Special arg to indicate child mode |
| 84 | + &syscall.ProcAttr{ |
| 85 | + Dir: c.WorkingDir, |
| 86 | + Env: os.Environ(), |
| 87 | + Files: []uintptr{0, 1, 2}, // stdin, stdout, stderr |
| 88 | + }, |
| 89 | + ) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("failed to fork child process: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + // Parent: wait for child to complete |
| 95 | + var status syscall.WaitStatus |
| 96 | + _, err = syscall.Wait4(pid, &status, 0, nil) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("failed to wait for child: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + if !status.Exited() || status.ExitStatus() != 0 { |
| 102 | + return fmt.Errorf("child process failed with status: %d", status.ExitStatus()) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
0 commit comments