sandbox-capsicum.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2011 Dag-Erling Smorgrav
  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_CAPSICUM
  18. #include <sys/types.h>
  19. #include <sys/param.h>
  20. #include <sys/time.h>
  21. #include <sys/resource.h>
  22. #include <sys/capsicum.h>
  23. #include <errno.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "log.h"
  30. #include "monitor.h"
  31. #include "ssh-sandbox.h"
  32. #include "xmalloc.h"
  33. /*
  34. * Capsicum sandbox that sets zero nfiles, nprocs and filesize rlimits,
  35. * limits rights on stdout, stdin, stderr, monitor and switches to
  36. * capability mode.
  37. */
  38. struct ssh_sandbox {
  39. struct monitor *monitor;
  40. pid_t child_pid;
  41. };
  42. struct ssh_sandbox *
  43. ssh_sandbox_init(struct monitor *monitor)
  44. {
  45. struct ssh_sandbox *box;
  46. /*
  47. * Strictly, we don't need to maintain any state here but we need
  48. * to return non-NULL to satisfy the API.
  49. */
  50. debug3("%s: preparing capsicum sandbox", __func__);
  51. box = xcalloc(1, sizeof(*box));
  52. box->monitor = monitor;
  53. box->child_pid = 0;
  54. return box;
  55. }
  56. void
  57. ssh_sandbox_child(struct ssh_sandbox *box)
  58. {
  59. struct rlimit rl_zero;
  60. cap_rights_t rights;
  61. rl_zero.rlim_cur = rl_zero.rlim_max = 0;
  62. if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
  63. fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
  64. __func__, strerror(errno));
  65. #ifndef SANDBOX_SKIP_RLIMIT_NOFILE
  66. if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
  67. fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
  68. __func__, strerror(errno));
  69. #endif
  70. if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
  71. fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
  72. __func__, strerror(errno));
  73. cap_rights_init(&rights);
  74. if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
  75. fatal("can't limit stdin: %m");
  76. if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS)
  77. fatal("can't limit stdout: %m");
  78. if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS)
  79. fatal("can't limit stderr: %m");
  80. cap_rights_init(&rights, CAP_READ, CAP_WRITE);
  81. if (cap_rights_limit(box->monitor->m_recvfd, &rights) < 0 &&
  82. errno != ENOSYS)
  83. fatal("%s: failed to limit the network socket", __func__);
  84. cap_rights_init(&rights, CAP_WRITE);
  85. if (cap_rights_limit(box->monitor->m_log_sendfd, &rights) < 0 &&
  86. errno != ENOSYS)
  87. fatal("%s: failed to limit the logging socket", __func__);
  88. if (cap_enter() < 0 && errno != ENOSYS)
  89. fatal("%s: failed to enter capability mode", __func__);
  90. }
  91. void
  92. ssh_sandbox_parent_finish(struct ssh_sandbox *box)
  93. {
  94. free(box);
  95. debug3("%s: finished", __func__);
  96. }
  97. void
  98. ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
  99. {
  100. box->child_pid = child_pid;
  101. }
  102. #endif /* SANDBOX_CAPSICUM */