forked from pjbgf/zaz
/
profile_verifier.go
89 lines (76 loc) · 2.23 KB
/
profile_verifier.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package seccomp
import (
"encoding/json"
"io"
"io/ioutil"
"github.com/opencontainers/runtime-spec/specs-go"
)
// ProfileVerifier holds the logic for validating seccomp profiles.
type ProfileVerifier struct {
profileReader io.Reader
}
// NewProfileVerifier initialises and returns a new ProfileVerifier.
func NewProfileVerifier(profileReader io.Reader) *ProfileVerifier {
return &ProfileVerifier{profileReader}
}
// Warning represents a warning about a system call.
type Warning struct {
SyscallName string
}
func (v *ProfileVerifier) getProfile() (*specs.LinuxSeccomp, error) {
content, err := ioutil.ReadAll(v.profileReader)
if err != nil {
return nil, ErrInvalidProfile
}
var profile specs.LinuxSeccomp
if err := json.Unmarshal([]byte(content), &profile); err != nil {
return nil, ErrInvalidProfile
}
return &profile, nil
}
// Run executes a seccomp profile validation, returning an error and a list
// of Warning with the high-risk system calls found.
func (v *ProfileVerifier) Run() ([]Warning, error) {
profile, err := v.getProfile()
if err != nil {
return nil, err
}
highRiskAllowed := make([]Warning, 0)
if profile.DefaultAction == specs.ActAllow || profile.DefaultAction == specs.ActLog {
if len(profile.Syscalls) == 0 {
for name := range highRiskSystemCalls {
highRiskAllowed = append(highRiskAllowed, Warning{SyscallName: name})
}
} else {
allowed := make(map[string]bool, len(highRiskSystemCalls))
for k, v := range highRiskSystemCalls {
allowed[k] = v
}
for _, c := range profile.Syscalls {
if c.Action == specs.ActErrno || c.Action == specs.ActKill || c.Action == specs.ActTrap {
for _, name := range c.Names {
allowed[name] = false
}
}
}
for name, callAllowed := range allowed {
if callAllowed {
highRiskAllowed = append(highRiskAllowed, Warning{SyscallName: name})
}
}
}
}
for _, c := range profile.Syscalls {
if c.Action == specs.ActAllow || c.Action == specs.ActLog {
for _, name := range c.Names {
if _, found := highRiskSystemCalls[name]; found {
highRiskAllowed = append(highRiskAllowed, Warning{SyscallName: name})
}
}
}
}
if len(highRiskAllowed) > 0 {
err = ErrHighRiskSyscallAllowed
}
return highRiskAllowed, err
}