|
4 | 4 | "fmt"
|
5 | 5 | "os"
|
6 | 6 | "syscall"
|
| 7 | + |
| 8 | + "golang.org/x/sys/unix" |
7 | 9 | )
|
8 | 10 |
|
9 | 11 | const (
|
@@ -104,3 +106,33 @@ func (c *IsolationConfig) RunIsolated() error {
|
104 | 106 |
|
105 | 107 | return nil
|
106 | 108 | }
|
| 109 | + |
| 110 | +// CreateNamespaces creates new user, mount, and network namespaces for the current process. |
| 111 | +// This isolates the process from the host system's users, filesystem, and network. |
| 112 | +// Must be called in the child process after fork. |
| 113 | +func CreateNamespaces() error { |
| 114 | + // Create user namespace first - this allows us to have root privileges |
| 115 | + // inside the namespace for subsequent mount/network operations |
| 116 | + if err := unshare(CLONE_NEWUSER); err != nil { |
| 117 | + return fmt.Errorf("failed to create user namespace: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + // Create mount namespace - gives us our own view of the filesystem |
| 121 | + if err := unshare(CLONE_NEWNS); err != nil { |
| 122 | + return fmt.Errorf("failed to create mount namespace: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + // Create network namespace - isolates network interfaces and routing |
| 126 | + if err := unshare(CLONE_NEWNET); err != nil { |
| 127 | + return fmt.Errorf("failed to create network namespace: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +// unshare is a wrapper around the unshare system call |
| 134 | +func unshare(flags int) error { |
| 135 | + // On non-Linux systems, return an error indicating it's not supported |
| 136 | + // On Linux, this will call the actual unshare syscall |
| 137 | + return fmt.Errorf("namespace isolation not supported on this platform") |
| 138 | +} |
0 commit comments