seccomp.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * linux/kernel/seccomp.c
  3. *
  4. * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
  5. *
  6. * This defines a simple but solid secure-computing mode.
  7. */
  8. #include <linux/seccomp.h>
  9. #include <linux/sched.h>
  10. /* #define SECCOMP_DEBUG 1 */
  11. #define NR_SECCOMP_MODES 1
  12. /*
  13. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  14. * To be fully secure this must be combined with rlimit
  15. * to limit the stack allocations too.
  16. */
  17. static int mode1_syscalls[] = {
  18. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  19. 0, /* null terminated */
  20. };
  21. #ifdef TIF_32BIT
  22. static int mode1_syscalls_32[] = {
  23. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  24. 0, /* null terminated */
  25. };
  26. #endif
  27. void __secure_computing(int this_syscall)
  28. {
  29. int mode = current->seccomp.mode;
  30. int * syscall;
  31. switch (mode) {
  32. case 1:
  33. syscall = mode1_syscalls;
  34. #ifdef TIF_32BIT
  35. if (test_thread_flag(TIF_32BIT))
  36. syscall = mode1_syscalls_32;
  37. #endif
  38. do {
  39. if (*syscall == this_syscall)
  40. return;
  41. } while (*++syscall);
  42. break;
  43. default:
  44. BUG();
  45. }
  46. #ifdef SECCOMP_DEBUG
  47. dump_stack();
  48. #endif
  49. do_exit(SIGKILL);
  50. }
  51. long prctl_get_seccomp(void)
  52. {
  53. return current->seccomp.mode;
  54. }
  55. long prctl_set_seccomp(unsigned long seccomp_mode)
  56. {
  57. long ret;
  58. /* can set it only once to be even more secure */
  59. ret = -EPERM;
  60. if (unlikely(current->seccomp.mode))
  61. goto out;
  62. ret = -EINVAL;
  63. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  64. current->seccomp.mode = seccomp_mode;
  65. set_thread_flag(TIF_SECCOMP);
  66. #ifdef TIF_NOTSC
  67. disable_TSC();
  68. #endif
  69. ret = 0;
  70. }
  71. out:
  72. return ret;
  73. }