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 | |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 46 | #include <linux/net.h> |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 47 | #include <linux/audit.h> |
| 48 | #include <linux/filter.h> |
| 49 | #include <linux/seccomp.h> |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 50 | #include <elf.h> |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 51 | |
| 52 | #include <asm/unistd.h> |
| 53 | |
| 54 | #include <errno.h> |
| 55 | #include <signal.h> |
| 56 | #include <stdarg.h> |
| 57 | #include <stddef.h> /* for offsetof */ |
| 58 | #include <stdio.h> |
| 59 | #include <stdlib.h> |
| 60 | #include <string.h> |
| 61 | #include <unistd.h> |
| 62 | |
| 63 | #include "log.h" |
| 64 | #include "ssh-sandbox.h" |
| 65 | #include "xmalloc.h" |
| 66 | |
| 67 | /* Linux seccomp_filter sandbox */ |
| 68 | #define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL |
| 69 | |
| 70 | /* Use a signal handler to emit violations when debugging */ |
| 71 | #ifdef SANDBOX_SECCOMP_FILTER_DEBUG |
| 72 | # undef SECCOMP_FILTER_FAIL |
| 73 | # define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP |
| 74 | #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */ |
| 75 | |
Damien Miller | 9e96b41 | 2017-03-14 12:24:47 +1100 | [diff] [blame] | 76 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 77 | # define ARG_LO_OFFSET 0 |
| 78 | # define ARG_HI_OFFSET sizeof(uint32_t) |
| 79 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 80 | # define ARG_LO_OFFSET sizeof(uint32_t) |
| 81 | # define ARG_HI_OFFSET 0 |
| 82 | #else |
| 83 | #error "Unknown endianness" |
| 84 | #endif |
| 85 | |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 86 | /* Simple helpers to avoid manual errors (but larger BPF programs). */ |
| 87 | #define SC_DENY(_nr, _errno) \ |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 88 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \ |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 89 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno)) |
| 90 | #define SC_ALLOW(_nr) \ |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 91 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \ |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 92 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW) |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 93 | #define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \ |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 94 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 6), \ |
Damien Miller | 9e96b41 | 2017-03-14 12:24:47 +1100 | [diff] [blame] | 95 | /* load and test first syscall argument, low word */ \ |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 96 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \ |
Damien Miller | 9e96b41 | 2017-03-14 12:24:47 +1100 | [diff] [blame] | 97 | offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_LO_OFFSET), \ |
| 98 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \ |
| 99 | ((_arg_val) & 0xFFFFFFFF), 0, 3), \ |
| 100 | /* load and test first syscall argument, high word */ \ |
| 101 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \ |
| 102 | offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_HI_OFFSET), \ |
| 103 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \ |
| 104 | (((uint32_t)((uint64_t)(_arg_val) >> 32)) & 0xFFFFFFFF), 0, 1), \ |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 105 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \ |
| 106 | /* reload syscall number; all rules expect it in accumulator */ \ |
| 107 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \ |
| 108 | offsetof(struct seccomp_data, nr)) |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 109 | |
| 110 | /* Syscall filtering set for preauth. */ |
| 111 | static const struct sock_filter preauth_insns[] = { |
| 112 | /* Ensure the syscall arch convention is as expected. */ |
| 113 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, |
| 114 | offsetof(struct seccomp_data, arch)), |
| 115 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0), |
| 116 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL), |
| 117 | /* Load the syscall number for checking. */ |
| 118 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, |
| 119 | offsetof(struct seccomp_data, nr)), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 120 | |
| 121 | /* Syscalls to non-fatally deny */ |
Damien Miller | f64062b | 2016-05-20 09:56:53 +1000 | [diff] [blame] | 122 | #ifdef __NR_lstat |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 123 | SC_DENY(__NR_lstat, EACCES), |
Damien Miller | f64062b | 2016-05-20 09:56:53 +1000 | [diff] [blame] | 124 | #endif |
| 125 | #ifdef __NR_lstat64 |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 126 | SC_DENY(__NR_lstat64, EACCES), |
Damien Miller | f64062b | 2016-05-20 09:56:53 +1000 | [diff] [blame] | 127 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 128 | #ifdef __NR_fstat |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 129 | SC_DENY(__NR_fstat, EACCES), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 130 | #endif |
| 131 | #ifdef __NR_fstat64 |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 132 | SC_DENY(__NR_fstat64, EACCES), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 133 | #endif |
| 134 | #ifdef __NR_open |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 135 | SC_DENY(__NR_open, EACCES), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 136 | #endif |
| 137 | #ifdef __NR_openat |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 138 | SC_DENY(__NR_openat, EACCES), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 139 | #endif |
| 140 | #ifdef __NR_newfstatat |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 141 | SC_DENY(__NR_newfstatat, EACCES), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 142 | #endif |
| 143 | #ifdef __NR_stat |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 144 | SC_DENY(__NR_stat, EACCES), |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 145 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 146 | #ifdef __NR_stat64 |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 147 | SC_DENY(__NR_stat64, EACCES), |
Damien Miller | 6434cb2 | 2014-02-06 11:17:50 +1100 | [diff] [blame] | 148 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 149 | |
| 150 | /* Syscalls to permit */ |
| 151 | #ifdef __NR_brk |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 152 | SC_ALLOW(__NR_brk), |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 153 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 154 | #ifdef __NR_clock_gettime |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 155 | SC_ALLOW(__NR_clock_gettime), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 156 | #endif |
| 157 | #ifdef __NR_close |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 158 | SC_ALLOW(__NR_close), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 159 | #endif |
| 160 | #ifdef __NR_exit |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 161 | SC_ALLOW(__NR_exit), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 162 | #endif |
| 163 | #ifdef __NR_exit_group |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 164 | SC_ALLOW(__NR_exit_group), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 165 | #endif |
djm@openbsd.org | 512cadd | 2015-06-29 22:35:12 +0000 | [diff] [blame] | 166 | #ifdef __NR_getpgid |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 167 | SC_ALLOW(__NR_getpgid), |
djm@openbsd.org | 512cadd | 2015-06-29 22:35:12 +0000 | [diff] [blame] | 168 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 169 | #ifdef __NR_getpid |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 170 | SC_ALLOW(__NR_getpid), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 171 | #endif |
Damien Miller | 26ad182 | 2015-09-10 10:57:41 +1000 | [diff] [blame] | 172 | #ifdef __NR_getrandom |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 173 | SC_ALLOW(__NR_getrandom), |
Damien Miller | 26ad182 | 2015-09-10 10:57:41 +1000 | [diff] [blame] | 174 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 175 | #ifdef __NR_gettimeofday |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 176 | SC_ALLOW(__NR_gettimeofday), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 177 | #endif |
| 178 | #ifdef __NR_madvise |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 179 | SC_ALLOW(__NR_madvise), |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 180 | #endif |
| 181 | #ifdef __NR_mmap |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 182 | SC_ALLOW(__NR_mmap), |
Damien Miller | 91f40d8 | 2013-02-22 11:37:00 +1100 | [diff] [blame] | 183 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 184 | #ifdef __NR_mmap2 |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 185 | SC_ALLOW(__NR_mmap2), |
Damien Miller | 0fa0ed0 | 2014-09-10 08:15:34 +1000 | [diff] [blame] | 186 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 187 | #ifdef __NR_mremap |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 188 | SC_ALLOW(__NR_mremap), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 189 | #endif |
| 190 | #ifdef __NR_munmap |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 191 | SC_ALLOW(__NR_munmap), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 192 | #endif |
| 193 | #ifdef __NR__newselect |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 194 | SC_ALLOW(__NR__newselect), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 195 | #endif |
| 196 | #ifdef __NR_poll |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 197 | SC_ALLOW(__NR_poll), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 198 | #endif |
Damien Miller | bc20205 | 2015-06-25 09:51:39 +1000 | [diff] [blame] | 199 | #ifdef __NR_pselect6 |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 200 | SC_ALLOW(__NR_pselect6), |
Damien Miller | bc20205 | 2015-06-25 09:51:39 +1000 | [diff] [blame] | 201 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 202 | #ifdef __NR_read |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 203 | SC_ALLOW(__NR_read), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 204 | #endif |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 205 | #ifdef __NR_rt_sigprocmask |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 206 | SC_ALLOW(__NR_rt_sigprocmask), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 207 | #endif |
| 208 | #ifdef __NR_select |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 209 | SC_ALLOW(__NR_select), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 210 | #endif |
| 211 | #ifdef __NR_shutdown |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 212 | SC_ALLOW(__NR_shutdown), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 213 | #endif |
| 214 | #ifdef __NR_sigprocmask |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 215 | SC_ALLOW(__NR_sigprocmask), |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 216 | #endif |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 217 | #ifdef __NR_time |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 218 | SC_ALLOW(__NR_time), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 219 | #endif |
| 220 | #ifdef __NR_write |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 221 | SC_ALLOW(__NR_write), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 222 | #endif |
| 223 | #ifdef __NR_socketcall |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 224 | SC_ALLOW_ARG(__NR_socketcall, 0, SYS_SHUTDOWN), |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 225 | #endif |
Damien Miller | 5f1596e | 2017-03-14 13:15:18 +1100 | [diff] [blame] | 226 | #if defined(__NR_ioctl) && defined(__s390__) |
| 227 | /* Allow ioctls for ICA crypto card on s390 */ |
Damien Miller | e3ea335 | 2017-03-14 17:48:43 +1100 | [diff] [blame^] | 228 | SC_ALLOW_ARG(__NR_ioctl, 1, Z90STAT_STATUS_MASK), |
| 229 | SC_ALLOW_ARG(__NR_ioctl, 1, ICARSAMODEXPO), |
| 230 | SC_ALLOW_ARG(__NR_ioctl, 1, ICARSACRT), |
Damien Miller | 5f1596e | 2017-03-14 13:15:18 +1100 | [diff] [blame] | 231 | #endif /* defined(__NR_ioctl) && defined(__s390__) */ |
Damien Miller | 99f33d7 | 2015-06-17 10:50:51 +1000 | [diff] [blame] | 232 | |
| 233 | /* Default deny */ |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 234 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL), |
| 235 | }; |
| 236 | |
| 237 | static const struct sock_fprog preauth_program = { |
| 238 | .len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])), |
| 239 | .filter = (struct sock_filter *)preauth_insns, |
| 240 | }; |
| 241 | |
| 242 | struct ssh_sandbox { |
| 243 | pid_t child_pid; |
| 244 | }; |
| 245 | |
| 246 | struct ssh_sandbox * |
Damien Miller | 868ea1e | 2014-01-17 16:47:04 +1100 | [diff] [blame] | 247 | ssh_sandbox_init(struct monitor *monitor) |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 248 | { |
| 249 | struct ssh_sandbox *box; |
| 250 | |
| 251 | /* |
| 252 | * Strictly, we don't need to maintain any state here but we need |
| 253 | * to return non-NULL to satisfy the API. |
| 254 | */ |
| 255 | debug3("%s: preparing seccomp filter sandbox", __func__); |
| 256 | box = xcalloc(1, sizeof(*box)); |
| 257 | box->child_pid = 0; |
| 258 | |
| 259 | return box; |
| 260 | } |
| 261 | |
| 262 | #ifdef SANDBOX_SECCOMP_FILTER_DEBUG |
| 263 | extern struct monitor *pmonitor; |
| 264 | void mm_log_handler(LogLevel level, const char *msg, void *ctx); |
| 265 | |
| 266 | static void |
| 267 | ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context) |
| 268 | { |
| 269 | char msg[256]; |
| 270 | |
| 271 | snprintf(msg, sizeof(msg), |
| 272 | "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)", |
| 273 | __func__, info->si_arch, info->si_syscall, info->si_call_addr); |
| 274 | mm_log_handler(SYSLOG_LEVEL_FATAL, msg, pmonitor); |
| 275 | _exit(1); |
| 276 | } |
| 277 | |
| 278 | static void |
| 279 | ssh_sandbox_child_debugging(void) |
| 280 | { |
| 281 | struct sigaction act; |
| 282 | sigset_t mask; |
| 283 | |
| 284 | debug3("%s: installing SIGSYS handler", __func__); |
| 285 | memset(&act, 0, sizeof(act)); |
| 286 | sigemptyset(&mask); |
| 287 | sigaddset(&mask, SIGSYS); |
| 288 | |
| 289 | act.sa_sigaction = &ssh_sandbox_violation; |
| 290 | act.sa_flags = SA_SIGINFO; |
| 291 | if (sigaction(SIGSYS, &act, NULL) == -1) |
| 292 | fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno)); |
| 293 | if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1) |
| 294 | fatal("%s: sigprocmask(SIGSYS): %s", |
| 295 | __func__, strerror(errno)); |
| 296 | } |
| 297 | #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */ |
| 298 | |
| 299 | void |
| 300 | ssh_sandbox_child(struct ssh_sandbox *box) |
| 301 | { |
| 302 | struct rlimit rl_zero; |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 303 | int nnp_failed = 0; |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 304 | |
| 305 | /* Set rlimits for completeness if possible. */ |
| 306 | rl_zero.rlim_cur = rl_zero.rlim_max = 0; |
| 307 | if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1) |
| 308 | fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s", |
| 309 | __func__, strerror(errno)); |
| 310 | if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1) |
| 311 | fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s", |
| 312 | __func__, strerror(errno)); |
| 313 | if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1) |
| 314 | fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s", |
| 315 | __func__, strerror(errno)); |
| 316 | |
| 317 | #ifdef SANDBOX_SECCOMP_FILTER_DEBUG |
| 318 | ssh_sandbox_child_debugging(); |
| 319 | #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */ |
| 320 | |
| 321 | debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__); |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 322 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) { |
| 323 | debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s", |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 324 | __func__, strerror(errno)); |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 325 | nnp_failed = 1; |
| 326 | } |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 327 | debug3("%s: attaching seccomp filter program", __func__); |
| 328 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1) |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 329 | debug("%s: prctl(PR_SET_SECCOMP): %s", |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 330 | __func__, strerror(errno)); |
Damien Miller | a0433a7 | 2012-07-06 10:27:10 +1000 | [diff] [blame] | 331 | else if (nnp_failed) |
| 332 | fatal("%s: SECCOMP_MODE_FILTER activated but " |
| 333 | "PR_SET_NO_NEW_PRIVS failed", __func__); |
Damien Miller | e0956e3 | 2012-04-04 11:27:54 +1000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void |
| 337 | ssh_sandbox_parent_finish(struct ssh_sandbox *box) |
| 338 | { |
| 339 | free(box); |
| 340 | debug3("%s: finished", __func__); |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) |
| 345 | { |
| 346 | box->child_pid = child_pid; |
| 347 | } |
| 348 | |
| 349 | #endif /* SANDBOX_SECCOMP_FILTER */ |