-
Notifications
You must be signed in to change notification settings - Fork 122
/
add_seccomp.go
66 lines (55 loc) · 1.33 KB
/
add_seccomp.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
package python
import (
"os"
"strconv"
"strings"
"syscall"
"github.com/langgenius/dify-sandbox/internal/core/lib"
"github.com/langgenius/dify-sandbox/internal/static/python_syscall"
)
//var allow_syscalls = []int{}
func InitSeccomp(uid int, gid int, enable_network bool) error {
err := syscall.Chroot(".")
if err != nil {
return err
}
err = syscall.Chdir("/")
if err != nil {
return err
}
lib.SetNoNewPrivs()
allowed_syscalls := []int{}
allowed_not_kill_syscalls := []int{}
allowed_not_kill_syscalls = append(allowed_not_kill_syscalls, python_syscall.ALLOW_ERROR_SYSCALLS...)
allowed_syscall := os.Getenv("ALLOWED_SYSCALLS")
if allowed_syscall != "" {
nums := strings.Split(allowed_syscall, ",")
for num := range nums {
syscall, err := strconv.Atoi(nums[num])
if err != nil {
continue
}
allowed_syscalls = append(allowed_syscalls, syscall)
}
} else {
allowed_syscalls = append(allowed_syscalls, python_syscall.ALLOW_SYSCALLS...)
if enable_network {
allowed_syscalls = append(allowed_syscalls, python_syscall.ALLOW_NETWORK_SYSCALLS...)
}
}
err = lib.Seccomp(allowed_syscalls, allowed_not_kill_syscalls)
if err != nil {
return err
}
// setuid
err = syscall.Setuid(uid)
if err != nil {
return err
}
// setgid
err = syscall.Setgid(gid)
if err != nil {
return err
}
return nil
}