blob: 0857f4b44f241222b13176982f0a476cf78bdff1 [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 Obes565e9782016-08-05 11:03:19 -040014#define MAX_LINE_LENGTH 1024
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070015#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 Obes7b2e29c2016-08-04 12:21:03 -040020int seccomp_can_softfail()
21{
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -040022#if defined(USE_SECCOMP_SOFTFAIL)
23 /*
24 * On Android devices seccomp is allowed to soft-fail on kernels < 3.8.
25 */
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040026 if (is_android()) {
27 if (kernel_lessthan_3_8())
28 return 1;
29 else
30 return 0;
31 } else {
32 return 1;
33 }
34#endif
35 return 0;
36}
37
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070038int str_to_op(const char *op_str)
39{
40 if (!strcmp(op_str, "==")) {
41 return EQ;
42 } else if (!strcmp(op_str, "!=")) {
43 return NE;
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080044 } else if (!strcmp(op_str, "&")) {
45 return SET;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070046 } else {
47 return 0;
48 }
49}
50
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070051struct sock_filter *new_instr_buf(size_t count)
52{
53 struct sock_filter *buf = calloc(count, sizeof(struct sock_filter));
54 if (!buf)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -070055 die("could not allocate BPF instruction buffer");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070056
57 return buf;
58}
59
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070060struct filter_block *new_filter_block()
61{
62 struct filter_block *block = calloc(1, sizeof(struct filter_block));
63 if (!block)
64 die("could not allocate BPF filter block");
65
66 block->instrs = NULL;
67 block->last = block->next = NULL;
68
69 return block;
70}
71
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -040072void append_filter_block(struct filter_block *head, struct sock_filter *instrs,
73 size_t len)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070074{
75 struct filter_block *new_last;
76
77 /*
78 * If |head| has no filter assigned yet,
79 * we don't create a new node.
80 */
81 if (head->instrs == NULL) {
82 new_last = head;
83 } else {
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070084 new_last = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070085 if (head->next != NULL) {
86 head->last->next = new_last;
87 head->last = new_last;
88 } else {
89 head->last = head->next = new_last;
90 }
91 head->total_len += len;
92 }
93
94 new_last->instrs = instrs;
95 new_last->total_len = new_last->len = len;
96 new_last->last = new_last->next = NULL;
97}
98
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070099void extend_filter_block_list(struct filter_block *list,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400100 struct filter_block *another)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700101{
102 if (list->last != NULL) {
103 list->last->next = another;
104 list->last = another->last;
105 } else {
106 list->next = another;
107 list->last = another->last;
108 }
109 list->total_len += another->total_len;
110}
111
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700112void append_ret_kill(struct filter_block *head)
113{
114 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
115 set_bpf_ret_kill(filter);
116 append_filter_block(head, filter, ONE_INSTR);
117}
118
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700119void append_ret_trap(struct filter_block *head)
120{
121 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
122 set_bpf_ret_trap(filter);
123 append_filter_block(head, filter, ONE_INSTR);
124}
125
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700126void append_ret_errno(struct filter_block *head, int errno_val)
127{
128 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
129 set_bpf_ret_errno(filter, errno_val);
130 append_filter_block(head, filter, ONE_INSTR);
131}
132
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700133void append_allow_syscall(struct filter_block *head, int nr)
134{
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700135 struct sock_filter *filter = new_instr_buf(ALLOW_SYSCALL_LEN);
136 size_t len = bpf_allow_syscall(filter, nr);
137 if (len != ALLOW_SYSCALL_LEN)
138 die("error building syscall number comparison");
139
140 append_filter_block(head, filter, len);
141}
142
143void allow_log_syscalls(struct filter_block *head)
144{
145 unsigned int i;
Kees Cook03b2af22014-12-18 17:11:13 -0800146 for (i = 0; i < log_syscalls_len; i++) {
147 warn("allowing syscall: %s", log_syscalls[i]);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700148 append_allow_syscall(head, lookup_syscall(log_syscalls[i]));
Kees Cook03b2af22014-12-18 17:11:13 -0800149 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700150}
151
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700152unsigned int get_label_id(struct bpf_labels *labels, const char *label_str)
153{
154 int label_id = bpf_label_id(labels, label_str);
155 if (label_id < 0)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700156 die("could not allocate BPF label string");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700157 return label_id;
158}
159
160unsigned int group_end_lbl(struct bpf_labels *labels, int nr, int idx)
161{
162 char lbl_str[MAX_BPF_LABEL_LEN];
163 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_%d_end", nr, idx);
164 return get_label_id(labels, lbl_str);
165}
166
167unsigned int success_lbl(struct bpf_labels *labels, int nr)
168{
169 char lbl_str[MAX_BPF_LABEL_LEN];
170 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_success", nr);
171 return get_label_id(labels, lbl_str);
172}
173
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800174int compile_atom(struct filter_block *head, char *atom,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400175 struct bpf_labels *labels, int nr, int group_idx)
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800176{
177 /* Splits the atom. */
178 char *atom_ptr;
179 char *argidx_str = strtok_r(atom, " ", &atom_ptr);
180 char *operator_str = strtok_r(NULL, " ", &atom_ptr);
181 char *constant_str = strtok_r(NULL, " ", &atom_ptr);
182
183 if (argidx_str == NULL || operator_str == NULL || constant_str == NULL)
184 return -1;
185
186 int op = str_to_op(operator_str);
187 if (op < MIN_OPERATOR)
188 return -1;
189
190 if (strncmp(argidx_str, "arg", 3)) {
191 return -1;
192 }
193
194 char *argidx_ptr;
195 long int argidx = strtol(argidx_str + 3, &argidx_ptr, 10);
196 /*
197 * Checks to see if an actual argument index
198 * was parsed.
199 */
200 if (argidx_ptr == argidx_str + 3)
201 return -1;
202
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700203 char *constant_str_ptr;
204 long int c = parse_constant(constant_str, &constant_str_ptr);
205 if (constant_str_ptr == constant_str)
206 return -1;
207
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800208 /*
209 * Looks up the label for the end of the AND statement
210 * this atom belongs to.
211 */
212 unsigned int id = group_end_lbl(labels, nr, group_idx);
213
214 /*
215 * Builds a BPF comparison between a syscall argument
216 * and a constant.
217 * The comparison lives inside an AND statement.
218 * If the comparison succeeds, we continue
219 * to the next comparison.
220 * If this comparison fails, the whole AND statement
221 * will fail, so we jump to the end of this AND statement.
222 */
223 struct sock_filter *comp_block;
224 size_t len = bpf_arg_comp(&comp_block, op, argidx, c, id);
225 if (len == 0)
226 return -1;
227
228 append_filter_block(head, comp_block, len);
229 return 0;
230}
231
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700232int compile_errno(struct filter_block *head, char *ret_errno)
233{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800234 char *errno_ptr;
235
236 /* Splits the 'return' keyword and the actual errno value. */
237 char *ret_str = strtok_r(ret_errno, " ", &errno_ptr);
238 if (strncmp(ret_str, "return", strlen("return")))
239 return -1;
240
241 char *errno_val_str = strtok_r(NULL, " ", &errno_ptr);
242
243 if (errno_val_str) {
244 char *errno_val_ptr;
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700245 int errno_val = parse_constant(errno_val_str, &errno_val_ptr);
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800246 /* Checks to see if we parsed an actual errno. */
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700247 if (errno_val_ptr == errno_val_str || errno_val == -1)
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800248 return -1;
249
250 append_ret_errno(head, errno_val);
251 } else {
252 append_ret_kill(head);
253 }
254 return 0;
255}
256
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700257struct filter_block *compile_section(int nr, const char *policy_line,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400258 unsigned int entry_lbl_id,
259 struct bpf_labels *labels)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700260{
261 /*
262 * |policy_line| should be an expression of the form:
263 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8"
264 *
265 * This is, an expression in DNF (disjunctive normal form);
266 * a disjunction ('||') of one or more conjunctions ('&&')
267 * of one or more atoms.
268 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700269 * Atoms are of the form "arg{DNUM} {OP} {NUM}"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700270 * where:
271 * - DNUM is a decimal number.
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800272 * - OP is an operator: ==, !=, or & (flags set).
273 * - NUM is an octal, decimal, or hexadecimal number.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700274 *
275 * When the syscall arguments make the expression true,
276 * the syscall is allowed. If not, the process is killed.
277 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700278 * To block a syscall without killing the process,
279 * |policy_line| can be of the form:
280 * "return <errno>"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700281 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700282 * This "return {NUM}" policy line will block the syscall,
283 * make it return -1 and set |errno| to NUM.
284 *
285 * A regular policy line can also include a "return <errno>" clause,
286 * separated by a semicolon (';'):
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700287 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}"
288 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700289 * If the syscall arguments don't make the expression true,
290 * the syscall will be blocked as above instead of killing the process.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700291 */
292
293 size_t len = 0;
294 int group_idx = 0;
295
296 /* Checks for overly long policy lines. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700297 if (strlen(policy_line) >= MAX_POLICY_LINE_LENGTH)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700298 return NULL;
299
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700300 /* We will modify |policy_line|, so let's make a copy. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700301 char *line = strndup(policy_line, MAX_POLICY_LINE_LENGTH);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700302 if (!line)
303 return NULL;
304
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700305 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700306 * We build the filter section as a collection of smaller
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700307 * "filter blocks" linked together in a singly-linked list.
308 */
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700309 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700310
311 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700312 * Filter sections begin with a label where the main filter
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700313 * will jump after checking the syscall number.
314 */
315 struct sock_filter *entry_label = new_instr_buf(ONE_INSTR);
316 set_bpf_lbl(entry_label, entry_lbl_id);
317 append_filter_block(head, entry_label, ONE_INSTR);
318
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700319 /* Checks whether we're unconditionally blocking this syscall. */
320 if (strncmp(line, "return", strlen("return")) == 0) {
321 if (compile_errno(head, line) < 0)
322 return NULL;
323 free(line);
324 return head;
325 }
326
327 /* Splits the optional "return <errno>" part. */
328 char *line_ptr;
329 char *arg_filter = strtok_r(line, ";", &line_ptr);
330 char *ret_errno = strtok_r(NULL, ";", &line_ptr);
331
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700332 /*
333 * Splits the policy line by '||' into conjunctions and each conjunction
334 * by '&&' into atoms.
335 */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800336 char *arg_filter_str = arg_filter;
337 char *group;
338 while ((group = tokenize(&arg_filter_str, "||")) != NULL) {
339 char *group_str = group;
340 char *comp;
341 while ((comp = tokenize(&group_str, "&&")) != NULL) {
342 /* Compiles each atom into a BPF block. */
343 if (compile_atom(head, comp, labels, nr, group_idx) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700344 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700345 }
346 /*
347 * If the AND statement succeeds, we're done,
348 * so jump to SUCCESS line.
349 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700350 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700351 struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS);
352 len = set_bpf_jump_lbl(group_end_block, id);
353 /*
354 * The end of each AND statement falls after the
355 * jump to SUCCESS.
356 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700357 id = group_end_lbl(labels, nr, group_idx++);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700358 len += set_bpf_lbl(group_end_block + len, id);
359 append_filter_block(head, group_end_block, len);
360 }
361
362 /*
363 * If no AND statements succeed, we end up here,
364 * because we never jumped to SUCCESS.
365 * If we have to return an errno, do it,
366 * otherwise just kill the task.
367 */
368 if (ret_errno) {
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800369 if (compile_errno(head, ret_errno) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700370 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700371 } else {
372 append_ret_kill(head);
373 }
374
375 /*
376 * Every time the filter succeeds we jump to a predefined SUCCESS
377 * label. Add that label and BPF RET_ALLOW code now.
378 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700379 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700380 struct sock_filter *success_block = new_instr_buf(TWO_INSTRS);
381 len = set_bpf_lbl(success_block, id);
382 len += set_bpf_ret_allow(success_block + len);
383 append_filter_block(head, success_block, len);
384
385 free(line);
386 return head;
387}
388
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400389int compile_filter(FILE *policy_file, struct sock_fprog *prog, int log_failures)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700390{
391 char line[MAX_LINE_LENGTH];
392 int line_count = 0;
393
394 struct bpf_labels labels;
395 labels.count = 0;
396
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800397 if (!policy_file)
Jorge Lucangeli Obes6135db92012-07-27 13:37:30 -0700398 return -1;
399
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700400 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700401 struct filter_block *arg_blocks = NULL;
402
403 /* Start filter by validating arch. */
404 struct sock_filter *valid_arch = new_instr_buf(ARCH_VALIDATION_LEN);
405 size_t len = bpf_validate_arch(valid_arch);
406 append_filter_block(head, valid_arch, len);
407
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700408 /* Load syscall number. */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700409 struct sock_filter *load_nr = new_instr_buf(ONE_INSTR);
410 len = bpf_load_syscall_nr(load_nr);
411 append_filter_block(head, load_nr, len);
412
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700413 /* If we're logging failures, allow the necessary syscalls first. */
414 if (log_failures)
415 allow_log_syscalls(head);
416
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700417 /*
418 * Loop through all the lines in the policy file.
419 * Build a jump table for the syscall number.
420 * If the policy line has an arg filter, build the arg filter
421 * as well.
422 * Chain the filter sections together and dump them into
423 * the final buffer at the end.
424 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800425 while (fgets(line, sizeof(line), policy_file)) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700426 ++line_count;
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800427 char *policy_line = line;
428 char *syscall_name = strsep(&policy_line, ":");
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700429 int nr = -1;
430
431 syscall_name = strip(syscall_name);
432
433 /* Allow comments and empty lines. */
434 if (*syscall_name == '#' || *syscall_name == '\0')
435 continue;
436
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800437 if (!policy_line)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700438 return -1;
439
440 nr = lookup_syscall(syscall_name);
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700441 if (nr < 0) {
442 warn("compile_filter: nonexistent syscall '%s'",
443 syscall_name);
Jorge Lucangeli Obesbe351a22016-01-22 17:18:51 -0800444 if (log_failures) {
445 /*
446 * If we're logging failures, assume we're in a
447 * debugging case and continue.
448 * This is not super risky because an invalid
449 * syscall name is likely caused by a typo or by
450 * leftover lines from a different architecture.
451 * In either case, not including a policy line
452 * is equivalent to killing the process if the
453 * syscall is made, so there's no added attack
454 * surface.
455 */
456 continue;
457 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700458 return -1;
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700459 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700460
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800461 policy_line = strip(policy_line);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700462
463 /*
464 * For each syscall, add either a simple ALLOW,
465 * or an arg filter block.
466 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800467 if (strcmp(policy_line, "1") == 0) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700468 /* Add simple ALLOW. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700469 append_allow_syscall(head, nr);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700470 } else {
471 /*
472 * Create and jump to the label that will hold
473 * the arg filter block.
474 */
475 unsigned int id = bpf_label_id(&labels, syscall_name);
476 struct sock_filter *nr_comp =
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400477 new_instr_buf(ALLOW_SYSCALL_LEN);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700478 bpf_allow_syscall_args(nr_comp, nr, id);
479 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
480
481 /* Build the arg filter block. */
482 struct filter_block *block =
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400483 compile_section(nr, policy_line, id, &labels);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700484
485 if (!block)
486 return -1;
487
488 if (arg_blocks) {
489 extend_filter_block_list(arg_blocks, block);
490 } else {
491 arg_blocks = block;
492 }
493 }
494 }
495
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700496 /*
497 * If none of the syscalls match, either fall back to KILL,
498 * or return TRAP.
499 */
500 if (!log_failures)
501 append_ret_kill(head);
502 else
503 append_ret_trap(head);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700504
505 /* Allocate the final buffer, now that we know its size. */
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400506 size_t final_filter_len =
507 head->total_len + (arg_blocks ? arg_blocks->total_len : 0);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700508 if (final_filter_len > BPF_MAXINSNS)
509 return -1;
510
511 struct sock_filter *final_filter =
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400512 calloc(final_filter_len, sizeof(struct sock_filter));
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700513
514 if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0)
515 return -1;
516
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400517 if (flatten_block_list(arg_blocks, final_filter, head->total_len,
518 final_filter_len) < 0)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700519 return -1;
520
521 free_block_list(head);
522 free_block_list(arg_blocks);
523
524 bpf_resolve_jumps(&labels, final_filter, final_filter_len);
525
526 free_label_strings(&labels);
527
528 prog->filter = final_filter;
529 prog->len = final_filter_len;
530 return 0;
531}
532
533int flatten_block_list(struct filter_block *head, struct sock_filter *filter,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400534 size_t index, size_t cap)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700535{
536 size_t _index = index;
537
538 struct filter_block *curr;
539 size_t i;
540
541 for (curr = head; curr; curr = curr->next) {
542 for (i = 0; i < curr->len; i++) {
543 if (_index >= cap)
544 return -1;
545 filter[_index++] = curr->instrs[i];
546 }
547 }
548 return 0;
549}
550
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700551void free_block_list(struct filter_block *head)
552{
553 struct filter_block *current, *prev;
554
555 current = head;
556 while (current) {
557 free(current->instrs);
558 prev = current;
559 current = current->next;
560 free(prev);
561 }
562}