2015-11-14 21:02:26 -05:00
|
|
|
// +build linux
|
|
|
|
|
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"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-11-03 00:00:15 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-07-13 09:41:30 -04:00
|
|
|
libseccomp "github.com/seccomp/libseccomp-golang"
|
2015-11-14 21:02:26 -05:00
|
|
|
)
|
|
|
|
|
2016-02-08 11:19:21 -05:00
|
|
|
//go:generate go run -tags 'seccomp' generate.go
|
|
|
|
|
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) {
|
2016-01-04 12:59:26 -05:00
|
|
|
var config types.Seccomp
|
|
|
|
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
|
|
|
|
2016-07-13 09:41:30 -04:00
|
|
|
var nativeToSeccomp = map[string]types.Arch{
|
|
|
|
"amd64": types.ArchX86_64,
|
|
|
|
"arm64": types.ArchAARCH64,
|
|
|
|
"mips64": types.ArchMIPS64,
|
|
|
|
"mips64n32": types.ArchMIPS64N32,
|
|
|
|
"mipsel64": types.ArchMIPSEL64,
|
|
|
|
"mipsel64n32": types.ArchMIPSEL64N32,
|
|
|
|
"s390x": types.ArchS390X,
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:52:47 -04:00
|
|
|
func setupSeccomp(config *types.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
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:52:47 -04:00
|
|
|
newConfig := &specs.LinuxSeccomp{}
|
2016-07-13 09:41:30 -04:00
|
|
|
|
|
|
|
var arch string
|
|
|
|
var native, err = libseccomp.GetNativeArch()
|
|
|
|
if err == nil {
|
|
|
|
arch = native.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Architectures) != 0 && len(config.ArchMap) != 0 {
|
|
|
|
return nil, errors.New("'architectures' and 'archMap' were specified in the seccomp profile, use either 'architectures' or 'archMap'")
|
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
|
|
|
|
// if config.Architectures == 0 then libseccomp will figure out the architecture to use
|
2016-07-13 09:41:30 -04:00
|
|
|
if len(config.Architectures) != 0 {
|
|
|
|
for _, a := range config.Architectures {
|
|
|
|
newConfig.Architectures = append(newConfig.Architectures, specs.Arch(a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.ArchMap) != 0 {
|
|
|
|
for _, a := range config.ArchMap {
|
|
|
|
seccompArch, ok := nativeToSeccomp[arch]
|
|
|
|
if ok {
|
|
|
|
if a.Arch == seccompArch {
|
|
|
|
newConfig.Architectures = append(newConfig.Architectures, specs.Arch(a.Arch))
|
|
|
|
for _, sa := range a.SubArches {
|
|
|
|
newConfig.Architectures = append(newConfig.Architectures, specs.Arch(sa))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:52:47 -04:00
|
|
|
newConfig.DefaultAction = specs.LinuxSeccompAction(config.DefaultAction)
|
2015-11-14 21:02:26 -05:00
|
|
|
|
2016-07-13 09:41:30 -04:00
|
|
|
Loop:
|
|
|
|
// Loop through all syscall blocks and convert them to libcontainer format after filtering them
|
2015-11-14 21:02:26 -05:00
|
|
|
for _, call := range config.Syscalls {
|
2016-07-13 09:41:30 -04:00
|
|
|
if len(call.Excludes.Arches) > 0 {
|
2017-11-10 00:18:48 -05:00
|
|
|
if inSlice(call.Excludes.Arches, arch) {
|
2016-07-13 09:41:30 -04:00
|
|
|
continue Loop
|
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
2016-07-13 09:41:30 -04:00
|
|
|
if len(call.Excludes.Caps) > 0 {
|
|
|
|
for _, c := range call.Excludes.Caps {
|
2018-03-14 05:55:54 -04:00
|
|
|
if inSlice(rs.Process.Capabilities.Bounding, c) {
|
2016-07-13 09:41:30 -04:00
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-03 00:00:15 -04:00
|
|
|
if call.Excludes.MinKernel != "" {
|
|
|
|
if ok, err := kernelGreaterEqualThan(call.Excludes.MinKernel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ok {
|
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 09:41:30 -04:00
|
|
|
if len(call.Includes.Arches) > 0 {
|
2017-11-10 00:18:48 -05:00
|
|
|
if !inSlice(call.Includes.Arches, arch) {
|
2016-07-13 09:41:30 -04:00
|
|
|
continue Loop
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
|
|
|
if len(call.Includes.Caps) > 0 {
|
|
|
|
for _, c := range call.Includes.Caps {
|
2018-03-14 05:55:54 -04:00
|
|
|
if !inSlice(rs.Process.Capabilities.Bounding, c) {
|
2016-07-13 09:41:30 -04:00
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-03 00:00:15 -04:00
|
|
|
if call.Includes.MinKernel != "" {
|
|
|
|
if ok, err := kernelGreaterEqualThan(call.Includes.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
|
|
|
if call.Name != "" && len(call.Names) != 0 {
|
|
|
|
return nil, errors.New("'name' and 'names' were specified in the seccomp profile, use either 'name' or 'names'")
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
2016-07-13 09:41:30 -04:00
|
|
|
if call.Name != "" {
|
2019-06-18 20:58:51 -04:00
|
|
|
newConfig.Syscalls = append(newConfig.Syscalls, createSpecsSyscall([]string{call.Name}, call.Action, call.Args))
|
|
|
|
} else {
|
|
|
|
newConfig.Syscalls = append(newConfig.Syscalls, createSpecsSyscall(call.Names, call.Action, call.Args))
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return newConfig, nil
|
|
|
|
}
|
2016-07-13 09:41:30 -04:00
|
|
|
|
2019-06-18 20:58:51 -04:00
|
|
|
func createSpecsSyscall(names []string, action types.Action, args []*types.Arg) specs.LinuxSyscall {
|
2017-04-27 17:52:47 -04:00
|
|
|
newCall := specs.LinuxSyscall{
|
2019-06-18 20:58:51 -04:00
|
|
|
Names: names,
|
2017-04-27 17:52:47 -04:00
|
|
|
Action: specs.LinuxSeccompAction(action),
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all the arguments of the syscall and convert them
|
|
|
|
for _, arg := range args {
|
2017-04-27 17:52:47 -04:00
|
|
|
newArg := specs.LinuxSeccompArg{
|
2016-07-13 09:41:30 -04:00
|
|
|
Index: arg.Index,
|
|
|
|
Value: arg.Value,
|
|
|
|
ValueTwo: arg.ValueTwo,
|
2017-04-27 17:52:47 -04:00
|
|
|
Op: specs.LinuxSeccompOperator(arg.Op),
|
2016-07-13 09:41:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
newCall.Args = append(newCall.Args, newArg)
|
|
|
|
}
|
|
|
|
return newCall
|
|
|
|
}
|
2019-02-05 14:31:44 -05:00
|
|
|
|
|
|
|
var currentKernelVersion *kernel.VersionInfo
|
|
|
|
|
|
|
|
func kernelGreaterEqualThan(v string) (bool, error) {
|
|
|
|
version, err := kernel.ParseRelease(v)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if currentKernelVersion == nil {
|
|
|
|
currentKernelVersion, err = kernel.GetKernelVersion()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kernel.CompareKernelVersion(*version, *currentKernelVersion) <= 0, nil
|
|
|
|
}
|