seccomp_internal.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // +build linux
  2. // Internal functions for libseccomp Go bindings
  3. // No exported functions
  4. package seccomp
  5. import (
  6. "fmt"
  7. "os"
  8. "syscall"
  9. )
  10. // Unexported C wrapping code - provides the C-Golang interface
  11. // Get the seccomp header in scope
  12. // Need stdlib.h for free() on cstrings
  13. // #cgo LDFLAGS: -lseccomp
  14. /*
  15. #include <stdlib.h>
  16. #include <seccomp.h>
  17. #if SCMP_VER_MAJOR < 2
  18. #error Minimum supported version of Libseccomp is v2.1.0
  19. #elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 1
  20. #error Minimum supported version of Libseccomp is v2.1.0
  21. #endif
  22. #define ARCH_BAD ~0
  23. const uint32_t C_ARCH_BAD = ARCH_BAD;
  24. #ifndef SCMP_ARCH_AARCH64
  25. #define SCMP_ARCH_AARCH64 ARCH_BAD
  26. #endif
  27. #ifndef SCMP_ARCH_MIPS
  28. #define SCMP_ARCH_MIPS ARCH_BAD
  29. #endif
  30. #ifndef SCMP_ARCH_MIPS64
  31. #define SCMP_ARCH_MIPS64 ARCH_BAD
  32. #endif
  33. #ifndef SCMP_ARCH_MIPS64N32
  34. #define SCMP_ARCH_MIPS64N32 ARCH_BAD
  35. #endif
  36. #ifndef SCMP_ARCH_MIPSEL
  37. #define SCMP_ARCH_MIPSEL ARCH_BAD
  38. #endif
  39. #ifndef SCMP_ARCH_MIPSEL64
  40. #define SCMP_ARCH_MIPSEL64 ARCH_BAD
  41. #endif
  42. #ifndef SCMP_ARCH_MIPSEL64N32
  43. #define SCMP_ARCH_MIPSEL64N32 ARCH_BAD
  44. #endif
  45. const uint32_t C_ARCH_NATIVE = SCMP_ARCH_NATIVE;
  46. const uint32_t C_ARCH_X86 = SCMP_ARCH_X86;
  47. const uint32_t C_ARCH_X86_64 = SCMP_ARCH_X86_64;
  48. const uint32_t C_ARCH_X32 = SCMP_ARCH_X32;
  49. const uint32_t C_ARCH_ARM = SCMP_ARCH_ARM;
  50. const uint32_t C_ARCH_AARCH64 = SCMP_ARCH_AARCH64;
  51. const uint32_t C_ARCH_MIPS = SCMP_ARCH_MIPS;
  52. const uint32_t C_ARCH_MIPS64 = SCMP_ARCH_MIPS64;
  53. const uint32_t C_ARCH_MIPS64N32 = SCMP_ARCH_MIPS64N32;
  54. const uint32_t C_ARCH_MIPSEL = SCMP_ARCH_MIPSEL;
  55. const uint32_t C_ARCH_MIPSEL64 = SCMP_ARCH_MIPSEL64;
  56. const uint32_t C_ARCH_MIPSEL64N32 = SCMP_ARCH_MIPSEL64N32;
  57. const uint32_t C_ACT_KILL = SCMP_ACT_KILL;
  58. const uint32_t C_ACT_TRAP = SCMP_ACT_TRAP;
  59. const uint32_t C_ACT_ERRNO = SCMP_ACT_ERRNO(0);
  60. const uint32_t C_ACT_TRACE = SCMP_ACT_TRACE(0);
  61. const uint32_t C_ACT_ALLOW = SCMP_ACT_ALLOW;
  62. // If TSync is not supported, make sure it doesn't map to a supported filter attribute
  63. // Don't worry about major version < 2, the minimum version checks should catch that case
  64. #if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 2
  65. #define SCMP_FLTATR_CTL_TSYNC _SCMP_CMP_MIN
  66. #endif
  67. const uint32_t C_ATTRIBUTE_DEFAULT = (uint32_t)SCMP_FLTATR_ACT_DEFAULT;
  68. const uint32_t C_ATTRIBUTE_BADARCH = (uint32_t)SCMP_FLTATR_ACT_BADARCH;
  69. const uint32_t C_ATTRIBUTE_NNP = (uint32_t)SCMP_FLTATR_CTL_NNP;
  70. const uint32_t C_ATTRIBUTE_TSYNC = (uint32_t)SCMP_FLTATR_CTL_TSYNC;
  71. const int C_CMP_NE = (int)SCMP_CMP_NE;
  72. const int C_CMP_LT = (int)SCMP_CMP_LT;
  73. const int C_CMP_LE = (int)SCMP_CMP_LE;
  74. const int C_CMP_EQ = (int)SCMP_CMP_EQ;
  75. const int C_CMP_GE = (int)SCMP_CMP_GE;
  76. const int C_CMP_GT = (int)SCMP_CMP_GT;
  77. const int C_CMP_MASKED_EQ = (int)SCMP_CMP_MASKED_EQ;
  78. const int C_VERSION_MAJOR = SCMP_VER_MAJOR;
  79. const int C_VERSION_MINOR = SCMP_VER_MINOR;
  80. const int C_VERSION_MICRO = SCMP_VER_MICRO;
  81. typedef struct scmp_arg_cmp* scmp_cast_t;
  82. // Wrapper to create an scmp_arg_cmp struct
  83. void*
  84. make_struct_arg_cmp(
  85. unsigned int arg,
  86. int compare,
  87. uint64_t a,
  88. uint64_t b
  89. )
  90. {
  91. struct scmp_arg_cmp *s = malloc(sizeof(struct scmp_arg_cmp));
  92. s->arg = arg;
  93. s->op = compare;
  94. s->datum_a = a;
  95. s->datum_b = b;
  96. return s;
  97. }
  98. */
  99. import "C"
  100. // Nonexported types
  101. type scmpFilterAttr uint32
  102. // Nonexported constants
  103. const (
  104. filterAttrActDefault scmpFilterAttr = iota
  105. filterAttrActBadArch scmpFilterAttr = iota
  106. filterAttrNNP scmpFilterAttr = iota
  107. filterAttrTsync scmpFilterAttr = iota
  108. )
  109. const (
  110. // An error return from certain libseccomp functions
  111. scmpError C.int = -1
  112. // Comparison boundaries to check for architecture validity
  113. archStart ScmpArch = ArchNative
  114. archEnd ScmpArch = ArchMIPSEL64N32
  115. // Comparison boundaries to check for action validity
  116. actionStart ScmpAction = ActKill
  117. actionEnd ScmpAction = ActAllow
  118. // Comparison boundaries to check for comparison operator validity
  119. compareOpStart ScmpCompareOp = CompareNotEqual
  120. compareOpEnd ScmpCompareOp = CompareMaskedEqual
  121. )
  122. var (
  123. // Error thrown on bad filter context
  124. errBadFilter = fmt.Errorf("filter is invalid or uninitialized")
  125. // Constants representing library major, minor, and micro versions
  126. verMajor = int(C.C_VERSION_MAJOR)
  127. verMinor = int(C.C_VERSION_MINOR)
  128. verMicro = int(C.C_VERSION_MICRO)
  129. )
  130. // Nonexported functions
  131. // Check if library version is greater than or equal to the given one
  132. func checkVersionAbove(major, minor, micro int) bool {
  133. return (verMajor > major) ||
  134. (verMajor == major && verMinor > minor) ||
  135. (verMajor == major && verMinor == minor && verMicro >= micro)
  136. }
  137. // Init function: Verify library version is appropriate
  138. func init() {
  139. if !checkVersionAbove(2, 1, 0) {
  140. fmt.Fprintf(os.Stderr, "Libseccomp version too low: minimum supported is 2.1.0, detected %d.%d.%d", C.C_VERSION_MAJOR, C.C_VERSION_MINOR, C.C_VERSION_MICRO)
  141. os.Exit(-1)
  142. }
  143. }
  144. // Filter helpers
  145. // Filter finalizer - ensure that kernel context for filters is freed
  146. func filterFinalizer(f *ScmpFilter) {
  147. f.Release()
  148. }
  149. // Get a raw filter attribute
  150. func (f *ScmpFilter) getFilterAttr(attr scmpFilterAttr) (C.uint32_t, error) {
  151. f.lock.Lock()
  152. defer f.lock.Unlock()
  153. if !f.valid {
  154. return 0x0, errBadFilter
  155. }
  156. if !checkVersionAbove(2, 2, 0) && attr == filterAttrTsync {
  157. return 0x0, fmt.Errorf("the thread synchronization attribute is not supported in this version of the library")
  158. }
  159. var attribute C.uint32_t
  160. retCode := C.seccomp_attr_get(f.filterCtx, attr.toNative(), &attribute)
  161. if retCode != 0 {
  162. return 0x0, syscall.Errno(-1 * retCode)
  163. }
  164. return attribute, nil
  165. }
  166. // Set a raw filter attribute
  167. func (f *ScmpFilter) setFilterAttr(attr scmpFilterAttr, value C.uint32_t) error {
  168. f.lock.Lock()
  169. defer f.lock.Unlock()
  170. if !f.valid {
  171. return errBadFilter
  172. }
  173. if !checkVersionAbove(2, 2, 0) && attr == filterAttrTsync {
  174. return fmt.Errorf("the thread synchronization attribute is not supported in this version of the library")
  175. }
  176. retCode := C.seccomp_attr_set(f.filterCtx, attr.toNative(), value)
  177. if retCode != 0 {
  178. return syscall.Errno(-1 * retCode)
  179. }
  180. return nil
  181. }
  182. // DOES NOT LOCK OR CHECK VALIDITY
  183. // Assumes caller has already done this
  184. // Wrapper for seccomp_rule_add_... functions
  185. func (f *ScmpFilter) addRuleWrapper(call ScmpSyscall, action ScmpAction, exact bool, cond C.scmp_cast_t) error {
  186. var length C.uint
  187. if cond != nil {
  188. length = 1
  189. } else {
  190. length = 0
  191. }
  192. var retCode C.int
  193. if exact {
  194. retCode = C.seccomp_rule_add_exact_array(f.filterCtx, action.toNative(), C.int(call), length, cond)
  195. } else {
  196. retCode = C.seccomp_rule_add_array(f.filterCtx, action.toNative(), C.int(call), length, cond)
  197. }
  198. if syscall.Errno(-1*retCode) == syscall.EFAULT {
  199. return fmt.Errorf("unrecognized syscall")
  200. } else if syscall.Errno(-1*retCode) == syscall.EPERM {
  201. return fmt.Errorf("requested action matches default action of filter")
  202. } else if retCode != 0 {
  203. return syscall.Errno(-1 * retCode)
  204. }
  205. return nil
  206. }
  207. // Generic add function for filter rules
  208. func (f *ScmpFilter) addRuleGeneric(call ScmpSyscall, action ScmpAction, exact bool, conds []ScmpCondition) error {
  209. f.lock.Lock()
  210. defer f.lock.Unlock()
  211. if !f.valid {
  212. return errBadFilter
  213. }
  214. if len(conds) == 0 {
  215. if err := f.addRuleWrapper(call, action, exact, nil); err != nil {
  216. return err
  217. }
  218. } else {
  219. // We don't support conditional filtering in library version v2.1
  220. if !checkVersionAbove(2, 2, 1) {
  221. return fmt.Errorf("conditional filtering requires libseccomp version >= 2.2.1")
  222. }
  223. for _, cond := range conds {
  224. cmpStruct := C.make_struct_arg_cmp(C.uint(cond.Argument), cond.Op.toNative(), C.uint64_t(cond.Operand1), C.uint64_t(cond.Operand2))
  225. defer C.free(cmpStruct)
  226. if err := f.addRuleWrapper(call, action, exact, C.scmp_cast_t(cmpStruct)); err != nil {
  227. return err
  228. }
  229. }
  230. }
  231. return nil
  232. }
  233. // Generic Helpers
  234. // Helper - Sanitize Arch token input
  235. func sanitizeArch(in ScmpArch) error {
  236. if in < archStart || in > archEnd {
  237. return fmt.Errorf("unrecognized architecture")
  238. }
  239. if in.toNative() == C.C_ARCH_BAD {
  240. return fmt.Errorf("architecture is not supported on this version of the library")
  241. }
  242. return nil
  243. }
  244. func sanitizeAction(in ScmpAction) error {
  245. inTmp := in & 0x0000FFFF
  246. if inTmp < actionStart || inTmp > actionEnd {
  247. return fmt.Errorf("unrecognized action")
  248. }
  249. if inTmp != ActTrace && inTmp != ActErrno && (in&0xFFFF0000) != 0 {
  250. return fmt.Errorf("highest 16 bits must be zeroed except for Trace and Errno")
  251. }
  252. return nil
  253. }
  254. func sanitizeCompareOp(in ScmpCompareOp) error {
  255. if in < compareOpStart || in > compareOpEnd {
  256. return fmt.Errorf("unrecognized comparison operator")
  257. }
  258. return nil
  259. }
  260. func archFromNative(a C.uint32_t) (ScmpArch, error) {
  261. switch a {
  262. case C.C_ARCH_X86:
  263. return ArchX86, nil
  264. case C.C_ARCH_X86_64:
  265. return ArchAMD64, nil
  266. case C.C_ARCH_X32:
  267. return ArchX32, nil
  268. case C.C_ARCH_ARM:
  269. return ArchARM, nil
  270. case C.C_ARCH_NATIVE:
  271. return ArchNative, nil
  272. case C.C_ARCH_AARCH64:
  273. return ArchARM64, nil
  274. case C.C_ARCH_MIPS:
  275. return ArchMIPS, nil
  276. case C.C_ARCH_MIPS64:
  277. return ArchMIPS64, nil
  278. case C.C_ARCH_MIPS64N32:
  279. return ArchMIPS64N32, nil
  280. case C.C_ARCH_MIPSEL:
  281. return ArchMIPSEL, nil
  282. case C.C_ARCH_MIPSEL64:
  283. return ArchMIPSEL64, nil
  284. case C.C_ARCH_MIPSEL64N32:
  285. return ArchMIPSEL64N32, nil
  286. default:
  287. return 0x0, fmt.Errorf("unrecognized architecture")
  288. }
  289. }
  290. // Only use with sanitized arches, no error handling
  291. func (a ScmpArch) toNative() C.uint32_t {
  292. switch a {
  293. case ArchX86:
  294. return C.C_ARCH_X86
  295. case ArchAMD64:
  296. return C.C_ARCH_X86_64
  297. case ArchX32:
  298. return C.C_ARCH_X32
  299. case ArchARM:
  300. return C.C_ARCH_ARM
  301. case ArchARM64:
  302. return C.C_ARCH_AARCH64
  303. case ArchMIPS:
  304. return C.C_ARCH_MIPS
  305. case ArchMIPS64:
  306. return C.C_ARCH_MIPS64
  307. case ArchMIPS64N32:
  308. return C.C_ARCH_MIPS64N32
  309. case ArchMIPSEL:
  310. return C.C_ARCH_MIPSEL
  311. case ArchMIPSEL64:
  312. return C.C_ARCH_MIPSEL64
  313. case ArchMIPSEL64N32:
  314. return C.C_ARCH_MIPSEL64N32
  315. case ArchNative:
  316. return C.C_ARCH_NATIVE
  317. default:
  318. return 0x0
  319. }
  320. }
  321. // Only use with sanitized ops, no error handling
  322. func (a ScmpCompareOp) toNative() C.int {
  323. switch a {
  324. case CompareNotEqual:
  325. return C.C_CMP_NE
  326. case CompareLess:
  327. return C.C_CMP_LT
  328. case CompareLessOrEqual:
  329. return C.C_CMP_LE
  330. case CompareEqual:
  331. return C.C_CMP_EQ
  332. case CompareGreaterEqual:
  333. return C.C_CMP_GE
  334. case CompareGreater:
  335. return C.C_CMP_GT
  336. case CompareMaskedEqual:
  337. return C.C_CMP_MASKED_EQ
  338. default:
  339. return 0x0
  340. }
  341. }
  342. func actionFromNative(a C.uint32_t) (ScmpAction, error) {
  343. aTmp := a & 0xFFFF
  344. switch a & 0xFFFF0000 {
  345. case C.C_ACT_KILL:
  346. return ActKill, nil
  347. case C.C_ACT_TRAP:
  348. return ActTrap, nil
  349. case C.C_ACT_ERRNO:
  350. return ActErrno.SetReturnCode(int16(aTmp)), nil
  351. case C.C_ACT_TRACE:
  352. return ActTrace.SetReturnCode(int16(aTmp)), nil
  353. case C.C_ACT_ALLOW:
  354. return ActAllow, nil
  355. default:
  356. return 0x0, fmt.Errorf("unrecognized action")
  357. }
  358. }
  359. // Only use with sanitized actions, no error handling
  360. func (a ScmpAction) toNative() C.uint32_t {
  361. switch a & 0xFFFF {
  362. case ActKill:
  363. return C.C_ACT_KILL
  364. case ActTrap:
  365. return C.C_ACT_TRAP
  366. case ActErrno:
  367. return C.C_ACT_ERRNO | (C.uint32_t(a) >> 16)
  368. case ActTrace:
  369. return C.C_ACT_TRACE | (C.uint32_t(a) >> 16)
  370. case ActAllow:
  371. return C.C_ACT_ALLOW
  372. default:
  373. return 0x0
  374. }
  375. }
  376. // Internal only, assumes safe attribute
  377. func (a scmpFilterAttr) toNative() uint32 {
  378. switch a {
  379. case filterAttrActDefault:
  380. return uint32(C.C_ATTRIBUTE_DEFAULT)
  381. case filterAttrActBadArch:
  382. return uint32(C.C_ATTRIBUTE_BADARCH)
  383. case filterAttrNNP:
  384. return uint32(C.C_ATTRIBUTE_NNP)
  385. case filterAttrTsync:
  386. return uint32(C.C_ATTRIBUTE_TSYNC)
  387. default:
  388. return 0x0
  389. }
  390. }