@@ -4,6 +4,7 @@ package jail
4
4
5
5
import (
6
6
"fmt"
7
+ "log"
7
8
"log/slog"
8
9
"os"
9
10
"os/exec"
@@ -171,6 +172,22 @@ func (r *commandRunner) run() error {
171
172
return nil
172
173
}
173
174
175
+ func (r * commandRunner ) runIgnoreErrors () error {
176
+ for _ , command := range r .commands {
177
+ command .cmd .SysProcAttr = & syscall.SysProcAttr {
178
+ AmbientCaps : command .ambientCaps ,
179
+ }
180
+
181
+ output , err := command .cmd .CombinedOutput ()
182
+ if err != nil {
183
+ log .Printf ("failed to %s: %v, output: %s" , command .description , err , output )
184
+ continue
185
+ }
186
+ }
187
+
188
+ return nil
189
+ }
190
+
174
191
// configureHostNetworkBeforeCmdExec prepares host-side networking before the target
175
192
// process is started. At this point the target process is not running, so its PID and network
176
193
// namespace ID are not yet known.
@@ -283,7 +300,6 @@ func (l *LinuxJail) configureIptables() error {
283
300
exec .Command ("iptables" , "-t" , "nat" , "-A" , "PREROUTING" , "-i" , l .vethHostName , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-ports" , fmt .Sprintf ("%d" , l .httpProxyPort )),
284
301
[]uintptr {uintptr (unix .CAP_NET_ADMIN )},
285
302
},
286
- // TODO: clean up this rules
287
303
{
288
304
"iptables FORWARD -s" ,
289
305
exec .Command ("iptables" , "-A" , "FORWARD" , "-s" , "192.168.100.0/24" , "-j" , "ACCEPT" ),
@@ -314,12 +330,15 @@ func (l *LinuxJail) cleanupNetworking() error {
314
330
description string
315
331
command * exec.Cmd
316
332
}{
317
- {"delete veth pair" , exec .Command ("ip" , "link" , "del" , vethHost )},
333
+ {
334
+ "delete veth pair" ,
335
+ exec .Command ("ip" , "link" , "del" , vethHost ),
336
+ },
318
337
}
319
338
320
339
for _ , command := range cleanupCmds {
321
340
if err := command .command .Run (); err != nil {
322
- return fmt . Errorf ("failed to %s: %v " , command .description , err )
341
+ l . logger . Error ("failed to execute command " , " command" , command .description , "error" , err )
323
342
}
324
343
}
325
344
@@ -328,20 +347,30 @@ func (l *LinuxJail) cleanupNetworking() error {
328
347
329
348
// cleanupIptables removes iptables rules
330
349
func (l * LinuxJail ) cleanupIptables () error {
331
- // Remove comprehensive TCP redirect rule
332
- cmd := exec .Command ("iptables" , "-t" , "nat" , "-D" , "PREROUTING" , "-i" , l .vethHostName , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-ports" , fmt .Sprintf ("%d" , l .httpProxyPort ))
333
- err := cmd .Run ()
334
- if err != nil {
335
- l .logger .Error ("Failed to remove TCP redirect rule" , "error" , err )
336
- // Continue with other cleanup even if this fails
337
- }
338
-
339
- // Remove NAT rule
340
- cmd = exec .Command ("iptables" , "-t" , "nat" , "-D" , "POSTROUTING" , "-s" , "192.168.100.0/24" , "-j" , "MASQUERADE" )
341
- err = cmd .Run ()
342
- if err != nil {
343
- l .logger .Error ("Failed to remove NAT rule" , "error" , err )
344
- // Continue with other cleanup even if this fails
350
+ runner := newCommandRunner ([]* command {
351
+ {
352
+ "Remove comprehensive TCP redirect rule" ,
353
+ exec .Command ("iptables" , "-t" , "nat" , "-D" , "PREROUTING" , "-i" , l .vethHostName , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-ports" , fmt .Sprintf ("%d" , l .httpProxyPort )),
354
+ []uintptr {uintptr (unix .CAP_NET_ADMIN )},
355
+ },
356
+ {
357
+ "Remove NAT rule" ,
358
+ exec .Command ("iptables" , "-t" , "nat" , "-D" , "POSTROUTING" , "-s" , "192.168.100.0/24" , "-j" , "MASQUERADE" ),
359
+ []uintptr {uintptr (unix .CAP_NET_ADMIN )},
360
+ },
361
+ {
362
+ "Remove iptables FORWARD -s" ,
363
+ exec .Command ("iptables" , "-D" , "FORWARD" , "-s" , "192.168.100.0/24" , "-j" , "ACCEPT" ),
364
+ []uintptr {uintptr (unix .CAP_NET_ADMIN )},
365
+ },
366
+ {
367
+ "Remove iptables FORWARD -d" ,
368
+ exec .Command ("iptables" , "-D" , "FORWARD" , "-d" , "192.168.100.0/24" , "-j" , "ACCEPT" ),
369
+ []uintptr {uintptr (unix .CAP_NET_ADMIN )},
370
+ },
371
+ })
372
+ if err := runner .runIgnoreErrors (); err != nil {
373
+ return err
345
374
}
346
375
347
376
return nil
0 commit comments