mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-24 01:24:26 +01:00
socket: Enforce socket length
A Unix domain socket is limited to 107 usable bytes on Linux. However, not all code creating socket paths was checking for this limits. Created a new `utils.BuildSocketPath()` function (with tests) to encapsulate the logic and updated all code creating sockets to use it. Fixes #268. Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
@@ -7,15 +7,22 @@ package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const cpBinaryName = "cp"
|
||||
|
||||
const fileMode0755 = os.FileMode(0755)
|
||||
|
||||
// MaxSocketPathLen is the effective maximum Unix domain socket length.
|
||||
//
|
||||
// See unix(7).
|
||||
const MaxSocketPathLen = 107
|
||||
|
||||
// FileCopy copys files from srcPath to dstPath
|
||||
func FileCopy(srcPath, dstPath string) error {
|
||||
if srcPath == "" {
|
||||
@@ -174,3 +181,22 @@ func MakeNameID(namedType, id string, maxLen int) string {
|
||||
|
||||
return nameID
|
||||
}
|
||||
|
||||
// BuildSocketPath concatenates the provided elements into a path and returns
|
||||
// it. If the resulting path is longer than the maximum permitted socket path
|
||||
// on Linux, it will return an error.
|
||||
func BuildSocketPath(elements ...string) (string, error) {
|
||||
result := filepath.Join(elements...)
|
||||
|
||||
if result == "" {
|
||||
return "", errors.New("empty path")
|
||||
}
|
||||
|
||||
l := len(result)
|
||||
|
||||
if l > MaxSocketPathLen {
|
||||
return "", fmt.Errorf("path too long (got %v, max %v): %s", l, MaxSocketPathLen, result)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user