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