config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "SCMP_ACT_LOG": configs.Log,
  22. }
  23. var archs = map[string]string{
  24. "SCMP_ARCH_X86": "x86",
  25. "SCMP_ARCH_X86_64": "amd64",
  26. "SCMP_ARCH_X32": "x32",
  27. "SCMP_ARCH_ARM": "arm",
  28. "SCMP_ARCH_AARCH64": "arm64",
  29. "SCMP_ARCH_MIPS": "mips",
  30. "SCMP_ARCH_MIPS64": "mips64",
  31. "SCMP_ARCH_MIPS64N32": "mips64n32",
  32. "SCMP_ARCH_MIPSEL": "mipsel",
  33. "SCMP_ARCH_MIPSEL64": "mipsel64",
  34. "SCMP_ARCH_MIPSEL64N32": "mipsel64n32",
  35. "SCMP_ARCH_PPC": "ppc",
  36. "SCMP_ARCH_PPC64": "ppc64",
  37. "SCMP_ARCH_PPC64LE": "ppc64le",
  38. "SCMP_ARCH_S390": "s390",
  39. "SCMP_ARCH_S390X": "s390x",
  40. }
  41. // ConvertStringToOperator converts a string into a Seccomp comparison operator.
  42. // Comparison operators use the names they are assigned by Libseccomp's header.
  43. // Attempting to convert a string that is not a valid operator results in an
  44. // error.
  45. func ConvertStringToOperator(in string) (configs.Operator, error) {
  46. if op, ok := operators[in]; ok == true {
  47. return op, nil
  48. }
  49. return 0, fmt.Errorf("string %s is not a valid operator for seccomp", in)
  50. }
  51. // ConvertStringToAction converts a string into a Seccomp rule match action.
  52. // Actions use the names they are assigned in Libseccomp's header, though some
  53. // (notable, SCMP_ACT_TRACE) are not available in this implementation and will
  54. // return errors.
  55. // Attempting to convert a string that is not a valid action results in an
  56. // error.
  57. func ConvertStringToAction(in string) (configs.Action, error) {
  58. if act, ok := actions[in]; ok == true {
  59. return act, nil
  60. }
  61. return 0, fmt.Errorf("string %s is not a valid action for seccomp", in)
  62. }
  63. // ConvertStringToArch converts a string into a Seccomp comparison arch.
  64. func ConvertStringToArch(in string) (string, error) {
  65. if arch, ok := archs[in]; ok == true {
  66. return arch, nil
  67. }
  68. return "", fmt.Errorf("string %s is not a valid arch for seccomp", in)
  69. }