Skip to content

Commit

Permalink
net: Shorten the path used by the client endpoint
Browse files Browse the repository at this point in the history
For communication over unixgram, both the server and the client
need an endpoint. They are filesystem paths which must be smaller than
104 bytes. This commit attempts to make them shorter while keeping them
unique and non-guessable.

Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau committed Sep 23, 2024
1 parent 8cdea09 commit 5e826b5
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/vf/virtionet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vf

import (
"fmt"
"math/rand"
"net"
"os"
"os/signal"
Expand All @@ -20,7 +21,18 @@ type VirtioNet struct {
}

func localUnixSocketPath(dir string) (string, error) {
tmpFile, err := os.CreateTemp(dir, fmt.Sprintf("vfkit-%d-*.sock", os.Getpid()))
// unix socket endpoints are filesystem paths, but their max length is
// quite small (a bit over 100 bytes).
// In this function we try to build a filename which is relatively
// unique, not easily guessable (to prevent hostile collisions), and
// short (`os.CreateTemp` filenames are a bit too long)
//
// os.Getpid() is unique but guessable. We append a short 16 bit random
// number to it. We only use hex values to make the representation more
// compact
filename := filepath.Join(dir, fmt.Sprintf("vfkit-%x-%x.sock", os.Getpid(), rand.Int31n(0xffff))) //#nosec G404 -- no need for crypto/rand here

tmpFile, err := os.OpenFile(filename, os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 5e826b5

Please sign in to comment.