2020-08-27 19:17:21 -04:00
|
|
|
//go:generate go run -tags 'seccomp' generate.go
|
2015-11-14 21:02:26 -05:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package seccomp // import "github.com/docker/docker/profiles/seccomp"
|
2015-11-14 21:02:26 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-07-13 09:41:30 -04:00
|
|
|
"errors"
|
2015-11-14 21:02:26 -05:00
|
|
|
"fmt"
|
2020-08-27 19:17:21 -04:00
|
|
|
"runtime"
|
2015-11-14 21:02:26 -05:00
|
|
|
|
2018-11-03 00:00:15 -04:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2015-11-14 21:02:26 -05:00
|
|
|
)
|
|
|
|
|
2016-01-19 17:57:03 -05:00
|
|
|
// GetDefaultProfile returns the default seccomp profile.
|
2017-04-27 17:52:47 -04:00
|
|
|
func GetDefaultProfile(rs *specs.Spec) (*specs.LinuxSeccomp, error) {
|
2016-07-13 09:41:30 -04:00
|
|
|
return setupSeccomp(DefaultProfile(), rs)
|
2015-12-18 13:01:58 -05:00
|
|
|
}
|
|
|
|
|
2016-11-25 05:40:54 -05:00
|
|
|
// LoadProfile takes a json string and decodes the seccomp profile.
|
2017-04-27 17:52:47 -04:00
|
|
|
func LoadProfile(body string, rs *specs.Spec) (*specs.LinuxSeccomp, error) {
|
2020-09-18 12:14:16 -04:00
|
|
|
var config Seccomp
|
2016-01-04 12:59:26 -05:00
|
|
|
if err := json.Unmarshal([]byte(body), &config); err != nil {
|
2015-11-14 21:02:26 -05:00
|
|
|
return nil, fmt.Errorf("Decoding seccomp profile failed: %v", err)
|
|
|
|
}
|
2016-07-13 09:41:30 -04:00
|
|
|
return setupSeccomp(&config, rs)
|
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
|
2020-08-27 19:17:21 -04:00
|
|
|
// libseccomp string => seccomp arch
|
2020-09-18 12:49:38 -04:00
|
|
|
var nativeToSeccomp = map[string]specs.Arch{
|
|
|
|
"x86": specs.ArchX86,
|
|
|
|
"amd64": specs.ArchX86_64,
|
|
|
|
"arm": specs.ArchARM,
|
|
|
|
"arm64": specs.ArchAARCH64,
|
|
|
|
"mips64": specs.ArchMIPS64,
|
|
|
|
"mips64n32": specs.ArchMIPS64N32,
|
|
|
|
"mipsel64": specs.ArchMIPSEL64,
|
|
|
|
"mips3l64n32": specs.ArchMIPSEL64N32,
|
|
|
|
"mipsle": specs.ArchMIPSEL,
|
|
|
|
"ppc": specs.ArchPPC,
|
|
|
|
"ppc64": specs.ArchPPC64,
|
|
|
|
"ppc64le": specs.ArchPPC64LE,
|
|
|
|
"s390": specs.ArchS390,
|
|
|
|
"s390x": specs.ArchS390X,
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
2020-08-27 19:17:21 -04:00
|
|
|
// GOARCH => libseccomp string
|
|
|
|
var goToNative = map[string]string{
|
|
|
|
"386": "x86",
|
|
|
|
"amd64": "amd64",
|
|
|
|
"arm": "arm",
|
|
|
|
"arm64": "arm64",
|
|
|
|
"mips64": "mips64",
|
|
|
|
"mips64p32": "mips64n32",
|
|
|
|
"mips64le": "mipsel64",
|
|
|
|
"mips64p32le": "mips3l64n32",
|
|
|
|
"mipsle": "mipsel",
|
|
|
|
"ppc": "ppc",
|
|
|
|
"ppc64": "ppc64",
|
|
|
|
"ppc64le": "ppc64le",
|
|
|
|
"s390": "s390",
|
|
|
|
"s390x": "s390x",
|
|
|
|
}
|
|
|
|
|
2017-11-10 00:18:48 -05:00
|
|
|
// inSlice tests whether a string is contained in a slice of strings or not.
|
|
|
|
// Comparison is case sensitive
|
|
|
|
func inSlice(slice []string, s string) bool {
|
|
|
|
for _, ss := range slice {
|
|
|
|
if s == ss {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-18 12:14:16 -04:00
|
|
|
func setupSeccomp(config *Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error) {
|
2015-11-14 21:02:26 -05:00
|
|
|
if config == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// No default action specified, no syscalls listed, assume seccomp disabled
|
|
|
|
if config.DefaultAction == "" && len(config.Syscalls) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-07-13 09:41:30 -04:00
|
|
|
if len(config.Architectures) != 0 && len(config.ArchMap) != 0 {
|
2021-07-16 09:42:56 -04:00
|
|
|
return nil, errors.New("both 'architectures' and 'archMap' are specified in the seccomp profile, use either 'architectures' or 'archMap'")
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
|
2021-07-16 11:55:04 -04:00
|
|
|
if len(config.LinuxSeccomp.Syscalls) != 0 {
|
|
|
|
// The Seccomp type overrides the LinuxSeccomp.Syscalls field,
|
|
|
|
// so 'this should never happen' when loaded from JSON, but could
|
|
|
|
// happen if someone constructs the Config from source.
|
|
|
|
return nil, errors.New("the LinuxSeccomp.Syscalls field should be empty")
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:55:04 -04:00
|
|
|
var (
|
|
|
|
// Copy all common / standard properties to the output profile
|
|
|
|
newConfig = &config.LinuxSeccomp
|
|
|
|
arch = goToNative[runtime.GOARCH]
|
|
|
|
)
|
2021-07-16 09:42:56 -04:00
|
|
|
if seccompArch, ok := nativeToSeccomp[arch]; ok {
|
2016-07-13 09:41:30 -04:00
|
|
|
for _, a := range config.ArchMap {
|
2020-08-27 19:17:21 -04:00
|
|
|
if a.Arch == seccompArch {
|
2020-09-18 12:49:38 -04:00
|
|
|
newConfig.Architectures = append(newConfig.Architectures, a.Arch)
|
|
|
|
newConfig.Architectures = append(newConfig.Architectures, a.SubArches...)
|
2020-08-27 19:17:21 -04:00
|
|
|
break
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 09:41:30 -04:00
|
|
|
Loop:
|
2021-07-16 09:42:56 -04:00
|
|
|
// Convert Syscall to OCI runtimes-spec specs.LinuxSyscall after filtering them.
|
2015-11-14 21:02:26 -05:00
|
|
|
for _, call := range config.Syscalls {
|
2021-07-16 09:42:56 -04:00
|
|
|
if call.Name != "" {
|
|
|
|
if len(call.Names) != 0 {
|
|
|
|
return nil, errors.New("both 'name' and 'names' are specified in the seccomp profile, use either 'name' or 'names'")
|
|
|
|
}
|
|
|
|
call.Names = []string{call.Name}
|
|
|
|
}
|
2021-02-09 08:46:50 -05:00
|
|
|
if call.Excludes != nil {
|
|
|
|
if len(call.Excludes.Arches) > 0 {
|
|
|
|
if inSlice(call.Excludes.Arches, arch) {
|
2016-07-13 09:41:30 -04:00
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 08:46:50 -05:00
|
|
|
if len(call.Excludes.Caps) > 0 {
|
|
|
|
for _, c := range call.Excludes.Caps {
|
|
|
|
if inSlice(rs.Process.Capabilities.Bounding, c) {
|
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
2018-11-03 00:00:15 -04:00
|
|
|
}
|
2021-02-09 08:46:50 -05:00
|
|
|
if call.Excludes.MinKernel != nil {
|
|
|
|
if ok, err := kernelGreaterEqualThan(*call.Excludes.MinKernel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ok {
|
|
|
|
continue Loop
|
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
2021-02-09 08:46:50 -05:00
|
|
|
if call.Includes != nil {
|
|
|
|
if len(call.Includes.Arches) > 0 {
|
|
|
|
if !inSlice(call.Includes.Arches, arch) {
|
2016-07-13 09:41:30 -04:00
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 08:46:50 -05:00
|
|
|
if len(call.Includes.Caps) > 0 {
|
|
|
|
for _, c := range call.Includes.Caps {
|
|
|
|
if !inSlice(rs.Process.Capabilities.Bounding, c) {
|
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if call.Includes.MinKernel != nil {
|
|
|
|
if ok, err := kernelGreaterEqualThan(*call.Includes.MinKernel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !ok {
|
|
|
|
continue Loop
|
|
|
|
}
|
2018-11-03 00:00:15 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 08:30:14 -05:00
|
|
|
newConfig.Syscalls = append(newConfig.Syscalls, call.LinuxSyscall)
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
|
|
|
|
2021-02-09 08:30:14 -05:00
|
|
|
return newConfig, nil
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|