main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "github.com/langgenius/dify-sandbox/internal/static/python_syscall"
  10. )
  11. const (
  12. SYSCALL_NUMS = 400
  13. )
  14. func run(allowed_syscalls []int) {
  15. // os.Chdir("/tmp/123")
  16. nums := []string{}
  17. for _, syscall := range allowed_syscalls {
  18. nums = append(nums, strconv.Itoa(syscall))
  19. }
  20. os.Setenv("ALLOWED_SYSCALLS", strings.Join(nums, ","))
  21. _, err := exec.Command("python3", "cmd/test/fuzz_python_amd64/test.py").Output()
  22. if err == nil {
  23. } else {
  24. fmt.Println("failed")
  25. }
  26. }
  27. func find_syscall(syscall int, syscalls []int) int {
  28. for i, s := range syscalls {
  29. if s == syscall {
  30. return i
  31. }
  32. }
  33. return -1
  34. }
  35. func main() {
  36. original := python_syscall.ALLOW_SYSCALLS
  37. original = append(original, python_syscall.ALLOW_NETWORK_SYSCALLS...)
  38. // generate task list
  39. list := make([][]int, SYSCALL_NUMS)
  40. for i := 0; i < SYSCALL_NUMS; i++ {
  41. list[i] = make([]int, len(original))
  42. copy(list[i], original)
  43. // add i
  44. if find_syscall(i, original) == -1 {
  45. list[i] = append(list[i], i)
  46. }
  47. for j := 281; j < 282; j++ {
  48. if find_syscall(j, list[i]) == -1 {
  49. list[i] = append(list[i], j)
  50. }
  51. }
  52. }
  53. lock := sync.Mutex{}
  54. wg := sync.WaitGroup{}
  55. i := 0
  56. // run 4 tasks concurrently
  57. for j := 0; j < 4; j++ {
  58. wg.Add(1)
  59. go func() {
  60. defer wg.Done()
  61. for {
  62. lock.Lock()
  63. if i >= len(list) {
  64. lock.Unlock()
  65. return
  66. }
  67. task := list[i]
  68. i++
  69. lock.Unlock()
  70. run(task)
  71. }
  72. }()
  73. }
  74. // wait for all tasks to finish
  75. wg.Wait()
  76. }