Skip to content

Commit 08cdf77

Browse files
committed
refs #1: Adopt gobwas/glob package for flexible path patterns
1 parent 6110617 commit 08cdf77

9 files changed

+63
-81
lines changed

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ FROM golang:1.8-alpine
22
# This container is for daily development.
33

44
RUN apk add --no-cache build-base git libseccomp-dev linux-headers
5-
RUN go get github.com/seccomp/libseccomp-golang
6-
RUN go get github.com/fatih/color
7-
RUN go get gopkg.in/yaml.v2
5+
RUN go get github.com/seccomp/libseccomp-golang && \
6+
go get github.com/fatih/color && \
7+
go get github.com/gobwas/glob && \
8+
go get gopkg.in/yaml.v2
89

910
# When running this image, mount the working copy root to
1011
# /go/src/github.com/lablup/sorna-jail

Dockerfile.builder-manylinux

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ RUN echo "deb http://ftp.debian.org/debian jessie-backports main" > /etc/apt/sou
44
&& apt update && apt -t jessie-backports install -y libseccomp-dev
55
RUN go get -u github.com/fatih/color && \
66
go get -u github.com/seccomp/libseccomp-golang && \
7+
go get -u github.com/gobwas/glob && \
78
go get -u gopkg.in/yaml.v2
89
CMD ["make", "inside-container"]
910

Dockerfile.builder-musllinux

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FROM golang:1.8-alpine
22
RUN apk add --no-cache build-base libseccomp-dev git linux-headers
33
RUN go get -u github.com/fatih/color && \
44
go get -u github.com/seccomp/libseccomp-golang && \
5+
go get -u github.com/gobwas/glob && \
56
go get -u gopkg.in/yaml.v2
67
CMD ["make", "inside-container"]
78

example_policy.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# - https://filippo.io/linux-syscall-table/
44

55
whitelist_paths:
6-
OP_OPEN: []
7-
OP_ACCESS: []
8-
OP_EXEC: []
9-
OP_STAT: []
10-
OP_CHMOD: ["/home/work/", "/tmp/"]
6+
OP_OPEN: ["*"]
7+
OP_ACCESS: ["*"]
8+
OP_EXEC: ["*"]
9+
OP_STAT: ["*"]
10+
OP_CHMOD: ["/home/work/*", "/tmp/*"]
1111
exec_allowance: 0
1212
fork_allowance: -1
1313
max_child_procs: 32

policy/filebased.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package policy
33
import (
44
"io/ioutil"
55
"log"
6-
"strings"
76

8-
"gopkg.in/yaml.v2"
7+
yaml "gopkg.in/yaml.v2"
98
)
109

