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