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
17 #include "includes.h"
18
19 #ifdef SANDBOX_DARWIN
20
21 #include <sys/types.h>
22
23 #include <sandbox.h>
24
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "log.h"
33 #include "ssh-sandbox.h"
34 #include "monitor.h"
35 #include "xmalloc.h"
36
37 /* Darwin/OS X sandbox */
38
39 struct ssh_sandbox {
40 pid_t child_pid;
41 };
42
43 struct ssh_sandbox *
ssh_sandbox_init(struct monitor * monitor)44 ssh_sandbox_init(struct monitor *monitor)
45 {
46 struct ssh_sandbox *box;
47
48 /*
49 * Strictly, we don't need to maintain any state here but we need
50 * to return non-NULL to satisfy the API.
51 */
52 debug3("%s: preparing Darwin sandbox", __func__);
53 box = xcalloc(1, sizeof(*box));
54 box->child_pid = 0;
55
56 return box;
57 }
58
59 void
ssh_sandbox_child(struct ssh_sandbox * box)60 ssh_sandbox_child(struct ssh_sandbox *box)
61 {
62 char *errmsg;
63 struct rlimit rl_zero;
64
65 debug3("%s: starting Darwin sandbox", __func__);
66 if (sandbox_init(kSBXProfilePureComputation, SANDBOX_NAMED,
67 &errmsg) == -1)
68 fatal("%s: sandbox_init: %s", __func__, errmsg);
69
70 /*
71 * The kSBXProfilePureComputation still allows sockets, so
72 * we must disable these using rlimit.
73 */
74 rl_zero.rlim_cur = rl_zero.rlim_max = 0;
75 if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
76 fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
77 __func__, strerror(errno));
78 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
79 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
80 __func__, strerror(errno));
81 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
82 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
83 __func__, strerror(errno));
84 }
85
86 void
ssh_sandbox_parent_finish(struct ssh_sandbox * box)87 ssh_sandbox_parent_finish(struct ssh_sandbox *box)
88 {
89 free(box);
90 debug3("%s: finished", __func__);
91 }
92
93 void
ssh_sandbox_parent_preauth(struct ssh_sandbox * box,pid_t child_pid)94 ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
95 {
96 box->child_pid = child_pid;
97 }
98
99 #endif /* SANDBOX_DARWIN */
100