seccomp.c 1.6 KB

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