Yeuoly před 1 rokem
rodič
revize
e437aff280

+ 131 - 0
cmd/test/fuzz_nodejs_amd64/main.go

@@ -0,0 +1,131 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"os/exec"
+	"strconv"
+	"strings"
+	"sync"
+
+	"github.com/langgenius/dify-sandbox/internal/static/nodejs_syscall"
+)
+
+const (
+	SYSCALL_NUMS = 1000
+)
+
+func run(allowed_syscalls []int) {
+	os.Chdir("/tmp/sandbox-2c8a41ec-04ae-4209-8ed7-17bd476803a6/tmp/sandbox-nodejs-project/node_temp/node_temp")
+
+	nums := []string{}
+	for _, syscall := range allowed_syscalls {
+		nums = append(nums, strconv.Itoa(syscall))
+	}
+	os.Setenv("ALLOWED_SYSCALLS", strings.Join(nums, ","))
+	_, err := exec.Command("node", "test.js", "65537", "1001", "{\"enable_network\":true}").Output()
+	if err == nil {
+	} else {
+		fmt.Println(err.Error())
+	}
+}
+
+func find_syscall(syscall int, syscalls []int) int {
+	for i, s := range syscalls {
+		if s == syscall {
+			return i
+		}
+	}
+	return -1
+}
+
+func main() {
+	original := nodejs_syscall.ALLOW_SYSCALLS
+	original = append(original, nodejs_syscall.ALLOW_NETWORK_SYSCALLS...)
+	original = append(original, nodejs_syscall.ALLOW_ERROR_SYSCALLS...)
+
+	// generate task list
+	list := make([][]int, SYSCALL_NUMS)
+	for i := 0; i < SYSCALL_NUMS; i++ {
+		list[i] = make([]int, len(original))
+		copy(list[i], original)
+		// add i
+		if find_syscall(i, original) == -1 {
+			list[i] = append(list[i], i)
+		}
+
+		// for j := 15; j < 16; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+
+		// for j := 24; j < 25; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+
+		// for j := 60; j < 61; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+
+		// for j := 186; j < 187; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+
+		// for j := 204; j < 205; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+
+		// for j := 273; j < 274; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+
+		// for j := 334; j < 335; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+
+		// for j := 435; j < 436; j++ {
+		// 	if find_syscall(j, list[i]) == -1 {
+		// 		list[i] = append(list[i], j)
+		// 	}
+		// }
+	}
+
+	lock := sync.Mutex{}
+	wg := sync.WaitGroup{}
+	i := 0
+
+	// run 4 tasks concurrently
+	for j := 0; j < 10; j++ {
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			for {
+				lock.Lock()
+				if i >= len(list) {
+					lock.Unlock()
+					return
+				}
+				task := list[i]
+				i++
+				lock.Unlock()
+				run(task)
+			}
+		}()
+	}
+
+	// wait for all tasks to finish
+	wg.Wait()
+}

+ 88 - 0
cmd/test/fuzz_python_amd64/main.go

@@ -0,0 +1,88 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"os/exec"
+	"strconv"
+	"strings"
+	"sync"
+
+	"github.com/langgenius/dify-sandbox/internal/static/python_syscall"
+)
+
+const (
+	SYSCALL_NUMS = 400
+)
+
+func run(allowed_syscalls []int) {
+	// os.Chdir("/tmp/123")
+
+	nums := []string{}
+	for _, syscall := range allowed_syscalls {
+		nums = append(nums, strconv.Itoa(syscall))
+	}
+	os.Setenv("ALLOWED_SYSCALLS", strings.Join(nums, ","))
+	_, err := exec.Command("python3", "cmd/test/fuzz_python_amd64/test.py").Output()
+	if err == nil {
+	} else {
+		fmt.Println("failed")
+	}
+}
+
+func find_syscall(syscall int, syscalls []int) int {
+	for i, s := range syscalls {
+		if s == syscall {
+			return i
+		}
+	}
+	return -1
+}
+
+func main() {
+	original := python_syscall.ALLOW_SYSCALLS
+	original = append(original, python_syscall.ALLOW_NETWORK_SYSCALLS...)
+
+	// generate task list
+	list := make([][]int, SYSCALL_NUMS)
+	for i := 0; i < SYSCALL_NUMS; i++ {
+		list[i] = make([]int, len(original))
+		copy(list[i], original)
+		// add i
+		if find_syscall(i, original) == -1 {
+			list[i] = append(list[i], i)
+		}
+
+		for j := 281; j < 282; j++ {
+			if find_syscall(j, list[i]) == -1 {
+				list[i] = append(list[i], j)
+			}
+		}
+	}
+
+	lock := sync.Mutex{}
+	wg := sync.WaitGroup{}
+	i := 0
+
+	// run 4 tasks concurrently
+	for j := 0; j < 4; j++ {
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			for {
+				lock.Lock()
+				if i >= len(list) {
+					lock.Unlock()
+					return
+				}
+				task := list[i]
+				i++
+				lock.Unlock()
+				run(task)
+			}
+		}()
+	}
+
+	// wait for all tasks to finish
+	wg.Wait()
+}

