config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package seccomp
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/opencontainers/runc/libcontainer/configs"
  6. )
  7. var operators = map[string]configs.Operator{
  8. "SCMP_CMP_NE": configs.NotEqualTo,
  9. "SCMP_CMP_LT": configs.LessThan,
  10. "SCMP_CMP_LE": configs.LessThanOrEqualTo,
  11. "SCMP_CMP_EQ": configs.EqualTo,
  12. "SCMP_CMP_GE": configs.GreaterThanOrEqualTo,
  13. "SCMP_CMP_GT": configs.GreaterThan,
  14. "SCMP_CMP_MASKED_EQ": configs.MaskEqualTo,
  15. }
  16. // KnownOperators returns the list of the known operations.
  17. // Used by `runc features`.
  18. func KnownOperators() []string {
  19. var res []string
  20. for k := range operators {
  21. res = append(res, k)
  22. }
  23. sort.Strings(res)
  24. return res
  25. }
  26. var actions = map[string]configs.Action{
  27. "SCMP_ACT_KILL": configs.Kill,
  28. "SCMP_ACT_ERRNO": configs.Errno,
  29. "SCMP_ACT_TRAP": configs.Trap,
  30. "SCMP_ACT_ALLOW": configs.Allow,
  31. "SCMP_ACT_TRACE": configs.Trace,
  32. "SCMP_ACT_LOG": configs.Log,
  33. "SCMP_ACT_NOTIFY": configs.Notify,
  34. "SCMP_ACT_KILL_THREAD": configs.KillThread,
  35. "SCMP_ACT_KILL_PROCESS": configs.KillProcess,
  36. }
  37. // KnownActions returns the list of the known actions.
  38. // Used by `runc features`.
  39. func KnownActions() []string {
  40. var res []string
  41. for k := range actions {
  42. res = append(res, k)
  43. }
  44. sort.Strings(res)
  45. return res
  46. }
  47. var archs = map[string]string{
  48. "SCMP_ARCH_X86": "x86",
  49. "SCMP_ARCH_X86_64": "amd64",
  50. "SCMP_ARCH_X32": "x32",
  51. "SCMP_ARCH_ARM": "arm",
  52. "SCMP_ARCH_AARCH64": "arm64",
  53. "SCMP_ARCH_MIPS": "mips",
  54. "SCMP_ARCH_MIPS64": "mips64",
  55. "SCMP_ARCH_MIPS64N32": "mips64n32",
  56. "SCMP_ARCH_MIPSEL": "mipsel",
  57. "SCMP_ARCH_MIPSEL64": "mipsel64",
  58. "SCMP_ARCH_MIPSEL64N32": "mipsel64n32",
  59. "SCMP_ARCH_PPC": "ppc",
  60. "SCMP_ARCH_PPC64": "ppc64",
  61. "SCMP_ARCH_PPC64LE": "ppc64le",
  62. "SCMP_ARCH_S390": "s390",
  63. "SCMP_ARCH_S390X": "s390x",
  64. }
  65. // KnownArchs returns the list of the known archs.
  66. // Used by `runc features`.
  67. func KnownArchs() []string {
  68. var res []string
  69. for k := range archs {
  70. res = append(res, k)
  71. }
  72. sort.Strings(res)
  73. return res
  74. }
  75. // ConvertStringToOperator converts a string into a Seccomp comparison operator.
  76. // Comparison operators use the names they are assigned by Libseccomp's header.
  77. // Attempting to convert a string that is not a valid operator results in an
  78. // error.
  79. func ConvertStringToOperator(in string) (configs.Operator, error) {
  80. if op, ok := operators[in]; ok {
  81. return op, nil
  82. }
  83. return 0, fmt.Errorf("string %s is not a valid operator for seccomp", in)
  84. }
  85. // ConvertStringToAction converts a string into a Seccomp rule match action.
  86. // Actions use the names they are assigned in Libseccomp's header.
  87. // Attempting to convert a string that is not a valid action results in an
  88. // error.
  89. func ConvertStringToAction(in string) (configs.Action, error) {
  90. if act, ok := actions[in]; ok {
  91. return act, nil
  92. }
  93. return 0, fmt.Errorf("string %s is not a valid action for seccomp", in)
  94. }
  95. // ConvertStringToArch converts a string into a Seccomp comparison arch.
  96. func ConvertStringToArch(in string) (string, error) {
  97. if arch, ok := archs[in]; ok {
  98. return arch, nil
  99. }
  100. return "", fmt.Errorf("string %s is not a valid arch for seccomp", in)
  101. }