|
Line 0
Link Here
|
|
|
1 |
/* $Id: sandbox-selinux.c,v 1.0 2011/01/17 10:15:30 jfch Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2011 Red Hat, Inc. All rights reserved. |
| 5 |
* Use is subject to license terms. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
* |
| 27 |
* Red Hat author: Jan F. Chadima <jchadima@redhat.com> |
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
#include "includes.h" |
| 32 |
|
| 33 |
#ifdef SANDBOX_SELINUX |
| 34 |
|
| 35 |
#include <sys/types.h> |
| 36 |
|
| 37 |
#include <errno.h> |
| 38 |
#include <stdarg.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
#include <unistd.h> |
| 43 |
#include <sys/resource.h> |
| 44 |
|
| 45 |
#include "log.h" |
| 46 |
#include "ssh-sandbox.h" |
| 47 |
#include "xmalloc.h" |
| 48 |
#include "openbsd-compat/port-linux.h" |
| 49 |
|
| 50 |
/* selinux based sandbox */ |
| 51 |
|
| 52 |
struct ssh_sandbox { |
| 53 |
pid_t child_pid; |
| 54 |
}; |
| 55 |
|
| 56 |
struct ssh_sandbox * |
| 57 |
ssh_sandbox_init(void) |
| 58 |
{ |
| 59 |
struct ssh_sandbox *box; |
| 60 |
|
| 61 |
/* |
| 62 |
* Strictly, we don't need to maintain any state here but we need |
| 63 |
* to return non-NULL to satisfy the API. |
| 64 |
*/ |
| 65 |
debug3("selinux sandbox init"); |
| 66 |
box = xcalloc(1, sizeof(*box)); |
| 67 |
box->child_pid = 0; |
| 68 |
return box; |
| 69 |
} |
| 70 |
|
| 71 |
void |
| 72 |
ssh_sandbox_child(struct ssh_sandbox *box) |
| 73 |
{ |
| 74 |
struct rlimit rl_zero; |
| 75 |
|
| 76 |
rl_zero.rlim_cur = rl_zero.rlim_max = 0; |
| 77 |
|
| 78 |
if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1) |
| 79 |
fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s", |
| 80 |
__func__, strerror(errno)); |
| 81 |
if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1) |
| 82 |
fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s", |
| 83 |
__func__, strerror(errno)); |
| 84 |
#ifdef HAVE_RLIMIT_NPROC |
| 85 |
if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1) |
| 86 |
fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s", |
| 87 |
__func__, strerror(errno)); |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
void |
| 92 |
ssh_sandbox_privileged_child(struct ssh_sandbox *box) |
| 93 |
{ |
| 94 |
switch (ssh_selinux_change_context("sshd_sandbox_t")) { |
| 95 |
case 0: |
| 96 |
debug3("selinux sandbox child sucessfully enabled"); |
| 97 |
break; |
| 98 |
case -2: |
| 99 |
logit("selinux sandbox not useful"); |
| 100 |
break; |
| 101 |
case -1: |
| 102 |
fatal("cannot set up selinux sandbox"); |
| 103 |
default: |
| 104 |
fatal("inmternal error in selinux sandbox"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
ssh_sandbox_parent_finish(struct ssh_sandbox *box) |
| 110 |
{ |
| 111 |
free(box); |
| 112 |
debug3("%s: finished", __func__); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) |
| 117 |
{ |
| 118 |
debug3("selinux sandbox parent sucessfully enabled"); |
| 119 |
box->child_pid = child_pid; |
| 120 |
} |
| 121 |
|
| 122 |
#endif /* SANDBOX_NULL */ |