seccomp_internal.go 13 KB

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