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