Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 Will Drewry <wad@dataspill.org> |
| 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 | /* |
| 18 | * Uncomment the SANDBOX_SECCOMP_FILTER_DEBUG macro below to help diagnose |
| 19 | * filter breakage during development. *Do not* use this in production, |
| 20 | * as it relies on making library calls that are unsafe in signal context. |
| 21 | * |
| 22 | * Instead, live systems the auditctl(8) may be used to monitor failures. |
| 23 | * E.g. |
| 24 | * auditctl -a task,always -F uid=<privsep uid> |
| 25 | */ |
| 26 | /* #define SANDBOX_SECCOMP_FILTER_DEBUG 1 */ |
| 27 | |
Damien Miller | 48abc47 | 2014-03-17 14:45:56 +1100 | [diff] [blame] | 28 | /* XXX it should be possible to do logging via the log socket safely */ |
| 29 | |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 30 | #ifdef SANDBOX_SECCOMP_FILTER_DEBUG |
| 31 | /* Use the kernel headers in case of an older toolchain. */ |
| 32 | # include <asm/siginfo.h> |
| 33 | # define __have_siginfo_t 1 |
| 34 | # define __have_sigval_t 1 |
| 35 | # define __have_sigevent_t 1 |
| 36 | #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */ |
| 37 | |
| 38 | #include "includes.h" |
| 39 | |
| 40 | #ifdef SANDBOX_SECCOMP_FILTER |
| 41 | |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/resource.h> |
| 44 | #include <sys/prctl.h> |
| 45 | |
| 46 | #include <linux/audit.h> |
| 47 | #include <linux/filter.h> |
| 48 | #include <linux/seccomp.h> |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 49 | #include <elf.h> |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 50 | |
| 51 | #include <asm/unistd.h> |
| 52 | |
| 53 | #include <errno.h> |
| 54 | #include <signal.h> |
| 55 | #include <stdarg.h> |
| 56 | #include <stddef.h> /* for offsetof */ |
| 57 | #include <stdio.h> |
| 58 | #include <stdlib.h> |
| 59 | #include <string.h> |
| 60 | #include <unistd.h> |
| 61 | |
| 62 | #include "log.h" |
| 63 | #include "ssh-sandbox.h" |
| 64 | #include "xmalloc.h" |
| 65 | |
| 66 | /* Linux seccomp_filter sandbox */ |
| 67 | #define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL |
| 68 | |
| 69 | /* Use a signal handler to emit violations when debugging */ |
| 70 | #ifdef SANDBOX_SECCOMP_FILTER_DEBUG |
| 71 | # undef SECCOMP_FILTER_FAIL |
| 72 | # define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP |
| 73 | #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */ |
| 74 | |
| 75 | /* Simple helpers to avoid manual errors (but larger BPF programs). */ |
| 76 | #define SC_DENY(_nr, _errno) \ |
| 77 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \ |
| 78 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno)) |
| 79 | #define SC_ALLOW(_nr) \ |
| 80 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \ |
| 81 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW) |
| 82 | |
| 83 | /* Syscall filtering set for preauth. */ |
| 84 | static const struct sock_filter preauth_insns[] = { |
| 85 | /* Ensure the syscall arch convention is as expected. */ |
| 86 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, |
| 87 | offsetof(struct seccomp_data, arch)), |
| 88 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0), |
| 89 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL), |
| 90 | /* Load the syscall number for checking. */ |
| 91 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, |
| 92 | offsetof(struct seccomp_data, nr)), |
| 93 | SC_DENY(open, EACCES), |
Damien Miller | 48abc47 | 2014-03-17 14:45:56 +1100 | [diff] [blame] | 94 | SC_DENY(stat, EACCES), |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 95 | SC_ALLOW(getpid), |
| 96 | SC_ALLOW(gettimeofday), |
Darren Tucker | e9887d1 | 2013-06-02 09:17:09 +1000 | [diff] [blame] | 97 | SC_ALLOW(clock_gettime), |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 98 | #ifdef __NR_time /* not defined on EABI ARM */ |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 99 | SC_ALLOW(time), |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 100 | #endif |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 101 | SC_ALLOW(read), |
| 102 | SC_ALLOW(write), |
| 103 | SC_ALLOW(close), |
Damien Miller | 6434cb2 | 2014-02-06 11:17:50 +1100 | [diff] [blame] | 104 | #ifdef __NR_shutdown /* not defined on archs that go via socketcall(2) */ |
Damien Miller | 7e5cec6 | 2014-01-31 09:25:34 +1100 | [diff] [blame] | 105 | SC_ALLOW(shutdown), |
Damien Miller | 6434cb2 | 2014-02-06 11:17:50 +1100 | [diff] [blame] | 106 | #endif |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 107 | SC_ALLOW(brk), |
| 108 | SC_ALLOW(poll), |
| 109 | #ifdef __NR__newselect |
| 110 | SC_ALLOW(_newselect), |
| 111 | #else |
| 112 | SC_ALLOW(select), |
| 113 | #endif |
| 114 | SC_ALLOW(madvise), |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 115 | #ifdef __NR_mmap2 /* EABI ARM only has mmap2() */ |
| 116 | SC_ALLOW(mmap2), |
| 117 | #endif |
| 118 | #ifdef __NR_mmap |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 119 | SC_ALLOW(mmap), |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 120 | #endif |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 121 | SC_ALLOW(munmap), |
| 122 | SC_ALLOW(exit_group), |
| 123 | #ifdef __NR_rt_sigprocmask |
| 124 | SC_ALLOW(rt_sigprocmask), |
| 125 | #else |
| 126 | SC_ALLOW(sigprocmask), |
| 127 | #endif |
| 128 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL), |
| 129 | }; |
| 130 | |
| 131 | static const struct sock_fprog preauth_program = { |
| 132 | .len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])), |
| 133 | .filter = (struct sock_filter *)preauth_insns, |
| 134 | }; |
| 135 | |
| 136 | struct ssh_sandbox { |
| 137 | pid_t child_pid; |
| 138 | }; |
| 139 | |
| 140 | struct ssh_sandbox * |
Damien Miller | 868ea1e | 2014-01-17 16:47:04 +1100 | [diff] [blame] | 141 | ssh_sandbox_init(struct monitor *monitor) |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 142 | { |
| 143 | struct ssh_sandbox *box; |
| 144 | |
| 145 | /* |
| 146 | * Strictly, we don't need to maintain any state here but we need |
| 147 | * to return non-NULL to satisfy the API. |
| 148 | */ |
| 149 | debug3("%s: preparing seccomp filter sandbox", __func__); |
| 150 | box = xcalloc(1, sizeof(*box)); |
| 151 | box->child_pid = 0; |
| 152 | |
| 153 | return box; |
| 154 | } |
| 155 | |
| 156 | #ifdef SANDBOX_SECCOMP_FILTER_DEBUG |
| 157 | extern struct monitor *pmonitor; |
| 158 | void mm_log_handler(LogLevel level, const char *msg, void *ctx); |
| 159 | |
| 160 | static void |
| 161 | ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context) |
| 162 | { |
| 163 | char msg[256]; |
| 164 | |
| 165 | snprintf(msg, sizeof(msg), |
| 166 | "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)", |
| 167 | __func__, info->si_arch, info->si_syscall, info->si_call_addr); |
| 168 | mm_log_handler(SYSLOG_LEVEL_FATAL, msg, pmonitor); |
| 169 | _exit(1); |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | ssh_sandbox_child_debugging(void) |
| 174 | { |
| 175 | struct sigaction act; |
| 176 | sigset_t mask; |
| 177 | |
| 178 | debug3("%s: installing SIGSYS handler", __func__); |
| 179 | memset(&act, 0, sizeof(act)); |
| 180 | sigemptyset(&mask); |
| 181 | sigaddset(&mask, SIGSYS); |
| 182 | |
| 183 | act.sa_sigaction = &ssh_sandbox_violation; |
| 184 | act.sa_flags = SA_SIGINFO; |
| 185 | if (sigaction(SIGSYS, &act, NULL) == -1) |
| 186 | fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno)); |
| 187 | if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1) |
| 188 | fatal("%s: sigprocmask(SIGSYS): %s", |
| 189 | __func__, strerror(errno)); |
| 190 | } |
| 191 | #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */ |
| 192 | |
| 193 | void |
| 194 | ssh_sandbox_child(struct ssh_sandbox *box) |
| 195 | { |
| 196 | struct rlimit rl_zero; |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 197 | int nnp_failed = 0; |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 198 | |
| 199 | /* Set rlimits for completeness if possible. */ |
| 200 | rl_zero.rlim_cur = rl_zero.rlim_max = 0; |
| 201 | if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1) |
| 202 | fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s", |
| 203 | __func__, strerror(errno)); |
| 204 | if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1) |
| 205 | fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s", |
| 206 | __func__, strerror(errno)); |
| 207 | if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1) |
| 208 | fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s", |
| 209 | __func__, strerror(errno)); |
| 210 | |
| 211 | #ifdef SANDBOX_SECCOMP_FILTER_DEBUG |
| 212 | ssh_sandbox_child_debugging(); |
| 213 | #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */ |
| 214 | |
| 215 | debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__); |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 216 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) { |
| 217 | debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s", |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 218 | __func__, strerror(errno)); |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 219 | nnp_failed = 1; |
| 220 | } |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 221 | debug3("%s: attaching seccomp filter program", __func__); |
| 222 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1) |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 223 | debug("%s: prctl(PR_SET_SECCOMP): %s", |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 224 | __func__, strerror(errno)); |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 225 | else if (nnp_failed) |
| 226 | fatal("%s: SECCOMP_MODE_FILTER activated but " |
| 227 | "PR_SET_NO_NEW_PRIVS failed", __func__); |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void |
| 231 | ssh_sandbox_parent_finish(struct ssh_sandbox *box) |
| 232 | { |
| 233 | free(box); |
| 234 | debug3("%s: finished", __func__); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) |
| 239 | { |
| 240 | box->child_pid = child_pid; |
| 241 | } |
| 242 | |
| 243 | #endif /* SANDBOX_SECCOMP_FILTER */ |