Browse code

builder: ensure libnetwork state file do not leak

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit 7c7e1689021afdd3775eecdd1e60ad8c0cc44678)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Tonis Tiigi authored on 2021/01/07 15:46:53
Showing 1 changed files
... ...
@@ -3,6 +3,7 @@
3 3
 package buildkit
4 4
 
5 5
 import (
6
+	"io/ioutil"
6 7
 	"os"
7 8
 	"path/filepath"
8 9
 	"strconv"
... ...
@@ -25,11 +26,24 @@ import (
25 25
 const networkName = "bridge"
26 26
 
27 27
 func newExecutor(root, cgroupParent string, net libnetwork.NetworkController, dnsConfig *oci.DNSConfig, rootless bool, idmap *idtools.IdentityMapping, apparmorProfile string) (executor.Executor, error) {
28
+	netRoot := filepath.Join(root, "net")
28 29
 	networkProviders := map[pb.NetMode]network.Provider{
29
-		pb.NetMode_UNSET: &bridgeProvider{NetworkController: net, Root: filepath.Join(root, "net")},
30
+		pb.NetMode_UNSET: &bridgeProvider{NetworkController: net, Root: netRoot},
30 31
 		pb.NetMode_HOST:  network.NewHostProvider(),
31 32
 		pb.NetMode_NONE:  network.NewNoneProvider(),
32 33
 	}
34
+
35
+	// make sure net state directory is cleared from previous state
36
+	fis, err := ioutil.ReadDir(netRoot)
37
+	if err == nil {
38
+		for _, fi := range fis {
39
+			fp := filepath.Join(netRoot, fi.Name())
40
+			if err := os.RemoveAll(fp); err != nil {
41
+				logrus.WithError(err).Errorf("failed to delete old network state: %v", fp)
42
+			}
43
+		}
44
+	}
45
+
33 46
 	return runcexecutor.New(runcexecutor.Opt{
34 47
 		Root:                filepath.Join(root, "executor"),
35 48
 		CommandCandidates:   []string{"runc"},
... ...
@@ -118,7 +132,10 @@ func (iface *lnInterface) Close() error {
118 118
 	if iface.sbx != nil {
119 119
 		go func() {
120 120
 			if err := iface.sbx.Delete(); err != nil {
121
-				logrus.Errorf("failed to delete builder network sandbox: %v", err)
121
+				logrus.WithError(err).Errorf("failed to delete builder network sandbox")
122
+			}
123
+			if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
124
+				logrus.WithError(err).Errorf("failed to delete builder sandbox directory")
122 125
 			}
123 126
 		}()
124 127
 	}