1
0

Seccomp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. // sigaction() siginfo_t SIG_UNBLOCK
  16. #define _POSIX_C_SOURCE 199309L
  17. #include "util/Seccomp.h"
  18. #include "util/Bits.h"
  19. #include "util/ArchInfo.h"
  20. // getpriority()
  21. #include <sys/resource.h>
  22. #include <signal.h>
  23. #include <sys/prctl.h>
  24. #include <errno.h>
  25. #include <linux/filter.h>
  26. #include <linux/seccomp.h>
  27. #include <linux/audit.h>
  28. #include <sys/syscall.h>
  29. #include <sys/socket.h>
  30. #include <sys/ioctl.h>
  31. #include <stddef.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. /**
  35. * A unique number which is returned as errno by getpriority(), a syscall we never use
  36. * this will be used by Seccomp_isWorking() to detect that the filter has been properly installed.
  37. */
  38. #define IS_WORKING_ERRNO 3333
  39. /**
  40. * Accessing the SIGSYS siginfo depends on the fields being defined by the libc.
  41. * Older libc do not yet include the needed definitions and accessor macros.
  42. * Work around that by falling back to si_value.sival_int which works on some
  43. * but not all architectures.
  44. */
  45. #if defined(si_syscall)
  46. # define GET_SYSCALL_NUM(si) ((si)->si_syscall)
  47. #else
  48. # warning "your libc doesn't define SIGSYS signal info!"
  49. # define GET_SYSCALL_NUM(si) ((si)->si_value.sival_int)
  50. #endif
  51. static void catchViolation(int sig, siginfo_t* si, void* threadContext)
  52. {
  53. printf("Attempted banned syscall number [%d] see doc/Seccomp.md for more information\n",
  54. GET_SYSCALL_NUM(si));
  55. Assert_failure("Disallowed Syscall");
  56. }
  57. struct Filter {
  58. int label;
  59. int jt;
  60. int jf;
  61. struct sock_filter sf;
  62. };
  63. static struct sock_fprog* compile(struct Filter* input, int inputLen, struct Allocator* alloc)
  64. {
  65. // compute gotos
  66. int totalOut = 0;
  67. for (int i = inputLen-1; i >= 0; i--) {
  68. struct Filter* a = &input[i];
  69. if (a->label == 0) {
  70. // check for unresolved gotos...
  71. Assert_true(a->jt == 0 && a->jf == 0);
  72. totalOut++;
  73. continue;
  74. }
  75. int diff = 0;
  76. for (int j = i-1; j >= 0; j--) {
  77. struct Filter* b = &input[j];
  78. if (b->label != 0) { continue; }
  79. if (b->jt == a->label) {
  80. b->sf.jt = diff;
  81. b->jt = 0;
  82. }
  83. if (b->jf == a->label) {
  84. b->sf.jf = diff;
  85. b->jf = 0;
  86. }
  87. diff++;
  88. }
  89. }
  90. // copy into output filter array...
  91. struct sock_filter* sf = Allocator_calloc(alloc, sizeof(struct sock_filter), totalOut);
  92. int outI = 0;
  93. for (int i = 0; i < inputLen; i++) {
  94. if (input[i].label == 0) {
  95. Bits_memcpyConst(&sf[outI++], &input[i].sf, sizeof(struct sock_filter));
  96. }
  97. Assert_true(outI <= totalOut);
  98. Assert_true(i != inputLen-1 || outI == totalOut);
  99. }
  100. struct sock_fprog* out = Allocator_malloc(alloc, sizeof(struct sock_fprog));
  101. out->len = (unsigned short) totalOut;
  102. out->filter = sf;
  103. return out;
  104. }
  105. #define RET_TRAP 0x00030000u
  106. #define RET_ERRNO(x) (0x00050000u | ((x) & 0x0000ffffu))
  107. #define RET_SUCCESS 0x7fff0000u
  108. static struct sock_fprog* mkFilter(struct Allocator* alloc, struct Except* eh)
  109. {
  110. // Adding exceptions to the syscall filter:
  111. //
  112. // echo '#include <sys/syscall.h>' | gcc -E -dM - | grep 'define __NR_' | sort
  113. // for the full list of system calls with syscall numbers (different per ABI)
  114. //
  115. // If gdb traps out it will look like this:
  116. //
  117. // Program received signal SIGSYS, Bad system call.
  118. // [Switching to Thread 0x7ffff7fdd740 (LWP 14673)]
  119. // 0x00007ffff74d1caa in mmap64 () at ../sysdeps/unix/syscall-template.S:81
  120. // 81 ../sysdeps/unix/syscall-template.S: No such file or directory.
  121. //
  122. // %eax should contain the system call number (on different ABIs YMMV)
  123. //
  124. // (gdb) print $eax
  125. // $1 = 9
  126. // (gdb)
  127. //
  128. // Consult your syscall table from the above gcc command...
  129. //
  130. // #define __NR_mmap 9
  131. //
  132. // Then add:
  133. //
  134. // IFEQ(__NR_mmap, success),
  135. //
  136. // And add a comment documenting where you needed that syscall :)
  137. #define STMT(code, val) { .sf = BPF_STMT(code, val) }
  138. #define JMPK(type, not, input, label) { \
  139. .sf = BPF_JUMP(BPF_JMP+(type)+BPF_K, (input), 0, 0), \
  140. .jt = (!(not) ? (label) : 0), \
  141. .jf = ((not) ? (label) : 0) \
  142. }
  143. // Create a label for jumps, the label must be represented by a non-zero integer.
  144. #define LABEL(lbl) { .label = (lbl) }
  145. // Load offset into the register
  146. #define LOAD(offset) STMT(BPF_LD+BPF_W+BPF_ABS, (offset))
  147. // Return constant value
  148. #define RET(val) STMT(BPF_RET+BPF_K, (val))
  149. // If-equal if the currently loaded value equals input, jump to label.
  150. #define IFEQ(input, label) JMPK(BPF_JEQ, 0, (input), (label))
  151. // If-not-equal if the currently loaded value is not equal to input, jump to label.
  152. #define IFNE(input, label) JMPK(BPF_JEQ, 1, (input), (label))
  153. // If-greater-than
  154. #define IFGT(input, label) JMPK(BPF_JGT, 0, (input), (label))
  155. // If-greater-than-or-equal-to
  156. #define IFGE(input, label) JMPK(BPF_JGE, 0, (input), (label))
  157. // If-less-than
  158. #define IFLT(input, label) JMPK(BPF_JGE, 1, (input), (label))
  159. // If-less-than-or-equal-to
  160. #define IFLE(input, label) JMPK(BPF_JGT, 1, (input), (label))
  161. // labels are integers so they must be predefined
  162. int success = 1;
  163. int fail = 2;
  164. int unmaskOnly = 3;
  165. int isworking = 4;
  166. int socket_setip = 5;
  167. int ioctl_setip = 6;
  168. uint32_t auditArch = ArchInfo_getAuditArch();
  169. struct Filter seccompFilter[] = {
  170. LOAD(offsetof(struct seccomp_data, arch)),
  171. IFNE(auditArch, fail),
  172. // Get the syscall num.
  173. LOAD(offsetof(struct seccomp_data, nr)),
  174. // udp
  175. #ifdef __NR_sendmsg
  176. IFEQ(__NR_sendmsg, success),
  177. #endif
  178. #ifdef __NR_recvmsg
  179. IFEQ(__NR_recvmsg, success),
  180. #endif
  181. // ETHInterface
  182. #ifdef __NR_sendto
  183. IFEQ(__NR_sendto, success),
  184. #endif
  185. #ifdef __NR_recvfrom
  186. IFEQ(__NR_recvfrom, success),
  187. #endif
  188. #ifdef __NR_socketcall
  189. // 32-bit: recvmsg is a socketcall
  190. IFEQ(__NR_socketcall, success),
  191. #endif
  192. // libuv
  193. IFEQ(__NR_epoll_ctl, success),
  194. #ifdef __NR_epoll_wait
  195. IFEQ(__NR_epoll_wait, success),
  196. #endif
  197. #ifdef __NR_epoll_pwait
  198. IFEQ(__NR_epoll_pwait, success),
  199. #endif
  200. // gettimeofday is required on some architectures
  201. #ifdef __NR_gettimeofday
  202. IFEQ(__NR_gettimeofday, success),
  203. #endif
  204. // TUN (and logging)
  205. IFEQ(__NR_write, success),
  206. IFEQ(__NR_read, success),
  207. // readv and writev are used by some libc (musl)
  208. #ifdef __NR_readv
  209. IFEQ(__NR_readv, success),
  210. #endif
  211. #ifdef __NR_writev
  212. IFEQ(__NR_writev, success),
  213. #endif
  214. // modern librt reads a read-only mapped section of kernel space which contains the time
  215. // older versions need system calls for getting the time.
  216. // i686 glibc-2.18's time() uses __NR_time
  217. // Raspberry Pi and BeagleBone Black don't provide __NR_time
  218. IFEQ(__NR_clock_gettime, success),
  219. #ifdef __NR_time
  220. IFEQ(__NR_time, success),
  221. #endif
  222. // malloc()
  223. IFEQ(__NR_brk, success),
  224. // abort()
  225. IFEQ(__NR_gettid, success),
  226. IFEQ(__NR_tgkill, success),
  227. IFEQ(__NR_rt_sigprocmask, unmaskOnly),
  228. // exit()
  229. IFEQ(__NR_exit_group, success),
  230. // Seccomp_isWorking()
  231. IFEQ(__NR_getpriority, isworking),
  232. // Securiy_checkPermissions() -> canOpenFiles()
  233. IFEQ(__NR_dup, success),
  234. IFEQ(__NR_close, success),
  235. // Security_checkPermissions() -> getMaxMem()
  236. // x86/ARM use ugetrlimit and mmap2
  237. // ARM does not even have __NR_getrlimit or __NR_mmap defined
  238. // and AMD64 does not have __NR_ugetrlimit or __NR_mmap2 defined
  239. #ifdef __NR_getrlimit
  240. IFEQ(__NR_getrlimit, success),
  241. #endif
  242. #ifdef __NR_ugetrlimit
  243. IFEQ(__NR_ugetrlimit, success),
  244. #endif
  245. #ifdef __NR_mmap
  246. IFEQ(__NR_mmap, success),
  247. #endif
  248. #ifdef __NR_mmap2
  249. IFEQ(__NR_mmap2, success),
  250. #endif
  251. IFEQ(__NR_munmap, success),
  252. // printf()
  253. IFEQ(__NR_fstat, success),
  254. #ifdef __NR_fstat64
  255. IFEQ(__NR_fstat64, success),
  256. #endif
  257. // for setting IP addresses...
  258. // socketForIfName()
  259. #ifdef __NR_socket
  260. IFEQ(__NR_socket, socket_setip),
  261. #endif
  262. IFEQ(__NR_ioctl, ioctl_setip),
  263. RET(SECCOMP_RET_TRAP),
  264. LABEL(socket_setip),
  265. LOAD(offsetof(struct seccomp_data, args[1])),
  266. IFEQ(SOCK_DGRAM, success),
  267. RET(SECCOMP_RET_TRAP),
  268. LABEL(ioctl_setip),
  269. LOAD(offsetof(struct seccomp_data, args[1])),
  270. IFEQ(SIOCGIFINDEX, success),
  271. IFEQ(SIOCGIFFLAGS, success),
  272. IFEQ(SIOCSIFFLAGS, success),
  273. IFEQ(SIOCSIFADDR, success),
  274. IFEQ(SIOCSIFNETMASK, success),
  275. IFEQ(SIOCSIFMTU, success),
  276. RET(SECCOMP_RET_TRAP),
  277. // We allow sigprocmask to *unmask* signals but we don't allow it to mask them.
  278. LABEL(unmaskOnly),
  279. LOAD(offsetof(struct seccomp_data, args[0])),
  280. IFEQ(SIG_UNBLOCK, success),
  281. RET(SECCOMP_RET_TRAP),
  282. LABEL(isworking),
  283. RET(RET_ERRNO(IS_WORKING_ERRNO)),
  284. LABEL(fail),
  285. RET(SECCOMP_RET_TRAP),
  286. LABEL(success),
  287. RET(SECCOMP_RET_ALLOW),
  288. };
  289. return compile(seccompFilter, sizeof(seccompFilter)/sizeof(seccompFilter[0]), alloc);
  290. }
  291. static void installFilter(struct sock_fprog* filter, struct Log* logger, struct Except* eh)
  292. {
  293. struct sigaction sa = { .sa_sigaction = catchViolation, .sa_flags = SA_SIGINFO };
  294. if (sigaction(SIGSYS, &sa, NULL)) {
  295. Log_warn(logger, "sigaction(SIGSYS) -> [%s]\n", strerror(errno));
  296. }
  297. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
  298. // don't worry about it.
  299. Log_warn(logger, "prctl(PR_SET_NO_NEW_PRIVS) -> [%s]\n", strerror(errno));
  300. }
  301. if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, filter) == -1) {
  302. Except_throw(eh, "prctl(PR_SET_SECCOMP) -> [%s]\n", strerror(errno));
  303. }
  304. }
  305. void Seccomp_dropPermissions(struct Allocator* tempAlloc, struct Log* logger, struct Except* eh)
  306. {
  307. struct sock_fprog* filter = mkFilter(tempAlloc, eh);
  308. installFilter(filter, logger, eh);
  309. if (!Seccomp_isWorking()) {
  310. Except_throw(eh, "Seccomp filter not installed properly, Seccomp_isWorking() -> false");
  311. }
  312. }
  313. int Seccomp_isWorking()
  314. {
  315. errno = 0;
  316. // If seccomp is not working, this will fail setting errno to EINVAL
  317. long ret = getpriority(1000, 1);
  318. int err = errno;
  319. // Inside of the kernel, it seems to check whether the errno return is sane
  320. // and if it is not, it treates it as a return value, IS_WORKING_ERRNO (3333) is very unique so
  321. // we'll check for either case just in case this changes.
  322. return (ret == -1 && err == IS_WORKING_ERRNO) || (ret == -IS_WORKING_ERRNO && err == 0);
  323. }
  324. int Seccomp_exists()
  325. {
  326. return 1;
  327. }