2016-11-24 11:26:11 +00:00
|
|
|
package seccomp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SyscallOpts contain options for parsing syscall rules
|
|
|
|
type SyscallOpts struct {
|
|
|
|
Action string
|
|
|
|
Syscall string
|
|
|
|
Index string
|
|
|
|
Value string
|
|
|
|
ValueTwo string
|
|
|
|
Operator string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseSyscallFlag takes a SyscallOpts struct and the seccomp configuration
|
|
|
|
// and sets the new syscall rule accordingly
|
2017-04-12 23:12:04 +00:00
|
|
|
func ParseSyscallFlag(args SyscallOpts, config *rspec.LinuxSeccomp) error {
|
2016-11-24 11:26:11 +00:00
|
|
|
var arguments []string
|
|
|
|
if args.Index != "" && args.Value != "" && args.ValueTwo != "" && args.Operator != "" {
|
|
|
|
arguments = []string{args.Action, args.Syscall, args.Index, args.Value,
|
|
|
|
args.ValueTwo, args.Operator}
|
|
|
|
} else {
|
|
|
|
arguments = []string{args.Action, args.Syscall}
|
|
|
|
}
|
|
|
|
|
|
|
|
action, _ := parseAction(arguments[0])
|
2017-07-20 04:07:01 +00:00
|
|
|
if action == config.DefaultAction && args.argsAreEmpty() {
|
|
|
|
// default already set, no need to make changes
|
|
|
|
return nil
|
2016-11-24 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 23:12:04 +00:00
|
|
|
var newSyscall rspec.LinuxSyscall
|
2016-11-24 11:26:11 +00:00
|
|
|
numOfArgs := len(arguments)
|
|
|
|
if numOfArgs == 6 || numOfArgs == 2 {
|
|
|
|
argStruct, err := parseArguments(arguments[1:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newSyscall = newSyscallStruct(arguments[1], action, argStruct)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("incorrect number of arguments to ParseSyscall: %d", numOfArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
descison, err := decideCourseOfAction(&newSyscall, config.Syscalls)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delimDescison := strings.Split(descison, ":")
|
|
|
|
|
|
|
|
if delimDescison[0] == seccompAppend {
|
|
|
|
config.Syscalls = append(config.Syscalls, newSyscall)
|
|
|
|
}
|
|
|
|
|
|
|
|
if delimDescison[0] == seccompOverwrite {
|
|
|
|
indexForOverwrite, err := strconv.ParseInt(delimDescison[1], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Syscalls[indexForOverwrite] = newSyscall
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-12 23:12:04 +00:00
|
|
|
var actions = map[string]rspec.LinuxSeccompAction{
|
2016-11-24 11:26:11 +00:00
|
|
|
"allow": rspec.ActAllow,
|
|
|
|
"errno": rspec.ActErrno,
|
|
|
|
"kill": rspec.ActKill,
|
|
|
|
"trace": rspec.ActTrace,
|
|
|
|
"trap": rspec.ActTrap,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take passed action, return the SCMP_ACT_<ACTION> version of it
|
2017-04-12 23:12:04 +00:00
|
|
|
func parseAction(action string) (rspec.LinuxSeccompAction, error) {
|
2016-11-24 11:26:11 +00:00
|
|
|
a, ok := actions[action]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unrecognized action: %s", action)
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseDefaultAction sets the default action of the seccomp configuration
|
|
|
|
// and then removes any rules that were already specified with this action
|
2017-04-12 23:12:04 +00:00
|
|
|
func ParseDefaultAction(action string, config *rspec.LinuxSeccomp) error {
|
2016-11-24 11:26:11 +00:00
|
|
|
if action == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultAction, err := parseAction(action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.DefaultAction = defaultAction
|
2017-07-20 04:07:01 +00:00
|
|
|
err = RemoveAllMatchingRules(config, defaultAction)
|
2016-11-24 11:26:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseDefaultActionForce simply sets the default action of the seccomp configuration
|
2017-04-12 23:12:04 +00:00
|
|
|
func ParseDefaultActionForce(action string, config *rspec.LinuxSeccomp) error {
|
2016-11-24 11:26:11 +00:00
|
|
|
if action == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultAction, err := parseAction(action)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.DefaultAction = defaultAction
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-12 23:12:04 +00:00
|
|
|
func newSyscallStruct(name string, action rspec.LinuxSeccompAction, args []rspec.LinuxSeccompArg) rspec.LinuxSyscall {
|
|
|
|
syscallStruct := rspec.LinuxSyscall{
|
|
|
|
Names: []string{name},
|
2016-11-24 11:26:11 +00:00
|
|
|
Action: action,
|
|
|
|
Args: args,
|
|
|
|
}
|
|
|
|
return syscallStruct
|
|
|
|
}
|
2017-07-20 04:07:01 +00:00
|
|
|
|
|
|
|
func (s SyscallOpts) argsAreEmpty() bool {
|
|
|
|
return (s.Index == "" &&
|
|
|
|
s.Value == "" &&
|
|
|
|
s.ValueTwo == "" &&
|
|
|
|
s.Operator == "")
|
|
|
|
}
|