Skip to content

Commit 384e0da

Browse files
use random name for network interface
1 parent bff1cd7 commit 384e0da

File tree

6 files changed

+70
-28
lines changed

6 files changed

+70
-28
lines changed

boundary.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (b *Boundary) ConfigureAfterRun(processPID int) {
8282
b.jailer.ConfigureAfterRun(processPID)
8383
}
8484

85+
func (b *Boundary) GetNetworkConfiguration() jail.NetworkConfiguration {
86+
return b.jailer.GetNetworkConfiguration()
87+
}
88+
8589
func (b *Boundary) Close() error {
8690
// Stop proxy server
8791
if b.proxyServer != nil {

cli/cli.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func Run(ctx context.Context, config Config, args []string) error {
102102
//fmt.Printf("%v\n", os.Environ())
103103
time.Sleep(time.Second * 3) // wait for parent to configure env
104104

105-
// TODO: uncomment
106-
vethNetJail := "veth_n_1111111"
105+
vethNetJail := os.Getenv("VETH_NET_JAIL")
106+
107107
err := jail.SetupChildNetworking(vethNetJail)
108108
if err != nil {
109109
fmt.Fprintf(os.Stderr, "failed setupChildNetworking: %v\n", err)
@@ -224,6 +224,7 @@ func Run(ctx context.Context, config Config, args []string) error {
224224
defer cancel()
225225
cmd := boundaryInstance.Command(os.Args)
226226
cmd.Env = append(cmd.Env, "CHILD=true")
227+
cmd.Env = append(cmd.Env, fmt.Sprintf("VETH_NET_JAIL=%v", boundaryInstance.GetNetworkConfiguration().VethNetJail))
227228
cmd.Stderr = os.Stderr
228229
cmd.Stdout = os.Stdout
229230
cmd.Stdin = os.Stdin

jail/jail.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ type Jailer interface {
1212
Command(command []string) *exec.Cmd
1313
ConfigureAfterRun(processPID int)
1414
Close() error
15+
GetNetworkConfiguration() NetworkConfiguration
16+
}
17+
18+
type NetworkConfiguration struct {
19+
VethNetJail string
1520
}
1621

1722
type Config struct {

jail/linux.go

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type LinuxJail struct {
1818
logger *slog.Logger
1919
namespace string
2020
vethHost string // Host-side veth interface name for iptables rules
21+
vethNetJail string // Host-side veth interface name for iptables rules
2122
commandEnv []string
2223
httpProxyPort int
2324
configDir string
@@ -49,6 +50,48 @@ func (l *LinuxJail) Start() error {
4950
e := getEnvs(l.configDir, l.caCertPath)
5051
l.commandEnv = mergeEnvs(e, map[string]string{})
5152

53+
// Create veth pair with short names (Linux interface names limited to 15 chars)
54+
// Generate unique ID to avoid conflicts
55+
uniqueID := fmt.Sprintf("%d", time.Now().UnixNano()%10000000) // 7 digits max
56+
vethHost := fmt.Sprintf("veth_h_%s", uniqueID) // veth_h_1234567 = 14 chars
57+
vethNetJail := fmt.Sprintf("veth_n_%s", uniqueID) // veth_n_1234567 = 14 chars
58+
59+
// Store veth interface name for iptables rules
60+
l.vethHost = vethHost
61+
l.vethNetJail = vethNetJail
62+
63+
setupCmds := []struct {
64+
description string
65+
command *exec.Cmd
66+
ambientCaps []uintptr
67+
}{
68+
{
69+
"create veth pair",
70+
exec.Command("ip", "link", "add", vethHost, "type", "veth", "peer", "name", vethNetJail),
71+
[]uintptr{uintptr(unix.CAP_NET_ADMIN)},
72+
},
73+
{
74+
"configure host veth",
75+
exec.Command("ip", "addr", "add", "192.168.100.1/24", "dev", vethHost),
76+
[]uintptr{uintptr(unix.CAP_NET_ADMIN)},
77+
},
78+
{
79+
"bring up host veth",
80+
exec.Command("ip", "link", "set", vethHost, "up"),
81+
[]uintptr{uintptr(unix.CAP_NET_ADMIN)},
82+
},
83+
}
84+
85+
for _, command := range setupCmds {
86+
command.command.SysProcAttr = &syscall.SysProcAttr{
87+
AmbientCaps: command.ambientCaps,
88+
}
89+
90+
if err := command.command.Run(); err != nil {
91+
return fmt.Errorf("failed to %s: %v", command.description, err)
92+
}
93+
}
94+
5295
return nil
5396
}
5497

@@ -91,6 +134,12 @@ func (l *LinuxJail) ConfigureAfterRun(pidInt int) {
91134
}
92135
}
93136

137+
func (l *LinuxJail) GetNetworkConfiguration() NetworkConfiguration {
138+
return NetworkConfiguration{
139+
VethNetJail: l.vethNetJail,
140+
}
141+
}
142+
94143
// Close removes the network namespace and iptables rules
95144
func (l *LinuxJail) Close() error {
96145
l.logger.Debug("Close called")
@@ -140,39 +189,14 @@ func (l *LinuxJail) createNamespace() error {
140189
func (l *LinuxJail) setupParentNetworking(pidInt int) error {
141190
PID := fmt.Sprintf("%v", pidInt)
142191

143-
// Create veth pair with short names (Linux interface names limited to 15 chars)
144-
// Generate unique ID to avoid conflicts
145-
uniqueID := fmt.Sprintf("%d", time.Now().UnixNano()%10000000) // 7 digits max
146-
uniqueID = "1111111"
147-
vethHost := fmt.Sprintf("veth_h_%s", uniqueID) // veth_h_1234567 = 14 chars
148-
vethNetJail := fmt.Sprintf("veth_n_%s", uniqueID) // veth_n_1234567 = 14 chars
149-
150-
// Store veth interface name for iptables rules
151-
l.vethHost = vethHost
152-
153192
setupCmds := []struct {
154193
description string
155194
command *exec.Cmd
156195
ambientCaps []uintptr
157196
}{
158-
{
159-
"create veth pair",
160-
exec.Command("ip", "link", "add", vethHost, "type", "veth", "peer", "name", vethNetJail),
161-
[]uintptr{uintptr(unix.CAP_NET_ADMIN)},
162-
},
163197
{
164198
"move veth to namespace",
165-
exec.Command("ip", "link", "set", vethNetJail, "netns", PID),
166-
[]uintptr{uintptr(unix.CAP_NET_ADMIN)},
167-
},
168-
{
169-
"configure host veth",
170-
exec.Command("ip", "addr", "add", "192.168.100.1/24", "dev", vethHost),
171-
[]uintptr{uintptr(unix.CAP_NET_ADMIN)},
172-
},
173-
{
174-
"bring up host veth",
175-
exec.Command("ip", "link", "set", vethHost, "up"),
199+
exec.Command("ip", "link", "set", l.vethNetJail, "netns", PID),
176200
[]uintptr{uintptr(unix.CAP_NET_ADMIN)},
177201
},
178202
}

jail/macos.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,7 @@ func (n *MacOSJail) cleanupTempFiles() {
342342
}
343343

344344
func (u *MacOSJail) ConfigureAfterRun(processPID int) {}
345+
346+
func (l *MacOSJail) GetNetworkConfiguration() NetworkConfiguration {
347+
return NetworkConfiguration{}
348+
}

jail/unprivileged.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ func (u *Unprivileged) Close() error {
6262
}
6363

6464
func (u *Unprivileged) ConfigureAfterRun(processPID int) {}
65+
66+
func (l *Unprivileged) GetNetworkConfiguration() NetworkConfiguration {
67+
return NetworkConfiguration{}
68+
}

0 commit comments

Comments
 (0)