sandbox-systrace.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* $OpenBSD: sandbox-systrace.c,v 1.18 2015/10/02 01:39:26 deraadt Exp $ */
  2. /*
  3. * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "includes.h"
  18. #ifdef SANDBOX_SYSTRACE
  19. #include <sys/types.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/syscall.h>
  22. #include <sys/socket.h>
  23. #include <sys/wait.h>
  24. #include <dev/systrace.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <limits.h>
  28. #include <signal.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "atomicio.h"
  35. #include "log.h"
  36. #include "ssh-sandbox.h"
  37. #include "xmalloc.h"
  38. struct sandbox_policy {
  39. int syscall;
  40. int action;
  41. };
  42. /* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */
  43. static const struct sandbox_policy preauth_policy[] = {
  44. { SYS_exit, SYSTR_POLICY_PERMIT },
  45. #ifdef SYS_kbind
  46. { SYS_kbind, SYSTR_POLICY_PERMIT },
  47. #endif
  48. { SYS_getpid, SYSTR_POLICY_PERMIT },
  49. { SYS_getpgid, SYSTR_POLICY_PERMIT },
  50. { SYS_clock_gettime, SYSTR_POLICY_PERMIT },
  51. { SYS_gettimeofday, SYSTR_POLICY_PERMIT },
  52. { SYS_nanosleep, SYSTR_POLICY_PERMIT },
  53. { SYS_sigprocmask, SYSTR_POLICY_PERMIT },
  54. #ifdef SYS_getentropy
  55. /* OpenBSD 5.6 and newer use getentropy(2) to seed arc4random(3). */
  56. { SYS_getentropy, SYSTR_POLICY_PERMIT },
  57. #else
  58. /* Previous releases used sysctl(3)'s kern.arnd variable. */
  59. { SYS___sysctl, SYSTR_POLICY_PERMIT },
  60. #endif
  61. #ifdef SYS_sendsyslog
  62. { SYS_sendsyslog, SYSTR_POLICY_PERMIT },
  63. #endif
  64. { SYS_madvise, SYSTR_POLICY_PERMIT },
  65. { SYS_mmap, SYSTR_POLICY_PERMIT },
  66. { SYS_mprotect, SYSTR_POLICY_PERMIT },
  67. { SYS_mquery, SYSTR_POLICY_PERMIT },
  68. { SYS_munmap, SYSTR_POLICY_PERMIT },
  69. { SYS_poll, SYSTR_POLICY_PERMIT },
  70. { SYS_select, SYSTR_POLICY_PERMIT },
  71. { SYS_read, SYSTR_POLICY_PERMIT },
  72. { SYS_write, SYSTR_POLICY_PERMIT },
  73. { SYS_shutdown, SYSTR_POLICY_PERMIT },
  74. { SYS_close, SYSTR_POLICY_PERMIT },
  75. { SYS_open, SYSTR_POLICY_NEVER },
  76. { -1, -1 }
  77. };
  78. struct ssh_sandbox {
  79. int systrace_fd;
  80. pid_t child_pid;
  81. void (*osigchld)(int);
  82. };
  83. struct ssh_sandbox *
  84. ssh_sandbox_init(struct monitor *monitor)
  85. {
  86. struct ssh_sandbox *box;
  87. debug3("%s: preparing systrace sandbox", __func__);
  88. box = xcalloc(1, sizeof(*box));
  89. box->systrace_fd = -1;
  90. box->child_pid = 0;
  91. box->osigchld = ssh_signal(SIGCHLD, SIG_IGN);
  92. return box;
  93. }
  94. void
  95. ssh_sandbox_child(struct ssh_sandbox *box)
  96. {
  97. debug3("%s: ready", __func__);
  98. ssh_signal(SIGCHLD, box->osigchld);
  99. if (kill(getpid(), SIGSTOP) != 0)
  100. fatal("%s: kill(%d, SIGSTOP)", __func__, getpid());
  101. debug3("%s: started", __func__);
  102. }
  103. static void
  104. ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid,
  105. const struct sandbox_policy *allowed_syscalls)
  106. {
  107. int dev_systrace, i, j, found, status;
  108. pid_t pid;
  109. struct systrace_policy policy;
  110. /* Wait for the child to send itself a SIGSTOP */
  111. debug3("%s: wait for child %ld", __func__, (long)child_pid);
  112. do {
  113. pid = waitpid(child_pid, &status, WUNTRACED);
  114. } while (pid == -1 && errno == EINTR);
  115. ssh_signal(SIGCHLD, box->osigchld);
  116. if (!WIFSTOPPED(status)) {
  117. if (WIFSIGNALED(status))
  118. fatal("%s: child terminated with signal %d",
  119. __func__, WTERMSIG(status));
  120. if (WIFEXITED(status))
  121. fatal("%s: child exited with status %d",
  122. __func__, WEXITSTATUS(status));
  123. fatal("%s: child not stopped", __func__);
  124. }
  125. debug3("%s: child %ld stopped", __func__, (long)child_pid);
  126. box->child_pid = child_pid;
  127. /* Set up systracing of child */
  128. if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
  129. fatal("%s: open(\"/dev/systrace\"): %s", __func__,
  130. strerror(errno));
  131. if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
  132. fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
  133. dev_systrace, strerror(errno));
  134. close(dev_systrace);
  135. debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
  136. if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
  137. fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
  138. box->systrace_fd, child_pid, strerror(errno));
  139. /* Allocate and assign policy */
  140. memset(&policy, 0, sizeof(policy));
  141. policy.strp_op = SYSTR_POLICY_NEW;
  142. policy.strp_maxents = SYS_MAXSYSCALL;
  143. if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
  144. fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
  145. box->systrace_fd, strerror(errno));
  146. policy.strp_op = SYSTR_POLICY_ASSIGN;
  147. policy.strp_pid = box->child_pid;
  148. if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
  149. fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
  150. __func__, box->systrace_fd, strerror(errno));
  151. /* Set per-syscall policy */
  152. for (i = 0; i < SYS_MAXSYSCALL; i++) {
  153. found = 0;
  154. for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
  155. if (allowed_syscalls[j].syscall == i) {
  156. found = 1;
  157. break;
  158. }
  159. }
  160. policy.strp_op = SYSTR_POLICY_MODIFY;
  161. policy.strp_code = i;
  162. policy.strp_policy = found ?
  163. allowed_syscalls[j].action : SYSTR_POLICY_KILL;
  164. if (found)
  165. debug3("%s: policy: enable syscall %d", __func__, i);
  166. if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
  167. fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
  168. __func__, box->systrace_fd, strerror(errno));
  169. }
  170. /* Signal the child to start running */
  171. debug3("%s: start child %ld", __func__, (long)child_pid);
  172. if (kill(box->child_pid, SIGCONT) != 0)
  173. fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid);
  174. }
  175. void
  176. ssh_sandbox_parent_finish(struct ssh_sandbox *box)
  177. {
  178. /* Closing this before the child exits will terminate it */
  179. close(box->systrace_fd);
  180. free(box);
  181. debug3("%s: finished", __func__);
  182. }
  183. void
  184. ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
  185. {
  186. ssh_sandbox_parent(box, child_pid, preauth_policy);
  187. }
  188. #endif /* SANDBOX_SYSTRACE */