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 | |
| 17 | #define error(_msg, ...) do { \ |
| 18 | fprintf(stderr, "minijail: error: " _msg, ## __VA_ARGS__); \ |
| 19 | abort(); \ |
| 20 | } while (0) |
| 21 | |
| 22 | int str_to_op(const char *op_str) |
| 23 | { |
| 24 | if (!strcmp(op_str, "==")) { |
| 25 | return EQ; |
| 26 | } else if (!strcmp(op_str, "!=")) { |
| 27 | return NE; |
| 28 | } else { |
| 29 | return 0; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #define ONE_INSTR 1 |
| 34 | #define TWO_INSTRS 2 |
| 35 | |
| 36 | struct sock_filter *new_instr_buf(size_t count) |
| 37 | { |
| 38 | struct sock_filter *buf = calloc(count, sizeof(struct sock_filter)); |
| 39 | if (!buf) |
| 40 | error("could not allocate BPF instruction buffer"); |
| 41 | |
| 42 | return buf; |
| 43 | } |
| 44 | |
| 45 | void append_filter_block(struct filter_block *head, |
| 46 | struct sock_filter *instrs, size_t len) |
| 47 | { |
| 48 | struct filter_block *new_last; |
| 49 | |
| 50 | /* |
| 51 | * If |head| has no filter assigned yet, |
| 52 | * we don't create a new node. |
| 53 | */ |
| 54 | if (head->instrs == NULL) { |
| 55 | new_last = head; |
| 56 | } else { |
| 57 | new_last = calloc(1, sizeof(struct filter_block)); |
| 58 | if (!new_last) |
| 59 | error("could not allocate BPF filter block"); |
| 60 | |
| 61 | if (head->next != NULL) { |
| 62 | head->last->next = new_last; |
| 63 | head->last = new_last; |
| 64 | } else { |
| 65 | head->last = head->next = new_last; |
| 66 | } |
| 67 | head->total_len += len; |
| 68 | } |
| 69 | |
| 70 | new_last->instrs = instrs; |
| 71 | new_last->total_len = new_last->len = len; |
| 72 | new_last->last = new_last->next = NULL; |
| 73 | } |
| 74 | |
| 75 | void append_ret_kill(struct filter_block *head) |
| 76 | { |
| 77 | struct sock_filter *filter = new_instr_buf(ONE_INSTR); |
| 78 | set_bpf_ret_kill(filter); |
| 79 | append_filter_block(head, filter, ONE_INSTR); |
| 80 | } |
| 81 | |
| 82 | void append_ret_errno(struct filter_block *head, int errno_val) |
| 83 | { |
| 84 | struct sock_filter *filter = new_instr_buf(ONE_INSTR); |
| 85 | set_bpf_ret_errno(filter, errno_val); |
| 86 | append_filter_block(head, filter, ONE_INSTR); |
| 87 | } |
| 88 | |
| 89 | unsigned int get_label_id(struct bpf_labels *labels, const char *label_str) |
| 90 | { |
| 91 | int label_id = bpf_label_id(labels, label_str); |
| 92 | if (label_id < 0) |
| 93 | error("could not allocate BPF label string"); |
| 94 | return label_id; |
| 95 | } |
| 96 | |
| 97 | unsigned int group_end_lbl(struct bpf_labels *labels, int nr, int idx) |
| 98 | { |
| 99 | char lbl_str[MAX_BPF_LABEL_LEN]; |
| 100 | snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_%d_end", nr, idx); |
| 101 | return get_label_id(labels, lbl_str); |
| 102 | } |
| 103 | |
| 104 | unsigned int success_lbl(struct bpf_labels *labels, int nr) |
| 105 | { |
| 106 | char lbl_str[MAX_BPF_LABEL_LEN]; |
| 107 | snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_success", nr); |
| 108 | return get_label_id(labels, lbl_str); |
| 109 | } |
| 110 | |
| 111 | struct filter_block *compile_section(int syscall_nr, const char *policy_line, |
| 112 | unsigned int entry_lbl_id, struct bpf_labels *labels) |
| 113 | { |
| 114 | /* |
| 115 | * |policy_line| should be an expression of the form: |
| 116 | * "arg0 == 3 && arg1 == 5 || arg0 == 0x8" |
| 117 | * |
| 118 | * This is, an expression in DNF (disjunctive normal form); |
| 119 | * a disjunction ('||') of one or more conjunctions ('&&') |
| 120 | * of one or more atoms. |
| 121 | * |
| 122 | * Atoms are of the form "arg{DNUM} OP NUM" |
| 123 | * where: |
| 124 | * - DNUM is a decimal number. |
| 125 | * - OP is a comparison operator (== or != for now). |
| 126 | * - NUM is a decimal or hexadecimal number. |
| 127 | * |
| 128 | * When the syscall arguments make the expression true, |
| 129 | * the syscall is allowed. If not, the process is killed. |
| 130 | * |
| 131 | * To avoid killing the process, a policy line can include an |
| 132 | * optional "return <errno>" clause: |
| 133 | * |
| 134 | * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}" |
| 135 | * |
| 136 | * In this case, the syscall will return -1 and |errno| will |
| 137 | * be set to NUM. |
| 138 | */ |
| 139 | |
| 140 | size_t len = 0; |
| 141 | int group_idx = 0; |
| 142 | |
| 143 | /* Checks for overly long policy lines. */ |
| 144 | if (strlen(policy_line) >= MAX_POLICY_LINE_LEN) |
| 145 | return NULL; |
| 146 | |
| 147 | /* strtok() modifies its first argument, so let's make a copy. */ |
| 148 | char *line = strndup(policy_line, MAX_POLICY_LINE_LEN); |
| 149 | if (!line) |
| 150 | return NULL; |
| 151 | |
| 152 | /* Splits the optional "return <errno>" part. */ |
| 153 | char *arg_filter = strtok(line, ";"); |
| 154 | char *ret_errno = strtok(NULL, ";"); |
| 155 | |
| 156 | /* |
| 157 | * We build the argument filter as a collection of smaller |
| 158 | * "filter blocks" linked together in a singly-linked list. |
| 159 | */ |
| 160 | struct filter_block *head = calloc(1, sizeof(struct filter_block)); |
| 161 | if (!head) |
| 162 | return NULL; |
| 163 | |
| 164 | head->instrs = NULL; |
| 165 | head->last = head->next = NULL; |
| 166 | |
| 167 | /* |
| 168 | * Argument filters begin with a label where the main filter |
| 169 | * will jump after checking the syscall number. |
| 170 | */ |
| 171 | struct sock_filter *entry_label = new_instr_buf(ONE_INSTR); |
| 172 | set_bpf_lbl(entry_label, entry_lbl_id); |
| 173 | append_filter_block(head, entry_label, ONE_INSTR); |
| 174 | |
| 175 | /* |
| 176 | * Splits the policy line by '||' into conjunctions and each conjunction |
| 177 | * by '&&' into atoms. |
| 178 | */ |
| 179 | char *arg_filter_str; |
| 180 | char *arg_filter_ptr; |
| 181 | for (arg_filter_str = arg_filter; ; arg_filter_str = NULL) { |
| 182 | char *group = strtok_r(arg_filter_str, "||", &arg_filter_ptr); |
| 183 | |
| 184 | if (group == NULL) |
| 185 | break; |
| 186 | |
| 187 | char *group_str; |
| 188 | char *group_ptr; |
| 189 | for (group_str = group; ; group_str = NULL) { |
| 190 | char *comp = strtok_r(group_str, "&&", &group_ptr); |
| 191 | |
| 192 | if (comp == NULL) |
| 193 | break; |
| 194 | |
| 195 | /* Splits each atom. */ |
| 196 | char *comp_ptr; |
| 197 | char *argidx_str = strtok_r(comp, " ", &comp_ptr); |
| 198 | char *operator_str = strtok_r(NULL, " ", &comp_ptr); |
| 199 | char *constant_str = strtok_r(NULL, " ", &comp_ptr); |
| 200 | |
| 201 | if (argidx_str == NULL || |
| 202 | operator_str == NULL || |
| 203 | constant_str == NULL) |
| 204 | return NULL; |
| 205 | |
| 206 | int op = str_to_op(operator_str); |
| 207 | |
| 208 | if (op < MIN_OPERATOR) |
| 209 | return NULL; |
| 210 | |
| 211 | if (strncmp(argidx_str, "arg", 3)) { |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | char *argidx_ptr; |
| 216 | long int argidx = strtol( |
| 217 | argidx_str + 3, &argidx_ptr, 10); |
| 218 | /* |
| 219 | * Checks to see if an actual argument index |
| 220 | * was parsed. |
| 221 | */ |
| 222 | if (argidx_ptr == argidx_str + 3) { |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | long int c = strtol(constant_str, NULL, 0); |
| 227 | unsigned int id = group_end_lbl( |
| 228 | labels, syscall_nr, group_idx); |
| 229 | |
| 230 | /* |
| 231 | * Builds a BPF comparison between a syscall argument |
| 232 | * and a constant. |
| 233 | * The comparison lives inside an AND statement. |
| 234 | * If the comparison succeeds, we continue |
| 235 | * to the next comparison. |
| 236 | * If this comparison fails, the whole AND statement |
| 237 | * will fail, so we jump to the end of this AND statement. |
| 238 | */ |
| 239 | struct sock_filter *comp_block; |
| 240 | len = bpf_arg_comp(&comp_block, |
| 241 | op, argidx, c, id); |
| 242 | if (len == 0) |
| 243 | return NULL; |
| 244 | |
| 245 | append_filter_block(head, comp_block, len); |
| 246 | } |
| 247 | /* |
| 248 | * If the AND statement succeeds, we're done, |
| 249 | * so jump to SUCCESS line. |
| 250 | */ |
| 251 | unsigned int id = success_lbl(labels, syscall_nr); |
| 252 | struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS); |
| 253 | len = set_bpf_jump_lbl(group_end_block, id); |
| 254 | /* |
| 255 | * The end of each AND statement falls after the |
| 256 | * jump to SUCCESS. |
| 257 | */ |
| 258 | id = group_end_lbl(labels, syscall_nr, group_idx++); |
| 259 | len += set_bpf_lbl(group_end_block + len, id); |
| 260 | append_filter_block(head, group_end_block, len); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * If no AND statements succeed, we end up here, |
| 265 | * because we never jumped to SUCCESS. |
| 266 | * If we have to return an errno, do it, |
| 267 | * otherwise just kill the task. |
| 268 | */ |
| 269 | if (ret_errno) { |
| 270 | char *errno_ptr; |
| 271 | |
| 272 | char *ret_str = strtok_r(ret_errno, " ", &errno_ptr); |
| 273 | if (strncmp(ret_str, "return", strlen("return"))) |
| 274 | return NULL; |
| 275 | |
| 276 | char *errno_val_str = strtok_r(NULL, " ", &errno_ptr); |
| 277 | |
| 278 | if (errno_val_str) { |
| 279 | char *errno_val_ptr; |
| 280 | int errno_val = strtol( |
| 281 | errno_val_str, &errno_val_ptr, 0); |
| 282 | /* Checks to see if we parsed an actual errno. */ |
| 283 | if (errno_val_ptr == errno_val_str) |
| 284 | return NULL; |
| 285 | |
| 286 | append_ret_errno(head, errno_val); |
| 287 | } else { |
| 288 | append_ret_kill(head); |
| 289 | } |
| 290 | } else { |
| 291 | append_ret_kill(head); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Every time the filter succeeds we jump to a predefined SUCCESS |
| 296 | * label. Add that label and BPF RET_ALLOW code now. |
| 297 | */ |
| 298 | unsigned int id = success_lbl(labels, syscall_nr); |
| 299 | struct sock_filter *success_block = new_instr_buf(TWO_INSTRS); |
| 300 | len = set_bpf_lbl(success_block, id); |
| 301 | len += set_bpf_ret_allow(success_block + len); |
| 302 | append_filter_block(head, success_block, len); |
| 303 | |
| 304 | free(line); |
| 305 | return head; |
| 306 | } |
| 307 | |
| 308 | void free_block_list(struct filter_block *head) |
| 309 | { |
| 310 | struct filter_block *current, *prev; |
| 311 | |
| 312 | current = head; |
| 313 | while (current) { |
| 314 | free(current->instrs); |
| 315 | prev = current; |
| 316 | current = current->next; |
| 317 | free(prev); |
| 318 | } |
| 319 | } |