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