config.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "SCMP_ARCH_PPC": "ppc",
  35. "SCMP_ARCH_PPC64": "ppc64",
  36. "SCMP_ARCH_PPC64LE": "ppc64le",
  37. "SCMP_ARCH_S390": "s390",
  38. "SCMP_ARCH_S390X": "s390x",
  39. }
  40. // ConvertStringToOperator converts a string into a Seccomp comparison operator.
  41. // Comparison operators use the names they are assigned by Libseccomp's header.
  42. // Attempting to convert a string that is not a valid operator results in an
  43. // error.
  44. func ConvertStringToOperator(in string) (configs.Operator, error) {
  45. if op, ok := operators[in]; ok == true {
  46. return op, nil
  47. }
  48. return 0, fmt.Errorf("string %s is not a valid operator for seccomp", in)
  49. }
  50. // ConvertStringToAction converts a string into a Seccomp rule match action.
  51. // Actions use the names they are assigned in Libseccomp's header, though some
  52. // (notable, SCMP_ACT_TRACE) are not available in this implementation and will
  53. // return errors.
  54. // Attempting to convert a string that is not a valid action results in an
  55. // error.
  56. func ConvertStringToAction(in string) (configs.Action, error) {
  57. if act, ok := actions[in]; ok == true {
  58. return act, nil
  59. }
  60. return 0, fmt.Errorf("string %s is not a valid action for seccomp", in)
  61. }
  62. // ConvertStringToArch converts a string into a Seccomp comparison arch.
  63. func ConvertStringToArch(in string) (string, error) {
  64. if arch, ok := archs[in]; ok == true {
  65. return arch, nil
  66. }
  67. return "", fmt.Errorf("string %s is not a valid arch for seccomp", in)
  68. }