seccomp-bpf.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * seccomp example for x86 (32-bit and 64-bit) with BPF macros
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
  5. * Authors:
  6. * Will Drewry <wad@chromium.org>
  7. * Kees Cook <keescook@chromium.org>
  8. *
  9. * Use of this source code is governed by a BSD-style license that can be
  10. * found in the LICENSE file.
  11. */
  12. #ifndef _SECCOMP_BPF_H_
  13. #define _SECCOMP_BPF_H_
  14. #define _GNU_SOURCE 1
  15. #include <stdio.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <endian.h>
  23. #include <sys/prctl.h>
  24. #ifndef PR_SET_NO_NEW_PRIVS
  25. # define PR_SET_NO_NEW_PRIVS 38
  26. #endif
  27. #include <linux/unistd.h>
  28. #include <linux/audit.h>
  29. #include <linux/filter.h>
  30. #ifdef HAVE_LINUX_SECCOMP_H
  31. # include <linux/seccomp.h>
  32. #endif
  33. #ifndef SECCOMP_MODE_FILTER
  34. #define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
  35. #define SECCOMP_RET_KILL 0x00000000U /* kill the task immediately */
  36. #define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
  37. #define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */
  38. #define SECCOMP_RET_LOG 0x00070000U
  39. #define SECCOMP_RET_LOGALLOW 0x7ffc0000U
  40. #define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */
  41. #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
  42. #define SECCOMP_RET_KILLPROCESS 0x80000000U
  43. #define SECCOMP_RET_ERROR(x) (SECCOMP_RET_ERRNO | ((x) & 0x0000ffffU))
  44. #define SECCOMP_RET_LOGGER(x) (SECCOMP_RET_LOG | ((x) & 0x0000ffffU))
  45. struct seccomp_data {
  46. int nr;
  47. __u32 arch;
  48. __u64 instruction_pointer;
  49. __u64 args[6];
  50. };
  51. #endif
  52. #ifndef SYS_SECCOMP
  53. # define SYS_SECCOMP 1
  54. #endif
  55. #define syscall_nr (offsetof(struct seccomp_data, nr))
  56. #define arch_nr (offsetof(struct seccomp_data, arch))
  57. #if defined(__i386__)
  58. # define REG_SYSCALL REG_EAX
  59. # define ARCH_NR AUDIT_ARCH_I386
  60. #elif defined(__x86_64__)
  61. # define REG_SYSCALL REG_RAX
  62. # define ARCH_NR AUDIT_ARCH_X86_64
  63. #elif defined(__mips__)
  64. # define REG_SYSCALL regs[2]
  65. # if __BYTE_ORDER == __LITTLE_ENDIAN
  66. # define ARCH_NR AUDIT_ARCH_MIPSEL
  67. # else
  68. # define ARCH_NR AUDIT_ARCH_MIPS
  69. # endif
  70. #elif defined(__arm__) && (defined(__ARM_EABI__) || defined(__thumb__))
  71. # define REG_SYSCALL regs.uregs[7]
  72. # if __BYTE_ORDER == __LITTLE_ENDIAN
  73. # define ARCH_NR AUDIT_ARCH_ARM
  74. # else
  75. # define ARCH_NR AUDIT_ARCH_ARMEB
  76. # endif
  77. #elif defined(__PPC__)
  78. # define REG_SYSCALL regs.gpr[0]
  79. # define ARCH_NR AUDIT_ARCH_PPC
  80. #else
  81. # warning "Platform does not support seccomp filter yet"
  82. # define REG_SYSCALL 0
  83. # define ARCH_NR 0
  84. #endif
  85. #endif /* _SECCOMP_BPF_H_ */