Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 9 | |
| 10 | #include "syscall_filter.h" |
| 11 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 12 | #include "util.h" |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 13 | |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 14 | #define MAX_LINE_LENGTH 1024 |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 15 | #define MAX_POLICY_LINE_LENGTH 1024 |
| 16 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 17 | #define ONE_INSTR 1 |
| 18 | #define TWO_INSTRS 2 |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 19 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 20 | int seccomp_can_softfail() |
| 21 | { |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 22 | #if defined(USE_SECCOMP_SOFTFAIL) |
| 23 | /* |
| 24 | * On Android devices seccomp is allowed to soft-fail on kernels < 3.8. |
| 25 | */ |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 26 | if (is_android()) { |
| 27 | if (kernel_lessthan_3_8()) |
| 28 | return 1; |
| 29 | else |
| 30 | return 0; |
| 31 | } else { |
| 32 | return 1; |
| 33 | } |
| 34 | #endif |
| 35 | return 0; |
| 36 | } |
| 37 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 38 | int str_to_op(const char *op_str) |
| 39 | { |
| 40 | if (!strcmp(op_str, "==")) { |
| 41 | return EQ; |
| 42 | } else if (!strcmp(op_str, "!=")) { |
| 43 | return NE; |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 44 | } else if (!strcmp(op_str, "&")) { |
| 45 | return SET; |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 46 | } else { |
| 47 | return 0; |
| 48 | } |
| 49 | } |
| 50 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 51 | struct sock_filter *new_instr_buf(size_t count) |
| 52 | { |
| 53 | struct sock_filter *buf = calloc(count, sizeof(struct sock_filter)); |
| 54 | if (!buf) |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 55 | die("could not allocate BPF instruction buffer"); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 56 | |
| 57 | return buf; |
| 58 | } |
| 59 | |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 60 | struct filter_block *new_filter_block() |
| 61 | { |
| 62 | struct filter_block *block = calloc(1, sizeof(struct filter_block)); |
| 63 | if (!block) |
| 64 | die("could not allocate BPF filter block"); |
| 65 | |
| 66 | block->instrs = NULL; |
| 67 | block->last = block->next = NULL; |
| 68 | |
| 69 | return block; |
| 70 | } |
| 71 | |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 72 | void append_filter_block(struct filter_block *head, struct sock_filter *instrs, |
| 73 | size_t len) |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 74 | { |
| 75 | struct filter_block *new_last; |
| 76 | |
| 77 | /* |
| 78 | * If |head| has no filter assigned yet, |
| 79 | * we don't create a new node. |
| 80 | */ |
| 81 | if (head->instrs == NULL) { |
| 82 | new_last = head; |
| 83 | } else { |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 84 | new_last = new_filter_block(); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 85 | if (head->next != NULL) { |
| 86 | head->last->next = new_last; |
| 87 | head->last = new_last; |
| 88 | } else { |
| 89 | head->last = head->next = new_last; |
| 90 | } |
| 91 | head->total_len += len; |
| 92 | } |
| 93 | |
| 94 | new_last->instrs = instrs; |
| 95 | new_last->total_len = new_last->len = len; |
| 96 | new_last->last = new_last->next = NULL; |
| 97 | } |
| 98 | |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 99 | void extend_filter_block_list(struct filter_block *list, |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 100 | struct filter_block *another) |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 101 | { |
| 102 | if (list->last != NULL) { |
| 103 | list->last->next = another; |
| 104 | list->last = another->last; |
| 105 | } else { |
| 106 | list->next = another; |
| 107 | list->last = another->last; |
| 108 | } |
| 109 | list->total_len += another->total_len; |
| 110 | } |
| 111 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 112 | void append_ret_kill(struct filter_block *head) |
| 113 | { |
| 114 | struct sock_filter *filter = new_instr_buf(ONE_INSTR); |
| 115 | set_bpf_ret_kill(filter); |
| 116 | append_filter_block(head, filter, ONE_INSTR); |
| 117 | } |
| 118 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 119 | void append_ret_trap(struct filter_block *head) |
| 120 | { |
| 121 | struct sock_filter *filter = new_instr_buf(ONE_INSTR); |
| 122 | set_bpf_ret_trap(filter); |
| 123 | append_filter_block(head, filter, ONE_INSTR); |
| 124 | } |
| 125 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 126 | void append_ret_errno(struct filter_block *head, int errno_val) |
| 127 | { |
| 128 | struct sock_filter *filter = new_instr_buf(ONE_INSTR); |
| 129 | set_bpf_ret_errno(filter, errno_val); |
| 130 | append_filter_block(head, filter, ONE_INSTR); |
| 131 | } |
| 132 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 133 | void append_allow_syscall(struct filter_block *head, int nr) |
| 134 | { |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 135 | struct sock_filter *filter = new_instr_buf(ALLOW_SYSCALL_LEN); |
| 136 | size_t len = bpf_allow_syscall(filter, nr); |
| 137 | if (len != ALLOW_SYSCALL_LEN) |
| 138 | die("error building syscall number comparison"); |
| 139 | |
| 140 | append_filter_block(head, filter, len); |
| 141 | } |
| 142 | |
| 143 | void allow_log_syscalls(struct filter_block *head) |
| 144 | { |
| 145 | unsigned int i; |
Kees Cook | 03b2af2 | 2014-12-18 17:11:13 -0800 | [diff] [blame] | 146 | for (i = 0; i < log_syscalls_len; i++) { |
| 147 | warn("allowing syscall: %s", log_syscalls[i]); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 148 | append_allow_syscall(head, lookup_syscall(log_syscalls[i])); |
Kees Cook | 03b2af2 | 2014-12-18 17:11:13 -0800 | [diff] [blame] | 149 | } |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 152 | unsigned int get_label_id(struct bpf_labels *labels, const char *label_str) |
| 153 | { |
| 154 | int label_id = bpf_label_id(labels, label_str); |
| 155 | if (label_id < 0) |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 156 | die("could not allocate BPF label string"); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 157 | return label_id; |
| 158 | } |
| 159 | |
| 160 | unsigned int group_end_lbl(struct bpf_labels *labels, int nr, int idx) |
| 161 | { |
| 162 | char lbl_str[MAX_BPF_LABEL_LEN]; |
| 163 | snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_%d_end", nr, idx); |
| 164 | return get_label_id(labels, lbl_str); |
| 165 | } |
| 166 | |
| 167 | unsigned int success_lbl(struct bpf_labels *labels, int nr) |
| 168 | { |
| 169 | char lbl_str[MAX_BPF_LABEL_LEN]; |
| 170 | snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_success", nr); |
| 171 | return get_label_id(labels, lbl_str); |
| 172 | } |
| 173 | |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 174 | int compile_atom(struct filter_block *head, char *atom, |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 175 | struct bpf_labels *labels, int nr, int group_idx) |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 176 | { |
| 177 | /* Splits the atom. */ |
| 178 | char *atom_ptr; |
| 179 | char *argidx_str = strtok_r(atom, " ", &atom_ptr); |
| 180 | char *operator_str = strtok_r(NULL, " ", &atom_ptr); |
| 181 | char *constant_str = strtok_r(NULL, " ", &atom_ptr); |
| 182 | |
| 183 | if (argidx_str == NULL || operator_str == NULL || constant_str == NULL) |
| 184 | return -1; |
| 185 | |
| 186 | int op = str_to_op(operator_str); |
| 187 | if (op < MIN_OPERATOR) |
| 188 | return -1; |
| 189 | |
| 190 | if (strncmp(argidx_str, "arg", 3)) { |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | char *argidx_ptr; |
| 195 | long int argidx = strtol(argidx_str + 3, &argidx_ptr, 10); |
| 196 | /* |
| 197 | * Checks to see if an actual argument index |
| 198 | * was parsed. |
| 199 | */ |
| 200 | if (argidx_ptr == argidx_str + 3) |
| 201 | return -1; |
| 202 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 203 | char *constant_str_ptr; |
| 204 | long int c = parse_constant(constant_str, &constant_str_ptr); |
| 205 | if (constant_str_ptr == constant_str) |
| 206 | return -1; |
| 207 | |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 208 | /* |
| 209 | * Looks up the label for the end of the AND statement |
| 210 | * this atom belongs to. |
| 211 | */ |
| 212 | unsigned int id = group_end_lbl(labels, nr, group_idx); |
| 213 | |
| 214 | /* |
| 215 | * Builds a BPF comparison between a syscall argument |
| 216 | * and a constant. |
| 217 | * The comparison lives inside an AND statement. |
| 218 | * If the comparison succeeds, we continue |
| 219 | * to the next comparison. |
| 220 | * If this comparison fails, the whole AND statement |
| 221 | * will fail, so we jump to the end of this AND statement. |
| 222 | */ |
| 223 | struct sock_filter *comp_block; |
| 224 | size_t len = bpf_arg_comp(&comp_block, op, argidx, c, id); |
| 225 | if (len == 0) |
| 226 | return -1; |
| 227 | |
| 228 | append_filter_block(head, comp_block, len); |
| 229 | return 0; |
| 230 | } |
| 231 | |
Jennifer Pullman | c0bfae0 | 2016-05-26 17:00:36 -0700 | [diff] [blame] | 232 | int compile_errno(struct filter_block *head, char *ret_errno, |
| 233 | int log_failures) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 234 | { |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 235 | char *errno_ptr; |
| 236 | |
| 237 | /* Splits the 'return' keyword and the actual errno value. */ |
| 238 | char *ret_str = strtok_r(ret_errno, " ", &errno_ptr); |
| 239 | if (strncmp(ret_str, "return", strlen("return"))) |
| 240 | return -1; |
| 241 | |
| 242 | char *errno_val_str = strtok_r(NULL, " ", &errno_ptr); |
| 243 | |
| 244 | if (errno_val_str) { |
| 245 | char *errno_val_ptr; |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 246 | int errno_val = parse_constant(errno_val_str, &errno_val_ptr); |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 247 | /* Checks to see if we parsed an actual errno. */ |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 248 | if (errno_val_ptr == errno_val_str || errno_val == -1) |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 249 | return -1; |
| 250 | |
| 251 | append_ret_errno(head, errno_val); |
| 252 | } else { |
Jennifer Pullman | c0bfae0 | 2016-05-26 17:00:36 -0700 | [diff] [blame] | 253 | if (!log_failures) |
| 254 | append_ret_kill(head); |
| 255 | else |
| 256 | append_ret_trap(head); |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 257 | } |
| 258 | return 0; |
| 259 | } |
| 260 | |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 261 | struct filter_block *compile_section(int nr, const char *policy_line, |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 262 | unsigned int entry_lbl_id, |
Jennifer Pullman | c0bfae0 | 2016-05-26 17:00:36 -0700 | [diff] [blame] | 263 | struct bpf_labels *labels, |
| 264 | int log_failures) |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 265 | { |
| 266 | /* |
| 267 | * |policy_line| should be an expression of the form: |
| 268 | * "arg0 == 3 && arg1 == 5 || arg0 == 0x8" |
| 269 | * |
| 270 | * This is, an expression in DNF (disjunctive normal form); |
| 271 | * a disjunction ('||') of one or more conjunctions ('&&') |
| 272 | * of one or more atoms. |
| 273 | * |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 274 | * Atoms are of the form "arg{DNUM} {OP} {NUM}" |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 275 | * where: |
| 276 | * - DNUM is a decimal number. |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 277 | * - OP is an operator: ==, !=, or & (flags set). |
| 278 | * - NUM is an octal, decimal, or hexadecimal number. |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 279 | * |
| 280 | * When the syscall arguments make the expression true, |
| 281 | * the syscall is allowed. If not, the process is killed. |
| 282 | * |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 283 | * To block a syscall without killing the process, |
| 284 | * |policy_line| can be of the form: |
| 285 | * "return <errno>" |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 286 | * |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 287 | * This "return {NUM}" policy line will block the syscall, |
| 288 | * make it return -1 and set |errno| to NUM. |
| 289 | * |
| 290 | * A regular policy line can also include a "return <errno>" clause, |
| 291 | * separated by a semicolon (';'): |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 292 | * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}" |
| 293 | * |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 294 | * If the syscall arguments don't make the expression true, |
| 295 | * the syscall will be blocked as above instead of killing the process. |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 296 | */ |
| 297 | |
| 298 | size_t len = 0; |
| 299 | int group_idx = 0; |
| 300 | |
| 301 | /* Checks for overly long policy lines. */ |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 302 | if (strlen(policy_line) >= MAX_POLICY_LINE_LENGTH) |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 303 | return NULL; |
| 304 | |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 305 | /* We will modify |policy_line|, so let's make a copy. */ |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 306 | char *line = strndup(policy_line, MAX_POLICY_LINE_LENGTH); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 307 | if (!line) |
| 308 | return NULL; |
| 309 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 310 | /* |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 311 | * We build the filter section as a collection of smaller |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 312 | * "filter blocks" linked together in a singly-linked list. |
| 313 | */ |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 314 | struct filter_block *head = new_filter_block(); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 315 | |
| 316 | /* |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 317 | * Filter sections begin with a label where the main filter |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 318 | * will jump after checking the syscall number. |
| 319 | */ |
| 320 | struct sock_filter *entry_label = new_instr_buf(ONE_INSTR); |
| 321 | set_bpf_lbl(entry_label, entry_lbl_id); |
| 322 | append_filter_block(head, entry_label, ONE_INSTR); |
| 323 | |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 324 | /* Checks whether we're unconditionally blocking this syscall. */ |
| 325 | if (strncmp(line, "return", strlen("return")) == 0) { |
Jennifer Pullman | c0bfae0 | 2016-05-26 17:00:36 -0700 | [diff] [blame] | 326 | if (compile_errno(head, line, log_failures) < 0) |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 327 | return NULL; |
| 328 | free(line); |
| 329 | return head; |
| 330 | } |
| 331 | |
| 332 | /* Splits the optional "return <errno>" part. */ |
| 333 | char *line_ptr; |
| 334 | char *arg_filter = strtok_r(line, ";", &line_ptr); |
| 335 | char *ret_errno = strtok_r(NULL, ";", &line_ptr); |
| 336 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 337 | /* |
| 338 | * Splits the policy line by '||' into conjunctions and each conjunction |
| 339 | * by '&&' into atoms. |
| 340 | */ |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 341 | char *arg_filter_str = arg_filter; |
| 342 | char *group; |
| 343 | while ((group = tokenize(&arg_filter_str, "||")) != NULL) { |
| 344 | char *group_str = group; |
| 345 | char *comp; |
| 346 | while ((comp = tokenize(&group_str, "&&")) != NULL) { |
| 347 | /* Compiles each atom into a BPF block. */ |
| 348 | if (compile_atom(head, comp, labels, nr, group_idx) < 0) |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 349 | return NULL; |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 350 | } |
| 351 | /* |
| 352 | * If the AND statement succeeds, we're done, |
| 353 | * so jump to SUCCESS line. |
| 354 | */ |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 355 | unsigned int id = success_lbl(labels, nr); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 356 | struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS); |
| 357 | len = set_bpf_jump_lbl(group_end_block, id); |
| 358 | /* |
| 359 | * The end of each AND statement falls after the |
| 360 | * jump to SUCCESS. |
| 361 | */ |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 362 | id = group_end_lbl(labels, nr, group_idx++); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 363 | len += set_bpf_lbl(group_end_block + len, id); |
| 364 | append_filter_block(head, group_end_block, len); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * If no AND statements succeed, we end up here, |
| 369 | * because we never jumped to SUCCESS. |
| 370 | * If we have to return an errno, do it, |
| 371 | * otherwise just kill the task. |
| 372 | */ |
| 373 | if (ret_errno) { |
Jennifer Pullman | c0bfae0 | 2016-05-26 17:00:36 -0700 | [diff] [blame] | 374 | if (compile_errno(head, ret_errno, log_failures) < 0) |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 375 | return NULL; |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 376 | } else { |
Jennifer Pullman | c0bfae0 | 2016-05-26 17:00:36 -0700 | [diff] [blame] | 377 | if (!log_failures) |
| 378 | append_ret_kill(head); |
| 379 | else |
| 380 | append_ret_trap(head); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Every time the filter succeeds we jump to a predefined SUCCESS |
| 385 | * label. Add that label and BPF RET_ALLOW code now. |
| 386 | */ |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 387 | unsigned int id = success_lbl(labels, nr); |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 388 | struct sock_filter *success_block = new_instr_buf(TWO_INSTRS); |
| 389 | len = set_bpf_lbl(success_block, id); |
| 390 | len += set_bpf_ret_allow(success_block + len); |
| 391 | append_filter_block(head, success_block, len); |
| 392 | |
| 393 | free(line); |
| 394 | return head; |
| 395 | } |
| 396 | |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 397 | int compile_filter(FILE *policy_file, struct sock_fprog *prog, int log_failures) |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 398 | { |
| 399 | char line[MAX_LINE_LENGTH]; |
| 400 | int line_count = 0; |
| 401 | |
| 402 | struct bpf_labels labels; |
| 403 | labels.count = 0; |
| 404 | |
Jorge Lucangeli Obes | b0cea43 | 2013-02-04 11:55:30 -0800 | [diff] [blame] | 405 | if (!policy_file) |
Jorge Lucangeli Obes | 6135db9 | 2012-07-27 13:37:30 -0700 | [diff] [blame] | 406 | return -1; |
| 407 | |
Jorge Lucangeli Obes | d96baf4 | 2013-03-26 15:11:30 -0700 | [diff] [blame] | 408 | struct filter_block *head = new_filter_block(); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 409 | struct filter_block *arg_blocks = NULL; |
| 410 | |
| 411 | /* Start filter by validating arch. */ |
| 412 | struct sock_filter *valid_arch = new_instr_buf(ARCH_VALIDATION_LEN); |
| 413 | size_t len = bpf_validate_arch(valid_arch); |
| 414 | append_filter_block(head, valid_arch, len); |
| 415 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 416 | /* Load syscall number. */ |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 417 | struct sock_filter *load_nr = new_instr_buf(ONE_INSTR); |
| 418 | len = bpf_load_syscall_nr(load_nr); |
| 419 | append_filter_block(head, load_nr, len); |
| 420 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 421 | /* If we're logging failures, allow the necessary syscalls first. */ |
| 422 | if (log_failures) |
| 423 | allow_log_syscalls(head); |
| 424 | |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 425 | /* |
| 426 | * Loop through all the lines in the policy file. |
| 427 | * Build a jump table for the syscall number. |
| 428 | * If the policy line has an arg filter, build the arg filter |
| 429 | * as well. |
| 430 | * Chain the filter sections together and dump them into |
| 431 | * the final buffer at the end. |
| 432 | */ |
Jorge Lucangeli Obes | b0cea43 | 2013-02-04 11:55:30 -0800 | [diff] [blame] | 433 | while (fgets(line, sizeof(line), policy_file)) { |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 434 | ++line_count; |
Jorge Lucangeli Obes | b0cea43 | 2013-02-04 11:55:30 -0800 | [diff] [blame] | 435 | char *policy_line = line; |
| 436 | char *syscall_name = strsep(&policy_line, ":"); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 437 | int nr = -1; |
| 438 | |
| 439 | syscall_name = strip(syscall_name); |
| 440 | |
| 441 | /* Allow comments and empty lines. */ |
| 442 | if (*syscall_name == '#' || *syscall_name == '\0') |
| 443 | continue; |
| 444 | |
Jorge Lucangeli Obes | b0cea43 | 2013-02-04 11:55:30 -0800 | [diff] [blame] | 445 | if (!policy_line) |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 446 | return -1; |
| 447 | |
| 448 | nr = lookup_syscall(syscall_name); |
Jorge Lucangeli Obes | 275b07f | 2012-08-28 11:50:29 -0700 | [diff] [blame] | 449 | if (nr < 0) { |
| 450 | warn("compile_filter: nonexistent syscall '%s'", |
| 451 | syscall_name); |
Jorge Lucangeli Obes | be351a2 | 2016-01-22 17:18:51 -0800 | [diff] [blame] | 452 | if (log_failures) { |
| 453 | /* |
| 454 | * If we're logging failures, assume we're in a |
| 455 | * debugging case and continue. |
| 456 | * This is not super risky because an invalid |
| 457 | * syscall name is likely caused by a typo or by |
| 458 | * leftover lines from a different architecture. |
| 459 | * In either case, not including a policy line |
| 460 | * is equivalent to killing the process if the |
| 461 | * syscall is made, so there's no added attack |
| 462 | * surface. |
| 463 | */ |
| 464 | continue; |
| 465 | } |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 466 | return -1; |
Jorge Lucangeli Obes | 275b07f | 2012-08-28 11:50:29 -0700 | [diff] [blame] | 467 | } |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 468 | |
Jorge Lucangeli Obes | b0cea43 | 2013-02-04 11:55:30 -0800 | [diff] [blame] | 469 | policy_line = strip(policy_line); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * For each syscall, add either a simple ALLOW, |
| 473 | * or an arg filter block. |
| 474 | */ |
Jorge Lucangeli Obes | b0cea43 | 2013-02-04 11:55:30 -0800 | [diff] [blame] | 475 | if (strcmp(policy_line, "1") == 0) { |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 476 | /* Add simple ALLOW. */ |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 477 | append_allow_syscall(head, nr); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 478 | } else { |
| 479 | /* |
| 480 | * Create and jump to the label that will hold |
| 481 | * the arg filter block. |
| 482 | */ |
| 483 | unsigned int id = bpf_label_id(&labels, syscall_name); |
| 484 | struct sock_filter *nr_comp = |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 485 | new_instr_buf(ALLOW_SYSCALL_LEN); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 486 | bpf_allow_syscall_args(nr_comp, nr, id); |
| 487 | append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN); |
| 488 | |
| 489 | /* Build the arg filter block. */ |
| 490 | struct filter_block *block = |
Jennifer Pullman | c0bfae0 | 2016-05-26 17:00:36 -0700 | [diff] [blame] | 491 | compile_section(nr, policy_line, id, &labels, |
| 492 | log_failures); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 493 | |
| 494 | if (!block) |
| 495 | return -1; |
| 496 | |
| 497 | if (arg_blocks) { |
| 498 | extend_filter_block_list(arg_blocks, block); |
| 499 | } else { |
| 500 | arg_blocks = block; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 505 | /* |
| 506 | * If none of the syscalls match, either fall back to KILL, |
| 507 | * or return TRAP. |
| 508 | */ |
| 509 | if (!log_failures) |
| 510 | append_ret_kill(head); |
| 511 | else |
| 512 | append_ret_trap(head); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 513 | |
| 514 | /* Allocate the final buffer, now that we know its size. */ |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 515 | size_t final_filter_len = |
| 516 | head->total_len + (arg_blocks ? arg_blocks->total_len : 0); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 517 | if (final_filter_len > BPF_MAXINSNS) |
| 518 | return -1; |
| 519 | |
| 520 | struct sock_filter *final_filter = |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 521 | calloc(final_filter_len, sizeof(struct sock_filter)); |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 522 | |
| 523 | if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0) |
| 524 | return -1; |
| 525 | |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 526 | if (flatten_block_list(arg_blocks, final_filter, head->total_len, |
| 527 | final_filter_len) < 0) |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 528 | return -1; |
| 529 | |
| 530 | free_block_list(head); |
| 531 | free_block_list(arg_blocks); |
| 532 | |
| 533 | bpf_resolve_jumps(&labels, final_filter, final_filter_len); |
| 534 | |
| 535 | free_label_strings(&labels); |
| 536 | |
| 537 | prog->filter = final_filter; |
| 538 | prog->len = final_filter_len; |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | int flatten_block_list(struct filter_block *head, struct sock_filter *filter, |
Jorge Lucangeli Obes | 565e978 | 2016-08-05 11:03:19 -0400 | [diff] [blame] | 543 | size_t index, size_t cap) |
Jorge Lucangeli Obes | d446726 | 2012-03-23 16:19:59 -0700 | [diff] [blame] | 544 | { |
| 545 | size_t _index = index; |
| 546 | |
| 547 | struct filter_block *curr; |
| 548 | size_t i; |
| 549 | |
| 550 | for (curr = head; curr; curr = curr->next) { |
| 551 | for (i = 0; i < curr->len; i++) { |
| 552 | if (_index >= cap) |
| 553 | return -1; |
| 554 | filter[_index++] = curr->instrs[i]; |
| 555 | } |
| 556 | } |
| 557 | return 0; |
| 558 | } |
| 559 | |
Jorge Lucangeli Obes | fc8ab53 | 2012-03-20 10:14:31 -0700 | [diff] [blame] | 560 | void free_block_list(struct filter_block *head) |
| 561 | { |
| 562 | struct filter_block *current, *prev; |
| 563 | |
| 564 | current = head; |
| 565 | while (current) { |
| 566 | free(current->instrs); |
| 567 | prev = current; |
| 568 | current = current->next; |
| 569 | free(prev); |
| 570 | } |
| 571 | } |