Skip to content

Commit c4eebde

Browse files
committed
in progress but need to switch to linux for dev
1 parent 9bf2f0f commit c4eebde

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/coder/squeeze
22

33
go 1.25.0
4+
5+
require golang.org/x/sys v0.35.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
2+
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=

main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,27 @@ import (
99
"github.com/coder/squeeze/squeeze"
1010
)
1111

12+
// runChildProcess handles the child process execution in isolated namespaces
13+
func runChildProcess() {
14+
// TODO: We need to pass config data from parent to child
15+
// For now, just create namespaces and exit to test
16+
17+
if err := squeeze.CreateNamespaces(); err != nil {
18+
fmt.Fprintf(os.Stderr, "Child: failed to create namespaces: %v\n", err)
19+
os.Exit(1)
20+
}
21+
22+
fmt.Printf("Child: successfully created namespaces\n")
23+
os.Exit(0)
24+
}
25+
1226
func main() {
27+
// Check if we're running as the child process for namespace setup
28+
if len(os.Args) > 1 && os.Args[1] == "squeeze-child" {
29+
runChildProcess()
30+
return
31+
}
32+
1333
var configFile = flag.String("config", "", "path to configuration file")
1434

1535
flag.Usage = func() {

squeeze/squeeze.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"os"
66
"syscall"
7+
8+
"golang.org/x/sys/unix"
79
)
810

911
const (
@@ -104,3 +106,33 @@ func (c *IsolationConfig) RunIsolated() error {
104106

105107
return nil
106108
}
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+
}

test_constants.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"golang.org/x/sys/unix"
6+
)
7+
8+
func main() {
9+
fmt.Printf("CLONE_NEWUSER: %x\n", unix.CLONE_NEWUSER)
10+
fmt.Printf("CLONE_NEWNS: %x\n", unix.CLONE_NEWNS)
11+
fmt.Printf("CLONE_NEWNET: %x\n", unix.CLONE_NEWNET)
12+
}

0 commit comments

Comments
 (0)