blob: 5597da259a3318bf3c80cc64e717f343eb698b53 [file] [log] [blame]
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -07002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -07004 */
5
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -07006#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -07009
10#include "syscall_filter.h"
11
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070012#include "util.h"
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070013
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070014#define MAX_LINE_LENGTH 1024
15#define MAX_POLICY_LINE_LENGTH 1024
16
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070017#define ONE_INSTR 1
18#define TWO_INSTRS 2
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070019
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070020int str_to_op(const char *op_str)
21{
22 if (!strcmp(op_str, "==")) {
23 return EQ;
24 } else if (!strcmp(op_str, "!=")) {
25 return NE;
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080026 } else if (!strcmp(op_str, "&")) {
27 return SET;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070028 } else {
29 return 0;
30 }
31}
32
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070033struct sock_filter *new_instr_buf(size_t count)
34{
35 struct sock_filter *buf = calloc(count, sizeof(struct sock_filter));
36 if (!buf)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -070037 die("could not allocate BPF instruction buffer");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070038
39 return buf;
40}
41
42void append_filter_block(struct filter_block *head,
43 struct sock_filter *instrs, size_t len)
44{
45 struct filter_block *new_last;
46
47 /*
48 * If |head| has no filter assigned yet,
49 * we don't create a new node.
50 */
51 if (head->instrs == NULL) {
52 new_last = head;
53 } else {
54 new_last = calloc(1, sizeof(struct filter_block));
55 if (!new_last)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -070056 die("could not allocate BPF filter block");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070057
58 if (head->next != NULL) {
59 head->last->next = new_last;
60 head->last = new_last;
61 } else {
62 head->last = head->next = new_last;
63 }
64 head->total_len += len;
65 }
66
67 new_last->instrs = instrs;
68 new_last->total_len = new_last->len = len;
69 new_last->last = new_last->next = NULL;
70}
71
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070072void extend_filter_block_list(struct filter_block *list,
73 struct filter_block *another)
74{
75 if (list->last != NULL) {
76 list->last->next = another;
77 list->last = another->last;
78 } else {
79 list->next = another;
80 list->last = another->last;
81 }
82 list->total_len += another->total_len;
83}
84
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070085void append_ret_kill(struct filter_block *head)
86{
87 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
88 set_bpf_ret_kill(filter);
89 append_filter_block(head, filter, ONE_INSTR);
90}
91
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070092void append_ret_trap(struct filter_block *head)
93{
94 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
95 set_bpf_ret_trap(filter);
96 append_filter_block(head, filter, ONE_INSTR);
97}
98
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070099void 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
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700106void append_allow_syscall(struct filter_block *head, int nr) {
107 struct sock_filter *filter = new_instr_buf(ALLOW_SYSCALL_LEN);
108 size_t len = bpf_allow_syscall(filter, nr);
109 if (len != ALLOW_SYSCALL_LEN)
110 die("error building syscall number comparison");
111
112 append_filter_block(head, filter, len);
113}
114
115void allow_log_syscalls(struct filter_block *head)
116{
117 unsigned int i;
118 for (i = 0; i < log_syscalls_len; i++)
119 append_allow_syscall(head, lookup_syscall(log_syscalls[i]));
120}
121
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700122unsigned int get_label_id(struct bpf_labels *labels, const char *label_str)
123{
124 int label_id = bpf_label_id(labels, label_str);
125 if (label_id < 0)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700126 die("could not allocate BPF label string");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700127 return label_id;
128}
129
130unsigned int group_end_lbl(struct bpf_labels *labels, int nr, int idx)
131{
132 char lbl_str[MAX_BPF_LABEL_LEN];
133 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_%d_end", nr, idx);
134 return get_label_id(labels, lbl_str);
135}
136
137unsigned int success_lbl(struct bpf_labels *labels, int nr)
138{
139 char lbl_str[MAX_BPF_LABEL_LEN];
140 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_success", nr);
141 return get_label_id(labels, lbl_str);
142}
143
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800144int compile_atom(struct filter_block *head, char *atom,
145 struct bpf_labels *labels, int nr, int group_idx)
146{
147 /* Splits the atom. */
148 char *atom_ptr;
149 char *argidx_str = strtok_r(atom, " ", &atom_ptr);
150 char *operator_str = strtok_r(NULL, " ", &atom_ptr);
151 char *constant_str = strtok_r(NULL, " ", &atom_ptr);
152
153 if (argidx_str == NULL || operator_str == NULL || constant_str == NULL)
154 return -1;
155
156 int op = str_to_op(operator_str);
157 if (op < MIN_OPERATOR)
158 return -1;
159
160 if (strncmp(argidx_str, "arg", 3)) {
161 return -1;
162 }
163
164 char *argidx_ptr;
165 long int argidx = strtol(argidx_str + 3, &argidx_ptr, 10);
166 /*
167 * Checks to see if an actual argument index
168 * was parsed.
169 */
170 if (argidx_ptr == argidx_str + 3)
171 return -1;
172
173 long int c = strtol(constant_str, NULL, 0);
174 /*
175 * Looks up the label for the end of the AND statement
176 * this atom belongs to.
177 */
178 unsigned int id = group_end_lbl(labels, nr, group_idx);
179
180 /*
181 * Builds a BPF comparison between a syscall argument
182 * and a constant.
183 * The comparison lives inside an AND statement.
184 * If the comparison succeeds, we continue
185 * to the next comparison.
186 * If this comparison fails, the whole AND statement
187 * will fail, so we jump to the end of this AND statement.
188 */
189 struct sock_filter *comp_block;
190 size_t len = bpf_arg_comp(&comp_block, op, argidx, c, id);
191 if (len == 0)
192 return -1;
193
194 append_filter_block(head, comp_block, len);
195 return 0;
196}
197
198int compile_errno(struct filter_block *head, char *ret_errno) {
199 char *errno_ptr;
200
201 /* Splits the 'return' keyword and the actual errno value. */
202 char *ret_str = strtok_r(ret_errno, " ", &errno_ptr);
203 if (strncmp(ret_str, "return", strlen("return")))
204 return -1;
205
206 char *errno_val_str = strtok_r(NULL, " ", &errno_ptr);
207
208 if (errno_val_str) {
209 char *errno_val_ptr;
210 int errno_val = strtol(
211 errno_val_str, &errno_val_ptr, 0);
212 /* Checks to see if we parsed an actual errno. */
213 if (errno_val_ptr == errno_val_str)
214 return -1;
215
216 append_ret_errno(head, errno_val);
217 } else {
218 append_ret_kill(head);
219 }
220 return 0;
221}
222
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700223struct filter_block *compile_section(int nr, const char *policy_line,
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700224 unsigned int entry_lbl_id, struct bpf_labels *labels)
225{
226 /*
227 * |policy_line| should be an expression of the form:
228 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8"
229 *
230 * This is, an expression in DNF (disjunctive normal form);
231 * a disjunction ('||') of one or more conjunctions ('&&')
232 * of one or more atoms.
233 *
234 * Atoms are of the form "arg{DNUM} OP NUM"
235 * where:
236 * - DNUM is a decimal number.
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800237 * - OP is an operator: ==, !=, or & (flags set).
238 * - NUM is an octal, decimal, or hexadecimal number.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700239 *
240 * When the syscall arguments make the expression true,
241 * the syscall is allowed. If not, the process is killed.
242 *
243 * To avoid killing the process, a policy line can include an
244 * optional "return <errno>" clause:
245 *
246 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}"
247 *
248 * In this case, the syscall will return -1 and |errno| will
249 * be set to NUM.
250 */
251
252 size_t len = 0;
253 int group_idx = 0;
254
255 /* Checks for overly long policy lines. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700256 if (strlen(policy_line) >= MAX_POLICY_LINE_LENGTH)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700257 return NULL;
258
259 /* strtok() modifies its first argument, so let's make a copy. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700260 char *line = strndup(policy_line, MAX_POLICY_LINE_LENGTH);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700261 if (!line)
262 return NULL;
263
264 /* Splits the optional "return <errno>" part. */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800265 char *line_ptr;
266 char *arg_filter = strtok_r(line, ";", &line_ptr);
267 char *ret_errno = strtok_r(NULL, ";", &line_ptr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700268
269 /*
270 * We build the argument filter as a collection of smaller
271 * "filter blocks" linked together in a singly-linked list.
272 */
273 struct filter_block *head = calloc(1, sizeof(struct filter_block));
274 if (!head)
275 return NULL;
276
277 head->instrs = NULL;
278 head->last = head->next = NULL;
279
280 /*
281 * Argument filters begin with a label where the main filter
282 * will jump after checking the syscall number.
283 */
284 struct sock_filter *entry_label = new_instr_buf(ONE_INSTR);
285 set_bpf_lbl(entry_label, entry_lbl_id);
286 append_filter_block(head, entry_label, ONE_INSTR);
287
288 /*
289 * Splits the policy line by '||' into conjunctions and each conjunction
290 * by '&&' into atoms.
291 */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800292 char *arg_filter_str = arg_filter;
293 char *group;
294 while ((group = tokenize(&arg_filter_str, "||")) != NULL) {
295 char *group_str = group;
296 char *comp;
297 while ((comp = tokenize(&group_str, "&&")) != NULL) {
298 /* Compiles each atom into a BPF block. */
299 if (compile_atom(head, comp, labels, nr, group_idx) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700300 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700301 }
302 /*
303 * If the AND statement succeeds, we're done,
304 * so jump to SUCCESS line.
305 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700306 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700307 struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS);
308 len = set_bpf_jump_lbl(group_end_block, id);
309 /*
310 * The end of each AND statement falls after the
311 * jump to SUCCESS.
312 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700313 id = group_end_lbl(labels, nr, group_idx++);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700314 len += set_bpf_lbl(group_end_block + len, id);
315 append_filter_block(head, group_end_block, len);
316 }
317
318 /*
319 * If no AND statements succeed, we end up here,
320 * because we never jumped to SUCCESS.
321 * If we have to return an errno, do it,
322 * otherwise just kill the task.
323 */
324 if (ret_errno) {
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800325 if (compile_errno(head, ret_errno) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700326 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700327 } else {
328 append_ret_kill(head);
329 }
330
331 /*
332 * Every time the filter succeeds we jump to a predefined SUCCESS
333 * label. Add that label and BPF RET_ALLOW code now.
334 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700335 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700336 struct sock_filter *success_block = new_instr_buf(TWO_INSTRS);
337 len = set_bpf_lbl(success_block, id);
338 len += set_bpf_ret_allow(success_block + len);
339 append_filter_block(head, success_block, len);
340
341 free(line);
342 return head;
343}
344
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700345int compile_filter(FILE *policy, struct sock_fprog *prog,
346 int log_failures)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700347{
348 char line[MAX_LINE_LENGTH];
349 int line_count = 0;
350
351 struct bpf_labels labels;
352 labels.count = 0;
353
Jorge Lucangeli Obes6135db92012-07-27 13:37:30 -0700354 if (!policy)
355 return -1;
356
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700357 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
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700369 /* Load syscall number. */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700370 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
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700374 /* If we're logging failures, allow the necessary syscalls first. */
375 if (log_failures)
376 allow_log_syscalls(head);
377
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700378 /*
379 * Loop through all the lines in the policy file.
380 * Build a jump table for the syscall number.
381 * If the policy line has an arg filter, build the arg filter
382 * as well.
383 * Chain the filter sections together and dump them into
384 * the final buffer at the end.
385 */
386 while (fgets(line, sizeof(line), policy)) {
387 ++line_count;
388 char *policy = line;
389 char *syscall_name = strsep(&policy, ":");
390 int nr = -1;
391
392 syscall_name = strip(syscall_name);
393
394 /* Allow comments and empty lines. */
395 if (*syscall_name == '#' || *syscall_name == '\0')
396 continue;
397
398 if (!policy)
399 return -1;
400
401 nr = lookup_syscall(syscall_name);
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700402 if (nr < 0) {
403 warn("compile_filter: nonexistent syscall '%s'",
404 syscall_name);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700405 return -1;
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700406 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700407
408 policy = strip(policy);
409
410 /*
411 * For each syscall, add either a simple ALLOW,
412 * or an arg filter block.
413 */
414 if (strcmp(policy, "1") == 0) {
415 /* Add simple ALLOW. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700416 append_allow_syscall(head, nr);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700417 } else {
418 /*
419 * Create and jump to the label that will hold
420 * the arg filter block.
421 */
422 unsigned int id = bpf_label_id(&labels, syscall_name);
423 struct sock_filter *nr_comp =
424 new_instr_buf(ALLOW_SYSCALL_LEN);
425 bpf_allow_syscall_args(nr_comp, nr, id);
426 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
427
428 /* Build the arg filter block. */
429 struct filter_block *block =
430 compile_section(nr, policy, id, &labels);
431
432 if (!block)
433 return -1;
434
435 if (arg_blocks) {
436 extend_filter_block_list(arg_blocks, block);
437 } else {
438 arg_blocks = block;
439 }
440 }
441 }
442
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700443 /*
444 * If none of the syscalls match, either fall back to KILL,
445 * or return TRAP.
446 */
447 if (!log_failures)
448 append_ret_kill(head);
449 else
450 append_ret_trap(head);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700451
452 /* Allocate the final buffer, now that we know its size. */
453 size_t final_filter_len = head->total_len +
454 (arg_blocks? arg_blocks->total_len : 0);
455 if (final_filter_len > BPF_MAXINSNS)
456 return -1;
457
458 struct sock_filter *final_filter =
459 calloc(final_filter_len, sizeof(struct sock_filter));
460
461 if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0)
462 return -1;
463
464 if (flatten_block_list(arg_blocks, final_filter,
465 head->total_len, final_filter_len) < 0)
466 return -1;
467
468 free_block_list(head);
469 free_block_list(arg_blocks);
470
471 bpf_resolve_jumps(&labels, final_filter, final_filter_len);
472
473 free_label_strings(&labels);
474
475 prog->filter = final_filter;
476 prog->len = final_filter_len;
477 return 0;
478}
479
480int flatten_block_list(struct filter_block *head, struct sock_filter *filter,
481 size_t index, size_t cap)
482{
483 size_t _index = index;
484
485 struct filter_block *curr;
486 size_t i;
487
488 for (curr = head; curr; curr = curr->next) {
489 for (i = 0; i < curr->len; i++) {
490 if (_index >= cap)
491 return -1;
492 filter[_index++] = curr->instrs[i];
493 }
494 }
495 return 0;
496}
497
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700498void free_block_list(struct filter_block *head)
499{
500 struct filter_block *current, *prev;
501
502 current = head;
503 while (current) {
504 free(current->instrs);
505 prev = current;
506 current = current->next;
507 free(prev);
508 }
509}