sandbox-darwin.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "includes.h"
  17. #ifdef SANDBOX_DARWIN
  18. #include <sys/types.h>
  19. #include <sandbox.h>
  20. #include <errno.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include "log.h"
  27. #include "ssh-sandbox.h"
  28. #include "monitor.h"
  29. #include "xmalloc.h"
  30. /* Darwin/OS X sandbox */
  31. struct ssh_sandbox {
  32. pid_t child_pid;
  33. };
  34. struct ssh_sandbox *
  35. ssh_sandbox_init(struct monitor *monitor)
  36. {
  37. struct ssh_sandbox *box;
  38. /*
  39. * Strictly, we don't need to maintain any state here but we need
  40. * to return non-NULL to satisfy the API.
  41. */
  42. debug3("%s: preparing Darwin sandbox", __func__);
  43. box = xcalloc(1, sizeof(*box));
  44. box->child_pid = 0;
  45. return box;
  46. }
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  49. void
  50. ssh_sandbox_child(struct ssh_sandbox *box)
  51. {
  52. char *errmsg;
  53. struct rlimit rl_zero;
  54. debug3("%s: starting Darwin sandbox", __func__);
  55. if (sandbox_init(kSBXProfilePureComputation, SANDBOX_NAMED,
  56. &errmsg) == -1)
  57. fatal("%s: sandbox_init: %s", __func__, errmsg);
  58. /*
  59. * The kSBXProfilePureComputation still allows sockets, so
  60. * we must disable these using rlimit.
  61. */
  62. rl_zero.rlim_cur = rl_zero.rlim_max = 0;
  63. if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
  64. fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
  65. __func__, strerror(errno));
  66. if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
  67. fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
  68. __func__, strerror(errno));
  69. if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
  70. fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
  71. __func__, strerror(errno));
  72. }
  73. #pragma clang diagnostic pop
  74. void
  75. ssh_sandbox_parent_finish(struct ssh_sandbox *box)
  76. {
  77. free(box);
  78. debug3("%s: finished", __func__);
  79. }
  80. void
  81. ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
  82. {
  83. box->child_pid = child_pid;
  84. }
  85. #endif /* SANDBOX_DARWIN */