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 */ 16 17 #include "includes.h" 18 __RCSID("$FreeBSD$"); 19 20 #ifdef SANDBOX_CAPSICUM 21 22 #include <sys/types.h> 23 #include <sys/time.h> 24 #include <sys/resource.h> 25 #include <sys/capsicum.h> 26 27 #include <errno.h> 28 #include <stdarg.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <capsicum_helpers.h> 34 35 #include "log.h" 36 #include "monitor.h" 37 #include "ssh-sandbox.h" 38 #include "xmalloc.h" 39 40 /* 41 * Capsicum sandbox that sets zero nfiles, nprocs and filesize rlimits, 42 * limits rights on stdout, stdin, stderr, monitor and switches to 43 * capability mode. 44 */ 45 46 struct ssh_sandbox { 47 struct monitor *monitor; 48 pid_t child_pid; 49 }; 50 51 struct ssh_sandbox * 52 ssh_sandbox_init(struct monitor *monitor) 53 { 54 struct ssh_sandbox *box; 55 56 /* 57 * Strictly, we don't need to maintain any state here but we need 58 * to return non-NULL to satisfy the API. 59 */ 60 debug3("%s: preparing capsicum sandbox", __func__); 61 box = xcalloc(1, sizeof(*box)); 62 box->monitor = monitor; 63 box->child_pid = 0; 64 65 return box; 66 } 67 68 void 69 ssh_sandbox_child(struct ssh_sandbox *box) 70 { 71 struct rlimit rl_zero; 72 cap_rights_t rights; 73 74 caph_cache_tzdata(); 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 #ifndef SANDBOX_SKIP_RLIMIT_NOFILE 82 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1) 83 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s", 84 __func__, strerror(errno)); 85 #endif 86 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1) 87 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s", 88 __func__, strerror(errno)); 89 90 cap_rights_init(&rights); 91 92 if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS) 93 fatal("can't limit stdin: %m"); 94 if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS) 95 fatal("can't limit stdout: %m"); 96 if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) 97 fatal("can't limit stderr: %m"); 98 99 cap_rights_init(&rights, CAP_READ, CAP_WRITE); 100 if (cap_rights_limit(box->monitor->m_recvfd, &rights) < 0 && 101 errno != ENOSYS) 102 fatal("%s: failed to limit the network socket", __func__); 103 cap_rights_init(&rights, CAP_WRITE); 104 if (cap_rights_limit(box->monitor->m_log_sendfd, &rights) < 0 && 105 errno != ENOSYS) 106 fatal("%s: failed to limit the logging socket", __func__); 107 if (cap_enter() < 0 && errno != ENOSYS) 108 fatal("%s: failed to enter capability mode", __func__); 109 110 } 111 112 void 113 ssh_sandbox_parent_finish(struct ssh_sandbox *box) 114 { 115 free(box); 116 debug3("%s: finished", __func__); 117 } 118 119 void 120 ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) 121 { 122 box->child_pid = child_pid; 123 } 124 125 #endif /* SANDBOX_CAPSICUM */ 126