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"
|
|
|
|
"fmt"
|
|
|
|
|
2016-01-04 12:59:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2016-03-19 17:07:17 -04:00
|
|
|
"github.com/opencontainers/specs/specs-go"
|
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.
|
2016-03-19 17:07:17 -04:00
|
|
|
func GetDefaultProfile() (*specs.Seccomp, error) {
|
2016-02-18 00:06:07 -05:00
|
|
|
return setupSeccomp(DefaultProfile)
|
2015-12-18 13:01:58 -05:00
|
|
|
}
|
|
|
|
|
2016-03-09 11:17:57 -05:00
|
|
|
// LoadProfile takes a file path and decodes the seccomp profile.
|
2016-03-19 17:07:17 -04:00
|
|
|
func LoadProfile(body string) (*specs.Seccomp, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return setupSeccomp(&config)
|
|
|
|
}
|
|
|
|
|
2016-03-19 17:07:17 -04:00
|
|
|
func setupSeccomp(config *types.Seccomp) (newConfig *specs.Seccomp, err 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-03-19 17:07:17 -04:00
|
|
|
newConfig = &specs.Seccomp{}
|
2015-11-14 21:02:26 -05:00
|
|
|
|
|
|
|
// if config.Architectures == 0 then libseccomp will figure out the architecture to use
|
|
|
|
if len(config.Architectures) > 0 {
|
|
|
|
for _, arch := range config.Architectures {
|
2016-03-19 17:07:17 -04:00
|
|
|
newConfig.Architectures = append(newConfig.Architectures, specs.Arch(arch))
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 17:07:17 -04:00
|
|
|
newConfig.DefaultAction = specs.Action(config.DefaultAction)
|
2015-11-14 21:02:26 -05:00
|
|
|
|
|
|
|
// Loop through all syscall blocks and convert them to libcontainer format
|
|
|
|
for _, call := range config.Syscalls {
|
2016-03-19 17:07:17 -04:00
|
|
|
newCall := specs.Syscall{
|
2015-11-14 21:02:26 -05:00
|
|
|
Name: call.Name,
|
2016-03-19 17:07:17 -04:00
|
|
|
Action: specs.Action(call.Action),
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all the arguments of the syscall and convert them
|
|
|
|
for _, arg := range call.Args {
|
2016-03-19 17:07:17 -04:00
|
|
|
newArg := specs.Arg{
|
2015-11-14 21:02:26 -05:00
|
|
|
Index: arg.Index,
|
|
|
|
Value: arg.Value,
|
|
|
|
ValueTwo: arg.ValueTwo,
|
2016-03-19 17:07:17 -04:00
|
|
|
Op: specs.Operator(arg.Op),
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
2016-03-19 17:07:17 -04:00
|
|
|
newCall.Args = append(newCall.Args, newArg)
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
2016-03-19 17:07:17 -04:00
|
|
|
newConfig.Syscalls = append(newConfig.Syscalls, newCall)
|
2015-11-14 21:02:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return newConfig, nil
|
|
|
|
}
|