2015-11-15 02:02:26 +00:00
|
|
|
// +build linux
|
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package seccomp // import "github.com/docker/docker/profiles/seccomp"
|
2015-11-15 02:02:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-07-13 13:41:30 +00:00
|
|
|
"errors"
|
2015-11-15 02:02:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-08-17 16:38:34 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-07-13 13:41:30 +00:00
|
|
|
libseccomp "github.com/seccomp/libseccomp-golang"
|
2015-11-15 02:02:26 +00:00
|
|
|
)
|
|
|
|
|
2016-02-08 16:19:21 +00:00
|
|
|
//go:generate go run -tags 'seccomp' generate.go
|
|
|
|
|
2016-01-19 22:57:03 +00:00
|
|
|
// GetDefaultProfile returns the default seccomp profile.
|
2017-04-27 21:52:47 +00:00
|
|
|
func GetDefaultProfile(rs *specs.Spec) (*specs.LinuxSeccomp, error) {
|
2016-07-13 13:41:30 +00:00
|
|
|
return setupSeccomp(DefaultProfile(), rs)
|
2015-12-18 18:01:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 10:40:54 +00:00
|
|
|
// LoadProfile takes a json string and decodes the seccomp profile.
|
2017-04-27 21:52:47 +00:00
|
|
|
func LoadProfile(body string, rs *specs.Spec) (*specs.LinuxSeccomp, error) {
|
2016-01-04 17:59:26 +00:00
|
|
|
var config types.Seccomp
|
|
|
|
if err := json.Unmarshal([]byte(body), &config); err != nil {
|
2015-11-15 02:02:26 +00:00
|
|
|
return nil, fmt.Errorf("Decoding seccomp profile failed: %v", err)
|
|
|
|
}
|
2016-07-13 13:41:30 +00:00
|
|
|
return setupSeccomp(&config, rs)
|
|
|
|
}
|
2015-11-15 02:02:26 +00:00
|
|
|
|
2016-07-13 13:41:30 +00: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-15 02:02:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 05:18:48 +00: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 21:52:47 +00:00
|
|
|
func setupSeccomp(config *types.Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error) {
|
2015-11-15 02:02:26 +00: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 21:52:47 +00:00
|
|
|
newConfig := &specs.LinuxSeccomp{}
|
2016-07-13 13:41:30 +00: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-15 02:02:26 +00:00
|
|
|
|
|
|
|
// if config.Architectures == 0 then libseccomp will figure out the architecture to use
|
2016-07-13 13:41:30 +00: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-15 02:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 21:52:47 +00:00
|
|
|
newConfig.DefaultAction = specs.LinuxSeccompAction(config.DefaultAction)
|
2015-11-15 02:02:26 +00:00
|
|
|
|
2016-07-13 13:41:30 +00:00
|
|
|
Loop:
|
|
|
|
// Loop through all syscall blocks and convert them to libcontainer format after filtering them
|
2015-11-15 02:02:26 +00:00
|
|
|
for _, call := range config.Syscalls {
|
2016-07-13 13:41:30 +00:00
|
|
|
if len(call.Excludes.Arches) > 0 {
|
2017-11-10 05:18:48 +00:00
|
|
|
if inSlice(call.Excludes.Arches, arch) {
|
2016-07-13 13:41:30 +00:00
|
|
|
continue Loop
|
|
|
|
}
|
2015-11-15 02:02:26 +00:00
|
|
|
}
|
2016-07-13 13:41:30 +00:00
|
|
|
if len(call.Excludes.Caps) > 0 {
|
|
|
|
for _, c := range call.Excludes.Caps {
|
2018-03-14 09:55:54 +00:00
|
|
|
if inSlice(rs.Process.Capabilities.Bounding, c) {
|
2016-07-13 13:41:30 +00:00
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(call.Includes.Arches) > 0 {
|
2017-11-10 05:18:48 +00:00
|
|
|
if !inSlice(call.Includes.Arches, arch) {
|
2016-07-13 13:41:30 +00:00
|
|
|
continue Loop
|
2015-11-15 02:02:26 +00:00
|
|
|
}
|
2016-07-13 13:41:30 +00:00
|
|
|
}
|
|
|
|
if len(call.Includes.Caps) > 0 {
|
|
|
|
for _, c := range call.Includes.Caps {
|
2018-03-14 09:55:54 +00:00
|
|
|
if !inSlice(rs.Process.Capabilities.Bounding, c) {
|
2016-07-13 13:41:30 +00:00
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-15 02:02:26 +00:00
|
|
|
|
2016-07-13 13:41:30 +00: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-15 02:02:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 13:41:30 +00: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-15 02:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newConfig, nil
|
|
|
|
}
|
2016-07-13 13:41:30 +00:00
|
|
|
|
2017-04-27 21:52:47 +00: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 13:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all the arguments of the syscall and convert them
|
|
|
|
for _, arg := range args {
|
2017-04-27 21:52:47 +00:00
|
|
|
newArg := specs.LinuxSeccompArg{
|
2016-07-13 13:41:30 +00:00
|
|
|
Index: arg.Index,
|
|
|
|
Value: arg.Value,
|
|
|
|
ValueTwo: arg.ValueTwo,
|
2017-04-27 21:52:47 +00:00
|
|
|
Op: specs.LinuxSeccompOperator(arg.Op),
|
2016-07-13 13:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newCall.Args = append(newCall.Args, newArg)
|
|
|
|
}
|
|
|
|
return newCall
|
|
|
|
}
|