config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package seccomp
  2. import (
  3. "fmt"
  4. "github.com/opencontainers/runc/libcontainer/configs"
  5. )
  6. var operators = map[string]configs.Operator{
  7. "SCMP_CMP_NE": configs.NotEqualTo,
  8. "SCMP_CMP_LT": configs.LessThan,
  9. "SCMP_CMP_LE": configs.LessThanOrEqualTo,
  10. "SCMP_CMP_EQ": configs.EqualTo,
  11. "SCMP_CMP_GE": configs.GreaterThanOrEqualTo,
  12. "SCMP_CMP_GT": configs.GreaterThan,
  13. "SCMP_CMP_MASKED_EQ": configs.MaskEqualTo,
  14. }
  15. var actions = map[string]configs.Action{
  16. "SCMP_ACT_KILL": configs.Kill,
  17. "SCMP_ACT_ERRNO": configs.Errno,
  18. "SCMP_ACT_TRAP": configs.Trap,
  19. "SCMP_ACT_ALLOW": configs.Allow,
  20. "SCMP_ACT_TRACE": configs.Trace,
  21. }
  22. var archs = map[string]string{
  23. "SCMP_ARCH_X86": "x86",
  24. "SCMP_ARCH_X86_64": "amd64",
  25. "SCMP_ARCH_X32": "x32",
  26. "SCMP_ARCH_ARM": "arm",
  27. "SCMP_ARCH_AARCH64": "arm64",
  28. "SCMP_ARCH_MIPS": "mips",
  29. "SCMP_ARCH_MIPS64": "mips64",
  30. "SCMP_ARCH_MIPS64N32": "mips64n32",
  31. "SCMP_ARCH_MIPSEL": "mipsel",
  32. "SCMP_ARCH_MIPSEL64": "mipsel64",
  33. "SCMP_ARCH_MIPSEL64N32": "mipsel64n32",
  34. }
  35. // ConvertStringToOperator converts a string into a Seccomp comparison operator.
  36. // Comparison operators use the names they are assigned by Libseccomp's header.
  37. // Attempting to convert a string that is not a valid operator results in an
  38. // error.
  39. func ConvertStringToOperator(in string) (configs.Operator, error) {
  40. if op, ok := operators[in]; ok == true {
  41. return op, nil
  42. }
  43. return 0, fmt.Errorf("string %s is not a valid operator for seccomp", in)
  44. }
  45. // ConvertStringToAction converts a string into a Seccomp rule match action.
  46. // Actions use the names they are assigned in Libseccomp's header, though some
  47. // (notable, SCMP_ACT_TRACE) are not available in this implementation and will
  48. // return errors.
  49. // Attempting to convert a string that is not a valid action results in an
  50. // error.
  51. func ConvertStringToAction(in string) (configs.Action, error) {
  52. if act, ok := actions[in]; ok == true {
  53. return act, nil
  54. }
  55. return 0, fmt.Errorf("string %s is not a valid action for seccomp", in)
  56. }
  57. // ConvertStringToArch converts a string into a Seccomp comparison arch.
  58. func ConvertStringToArch(in string) (string, error) {
  59. if arch, ok := archs[in]; ok == true {
  60. return arch, nil
  61. }
  62. return "", fmt.Errorf("string %s is not a valid arch for seccomp", in)
  63. }