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