forked from arm/remoteproc-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespaces.go
More file actions
54 lines (46 loc) · 1.25 KB
/
Copy pathnamespaces.go
File metadata and controls
54 lines (46 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package proxy
import (
"fmt"
"log/slog"
"github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
)
var specNamespacesToUnixCloneFlags = map[specs.LinuxNamespaceType]uintptr{
specs.CgroupNamespace: unix.CLONE_NEWCGROUP,
specs.IPCNamespace: unix.CLONE_NEWIPC,
specs.MountNamespace: unix.CLONE_NEWNS,
specs.NetworkNamespace: unix.CLONE_NEWNET,
specs.PIDNamespace: unix.CLONE_NEWPID,
specs.TimeNamespace: unix.CLONE_NEWTIME,
specs.UserNamespace: unix.CLONE_NEWUSER,
specs.UTSNamespace: unix.CLONE_NEWUTS,
}
func ParseNamespaceFlags(namespaces []specs.LinuxNamespace) (uintptr, error) {
if namespaces == nil {
return 0, nil
}
var flags uintptr
for _, ns := range namespaces {
if ns.Path != "" {
continue
}
flag, ok := specNamespacesToUnixCloneFlags[ns.Type]
if !ok {
err := fmt.Errorf("unknown namespace type %q", ns.Type)
return 0, err
}
flags |= flag
}
return flags, nil
}
func LinuxCloneFlags(logger *slog.Logger, isRoot bool, namespaces []specs.LinuxNamespace) (uintptr, error) {
flags, err := ParseNamespaceFlags(namespaces)
if err != nil {
return 0, err
}
if !isRoot && flags != 0 {
logger.Warn("running non-root; namespace isolation disabled")
return 0, nil
}
return flags, nil
}