Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/seccomp.c |
| 3 | * |
| 4 | * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com> |
| 5 | * |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 6 | * Copyright (C) 2012 Google, Inc. |
| 7 | * Will Drewry <wad@chromium.org> |
| 8 | * |
| 9 | * This defines a simple but solid secure-computing facility. |
| 10 | * |
| 11 | * Mode 1 uses a fixed list of allowed system calls. |
| 12 | * Mode 2 allows user-defined system call filters in the form |
| 13 | * of Berkeley Packet Filters/Linux Socket Filters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 17 | #include <linux/audit.h> |
Roland McGrath | 5b10174 | 2009-02-27 23:25:54 -0800 | [diff] [blame] | 18 | #include <linux/compat.h> |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 19 | #include <linux/sched.h> |
| 20 | #include <linux/seccomp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* #define SECCOMP_DEBUG 1 */ |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 23 | |
| 24 | #ifdef CONFIG_SECCOMP_FILTER |
| 25 | #include <asm/syscall.h> |
| 26 | #include <linux/filter.h> |
Will Drewry | fb0fadf | 2012-04-12 16:48:02 -0500 | [diff] [blame] | 27 | #include <linux/ptrace.h> |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 28 | #include <linux/security.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/tracehook.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | |
| 33 | /** |
| 34 | * struct seccomp_filter - container for seccomp BPF programs |
| 35 | * |
| 36 | * @usage: reference count to manage the object lifetime. |
| 37 | * get/put helpers should be used when accessing an instance |
| 38 | * outside of a lifetime-guarded section. In general, this |
| 39 | * is only needed for handling filters shared across tasks. |
| 40 | * @prev: points to a previously installed, or inherited, filter |
| 41 | * @len: the number of instructions in the program |
| 42 | * @insns: the BPF program instructions to evaluate |
| 43 | * |
| 44 | * seccomp_filter objects are organized in a tree linked via the @prev |
| 45 | * pointer. For any task, it appears to be a singly-linked list starting |
| 46 | * with current->seccomp.filter, the most recently attached or inherited filter. |
| 47 | * However, multiple filters may share a @prev node, by way of fork(), which |
| 48 | * results in a unidirectional tree existing in memory. This is similar to |
| 49 | * how namespaces work. |
| 50 | * |
| 51 | * seccomp_filter objects should never be modified after being attached |
| 52 | * to a task_struct (other than @usage). |
| 53 | */ |
| 54 | struct seccomp_filter { |
| 55 | atomic_t usage; |
| 56 | struct seccomp_filter *prev; |
| 57 | unsigned short len; /* Instruction count */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 58 | struct sock_filter_int insnsi[]; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /* Limit any path through the tree to 256KB worth of instructions. */ |
| 62 | #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) |
| 63 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 64 | /* |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 65 | * Endianness is explicitly ignored and left for BPF program authors to manage |
| 66 | * as per the specific architecture. |
| 67 | */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 68 | static void populate_seccomp_data(struct seccomp_data *sd) |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 69 | { |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 70 | struct task_struct *task = current; |
| 71 | struct pt_regs *regs = task_pt_regs(task); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 72 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 73 | sd->nr = syscall_get_nr(task, regs); |
| 74 | sd->arch = syscall_get_arch(task, regs); |
| 75 | |
| 76 | /* Unroll syscall_get_args to help gcc on arm. */ |
| 77 | syscall_get_arguments(task, regs, 0, 1, (unsigned long *) &sd->args[0]); |
| 78 | syscall_get_arguments(task, regs, 1, 1, (unsigned long *) &sd->args[1]); |
| 79 | syscall_get_arguments(task, regs, 2, 1, (unsigned long *) &sd->args[2]); |
| 80 | syscall_get_arguments(task, regs, 3, 1, (unsigned long *) &sd->args[3]); |
| 81 | syscall_get_arguments(task, regs, 4, 1, (unsigned long *) &sd->args[4]); |
| 82 | syscall_get_arguments(task, regs, 5, 1, (unsigned long *) &sd->args[5]); |
| 83 | |
| 84 | sd->instruction_pointer = KSTK_EIP(task); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * seccomp_check_filter - verify seccomp filter code |
| 89 | * @filter: filter to verify |
| 90 | * @flen: length of filter |
| 91 | * |
| 92 | * Takes a previously checked filter (by sk_chk_filter) and |
| 93 | * redirects all filter code that loads struct sk_buff data |
| 94 | * and related data through seccomp_bpf_load. It also |
| 95 | * enforces length and alignment checking of those loads. |
| 96 | * |
| 97 | * Returns 0 if the rule set is legal or -EINVAL if not. |
| 98 | */ |
| 99 | static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) |
| 100 | { |
| 101 | int pc; |
| 102 | for (pc = 0; pc < flen; pc++) { |
| 103 | struct sock_filter *ftest = &filter[pc]; |
| 104 | u16 code = ftest->code; |
| 105 | u32 k = ftest->k; |
| 106 | |
| 107 | switch (code) { |
| 108 | case BPF_S_LD_W_ABS: |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 109 | ftest->code = BPF_LDX | BPF_W | BPF_ABS; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 110 | /* 32-bit aligned and not out of bounds. */ |
| 111 | if (k >= sizeof(struct seccomp_data) || k & 3) |
| 112 | return -EINVAL; |
| 113 | continue; |
| 114 | case BPF_S_LD_W_LEN: |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 115 | ftest->code = BPF_LD | BPF_IMM; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 116 | ftest->k = sizeof(struct seccomp_data); |
| 117 | continue; |
| 118 | case BPF_S_LDX_W_LEN: |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 119 | ftest->code = BPF_LDX | BPF_IMM; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 120 | ftest->k = sizeof(struct seccomp_data); |
| 121 | continue; |
| 122 | /* Explicitly include allowed calls. */ |
| 123 | case BPF_S_RET_K: |
| 124 | case BPF_S_RET_A: |
| 125 | case BPF_S_ALU_ADD_K: |
| 126 | case BPF_S_ALU_ADD_X: |
| 127 | case BPF_S_ALU_SUB_K: |
| 128 | case BPF_S_ALU_SUB_X: |
| 129 | case BPF_S_ALU_MUL_K: |
| 130 | case BPF_S_ALU_MUL_X: |
| 131 | case BPF_S_ALU_DIV_X: |
| 132 | case BPF_S_ALU_AND_K: |
| 133 | case BPF_S_ALU_AND_X: |
| 134 | case BPF_S_ALU_OR_K: |
| 135 | case BPF_S_ALU_OR_X: |
Nicolas Schichan | d132747 | 2013-03-15 18:02:00 +0100 | [diff] [blame] | 136 | case BPF_S_ALU_XOR_K: |
| 137 | case BPF_S_ALU_XOR_X: |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 138 | case BPF_S_ALU_LSH_K: |
| 139 | case BPF_S_ALU_LSH_X: |
| 140 | case BPF_S_ALU_RSH_K: |
| 141 | case BPF_S_ALU_RSH_X: |
| 142 | case BPF_S_ALU_NEG: |
| 143 | case BPF_S_LD_IMM: |
| 144 | case BPF_S_LDX_IMM: |
| 145 | case BPF_S_MISC_TAX: |
| 146 | case BPF_S_MISC_TXA: |
| 147 | case BPF_S_ALU_DIV_K: |
| 148 | case BPF_S_LD_MEM: |
| 149 | case BPF_S_LDX_MEM: |
| 150 | case BPF_S_ST: |
| 151 | case BPF_S_STX: |
| 152 | case BPF_S_JMP_JA: |
| 153 | case BPF_S_JMP_JEQ_K: |
| 154 | case BPF_S_JMP_JEQ_X: |
| 155 | case BPF_S_JMP_JGE_K: |
| 156 | case BPF_S_JMP_JGE_X: |
| 157 | case BPF_S_JMP_JGT_K: |
| 158 | case BPF_S_JMP_JGT_X: |
| 159 | case BPF_S_JMP_JSET_K: |
| 160 | case BPF_S_JMP_JSET_X: |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 161 | sk_decode_filter(ftest, ftest); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 162 | continue; |
| 163 | default: |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * seccomp_run_filters - evaluates all seccomp filters against @syscall |
| 172 | * @syscall: number of the current system call |
| 173 | * |
| 174 | * Returns valid seccomp BPF response codes. |
| 175 | */ |
| 176 | static u32 seccomp_run_filters(int syscall) |
| 177 | { |
| 178 | struct seccomp_filter *f; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 179 | struct seccomp_data sd; |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 180 | u32 ret = SECCOMP_RET_ALLOW; |
| 181 | |
| 182 | /* Ensure unexpected behavior doesn't result in failing open. */ |
| 183 | if (WARN_ON(current->seccomp.filter == NULL)) |
| 184 | return SECCOMP_RET_KILL; |
| 185 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 186 | populate_seccomp_data(&sd); |
| 187 | |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 188 | /* |
| 189 | * All filters in the list are evaluated and the lowest BPF return |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 190 | * value always takes priority (ignoring the DATA). |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 191 | */ |
| 192 | for (f = current->seccomp.filter; f; f = f->prev) { |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 193 | u32 cur_ret = sk_run_filter_int_seccomp(&sd, f->insnsi); |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 194 | if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION)) |
| 195 | ret = cur_ret; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 196 | } |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * seccomp_attach_filter: Attaches a seccomp filter to current. |
| 202 | * @fprog: BPF program to install |
| 203 | * |
| 204 | * Returns 0 on success or an errno on failure. |
| 205 | */ |
| 206 | static long seccomp_attach_filter(struct sock_fprog *fprog) |
| 207 | { |
| 208 | struct seccomp_filter *filter; |
| 209 | unsigned long fp_size = fprog->len * sizeof(struct sock_filter); |
| 210 | unsigned long total_insns = fprog->len; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 211 | struct sock_filter *fp; |
| 212 | int new_len; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 213 | long ret; |
| 214 | |
| 215 | if (fprog->len == 0 || fprog->len > BPF_MAXINSNS) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | for (filter = current->seccomp.filter; filter; filter = filter->prev) |
| 219 | total_insns += filter->len + 4; /* include a 4 instr penalty */ |
| 220 | if (total_insns > MAX_INSNS_PER_PATH) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | /* |
| 224 | * Installing a seccomp filter requires that the task have |
| 225 | * CAP_SYS_ADMIN in its namespace or be running with no_new_privs. |
| 226 | * This avoids scenarios where unprivileged tasks can affect the |
| 227 | * behavior of privileged children. |
| 228 | */ |
| 229 | if (!current->no_new_privs && |
| 230 | security_capable_noaudit(current_cred(), current_user_ns(), |
| 231 | CAP_SYS_ADMIN) != 0) |
| 232 | return -EACCES; |
| 233 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 234 | fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN); |
| 235 | if (!fp) |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 236 | return -ENOMEM; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 237 | |
| 238 | /* Copy the instructions from fprog. */ |
| 239 | ret = -EFAULT; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 240 | if (copy_from_user(fp, fprog->filter, fp_size)) |
| 241 | goto free_prog; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 242 | |
| 243 | /* Check and rewrite the fprog via the skb checker */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 244 | ret = sk_chk_filter(fp, fprog->len); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 245 | if (ret) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 246 | goto free_prog; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 247 | |
| 248 | /* Check and rewrite the fprog for seccomp use */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 249 | ret = seccomp_check_filter(fp, fprog->len); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 250 | if (ret) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 251 | goto free_prog; |
| 252 | |
| 253 | /* Convert 'sock_filter' insns to 'sock_filter_int' insns */ |
| 254 | ret = sk_convert_filter(fp, fprog->len, NULL, &new_len); |
| 255 | if (ret) |
| 256 | goto free_prog; |
| 257 | |
| 258 | /* Allocate a new seccomp_filter */ |
| 259 | filter = kzalloc(sizeof(struct seccomp_filter) + |
| 260 | sizeof(struct sock_filter_int) * new_len, |
| 261 | GFP_KERNEL|__GFP_NOWARN); |
| 262 | if (!filter) |
| 263 | goto free_prog; |
| 264 | |
| 265 | ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len); |
| 266 | if (ret) |
| 267 | goto free_filter; |
| 268 | |
| 269 | atomic_set(&filter->usage, 1); |
| 270 | filter->len = new_len; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * If there is an existing filter, make it the prev and don't drop its |
| 274 | * task reference. |
| 275 | */ |
| 276 | filter->prev = current->seccomp.filter; |
| 277 | current->seccomp.filter = filter; |
| 278 | return 0; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 279 | |
| 280 | free_filter: |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 281 | kfree(filter); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame^] | 282 | free_prog: |
| 283 | kfree(fp); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * seccomp_attach_user_filter - attaches a user-supplied sock_fprog |
| 289 | * @user_filter: pointer to the user data containing a sock_fprog. |
| 290 | * |
| 291 | * Returns 0 on success and non-zero otherwise. |
| 292 | */ |
| 293 | long seccomp_attach_user_filter(char __user *user_filter) |
| 294 | { |
| 295 | struct sock_fprog fprog; |
| 296 | long ret = -EFAULT; |
| 297 | |
| 298 | #ifdef CONFIG_COMPAT |
| 299 | if (is_compat_task()) { |
| 300 | struct compat_sock_fprog fprog32; |
| 301 | if (copy_from_user(&fprog32, user_filter, sizeof(fprog32))) |
| 302 | goto out; |
| 303 | fprog.len = fprog32.len; |
| 304 | fprog.filter = compat_ptr(fprog32.filter); |
| 305 | } else /* falls through to the if below. */ |
| 306 | #endif |
| 307 | if (copy_from_user(&fprog, user_filter, sizeof(fprog))) |
| 308 | goto out; |
| 309 | ret = seccomp_attach_filter(&fprog); |
| 310 | out: |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | /* get_seccomp_filter - increments the reference count of the filter on @tsk */ |
| 315 | void get_seccomp_filter(struct task_struct *tsk) |
| 316 | { |
| 317 | struct seccomp_filter *orig = tsk->seccomp.filter; |
| 318 | if (!orig) |
| 319 | return; |
| 320 | /* Reference count is bounded by the number of total processes. */ |
| 321 | atomic_inc(&orig->usage); |
| 322 | } |
| 323 | |
| 324 | /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ |
| 325 | void put_seccomp_filter(struct task_struct *tsk) |
| 326 | { |
| 327 | struct seccomp_filter *orig = tsk->seccomp.filter; |
| 328 | /* Clean up single-reference branches iteratively. */ |
| 329 | while (orig && atomic_dec_and_test(&orig->usage)) { |
| 330 | struct seccomp_filter *freeme = orig; |
| 331 | orig = orig->prev; |
| 332 | kfree(freeme); |
| 333 | } |
| 334 | } |
Will Drewry | bb6ea43 | 2012-04-12 16:48:01 -0500 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * seccomp_send_sigsys - signals the task to allow in-process syscall emulation |
| 338 | * @syscall: syscall number to send to userland |
| 339 | * @reason: filter-supplied reason code to send to userland (via si_errno) |
| 340 | * |
| 341 | * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info. |
| 342 | */ |
| 343 | static void seccomp_send_sigsys(int syscall, int reason) |
| 344 | { |
| 345 | struct siginfo info; |
| 346 | memset(&info, 0, sizeof(info)); |
| 347 | info.si_signo = SIGSYS; |
| 348 | info.si_code = SYS_SECCOMP; |
| 349 | info.si_call_addr = (void __user *)KSTK_EIP(current); |
| 350 | info.si_errno = reason; |
| 351 | info.si_arch = syscall_get_arch(current, task_pt_regs(current)); |
| 352 | info.si_syscall = syscall; |
| 353 | force_sig_info(SIGSYS, &info, current); |
| 354 | } |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 355 | #endif /* CONFIG_SECCOMP_FILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Secure computing mode 1 allows only read/write/exit/sigreturn. |
| 359 | * To be fully secure this must be combined with rlimit |
| 360 | * to limit the stack allocations too. |
| 361 | */ |
| 362 | static int mode1_syscalls[] = { |
| 363 | __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn, |
| 364 | 0, /* null terminated */ |
| 365 | }; |
| 366 | |
Roland McGrath | 5b10174 | 2009-02-27 23:25:54 -0800 | [diff] [blame] | 367 | #ifdef CONFIG_COMPAT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | static int mode1_syscalls_32[] = { |
| 369 | __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32, |
| 370 | 0, /* null terminated */ |
| 371 | }; |
| 372 | #endif |
| 373 | |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 374 | int __secure_computing(int this_syscall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
| 376 | int mode = current->seccomp.mode; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 377 | int exit_sig = 0; |
| 378 | int *syscall; |
Will Drewry | 8156b45 | 2012-04-17 14:48:58 -0500 | [diff] [blame] | 379 | u32 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | switch (mode) { |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 382 | case SECCOMP_MODE_STRICT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | syscall = mode1_syscalls; |
Roland McGrath | 5b10174 | 2009-02-27 23:25:54 -0800 | [diff] [blame] | 384 | #ifdef CONFIG_COMPAT |
| 385 | if (is_compat_task()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | syscall = mode1_syscalls_32; |
| 387 | #endif |
| 388 | do { |
| 389 | if (*syscall == this_syscall) |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 390 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } while (*++syscall); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 392 | exit_sig = SIGKILL; |
Will Drewry | 8156b45 | 2012-04-17 14:48:58 -0500 | [diff] [blame] | 393 | ret = SECCOMP_RET_KILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | break; |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 395 | #ifdef CONFIG_SECCOMP_FILTER |
Will Drewry | 8156b45 | 2012-04-17 14:48:58 -0500 | [diff] [blame] | 396 | case SECCOMP_MODE_FILTER: { |
| 397 | int data; |
Andy Lutomirski | 87b526d | 2012-10-01 11:40:45 -0700 | [diff] [blame] | 398 | struct pt_regs *regs = task_pt_regs(current); |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 399 | ret = seccomp_run_filters(this_syscall); |
| 400 | data = ret & SECCOMP_RET_DATA; |
Will Drewry | 8156b45 | 2012-04-17 14:48:58 -0500 | [diff] [blame] | 401 | ret &= SECCOMP_RET_ACTION; |
| 402 | switch (ret) { |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 403 | case SECCOMP_RET_ERRNO: |
| 404 | /* Set the low-order 16-bits as a errno. */ |
Andy Lutomirski | 87b526d | 2012-10-01 11:40:45 -0700 | [diff] [blame] | 405 | syscall_set_return_value(current, regs, |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 406 | -data, 0); |
| 407 | goto skip; |
Will Drewry | bb6ea43 | 2012-04-12 16:48:01 -0500 | [diff] [blame] | 408 | case SECCOMP_RET_TRAP: |
| 409 | /* Show the handler the original registers. */ |
Andy Lutomirski | 87b526d | 2012-10-01 11:40:45 -0700 | [diff] [blame] | 410 | syscall_rollback(current, regs); |
Will Drewry | bb6ea43 | 2012-04-12 16:48:01 -0500 | [diff] [blame] | 411 | /* Let the filter pass back 16 bits of data. */ |
| 412 | seccomp_send_sigsys(this_syscall, data); |
| 413 | goto skip; |
Will Drewry | fb0fadf | 2012-04-12 16:48:02 -0500 | [diff] [blame] | 414 | case SECCOMP_RET_TRACE: |
| 415 | /* Skip these calls if there is no tracer. */ |
Andy Lutomirski | 87b526d | 2012-10-01 11:40:45 -0700 | [diff] [blame] | 416 | if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) { |
| 417 | syscall_set_return_value(current, regs, |
| 418 | -ENOSYS, 0); |
Will Drewry | fb0fadf | 2012-04-12 16:48:02 -0500 | [diff] [blame] | 419 | goto skip; |
Andy Lutomirski | 87b526d | 2012-10-01 11:40:45 -0700 | [diff] [blame] | 420 | } |
Will Drewry | fb0fadf | 2012-04-12 16:48:02 -0500 | [diff] [blame] | 421 | /* Allow the BPF to provide the event message */ |
| 422 | ptrace_event(PTRACE_EVENT_SECCOMP, data); |
| 423 | /* |
| 424 | * The delivery of a fatal signal during event |
| 425 | * notification may silently skip tracer notification. |
| 426 | * Terminating the task now avoids executing a system |
| 427 | * call that may not be intended. |
| 428 | */ |
| 429 | if (fatal_signal_pending(current)) |
| 430 | break; |
Andy Lutomirski | 87b526d | 2012-10-01 11:40:45 -0700 | [diff] [blame] | 431 | if (syscall_get_nr(current, regs) < 0) |
| 432 | goto skip; /* Explicit request to skip. */ |
| 433 | |
Will Drewry | fb0fadf | 2012-04-12 16:48:02 -0500 | [diff] [blame] | 434 | return 0; |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 435 | case SECCOMP_RET_ALLOW: |
| 436 | return 0; |
| 437 | case SECCOMP_RET_KILL: |
| 438 | default: |
| 439 | break; |
| 440 | } |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 441 | exit_sig = SIGSYS; |
| 442 | break; |
Will Drewry | 8156b45 | 2012-04-17 14:48:58 -0500 | [diff] [blame] | 443 | } |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 444 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | default: |
| 446 | BUG(); |
| 447 | } |
| 448 | |
| 449 | #ifdef SECCOMP_DEBUG |
| 450 | dump_stack(); |
| 451 | #endif |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 452 | audit_seccomp(this_syscall, exit_sig, ret); |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 453 | do_exit(exit_sig); |
Will Drewry | 8156b45 | 2012-04-17 14:48:58 -0500 | [diff] [blame] | 454 | #ifdef CONFIG_SECCOMP_FILTER |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 455 | skip: |
| 456 | audit_seccomp(this_syscall, exit_sig, ret); |
Will Drewry | 8156b45 | 2012-04-17 14:48:58 -0500 | [diff] [blame] | 457 | #endif |
Will Drewry | acf3b2c | 2012-04-12 16:47:59 -0500 | [diff] [blame] | 458 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 460 | |
| 461 | long prctl_get_seccomp(void) |
| 462 | { |
| 463 | return current->seccomp.mode; |
| 464 | } |
| 465 | |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 466 | /** |
| 467 | * prctl_set_seccomp: configures current->seccomp.mode |
| 468 | * @seccomp_mode: requested mode to use |
| 469 | * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER |
| 470 | * |
| 471 | * This function may be called repeatedly with a @seccomp_mode of |
| 472 | * SECCOMP_MODE_FILTER to install additional filters. Every filter |
| 473 | * successfully installed will be evaluated (in reverse order) for each system |
| 474 | * call the task makes. |
| 475 | * |
| 476 | * Once current->seccomp.mode is non-zero, it may not be changed. |
| 477 | * |
| 478 | * Returns 0 on success or -EINVAL on failure. |
| 479 | */ |
| 480 | long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter) |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 481 | { |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 482 | long ret = -EINVAL; |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 483 | |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 484 | if (current->seccomp.mode && |
| 485 | current->seccomp.mode != seccomp_mode) |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 486 | goto out; |
| 487 | |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 488 | switch (seccomp_mode) { |
| 489 | case SECCOMP_MODE_STRICT: |
| 490 | ret = 0; |
Andrea Arcangeli | cf99aba | 2007-07-15 23:41:33 -0700 | [diff] [blame] | 491 | #ifdef TIF_NOTSC |
| 492 | disable_TSC(); |
| 493 | #endif |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 494 | break; |
| 495 | #ifdef CONFIG_SECCOMP_FILTER |
| 496 | case SECCOMP_MODE_FILTER: |
| 497 | ret = seccomp_attach_user_filter(filter); |
| 498 | if (ret) |
| 499 | goto out; |
| 500 | break; |
| 501 | #endif |
| 502 | default: |
| 503 | goto out; |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Will Drewry | e2cfabdf | 2012-04-12 16:47:57 -0500 | [diff] [blame] | 506 | current->seccomp.mode = seccomp_mode; |
| 507 | set_thread_flag(TIF_SECCOMP); |
| 508 | out: |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 509 | return ret; |
| 510 | } |