main.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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", ".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 := 22; j < 23; j++ {
  48. if find_syscall(j, list[i]) == -1 {
  49. list[i] = append(list[i], j)
  50. }
  51. }
  52. for j := 124; j < 125; j++ {
  53. if find_syscall(j, list[i]) == -1 {
  54. list[i] = append(list[i], j)
  55. }
  56. }
  57. }
  58. lock := sync.Mutex{}
  59. wg := sync.WaitGroup{}
  60. i := 0
  61. // run 4 tasks concurrently
  62. for j := 0; j < 4; j++ {
  63. wg.Add(1)
  64. go func() {
  65. defer wg.Done()
  66. for {
  67. lock.Lock()
  68. if i >= len(list) {
  69. lock.Unlock()
  70. return
  71. }
  72. task := list[i]
  73. i++
  74. lock.Unlock()
  75. run(task)
  76. }
  77. }()
  78. }
  79. // wait for all tasks to finish
  80. wg.Wait()
  81. }