2 * Copyright (c) 2011 Dag-Erling Smorgrav
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.
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.
20 #ifdef SANDBOX_CAPSICUM
22 #include <sys/types.h>
23 #include <sys/param.h>
25 #include <sys/resource.h>
26 #include <sys/capsicum.h>
34 #include <capsicum_helpers.h>
38 #include "ssh-sandbox.h"
42 * Capsicum sandbox that sets zero nfiles, nprocs and filesize rlimits,
43 * limits rights on stdout, stdin, stderr, monitor and switches to
48 struct monitor *monitor;
53 ssh_sandbox_init(struct monitor *monitor)
55 struct ssh_sandbox *box;
58 * Strictly, we don't need to maintain any state here but we need
59 * to return non-NULL to satisfy the API.
61 debug3("%s: preparing capsicum sandbox", __func__);
62 box = xcalloc(1, sizeof(*box));
63 box->monitor = monitor;
70 ssh_sandbox_child(struct ssh_sandbox *box)
72 struct rlimit rl_zero;
77 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 #ifndef SANDBOX_SKIP_RLIMIT_NOFILE
83 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
84 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
85 __func__, strerror(errno));
87 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
88 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
89 __func__, strerror(errno));
91 cap_rights_init(&rights);
93 if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
94 fatal("can't limit stdin: %m");
95 if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS)
96 fatal("can't limit stdout: %m");
97 if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS)
98 fatal("can't limit stderr: %m");
100 cap_rights_init(&rights, CAP_READ, CAP_WRITE);
101 if (cap_rights_limit(box->monitor->m_recvfd, &rights) < 0 &&
103 fatal("%s: failed to limit the network socket", __func__);
104 cap_rights_init(&rights, CAP_WRITE);
105 if (cap_rights_limit(box->monitor->m_log_sendfd, &rights) < 0 &&
107 fatal("%s: failed to limit the logging socket", __func__);
108 if (cap_enter() < 0 && errno != ENOSYS)
109 fatal("%s: failed to enter capability mode", __func__);
114 ssh_sandbox_parent_finish(struct ssh_sandbox *box)
117 debug3("%s: finished", __func__);
121 ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
123 box->child_pid = child_pid;
126 #endif /* SANDBOX_CAPSICUM */