1110
type FileBasedPolicy struct {
@@ -14,22 +13,12 @@ type FileBasedPolicy struct {
1413
}
1514

1615
func (p FileBasedPolicy) CheckPathOp(path string, op PathOps, mode int) bool {
17-
var allow bool
18-
switch op {
19-
case OP_CHMOD:
20-
allow = false
21-
for _, prefix := range p.conf.WhitelistPaths[op] {
22-
if strings.HasPrefix(path, prefix) {
23-
allow = true
24-
break
25-
}
16+
for _, matcher := range p.conf.WhitelistPaths[op] {
17+
if matcher.Match(path) {
18+
return true
2619
}
27-
case OP_EXEC:
28-
allow = true
29-
default:
30-
allow = true
3120
}
32-
return allow
21+
return false
3322
}
3423

3524
func (p FileBasedPolicy) GetExecAllowance() int {

policy/interfaces.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package policy
22

33
import (
44
"fmt"
5+
6+
glob "github.com/gobwas/glob"
57
)
68

79
type SandboxPolicy interface {
@@ -39,15 +41,33 @@ func (o *PathOps) UnmarshalYAML(unmarshal func(interface{}) error) error {
3941
return nil
4042
}
4143

44+
type PatternMatcher struct {
45+
glob.Glob
46+
}
47+
48+
func (p *PatternMatcher) UnmarshalYAML(unmarshal func(interface{}) error) error {
49+
var raw string
50+
var err error
51+
if err = unmarshal(&raw); err != nil {
52+
return err
53+
}
54+
var g glob.Glob
55+
if g, err = glob.Compile(raw); err != nil {
56+
return err
57+
}
58+
*p = PatternMatcher{g}
59+
return nil
60+
}
61+
4262
type PolicyConf struct {
43-
WhitelistPaths map[PathOps][]string `yaml:"whitelist_paths"`
44-
ExecAllowance int `yaml:"exec_allowance"`
45-
ForkAllowance int `yaml:"fork_allowance"`
46-
MaxChildProcs uint `yaml:"max_child_procs"`
47-
ExtraEnvs []string `yaml:"extra_envs"`
48-
PreservedEnvKeys []string `yaml:"preserved_env_keys"`
49-
TracedSyscalls []string `yaml:"traced_syscalls"`
50-
AllowedSyscalls []string `yaml:"allowed_syscalls"`
63+
WhitelistPaths map[PathOps][]PatternMatcher `yaml:"whitelist_paths"`
64+
ExecAllowance int `yaml:"exec_allowance"`
65+
ForkAllowance int `yaml:"fork_allowance"`
66+
MaxChildProcs uint `yaml:"max_child_procs"`
67+
ExtraEnvs []string `yaml:"extra_envs"`
68+
PreservedEnvKeys []string `yaml:"preserved_env_keys"`
69+
TracedSyscalls []string `yaml:"traced_syscalls"`
70+
AllowedSyscalls []string `yaml:"allowed_syscalls"`
5171
}
5272

5373
var defaultConf PolicyConf
@@ -64,12 +84,12 @@ func init() {
6484
"OP_STAT": OP_STAT,
6585
"OP_CHMOD": OP_CHMOD,
6686
}
67-
defaultConf.WhitelistPaths = map[PathOps][]string{
68-
OP_OPEN: []string{},
69-
OP_ACCESS: []string{},
70-
OP_EXEC: []string{},
71-
OP_STAT: []string{},
72-
OP_CHMOD: []string{"/home/work/", "/tmp/"},
87+
defaultConf.WhitelistPaths = map[PathOps][]PatternMatcher{
88+
OP_OPEN: []PatternMatcher{PatternMatcher{glob.MustCompile("*")}},
89+
OP_ACCESS: []PatternMatcher{PatternMatcher{glob.MustCompile("*")}},
90+
OP_EXEC: []PatternMatcher{PatternMatcher{glob.MustCompile("*")}},
91+
OP_STAT: []PatternMatcher{PatternMatcher{glob.MustCompile("*")}},
92+
OP_CHMOD: []PatternMatcher{PatternMatcher{glob.MustCompile("/home/work/*")}, PatternMatcher{glob.MustCompile("/tmp/*")}},
7393
}
7494
defaultConf.ExecAllowance = 0
7595
defaultConf.ForkAllowance = -1

policy/julia.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
package policy
22

3-
import (
4-
"strings"
5-
)
6-
73
type JuliaPolicy struct {
84
}
95

106
func (p JuliaPolicy) CheckPathOp(path string, op PathOps, mode int) bool {
11-
var allow bool
12-
switch op {
13-
case OP_CHMOD:
14-
allow = false
15-
for _, prefix := range defaultConf.WhitelistPaths[op] {
16-
if strings.HasPrefix(path, prefix) {
17-
allow = true
18-
break
19-
}
7+
for _, matcher := range defaultConf.WhitelistPaths[op] {
8+
if matcher.Match(path) {
9+
return true
2010
}
21-
default:
22-
allow = true
2311
}
24-
return allow
12+
return false
2513
}
2614

2715
func (p JuliaPolicy) GetExecAllowance() int {

policy/python-tensorflow.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
package policy
22

3-
import (
4-
"strings"
5-
)
6-
73
type PythonTensorFlowPolicy struct {
84
}
95

106
func (p PythonTensorFlowPolicy) CheckPathOp(path string, op PathOps, mode int) bool {
11-
var allow bool
12-
switch op {
13-
case OP_CHMOD:
14-
allow = false
15-
for _, prefix := range defaultConf.WhitelistPaths[op] {
16-
if strings.HasPrefix(path, prefix) {
17-
allow = true
18-
break
19-
}
7+
for _, matcher := range defaultConf.WhitelistPaths[op] {
8+
if matcher.Match(path) {
9+
return true
2010
}
21-
default:
22-
allow = true
2311
}
24-
return allow
12+
return false
2513
}
2614

2715
func (p PythonTensorFlowPolicy) GetExecAllowance() int {

policy/python.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
package policy
22

3-
import (
4-
"strings"
5-
)
6-
73
type PythonPolicy struct {
84
}
95

106
func (p PythonPolicy) CheckPathOp(path string, op PathOps, mode int) bool {
11-
var allow bool
12-
switch op {
13-
case OP_CHMOD:
14-
allow = strings.HasPrefix(path, "/home/work/")
15-
default:
16-
allow = true
7+
for _, matcher := range defaultConf.WhitelistPaths[op] {
8+
if matcher.Match(path) {
9+
return true
10+
}
1711
}
18-
return allow
12+
return false
1913
}
2014

2115
func (p PythonPolicy) GetExecAllowance() int {

0 commit comments

Comments
 (0)