|
Added
Link Here
|
| 1 |
/* |
| 2 |
* Copyright (c) 2011 Dag-Erling Smorgrav |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ "includes.h" |
| 16 |
|
| 17 |
#ifdef SANDBOX_CAPSICUM |
| 18 |
|
| 19 |
#include <sys/types.h> |
| 20 |
#include <sys/param.h> |
| 21 |
#include <sys/time.h> |
| 22 |
#include <sys/resource.h> |
| 23 |
#include <sys/capability.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 "monitor.h" |
| 34 |
#include "ssh-sandbox.h" |
| 35 |
#include "xmalloc.h" |
| 36 |
|
| 37 |
/* |
| 38 |
* Capsicum sandbox that sets zero nfiles, nprocs and filesize rlimits, |
| 39 |
* limits file descriptors on monitoring object, |
| 40 |
* and switches to capability mode |
| 41 |
*/ |
| 42 |
|
| 43 |
struct ssh_sandbox { |
| 44 |
pid_t child_pid; |
| 45 |
}; |
| 46 |
|
| 47 |
struct ssh_sandbox * |
| 48 |
ssh_sandbox_init(void) |
| 49 |
{ |
| 50 |
struct ssh_sandbox *box; |
| 51 |
|
| 52 |
/* |
| 53 |
* Strictly, we don't need to maintain any state here but we need |
| 54 |
* to return non-NULL to satisfy the API. |
| 55 |
*/ |
| 56 |
debug3("%s: preparing capsicum sandbox", __func__); |
| 57 |
box = xcalloc(1, sizeof(*box)); |
| 58 |
box->child_pid = 0; |
| 59 |
|
| 60 |
return box; |
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
ssh_sandbox_child(struct ssh_sandbox *box) |
| 65 |
{ |
| 66 |
struct rlimit rl_zero; |
| 67 |
|
| 68 |
rl_zero.rlim_cur = rl_zero.rlim_max = 0; |
| 69 |
|
| 70 |
if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1) |
| 71 |
fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s", |
| 72 |
__func__, strerror(errno)); |
| 73 |
if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1) |
| 74 |
fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s", |
| 75 |
__func__, strerror(errno)); |
| 76 |
if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1) |
| 77 |
fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s", |
| 78 |
__func__, strerror(errno)); |
| 79 |
if (cap_rights_limit(box->monitor->m_recvfd, CAP_READ | CAP_WRITE) == -1) |
| 80 |
fatal("%s: failed to limit the network socket", __func__); |
| 81 |
if (cap_rights_limit(box->monitor->m_log_sendfd, CAP_WRITE) == -1) |
| 82 |
fatal("%s: failed to limit the logging socket", __func__); |
| 83 |
if (cap_rights_limit(STDIN_FILENO, CAP_NONE) < 0 && errno != ENOSYS) |
| 84 |
error("can't limit stdio: %m"); |
| 85 |
if (cap_rights_limit(STDOUT_FILENO, CAP_NONE) < 0 && errno != ENOSYS) |
| 86 |
error("can't limit stdio: %m"); |
| 87 |
if (cap_rights_limit(STDERR_FILENO, CAP_NONE) < 0 && errno != ENOSYS) |
| 88 |
error("can't limit stdio: %m"); |
| 89 |
if (cap_enter() != 0) |
| 90 |
fatal("%s: failed to enter capability mode", __func__); |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
ssh_sandbox_parent_finish(struct ssh_sandbox *box) |
| 95 |
{ |
| 96 |
free(box); |
| 97 |
debug3("%s: finished", __func__); |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) |
| 102 |
{ |
| 103 |
box->child_pid = child_pid; |
| 104 |
} |
| 105 |
|
| 106 |
#endif /* SANDBOX_CAPSICUM */ |