Skip to content

Commit 3e27b75

Browse files
Luap99TomSweeneyRedHat
authored andcommitted
[v0.60] rootlessnetns: fix cleanup for errors in Run
The Run() function is used to run long running command in the netns, namly podman unshare --rootless-netns used that. As such the function actually unlocks for the main command as otherwise a user could hold the lock forever effectively causing deadlocks. Now because we unlock the ref count might change during that time. And just because we create the netns doesn't mean there are now other users of the netns. Therefore the cleanup in runInner() was wrong in that case causing problems for other running containers. To fix this make sure we do not cleanup in the Run() case unless the count is 0. Signed-off-by: Paul Holzinger <[email protected]> Signed-off-by: tomsweeneyredhat <[email protected]>
1 parent 65eb22b commit 3e27b75

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Diff for: libnetwork/internal/rootlessnetns/netns_linux.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,14 @@ func (n *Netns) mountCNIVarDir() error {
512512
return nil
513513
}
514514

515-
func (n *Netns) runInner(toRun func() error) (err error) {
515+
func (n *Netns) runInner(toRun func() error, cleanup bool) (err error) {
516516
nsRef, newNs, err := n.getOrCreateNetns()
517517
if err != nil {
518518
return err
519519
}
520520
defer nsRef.Close()
521-
// If a new netns was created make sure to clean it up again on an error to not leak it.
522-
if newNs {
521+
// If a new netns was created make sure to clean it up again on an error to not leak it if requested.
522+
if newNs && cleanup {
523523
defer func() {
524524
if err != nil {
525525
if err := n.cleanup(); err != nil {
@@ -555,7 +555,7 @@ func (n *Netns) runInner(toRun func() error) (err error) {
555555
}
556556

557557
func (n *Netns) Setup(nets int, toRun func() error) error {
558-
err := n.runInner(toRun)
558+
err := n.runInner(toRun, true)
559559
if err != nil {
560560
return err
561561
}
@@ -564,7 +564,7 @@ func (n *Netns) Setup(nets int, toRun func() error) error {
564564
}
565565

566566
func (n *Netns) Teardown(nets int, toRun func() error) error {
567-
err := n.runInner(toRun)
567+
err := n.runInner(toRun, true)
568568
if err != nil {
569569
return err
570570
}
@@ -601,7 +601,7 @@ func (n *Netns) Run(lock *lockfile.LockFile, toRun func() error) error {
601601
return err
602602
}
603603

604-
inErr := n.runInner(inner)
604+
inErr := n.runInner(inner, false)
605605
// make sure to always reset the ref counter afterwards
606606
count, err := refCount(n.dir, -1)
607607
if err != nil {
@@ -611,9 +611,8 @@ func (n *Netns) Run(lock *lockfile.LockFile, toRun func() error) error {
611611
logrus.Errorf("Failed to decrement ref count: %v", err)
612612
return inErr
613613
}
614-
// runInner() already cleans up the netns when it created a new one on errors
615-
// so we only need to do that if there was no error.
616-
if inErr == nil && count == 0 {
614+
615+
if count == 0 {
617616
err = n.cleanup()
618617
if err != nil {
619618
return wrapError("cleanup", err)

0 commit comments

Comments
 (0)