1225825Sdes/* $OpenBSD: sandbox-rlimit.c,v 1.3 2011/06/23 09:34:13 djm 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/param.h> 24225825Sdes#include <sys/time.h> 25225825Sdes#include <sys/resource.h> 26225825Sdes 27225825Sdes#include <errno.h> 28225825Sdes#include <stdarg.h> 29225825Sdes#include <stdio.h> 30225825Sdes#include <stdlib.h> 31225825Sdes#include <string.h> 32225825Sdes#include <unistd.h> 33225825Sdes 34225825Sdes#include "log.h" 35225825Sdes#include "ssh-sandbox.h" 36225825Sdes#include "xmalloc.h" 37225825Sdes 38225825Sdes/* Minimal sandbox that sets zero nfiles, nprocs and filesize rlimits */ 39225825Sdes 40225825Sdesstruct ssh_sandbox { 41225825Sdes pid_t child_pid; 42225825Sdes}; 43225825Sdes 44225825Sdesstruct ssh_sandbox * 45262566Sdesssh_sandbox_init(struct monitor *monitor) 46225825Sdes{ 47225825Sdes struct ssh_sandbox *box; 48225825Sdes 49225825Sdes /* 50225825Sdes * Strictly, we don't need to maintain any state here but we need 51225825Sdes * to return non-NULL to satisfy the API. 52225825Sdes */ 53225825Sdes debug3("%s: preparing rlimit sandbox", __func__); 54225825Sdes box = xcalloc(1, sizeof(*box)); 55225825Sdes box->child_pid = 0; 56225825Sdes 57225825Sdes return box; 58225825Sdes} 59225825Sdes 60225825Sdesvoid 61225825Sdesssh_sandbox_child(struct ssh_sandbox *box) 62225825Sdes{ 63225825Sdes struct rlimit rl_zero; 64225825Sdes 65225825Sdes rl_zero.rlim_cur = rl_zero.rlim_max = 0; 66225825Sdes 67240075Sdes#ifndef SANDBOX_SKIP_RLIMIT_FSIZE 68225825Sdes if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1) 69225825Sdes fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s", 70225825Sdes __func__, strerror(errno)); 71240075Sdes#endif 72262566Sdes#ifndef SANDBOX_SKIP_RLIMIT_NOFILE 73225825Sdes if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1) 74225825Sdes fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s", 75225825Sdes __func__, strerror(errno)); 76262566Sdes#endif 77225825Sdes#ifdef HAVE_RLIMIT_NPROC 78225825Sdes if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1) 79225825Sdes fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s", 80225825Sdes __func__, strerror(errno)); 81225825Sdes#endif 82225825Sdes} 83225825Sdes 84225825Sdesvoid 85225825Sdesssh_sandbox_parent_finish(struct ssh_sandbox *box) 86225825Sdes{ 87225825Sdes free(box); 88225825Sdes debug3("%s: finished", __func__); 89225825Sdes} 90225825Sdes 91225825Sdesvoid 92225825Sdesssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) 93225825Sdes{ 94225825Sdes box->child_pid = child_pid; 95225825Sdes} 96225825Sdes 97225825Sdes#endif /* SANDBOX_RLIMIT */ 98