1323134Sdes/* $OpenBSD: sandbox-rlimit.c,v 1.4 2016/09/12 01:22:38 deraadt Exp $ */
2225825Sdes/*
3225825Sdes * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
4225825Sdes *
5225825Sdes * Permission to use, copy, modify, and distribute this software for any
6225825Sdes * purpose with or without fee is hereby granted, provided that the above
7225825Sdes * copyright notice and this permission notice appear in all copies.
8225825Sdes *
9225825Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10225825Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11225825Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12225825Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13225825Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14225825Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15225825Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16225825Sdes */
17225825Sdes
18225825Sdes#include "includes.h"
19225825Sdes
20225825Sdes#ifdef SANDBOX_RLIMIT
21225825Sdes
22225825Sdes#include <sys/types.h>
23225825Sdes#include <sys/time.h>
24225825Sdes#include <sys/resource.h>
25225825Sdes
26225825Sdes#include <errno.h>
27225825Sdes#include <stdarg.h>
28225825Sdes#include <stdio.h>
29225825Sdes#include <stdlib.h>
30225825Sdes#include <string.h>
31225825Sdes#include <unistd.h>
32225825Sdes
33225825Sdes#include "log.h"
34225825Sdes#include "ssh-sandbox.h"
35225825Sdes#include "xmalloc.h"
36225825Sdes
37225825Sdes/* Minimal sandbox that sets zero nfiles, nprocs and filesize rlimits */
38225825Sdes
39225825Sdesstruct ssh_sandbox {
40225825Sdes	pid_t child_pid;
41225825Sdes};
42225825Sdes
43225825Sdesstruct ssh_sandbox *
44261320Sdesssh_sandbox_init(struct monitor *monitor)
45225825Sdes{
46225825Sdes	struct ssh_sandbox *box;
47225825Sdes
48225825Sdes	/*
49225825Sdes	 * Strictly, we don't need to maintain any state here but we need
50225825Sdes	 * to return non-NULL to satisfy the API.
51225825Sdes	 */
52225825Sdes	debug3("%s: preparing rlimit sandbox", __func__);
53225825Sdes	box = xcalloc(1, sizeof(*box));
54225825Sdes	box->child_pid = 0;
55225825Sdes
56225825Sdes	return box;
57225825Sdes}
58225825Sdes
59225825Sdesvoid
60225825Sdesssh_sandbox_child(struct ssh_sandbox *box)
61225825Sdes{
62225825Sdes	struct rlimit rl_zero;
63225825Sdes
64225825Sdes	rl_zero.rlim_cur = rl_zero.rlim_max = 0;
65225825Sdes
66240075Sdes#ifndef SANDBOX_SKIP_RLIMIT_FSIZE
67225825Sdes	if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
68225825Sdes		fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
69225825Sdes			__func__, strerror(errno));
70240075Sdes#endif
71261320Sdes#ifndef SANDBOX_SKIP_RLIMIT_NOFILE
72225825Sdes	if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
73225825Sdes		fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
74225825Sdes			__func__, strerror(errno));
75261320Sdes#endif
76225825Sdes#ifdef HAVE_RLIMIT_NPROC
77225825Sdes	if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
78225825Sdes		fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
79225825Sdes			__func__, strerror(errno));
80225825Sdes#endif
81225825Sdes}
82225825Sdes
83225825Sdesvoid
84225825Sdesssh_sandbox_parent_finish(struct ssh_sandbox *box)
85225825Sdes{
86225825Sdes	free(box);
87225825Sdes	debug3("%s: finished", __func__);
88225825Sdes}
89225825Sdes
90225825Sdesvoid
91225825Sdesssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
92225825Sdes{
93225825Sdes	box->child_pid = child_pid;
94225825Sdes}
95225825Sdes
96225825Sdes#endif /* SANDBOX_RLIMIT */
97