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 "sandbox.h"
34#include "xmalloc.h"
35
36/* Darwin/OS X sandbox */
37
38struct ssh_sandbox {
39	pid_t child_pid;
40};
41
42struct ssh_sandbox *
43ssh_sandbox_init(void)
44{
45	struct ssh_sandbox *box;
46
47	/*
48	 * Strictly, we don't need to maintain any state here but we need
49	 * to return non-NULL to satisfy the API.
50	 */
51	debug3("%s: preparing Darwin sandbox", __func__);
52	box = xcalloc(1, sizeof(*box));
53	box->child_pid = 0;
54
55	return box;
56}
57
58void
59ssh_sandbox_child(struct ssh_sandbox *box)
60{
61	char *errmsg;
62	struct rlimit rl_zero;
63
64	debug3("%s: starting Darwin sandbox", __func__);
65#ifdef __APPLE_SANDBOX_NAMED_EXTERNAL__
66	if (sandbox_init("/System/Library/Sandbox/Profiles/org.openssh.sshd.sb",
67		SANDBOX_NAMED_EXTERNAL, &errmsg) == -1)
68#else
69	if (sandbox_init(kSBXProfilePureComputation, SANDBOX_NAMED,
70	    &errmsg) == -1)
71#endif
72		fatal("%s: sandbox_init: %s", __func__, errmsg);
73
74	/*
75	 * The kSBXProfilePureComputation still allows sockets, so
76	 * we must disable these using rlimit.
77	 */
78	rl_zero.rlim_cur = rl_zero.rlim_max = 0;
79	if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
80		fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
81			__func__, strerror(errno));
82	if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
83		fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
84			__func__, strerror(errno));
85	if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
86		fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
87			__func__, strerror(errno));
88}
89
90void
91ssh_sandbox_parent_finish(struct ssh_sandbox *box)
92{
93	free(box);
94	debug3("%s: finished", __func__);
95}
96
97void
98ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
99{
100	box->child_pid = child_pid;
101}
102
103#endif /* SANDBOX_DARWIN */
104