xref: /netbsd-src/crypto/external/bsd/openssh/dist/sandbox-rlimit.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $OpenBSD: sandbox-rlimit.c,v 1.3 2011/06/23 09:34:13 djm Exp $ */
2 /*
3  * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "includes.h"
19 __RCSID("$NetBSD: sandbox-rlimit.c,v 1.2 2011/09/07 17:49:19 christos Exp $");
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/time.h>
23 #include <sys/resource.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 "xmalloc.h"
35 
36 /* Minimal sandbox that sets zero nfiles, nprocs and filesize rlimits */
37 
38 struct ssh_sandbox {
39 	pid_t child_pid;
40 };
41 
42 struct ssh_sandbox *
43 ssh_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 rlimit sandbox", __func__);
52 	box = xcalloc(1, sizeof(*box));
53 	box->child_pid = 0;
54 
55 	return box;
56 }
57 
58 void
59 ssh_sandbox_child(struct ssh_sandbox *box)
60 {
61 	struct rlimit rl_zero;
62 
63 	rl_zero.rlim_cur = rl_zero.rlim_max = 0;
64 
65 	if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
66 		fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
67 			__func__, strerror(errno));
68 	if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
69 		fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
70 			__func__, strerror(errno));
71 	if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
72 		fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
73 			__func__, strerror(errno));
74 }
75 
76 void
77 ssh_sandbox_parent_finish(struct ssh_sandbox *box)
78 {
79 	free(box);
80 	debug3("%s: finished", __func__);
81 }
82 
83 void
84 ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
85 {
86 	box->child_pid = child_pid;
87 	/* Nothing to do here */
88 }
89 
90