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