blob: 1372193f915f29df439b559dd338e1f68f652c95 [file] [log] [blame]
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -07001/* 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 Obesd4467262012-03-23 16:19:59 -070017#include "libsyscalls.h"
18
19#define MAX_LINE_LENGTH 1024
20
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070021#define error(_msg, ...) do { \
22 fprintf(stderr, "minijail: error: " _msg, ## __VA_ARGS__); \
23 abort(); \
24} while (0)
25
26int 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
40struct 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
49void 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 Obesd4467262012-03-23 16:19:59 -070079void 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 Obesfc8ab532012-03-20 10:14:31 -070092void 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
99void 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
106unsigned 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
114unsigned 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
121unsigned 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 Obesd4467262012-03-23 16:19:59 -0700128struct filter_block *compile_section(int nr, const char *policy_line,
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700129 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 Obesd4467262012-03-23 16:19:59 -0700245 labels, nr, group_idx);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700246
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 Obesd4467262012-03-23 16:19:59 -0700268 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700269 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 Obesd4467262012-03-23 16:19:59 -0700275 id = group_end_lbl(labels, nr, group_idx++);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700276 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 Obesd4467262012-03-23 16:19:59 -0700315 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700316 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 Obesd4467262012-03-23 16:19:59 -0700325int 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
334char *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
346int 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
354 struct filter_block *head = calloc(1, sizeof(struct filter_block));
355 if (!head)
356 return -1;
357 head->instrs = NULL;
358 head->last = head->next = NULL;
359 struct filter_block *arg_blocks = NULL;
360
361 /* Start filter by validating arch. */
362 struct sock_filter *valid_arch = new_instr_buf(ARCH_VALIDATION_LEN);
363 size_t len = bpf_validate_arch(valid_arch);
364 append_filter_block(head, valid_arch, len);
365
366 /* Loading syscall number. */
367 struct sock_filter *load_nr = new_instr_buf(ONE_INSTR);
368 len = bpf_load_syscall_nr(load_nr);
369 append_filter_block(head, load_nr, len);
370
371 /*
372 * Loop through all the lines in the policy file.
373 * Build a jump table for the syscall number.
374 * If the policy line has an arg filter, build the arg filter
375 * as well.
376 * Chain the filter sections together and dump them into
377 * the final buffer at the end.
378 */
379 while (fgets(line, sizeof(line), policy)) {
380 ++line_count;
381 char *policy = line;
382 char *syscall_name = strsep(&policy, ":");
383 int nr = -1;
384
385 syscall_name = strip(syscall_name);
386
387 /* Allow comments and empty lines. */
388 if (*syscall_name == '#' || *syscall_name == '\0')
389 continue;
390
391 if (!policy)
392 return -1;
393
394 nr = lookup_syscall(syscall_name);
395 if (nr < 0)
396 return -1;
397
398 policy = strip(policy);
399
400 /*
401 * For each syscall, add either a simple ALLOW,
402 * or an arg filter block.
403 */
404 if (strcmp(policy, "1") == 0) {
405 /* Add simple ALLOW. */
406 struct sock_filter *nr_comp =
407 new_instr_buf(ALLOW_SYSCALL_LEN);
408 bpf_allow_syscall(nr_comp, nr);
409 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
410 } else {
411 /*
412 * Create and jump to the label that will hold
413 * the arg filter block.
414 */
415 unsigned int id = bpf_label_id(&labels, syscall_name);
416 struct sock_filter *nr_comp =
417 new_instr_buf(ALLOW_SYSCALL_LEN);
418 bpf_allow_syscall_args(nr_comp, nr, id);
419 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
420
421 /* Build the arg filter block. */
422 struct filter_block *block =
423 compile_section(nr, policy, id, &labels);
424
425 if (!block)
426 return -1;
427
428 if (arg_blocks) {
429 extend_filter_block_list(arg_blocks, block);
430 } else {
431 arg_blocks = block;
432 }
433 }
434 }
435
436 /* If none of the syscalls match, fall back to KILL. */
437 struct sock_filter *kill_filter = new_instr_buf(ONE_INSTR);
438 set_bpf_ret_kill(kill_filter);
439 append_filter_block(head, kill_filter, ONE_INSTR);
440
441 /* Allocate the final buffer, now that we know its size. */
442 size_t final_filter_len = head->total_len +
443 (arg_blocks? arg_blocks->total_len : 0);
444 if (final_filter_len > BPF_MAXINSNS)
445 return -1;
446
447 struct sock_filter *final_filter =
448 calloc(final_filter_len, sizeof(struct sock_filter));
449
450 if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0)
451 return -1;
452
453 if (flatten_block_list(arg_blocks, final_filter,
454 head->total_len, final_filter_len) < 0)
455 return -1;
456
457 free_block_list(head);
458 free_block_list(arg_blocks);
459
460 bpf_resolve_jumps(&labels, final_filter, final_filter_len);
461
462 free_label_strings(&labels);
463
464 prog->filter = final_filter;
465 prog->len = final_filter_len;
466 return 0;
467}
468
469int flatten_block_list(struct filter_block *head, struct sock_filter *filter,
470 size_t index, size_t cap)
471{
472 size_t _index = index;
473
474 struct filter_block *curr;
475 size_t i;
476
477 for (curr = head; curr; curr = curr->next) {
478 for (i = 0; i < curr->len; i++) {
479 if (_index >= cap)
480 return -1;
481 filter[_index++] = curr->instrs[i];
482 }
483 }
484 return 0;
485}
486
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700487void free_block_list(struct filter_block *head)
488{
489 struct filter_block *current, *prev;
490
491 current = head;
492 while (current) {
493 free(current->instrs);
494 prev = current;
495 current = current->next;
496 free(prev);
497 }
498}