aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/opencontainers/runtime-tools/generate/seccomp/parse_arguments.go
blob: 2b4c394e679e61197c682c23d170946ed558c92e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package seccomp

import (
	"fmt"
	"strconv"

	rspec "github.com/opencontainers/runtime-spec/specs-go"
)

// parseArguments takes a list of arguments (delimArgs). It parses and fills out
// the argument information and returns a slice of arg structs
func parseArguments(delimArgs []string) ([]rspec.LinuxSeccompArg, error) {
	nilArgSlice := []rspec.LinuxSeccompArg{}
	numberOfArgs := len(delimArgs)

	// No parameters passed with syscall
	if numberOfArgs == 1 {
		return nilArgSlice, nil
	}

	// Correct number of parameters passed with syscall
	if numberOfArgs == 5 {
		syscallIndex, err := strconv.ParseUint(delimArgs[1], 10, 0)
		if err != nil {
			return nilArgSlice, err
		}

		syscallValue, err := strconv.ParseUint(delimArgs[2], 10, 64)
		if err != nil {
			return nilArgSlice, err
		}

		syscallValueTwo, err := strconv.ParseUint(delimArgs[3], 10, 64)
		if err != nil {
			return nilArgSlice, err
		}

		syscallOp, err := parseOperator(delimArgs[4])
		if err != nil {
			return nilArgSlice, err
		}

		argStruct := rspec.LinuxSeccompArg{
			Index:    uint(syscallIndex),
			Value:    syscallValue,
			ValueTwo: syscallValueTwo,
			Op:       syscallOp,
		}

		argSlice := []rspec.LinuxSeccompArg{}
		argSlice = append(argSlice, argStruct)
		return argSlice, nil
	}

	return nilArgSlice, fmt.Errorf("incorrect number of arguments passed with syscall: %d", numberOfArgs)
}

func parseOperator(operator string) (rspec.LinuxSeccompOperator, error) {
	operators := map[string]rspec.LinuxSeccompOperator{
		"NE": rspec.OpNotEqual,
		"LT": rspec.OpLessThan,
		"LE": rspec.OpLessEqual,
		"EQ": rspec.OpEqualTo,
		"GE": rspec.OpGreaterEqual,
		"GT": rspec.OpGreaterThan,
		"ME": rspec.OpMaskedEqual,
	}
	o, ok := operators[operator]
	if !ok {
		return "", fmt.Errorf("unrecognized operator: %s", operator)
	}
	return o, nil
}