Skip to content

Commit c94cb00

Browse files
authored
Merge pull request #6814 from mikebrow/cherrypick-#6806-release-1.5
[release/1.5] check for duplicate nspath possibilities
2 parents cadce9e + 283058c commit c94cb00

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

pkg/netns/netns_linux.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ import (
5353
// path to the network namespace.
5454
func newNS(baseDir string) (nsPath string, err error) {
5555
b := make([]byte, 16)
56-
if _, err := rand.Reader.Read(b); err != nil {
57-
return "", errors.Wrap(err, "failed to generate random netns name")
56+
57+
_, err = rand.Read(b)
58+
if err != nil {
59+
return "", fmt.Errorf("failed to generate random netns name: %w", err)
5860
}
5961

6062
// Create the directory for mounting network namespaces
@@ -64,10 +66,10 @@ func newNS(baseDir string) (nsPath string, err error) {
6466
return "", err
6567
}
6668

67-
// create an empty file at the mount point
69+
// create an empty file at the mount point and fail if it already exists
6870
nsName := fmt.Sprintf("cni-%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
6971
nsPath = path.Join(baseDir, nsName)
70-
mountPointFd, err := os.Create(nsPath)
72+
mountPointFd, err := os.OpenFile(nsPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
7173
if err != nil {
7274
return "", err
7375
}

0 commit comments

Comments
 (0)