blob: 9d4ad58e49f4a2d5485929a4c83630ead2e4c6fc [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
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070042struct filter_block *new_filter_block()
43{
44 struct filter_block *block = calloc(1, sizeof(struct filter_block));
45 if (!block)
46 die("could not allocate BPF filter block");
47
48 block->instrs = NULL;
49 block->last = block->next = NULL;
50
51 return block;
52}
53
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070054void append_filter_block(struct filter_block *head,
55 struct sock_filter *instrs, size_t len)
56{
57 struct filter_block *new_last;
58
59 /*
60 * If |head| has no filter assigned yet,
61 * we don't create a new node.
62 */
63 if (head->instrs == NULL) {
64 new_last = head;
65 } else {
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070066 new_last = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070067 if (head->next != NULL) {
68 head->last->next = new_last;
69 head->last = new_last;
70 } else {
71 head->last = head->next = new_last;
72 }
73 head->total_len += len;
74 }
75
76 new_last->instrs = instrs;
77 new_last->total_len = new_last->len = len;
78 new_last->last = new_last->next = NULL;
79}
80
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070081void extend_filter_block_list(struct filter_block *list,
82 struct filter_block *another)
83{
84 if (list->last != NULL) {
85 list->last->next = another;
86 list->last = another->last;
87 } else {
88 list->next = another;
89 list->last = another->last;
90 }
91 list->total_len += another->total_len;
92}
93
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070094void append_ret_kill(struct filter_block *head)
95{
96 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
97 set_bpf_ret_kill(filter);
98 append_filter_block(head, filter, ONE_INSTR);
99}
100
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700101void append_ret_trap(struct filter_block *head)
102{
103 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
104 set_bpf_ret_trap(filter);
105 append_filter_block(head, filter, ONE_INSTR);
106}
107
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700108void append_ret_errno(struct filter_block *head, int errno_val)
109{
110 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
111 set_bpf_ret_errno(filter, errno_val);
112 append_filter_block(head, filter, ONE_INSTR);
113}
114
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700115void append_allow_syscall(struct filter_block *head, int nr) {
116 struct sock_filter *filter = new_instr_buf(ALLOW_SYSCALL_LEN);
117 size_t len = bpf_allow_syscall(filter, nr);
118 if (len != ALLOW_SYSCALL_LEN)
119 die("error building syscall number comparison");
120
121 append_filter_block(head, filter, len);
122}
123
124void allow_log_syscalls(struct filter_block *head)
125{
126 unsigned int i;
127 for (i = 0; i < log_syscalls_len; i++)
128 append_allow_syscall(head, lookup_syscall(log_syscalls[i]));
129}
130
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700131unsigned int get_label_id(struct bpf_labels *labels, const char *label_str)
132{
133 int label_id = bpf_label_id(labels, label_str);
134 if (label_id < 0)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700135 die("could not allocate BPF label string");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700136 return label_id;
137}
138
139unsigned int group_end_lbl(struct bpf_labels *labels, int nr, int idx)
140{
141 char lbl_str[MAX_BPF_LABEL_LEN];
142 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_%d_end", nr, idx);
143 return get_label_id(labels, lbl_str);
144}
145
146unsigned int success_lbl(struct bpf_labels *labels, int nr)
147{
148 char lbl_str[MAX_BPF_LABEL_LEN];
149 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_success", nr);
150 return get_label_id(labels, lbl_str);
151}
152
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800153int compile_atom(struct filter_block *head, char *atom,
154 struct bpf_labels *labels, int nr, int group_idx)
155{
156 /* Splits the atom. */
157 char *atom_ptr;
158 char *argidx_str = strtok_r(atom, " ", &atom_ptr);
159 char *operator_str = strtok_r(NULL, " ", &atom_ptr);
160 char *constant_str = strtok_r(NULL, " ", &atom_ptr);
161
162 if (argidx_str == NULL || operator_str == NULL || constant_str == NULL)
163 return -1;
164
165 int op = str_to_op(operator_str);
166 if (op < MIN_OPERATOR)
167 return -1;
168
169 if (strncmp(argidx_str, "arg", 3)) {
170 return -1;
171 }
172
173 char *argidx_ptr;
174 long int argidx = strtol(argidx_str + 3, &argidx_ptr, 10);
175 /*
176 * Checks to see if an actual argument index
177 * was parsed.
178 */
179 if (argidx_ptr == argidx_str + 3)
180 return -1;
181
182 long int c = strtol(constant_str, NULL, 0);
183 /*
184 * Looks up the label for the end of the AND statement
185 * this atom belongs to.
186 */
187 unsigned int id = group_end_lbl(labels, nr, group_idx);
188
189 /*
190 * Builds a BPF comparison between a syscall argument
191 * and a constant.
192 * The comparison lives inside an AND statement.
193 * If the comparison succeeds, we continue
194 * to the next comparison.
195 * If this comparison fails, the whole AND statement
196 * will fail, so we jump to the end of this AND statement.
197 */
198 struct sock_filter *comp_block;
199 size_t len = bpf_arg_comp(&comp_block, op, argidx, c, id);
200 if (len == 0)
201 return -1;
202
203 append_filter_block(head, comp_block, len);
204 return 0;
205}
206
207int compile_errno(struct filter_block *head, char *ret_errno) {
208 char *errno_ptr;
209
210 /* Splits the 'return' keyword and the actual errno value. */
211 char *ret_str = strtok_r(ret_errno, " ", &errno_ptr);
212 if (strncmp(ret_str, "return", strlen("return")))
213 return -1;
214
215 char *errno_val_str = strtok_r(NULL, " ", &errno_ptr);
216
217 if (errno_val_str) {
218 char *errno_val_ptr;
219 int errno_val = strtol(
220 errno_val_str, &errno_val_ptr, 0);
221 /* Checks to see if we parsed an actual errno. */
222 if (errno_val_ptr == errno_val_str)
223 return -1;
224
225 append_ret_errno(head, errno_val);
226 } else {
227 append_ret_kill(head);
228 }
229 return 0;
230}
231
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700232struct filter_block *compile_section(int nr, const char *policy_line,
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700233 unsigned int entry_lbl_id, struct bpf_labels *labels)
234{
235 /*
236 * |policy_line| should be an expression of the form:
237 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8"
238 *
239 * This is, an expression in DNF (disjunctive normal form);
240 * a disjunction ('||') of one or more conjunctions ('&&')
241 * of one or more atoms.
242 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700243 * Atoms are of the form "arg{DNUM} {OP} {NUM}"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700244 * where:
245 * - DNUM is a decimal number.
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800246 * - OP is an operator: ==, !=, or & (flags set).
247 * - NUM is an octal, decimal, or hexadecimal number.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700248 *
249 * When the syscall arguments make the expression true,
250 * the syscall is allowed. If not, the process is killed.
251 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700252 * To block a syscall without killing the process,
253 * |policy_line| can be of the form:
254 * "return <errno>"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700255 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700256 * This "return {NUM}" policy line will block the syscall,
257 * make it return -1 and set |errno| to NUM.
258 *
259 * A regular policy line can also include a "return <errno>" clause,
260 * separated by a semicolon (';'):
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700261 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}"
262 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700263 * If the syscall arguments don't make the expression true,
264 * the syscall will be blocked as above instead of killing the process.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700265 */
266
267 size_t len = 0;
268 int group_idx = 0;
269
270 /* Checks for overly long policy lines. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700271 if (strlen(policy_line) >= MAX_POLICY_LINE_LENGTH)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700272 return NULL;
273
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700274 /* We will modify |policy_line|, so let's make a copy. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700275 char *line = strndup(policy_line, MAX_POLICY_LINE_LENGTH);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700276 if (!line)
277 return NULL;
278
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700279 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700280 * We build the filter section as a collection of smaller
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700281 * "filter blocks" linked together in a singly-linked list.
282 */
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700283 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700284
285 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700286 * Filter sections begin with a label where the main filter
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700287 * will jump after checking the syscall number.
288 */
289 struct sock_filter *entry_label = new_instr_buf(ONE_INSTR);
290 set_bpf_lbl(entry_label, entry_lbl_id);
291 append_filter_block(head, entry_label, ONE_INSTR);
292
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700293 /* Checks whether we're unconditionally blocking this syscall. */
294 if (strncmp(line, "return", strlen("return")) == 0) {
295 if (compile_errno(head, line) < 0)
296 return NULL;
297 free(line);
298 return head;
299 }
300
301 /* Splits the optional "return <errno>" part. */
302 char *line_ptr;
303 char *arg_filter = strtok_r(line, ";", &line_ptr);
304 char *ret_errno = strtok_r(NULL, ";", &line_ptr);
305
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700306 /*
307 * Splits the policy line by '||' into conjunctions and each conjunction
308 * by '&&' into atoms.
309 */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800310 char *arg_filter_str = arg_filter;
311 char *group;
312 while ((group = tokenize(&arg_filter_str, "||")) != NULL) {
313 char *group_str = group;
314 char *comp;
315 while ((comp = tokenize(&group_str, "&&")) != NULL) {
316 /* Compiles each atom into a BPF block. */
317 if (compile_atom(head, comp, labels, nr, group_idx) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700318 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700319 }
320 /*
321 * If the AND statement succeeds, we're done,
322 * so jump to SUCCESS line.
323 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700324 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700325 struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS);
326 len = set_bpf_jump_lbl(group_end_block, id);
327 /*
328 * The end of each AND statement falls after the
329 * jump to SUCCESS.
330 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700331 id = group_end_lbl(labels, nr, group_idx++);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700332 len += set_bpf_lbl(group_end_block + len, id);
333 append_filter_block(head, group_end_block, len);
334 }
335
336 /*
337 * If no AND statements succeed, we end up here,
338 * because we never jumped to SUCCESS.
339 * If we have to return an errno, do it,
340 * otherwise just kill the task.
341 */
342 if (ret_errno) {
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800343 if (compile_errno(head, ret_errno) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700344 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700345 } else {
346 append_ret_kill(head);
347 }
348
349 /*
350 * Every time the filter succeeds we jump to a predefined SUCCESS
351 * label. Add that label and BPF RET_ALLOW code now.
352 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700353 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700354 struct sock_filter *success_block = new_instr_buf(TWO_INSTRS);
355 len = set_bpf_lbl(success_block, id);
356 len += set_bpf_ret_allow(success_block + len);
357 append_filter_block(head, success_block, len);
358
359 free(line);
360 return head;
361}
362
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800363int compile_filter(FILE *policy_file, struct sock_fprog *prog,
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700364 int log_failures)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700365{
366 char line[MAX_LINE_LENGTH];
367 int line_count = 0;
368
369 struct bpf_labels labels;
370 labels.count = 0;
371
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800372 if (!policy_file)
Jorge Lucangeli Obes6135db92012-07-27 13:37:30 -0700373 return -1;
374
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700375 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700376 struct filter_block *arg_blocks = NULL;
377
378 /* Start filter by validating arch. */
379 struct sock_filter *valid_arch = new_instr_buf(ARCH_VALIDATION_LEN);
380 size_t len = bpf_validate_arch(valid_arch);
381 append_filter_block(head, valid_arch, len);
382
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700383 /* Load syscall number. */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700384 struct sock_filter *load_nr = new_instr_buf(ONE_INSTR);
385 len = bpf_load_syscall_nr(load_nr);
386 append_filter_block(head, load_nr, len);
387
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700388 /* If we're logging failures, allow the necessary syscalls first. */
389 if (log_failures)
390 allow_log_syscalls(head);
391
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700392 /*
393 * Loop through all the lines in the policy file.
394 * Build a jump table for the syscall number.
395 * If the policy line has an arg filter, build the arg filter
396 * as well.
397 * Chain the filter sections together and dump them into
398 * the final buffer at the end.
399 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800400 while (fgets(line, sizeof(line), policy_file)) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700401 ++line_count;
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800402 char *policy_line = line;
403 char *syscall_name = strsep(&policy_line, ":");
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700404 int nr = -1;
405
406 syscall_name = strip(syscall_name);
407
408 /* Allow comments and empty lines. */
409 if (*syscall_name == '#' || *syscall_name == '\0')
410 continue;
411
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800412 if (!policy_line)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700413 return -1;
414
415 nr = lookup_syscall(syscall_name);
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700416 if (nr < 0) {
417 warn("compile_filter: nonexistent syscall '%s'",
418 syscall_name);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700419 return -1;
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700420 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700421
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800422 policy_line = strip(policy_line);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700423
424 /*
425 * For each syscall, add either a simple ALLOW,
426 * or an arg filter block.
427 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800428 if (strcmp(policy_line, "1") == 0) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700429 /* Add simple ALLOW. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700430 append_allow_syscall(head, nr);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700431 } else {
432 /*
433 * Create and jump to the label that will hold
434 * the arg filter block.
435 */
436 unsigned int id = bpf_label_id(&labels, syscall_name);
437 struct sock_filter *nr_comp =
438 new_instr_buf(ALLOW_SYSCALL_LEN);
439 bpf_allow_syscall_args(nr_comp, nr, id);
440 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
441
442 /* Build the arg filter block. */
443 struct filter_block *block =
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800444 compile_section(nr, policy_line, id, &labels);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700445
446 if (!block)
447 return -1;
448
449 if (arg_blocks) {
450 extend_filter_block_list(arg_blocks, block);
451 } else {
452 arg_blocks = block;
453 }
454 }
455 }
456
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700457 /*
458 * If none of the syscalls match, either fall back to KILL,
459 * or return TRAP.
460 */
461 if (!log_failures)
462 append_ret_kill(head);
463 else
464 append_ret_trap(head);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700465
466 /* Allocate the final buffer, now that we know its size. */
467 size_t final_filter_len = head->total_len +
468 (arg_blocks? arg_blocks->total_len : 0);
469 if (final_filter_len > BPF_MAXINSNS)
470 return -1;
471
472 struct sock_filter *final_filter =
473 calloc(final_filter_len, sizeof(struct sock_filter));
474
475 if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0)
476 return -1;
477
478 if (flatten_block_list(arg_blocks, final_filter,
479 head->total_len, final_filter_len) < 0)
480 return -1;
481
482 free_block_list(head);
483 free_block_list(arg_blocks);
484
485 bpf_resolve_jumps(&labels, final_filter, final_filter_len);
486
487 free_label_strings(&labels);
488
489 prog->filter = final_filter;
490 prog->len = final_filter_len;
491 return 0;
492}
493
494int flatten_block_list(struct filter_block *head, struct sock_filter *filter,
495 size_t index, size_t cap)
496{
497 size_t _index = index;
498
499 struct filter_block *curr;
500 size_t i;
501
502 for (curr = head; curr; curr = curr->next) {
503 for (i = 0; i < curr->len; i++) {
504 if (_index >= cap)
505 return -1;
506 filter[_index++] = curr->instrs[i];
507 }
508 }
509 return 0;
510}
511
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700512void free_block_list(struct filter_block *head)
513{
514 struct filter_block *current, *prev;
515
516 current = head;
517 while (current) {
518 free(current->instrs);
519 prev = current;
520 current = current->next;
521 free(prev);
522 }
523}