+ 78 - 0
cmd/test/fuzz_python_amd64/test.py

@@ -0,0 +1,78 @@
+import ctypes
+import os
+import sys
+import json
+import time
+import traceback
+
+# setup sys.excepthook
+def excepthook(type, value, tb):
+    sys.stderr.write("".join(traceback.format_exception(type, value, tb)))
+    sys.stderr.flush()
+    sys.exit(-1)
+
+sys.excepthook = excepthook
+
+lib = ctypes.CDLL("/tmp/sandbox-python/python.so")
+lib.DifySeccomp.argtypes = [ctypes.c_uint32, ctypes.c_uint32, ctypes.c_bool]
+lib.DifySeccomp.restype = None
+
+
+import json
+import base64
+import subprocess
+import os
+
+import requests
+from netrc import netrc, NetrcParseError
+import urllib3
+import socket
+import json
+import datetime
+from datetime import datetime
+datetime.strptime('2021-01-01', '%Y-%m-%d')
+import math
+import random
+import re
+import string
+import sys
+import time
+import traceback
+import uuid
+import os
+import base64
+import hashlib
+import hmac
+import binascii
+import collections
+import functools
+import operator
+import itertools
+
+os.chdir("/")
+
+lib.DifySeccomp(65537, 1001, 1)
+
+# declare main function here
+def main() -> dict:
+    import requests
+    return {
+        "result": requests.get("https://bilibili.com").text,
+    }
+
+from json import loads, dumps
+from base64 import b64decode
+
+# execute main function, and return the result
+# inputs is a dict, and it
+inputs = b64decode('e30=').decode('utf-8')
+output = main(**json.loads(inputs))
+
+# convert output to json and print
+output = dumps(output, indent=4)
+
+result = f'''<<RESULT>>
+{output}
+<<RESULT>>'''
+
+print(result)

+ 15 - 5
internal/static/nodejs_syscall/syscalls_amd64.go

@@ -4,21 +4,32 @@ package nodejs_syscall
 
 import "syscall"
 
