|
@@ -1,18 +1,13 @@
|
|
|
package nodejs
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
- "encoding/binary"
|
|
|
- "errors"
|
|
|
"os"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"syscall"
|
|
|
- "unsafe"
|
|
|
|
|
|
"github.com/langgenius/dify-sandbox/internal/core/lib"
|
|
|
"github.com/langgenius/dify-sandbox/internal/static/nodejs_syscall"
|
|
|
- sg "github.com/seccomp/libseccomp-golang"
|
|
|
)
|
|
|
|
|
|
//var allow_syscalls = []int{}
|
|
@@ -29,10 +24,8 @@ func InitSeccomp(uid int, gid int, enable_network bool) error {
|
|
|
|
|
|
lib.SetNoNewPrivs()
|
|
|
|
|
|
- ctx, err := sg.NewFilter(sg.ActKillProcess)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+ allowed_syscalls := []int{}
|
|
|
+ allowed_not_kill_syscalls := []int{}
|
|
|
|
|
|
allowed_syscall := os.Getenv("ALLOWED_SYSCALLS")
|
|
|
if allowed_syscall != "" {
|
|
@@ -40,77 +33,23 @@ func InitSeccomp(uid int, gid int, enable_network bool) error {
|
|
|
for num := range nums {
|
|
|
syscall, err := strconv.Atoi(nums[num])
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = ctx.AddRule(sg.ScmpSyscall(syscall), sg.ActAllow)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+ continue
|
|
|
}
|
|
|
+ allowed_syscalls = append(allowed_syscalls, syscall)
|
|
|
}
|
|
|
} else {
|
|
|
- for _, syscall := range nodejs_syscall.ALLOW_SYSCALLS {
|
|
|
- err = ctx.AddRule(sg.ScmpSyscall(syscall), sg.ActAllow)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for _, syscall := range nodejs_syscall.ALLOW_ERROR_SYSCALLS {
|
|
|
- err = ctx.AddRule(sg.ScmpSyscall(syscall), sg.ActErrno)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
+ allowed_syscalls = append(allowed_syscalls, nodejs_syscall.ALLOW_SYSCALLS...)
|
|
|
+ allowed_not_kill_syscalls = append(allowed_not_kill_syscalls, nodejs_syscall.ALLOW_ERROR_SYSCALLS...)
|
|
|
|
|
|
if enable_network {
|
|
|
- for _, syscall := range nodejs_syscall.ALLOW_NETWORK_SYSCALLS {
|
|
|
- err = ctx.AddRule(sg.ScmpSyscall(syscall), sg.ActAllow)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
+ allowed_syscalls = append(allowed_syscalls, nodejs_syscall.ALLOW_NETWORK_SYSCALLS...)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- reader, writer, err := os.Pipe()
|
|
|
+ err = lib.Seccomp(allowed_syscalls, allowed_not_kill_syscalls)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- defer reader.Close()
|
|
|
- defer writer.Close()
|
|
|
-
|
|
|
- file := os.NewFile(uintptr(writer.Fd()), "pipe")
|
|
|
- ctx.ExportBPF(file)
|
|
|
-
|
|
|
- // read from pipe
|
|
|
- data := make([]byte, 4096)
|
|
|
- n, err := reader.Read(data)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- // load bpf
|
|
|
- sock_filters := make([]syscall.SockFilter, n/8)
|
|
|
- bytesBuffer := bytes.NewBuffer(data)
|
|
|
- err = binary.Read(bytesBuffer, binary.LittleEndian, &sock_filters)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- bpf := syscall.SockFprog{
|
|
|
- Len: uint16(len(sock_filters)),
|
|
|
- Filter: &sock_filters[0],
|
|
|
- }
|
|
|
-
|
|
|
- _, _, err2 := syscall.Syscall(
|
|
|
- syscall.SYS_SECCOMP,
|
|
|
- uintptr(lib.SeccompSetModeFilter),
|
|
|
- uintptr(lib.SeccompFilterFlagTSYNC),
|
|
|
- uintptr(unsafe.Pointer(&bpf)),
|
|
|
- )
|
|
|
-
|
|
|
- if err2 != 0 {
|
|
|
- return errors.New("seccomp error")
|
|
|
- }
|
|
|
|
|
|
// setuid
|
|
|
err = syscall.Setuid(uid)
|