2015-11-14 21:02:26 -05:00
|
|
|
// +build linux
|
|
|
|
|
2016-01-19 17:57:03 -05:00
|
|
|
package 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"
|
2016-07-13 09:41:30 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2016-08-17 12:38:34 -04:00
|
|
|
"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-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 {
|
|
|
|
if stringutils.InSlice(call.Excludes.Arches, arch) {
|
|
|
|
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 {
|
2017-04-27 17:52:47 -04:00
|
|
|
if stringutils.InSlice(rs.Process.Capabilities.Effective, c) {
|
2016-07-13 09:41:30 -04:00
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(call.Includes.Arches) > 0 {
|
|
|
|
if !stringutils.InSlice(call.Includes.Arches, arch) {
|
|
|
|
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 {
|
2017-04-27 17:52:47 -04:00
|
|
|
if !stringutils.InSlice(rs.Process.Capabilities.Effective, c) {
|
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 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 != "" {
|
|
|
|
newConfig.Syscalls = append(newConfig.Syscalls, createSpecsSyscall(call.Name, call.Action, call.Args))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range call.Names {
|
|
|
|
newConfig.Syscalls = append(newConfig.Syscalls, createSpecsSyscall(n, call.Action, call.Args))
|
|
|
|
}
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return newConfig, nil
|
|
|
|
}
|
2016-07-13 09:41:30 -04:00
|
|
|
|
2017-04-27 17:52:47 -04:00
|
|
|
func createSpecsSyscall(name string, action types.Action, args []*types.Arg) specs.LinuxSyscall {
|
|
|
|
newCall := specs.LinuxSyscall{
|
|
|
|
Names: []string{name},
|
|
|
|
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
|
|
|
|
}
|