+const (
+	//334
+	SYS_RSEQ = 334
+	// 435
+	SYS_CLONE3 = 435
+)
+
 var ALLOW_SYSCALLS = []int{
 	syscall.SYS_OPEN, syscall.SYS_WRITE, syscall.SYS_CLOSE, syscall.SYS_READ,
 	syscall.SYS_OPENAT, syscall.SYS_NEWFSTATAT, syscall.SYS_IOCTL, syscall.SYS_LSEEK,
 	syscall.SYS_FSTAT,
 	syscall.SYS_MPROTECT, syscall.SYS_MMAP, syscall.SYS_MUNMAP,
+	syscall.SYS_MREMAP,
 	syscall.SYS_BRK,
 	syscall.SYS_RT_SIGACTION, syscall.SYS_RT_SIGPROCMASK,
 	syscall.SYS_MADVISE, syscall.SYS_GETPID, syscall.SYS_GETUID,
-	syscall.SYS_FCNTL, syscall.SYS_SIGALTSTACK,
+	syscall.SYS_FCNTL, syscall.SYS_SIGALTSTACK, syscall.SYS_RT_SIGRETURN,
 	syscall.SYS_FUTEX,
 	syscall.SYS_EXIT_GROUP,
 	syscall.SYS_EPOLL_CTL,
 	syscall.SYS_EPOLL_PWAIT,
+	syscall.SYS_SCHED_YIELD, syscall.SYS_EXIT,
+	syscall.SYS_SCHED_GETAFFINITY, syscall.SYS_SET_ROBUST_LIST,
+	SYS_RSEQ,
 
-	syscall.SYS_SETUID, syscall.SYS_SETGID,
+	syscall.SYS_SETUID, syscall.SYS_SETGID, syscall.SYS_GETTID,
 
 	syscall.SYS_CLOCK_GETTIME, syscall.SYS_GETTIMEOFDAY, syscall.SYS_NANOSLEEP,
 	syscall.SYS_TIME,
@@ -30,13 +41,12 @@ var ALLOW_SYSCALLS = []int{
 }
 
 var ALLOW_ERROR_SYSCALLS = []int{
-	syscall.SYS_CLONE,
+	syscall.SYS_CLONE, SYS_CLONE3,
 }
 
 var ALLOW_NETWORK_SYSCALLS = []int{
 	syscall.SYS_SOCKET, syscall.SYS_CONNECT, syscall.SYS_BIND, syscall.SYS_LISTEN, syscall.SYS_ACCEPT, syscall.SYS_SENDTO, syscall.SYS_RECVFROM,
 	syscall.SYS_GETSOCKNAME, syscall.SYS_RECVMSG, syscall.SYS_GETPEERNAME, syscall.SYS_SETSOCKOPT, syscall.SYS_PPOLL, syscall.SYS_UNAME,
 	syscall.SYS_SENDMSG, syscall.SYS_GETSOCKOPT,
-	syscall.SYS_IOCTL, syscall.SYS_LSEEK,
-	syscall.SYS_FSTAT, syscall.SYS_FCNTL, syscall.SYS_FSTATFS,
+	syscall.SYS_FCNTL, syscall.SYS_FSTATFS,
 }

+ 6 - 3
internal/static/python_syscall/syscalls_amd64.go

@@ -17,14 +17,17 @@ var ALLOW_SYSCALLS = []int{
 	// thread
 	syscall.SYS_FUTEX,
 	// memory
-	syscall.SYS_MMAP, syscall.SYS_BRK, syscall.SYS_MPROTECT, syscall.SYS_MUNMAP, syscall.SYS_RT_SIGRETURN, syscall.SYS_MREMAP,
+	syscall.SYS_MMAP, syscall.SYS_BRK, syscall.SYS_MPROTECT, syscall.SYS_MUNMAP, syscall.SYS_RT_SIGRETURN,
+	syscall.SYS_MREMAP,
 
 	// user/group
 	syscall.SYS_SETUID, syscall.SYS_SETGID, syscall.SYS_GETUID,
 	// process
 	syscall.SYS_GETPID, syscall.SYS_GETPPID, syscall.SYS_GETTID,
 	syscall.SYS_EXIT, syscall.SYS_EXIT_GROUP,
-	syscall.SYS_TGKILL, syscall.SYS_RT_SIGACTION,
+	syscall.SYS_TGKILL, syscall.SYS_RT_SIGACTION, syscall.SYS_IOCTL,
+	syscall.SYS_SCHED_YIELD,
+
 	// time
 	syscall.SYS_CLOCK_GETTIME, syscall.SYS_GETTIMEOFDAY, syscall.SYS_NANOSLEEP,
 	syscall.SYS_EPOLL_CTL, syscall.SYS_CLOCK_NANOSLEEP, syscall.SYS_PSELECT6,
@@ -37,5 +40,5 @@ var ALLOW_NETWORK_SYSCALLS = []int{
 	syscall.SYS_SOCKET, syscall.SYS_CONNECT, syscall.SYS_BIND, syscall.SYS_LISTEN, syscall.SYS_ACCEPT, syscall.SYS_SENDTO, syscall.SYS_RECVFROM,
 	syscall.SYS_GETSOCKNAME, syscall.SYS_RECVMSG, syscall.SYS_GETPEERNAME, syscall.SYS_SETSOCKOPT, syscall.SYS_PPOLL, syscall.SYS_UNAME,
 	syscall.SYS_SENDMSG, SYS_SENDMMSG, syscall.SYS_GETSOCKOPT,
-	syscall.SYS_FSTAT, syscall.SYS_FCNTL, syscall.SYS_FSTATFS, syscall.SYS_POLL,
+	syscall.SYS_FSTAT, syscall.SYS_FCNTL, syscall.SYS_FSTATFS, syscall.SYS_POLL, syscall.SYS_EPOLL_PWAIT,
 }