sandbox-rlimit.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* $OpenBSD: sandbox-rlimit.c,v 1.4 2016/09/12 01:22:38 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_RLIMIT
  19. #include <sys/types.h>
  20. #include <sys/time.h>
  21. #include <sys/resource.h>
  22. #include <errno.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include "log.h"
  29. #include "ssh-sandbox.h"
  30. #include "xmalloc.h"
  31. /* Minimal sandbox that sets zero nfiles, nprocs and filesize rlimits */
  32. struct ssh_sandbox {
  33. pid_t child_pid;
  34. };
  35. struct ssh_sandbox *
  36. ssh_sandbox_init(struct monitor *monitor)
  37. {
  38. struct ssh_sandbox *box;
  39. /*
  40. * Strictly, we don't need to maintain any state here but we need
  41. * to return non-NULL to satisfy the API.
  42. */
  43. debug3("%s: preparing rlimit sandbox", __func__);
  44. box = xcalloc(1, sizeof(*box));
  45. box->child_pid = 0;
  46. return box;
  47. }
  48. void
  49. ssh_sandbox_child(struct ssh_sandbox *box)
  50. {
  51. struct rlimit rl_zero;
  52. rl_zero.rlim_cur = rl_zero.rlim_max = 0;
  53. #ifndef SANDBOX_SKIP_RLIMIT_FSIZE
  54. if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
  55. fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
  56. __func__, strerror(errno));
  57. #endif
  58. #ifndef SANDBOX_SKIP_RLIMIT_NOFILE
  59. if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
  60. fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
  61. __func__, strerror(errno));
  62. #endif
  63. #ifdef HAVE_RLIMIT_NPROC
  64. if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
  65. fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
  66. __func__, strerror(errno));
  67. #endif
  68. }
  69. void
  70. ssh_sandbox_parent_finish(struct ssh_sandbox *box)
  71. {
  72. free(box);
  73. debug3("%s: finished", __func__);
  74. }
  75. void
  76. ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
  77. {
  78. box->child_pid = child_pid;
  79. }
  80. #endif /* SANDBOX_RLIMIT */