xref: /dflybsd-src/crypto/openssh/sandbox-capsicum.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /*
2*ba1276acSMatthew Dillon  * Copyright (c) 2011 Dag-Erling Smorgrav
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_CAPSICUM
20*ba1276acSMatthew Dillon 
21*ba1276acSMatthew Dillon #include <sys/types.h>
22*ba1276acSMatthew Dillon #include <sys/time.h>
23*ba1276acSMatthew Dillon #include <sys/resource.h>
24*ba1276acSMatthew Dillon #include <sys/capsicum.h>
25*ba1276acSMatthew Dillon 
26*ba1276acSMatthew Dillon #include <errno.h>
27*ba1276acSMatthew Dillon #include <stdarg.h>
28*ba1276acSMatthew Dillon #include <stdio.h>
29*ba1276acSMatthew Dillon #include <stdlib.h>
30*ba1276acSMatthew Dillon #include <string.h>
31*ba1276acSMatthew Dillon #include <unistd.h>
32*ba1276acSMatthew Dillon #ifdef HAVE_CAPSICUM_HELPERS_H
33*ba1276acSMatthew Dillon #include <capsicum_helpers.h>
34*ba1276acSMatthew Dillon #endif
35*ba1276acSMatthew Dillon 
36*ba1276acSMatthew Dillon #include "log.h"
37*ba1276acSMatthew Dillon #include "monitor.h"
38*ba1276acSMatthew Dillon #include "ssh-sandbox.h"
39*ba1276acSMatthew Dillon #include "xmalloc.h"
40*ba1276acSMatthew Dillon 
41*ba1276acSMatthew Dillon /*
42*ba1276acSMatthew Dillon  * Capsicum sandbox that sets zero nfiles, nprocs and filesize rlimits,
43*ba1276acSMatthew Dillon  * limits rights on stdout, stdin, stderr, monitor and switches to
44*ba1276acSMatthew Dillon  * capability mode.
45*ba1276acSMatthew Dillon  */
46*ba1276acSMatthew Dillon 
47*ba1276acSMatthew Dillon struct ssh_sandbox {
48*ba1276acSMatthew Dillon 	struct monitor *monitor;
49*ba1276acSMatthew Dillon 	pid_t child_pid;
50*ba1276acSMatthew Dillon };
51*ba1276acSMatthew Dillon 
52*ba1276acSMatthew Dillon struct ssh_sandbox *
ssh_sandbox_init(struct monitor * monitor)53*ba1276acSMatthew Dillon ssh_sandbox_init(struct monitor *monitor)
54*ba1276acSMatthew Dillon {
55*ba1276acSMatthew Dillon 	struct ssh_sandbox *box;
56*ba1276acSMatthew Dillon 
57*ba1276acSMatthew Dillon 	/*
58*ba1276acSMatthew Dillon 	 * Strictly, we don't need to maintain any state here but we need
59*ba1276acSMatthew Dillon 	 * to return non-NULL to satisfy the API.
60*ba1276acSMatthew Dillon 	 */
61*ba1276acSMatthew Dillon 	debug3("%s: preparing capsicum sandbox", __func__);
62*ba1276acSMatthew Dillon 	box = xcalloc(1, sizeof(*box));
63*ba1276acSMatthew Dillon 	box->monitor = monitor;
64*ba1276acSMatthew Dillon 	box->child_pid = 0;
65*ba1276acSMatthew Dillon 
66*ba1276acSMatthew Dillon 	return box;
67*ba1276acSMatthew Dillon }
68*ba1276acSMatthew Dillon 
69*ba1276acSMatthew Dillon void
ssh_sandbox_child(struct ssh_sandbox * box)70*ba1276acSMatthew Dillon ssh_sandbox_child(struct ssh_sandbox *box)
71*ba1276acSMatthew Dillon {
72*ba1276acSMatthew Dillon 	struct rlimit rl_zero;
73*ba1276acSMatthew Dillon 	cap_rights_t rights;
74*ba1276acSMatthew Dillon 
75*ba1276acSMatthew Dillon #ifdef HAVE_CAPH_CACHE_TZDATA
76*ba1276acSMatthew Dillon 	caph_cache_tzdata();
77*ba1276acSMatthew Dillon #endif
78*ba1276acSMatthew Dillon 
79*ba1276acSMatthew Dillon 	rl_zero.rlim_cur = rl_zero.rlim_max = 0;
80*ba1276acSMatthew Dillon 
81*ba1276acSMatthew Dillon 	if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
82*ba1276acSMatthew Dillon 		fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
83*ba1276acSMatthew Dillon 			__func__, strerror(errno));
84*ba1276acSMatthew Dillon #ifndef SANDBOX_SKIP_RLIMIT_NOFILE
85*ba1276acSMatthew Dillon 	if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
86*ba1276acSMatthew Dillon 		fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
87*ba1276acSMatthew Dillon 			__func__, strerror(errno));
88*ba1276acSMatthew Dillon #endif
89*ba1276acSMatthew Dillon 	if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
90*ba1276acSMatthew Dillon 		fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
91*ba1276acSMatthew Dillon 			__func__, strerror(errno));
92*ba1276acSMatthew Dillon 
93*ba1276acSMatthew Dillon 	cap_rights_init(&rights);
94*ba1276acSMatthew Dillon 
95*ba1276acSMatthew Dillon 	if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
96*ba1276acSMatthew Dillon 		fatal("can't limit stdin: %m");
97*ba1276acSMatthew Dillon 	if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS)
98*ba1276acSMatthew Dillon 		fatal("can't limit stdout: %m");
99*ba1276acSMatthew Dillon 	if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS)
100*ba1276acSMatthew Dillon 		fatal("can't limit stderr: %m");
101*ba1276acSMatthew Dillon 
102*ba1276acSMatthew Dillon 	cap_rights_init(&rights, CAP_READ, CAP_WRITE);
103*ba1276acSMatthew Dillon 	if (cap_rights_limit(box->monitor->m_recvfd, &rights) < 0 &&
104*ba1276acSMatthew Dillon 	    errno != ENOSYS)
105*ba1276acSMatthew Dillon 		fatal("%s: failed to limit the network socket", __func__);
106*ba1276acSMatthew Dillon 	cap_rights_init(&rights, CAP_WRITE);
107*ba1276acSMatthew Dillon 	if (cap_rights_limit(box->monitor->m_log_sendfd, &rights) < 0 &&
108*ba1276acSMatthew Dillon 	    errno != ENOSYS)
109*ba1276acSMatthew Dillon 		fatal("%s: failed to limit the logging socket", __func__);
110*ba1276acSMatthew Dillon 	if (cap_enter() < 0 && errno != ENOSYS)
111*ba1276acSMatthew Dillon 		fatal("%s: failed to enter capability mode", __func__);
112*ba1276acSMatthew Dillon 
113*ba1276acSMatthew Dillon }
114*ba1276acSMatthew Dillon 
115*ba1276acSMatthew Dillon void
ssh_sandbox_parent_finish(struct ssh_sandbox * box)116*ba1276acSMatthew Dillon ssh_sandbox_parent_finish(struct ssh_sandbox *box)
117*ba1276acSMatthew Dillon {
118*ba1276acSMatthew Dillon 	free(box);
119*ba1276acSMatthew Dillon 	debug3("%s: finished", __func__);
120*ba1276acSMatthew Dillon }
121*ba1276acSMatthew Dillon 
122*ba1276acSMatthew Dillon void
ssh_sandbox_parent_preauth(struct ssh_sandbox * box,pid_t child_pid)123*ba1276acSMatthew Dillon ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
124*ba1276acSMatthew Dillon {
125*ba1276acSMatthew Dillon 	box->child_pid = child_pid;
126*ba1276acSMatthew Dillon }
127*ba1276acSMatthew Dillon 
128*ba1276acSMatthew Dillon #endif /* SANDBOX_CAPSICUM */
129