blob: b0dae77b3094cfb0e5f9ae1060998be1c515e9c0 [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 Obes7b2e29c2016-08-04 12:21:03 -040020int seccomp_can_softfail()
21{
22#if SECCOMP_SOFTFAIL
23 if (is_android()) {
24 if (kernel_lessthan_3_8())
25 return 1;
26 else
27 return 0;
28 } else {
29 return 1;
30 }
31#endif
32 return 0;
33}
34
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070035int str_to_op(const char *op_str)
36{
37 if (!strcmp(op_str, "==")) {
38 return EQ;
39 } else if (!strcmp(op_str, "!=")) {
40 return NE;
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080041 } else if (!strcmp(op_str, "&")) {
42 return SET;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070043 } else {
44 return 0;
45 }
46}
47
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070048struct sock_filter *new_instr_buf(size_t count)
49{
50 struct sock_filter *buf = calloc(count, sizeof(struct sock_filter));
51 if (!buf)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -070052 die("could not allocate BPF instruction buffer");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070053
54 return buf;
55}
56
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070057struct filter_block *new_filter_block()
58{
59 struct filter_block *block = calloc(1, sizeof(struct filter_block));
60 if (!block)
61 die("could not allocate BPF filter block");
62
63 block->instrs = NULL;
64 block->last = block->next = NULL;
65
66 return block;
67}
68
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070069void append_filter_block(struct filter_block *head,
70 struct sock_filter *instrs, size_t len)
71{
72 struct filter_block *new_last;
73
74 /*
75 * If |head| has no filter assigned yet,
76 * we don't create a new node.
77 */
78 if (head->instrs == NULL) {
79 new_last = head;
80 } else {
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070081 new_last = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070082 if (head->next != NULL) {
83 head->last->next = new_last;
84 head->last = new_last;
85 } else {
86 head->last = head->next = new_last;
87 }
88 head->total_len += len;
89 }
90
91 new_last->instrs = instrs;
92 new_last->total_len = new_last->len = len;
93 new_last->last = new_last->next = NULL;
94}
95
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070096void extend_filter_block_list(struct filter_block *list,
97 struct filter_block *another)
98{
99 if (list->last != NULL) {
100 list->last->next = another;
101 list->last = another->last;
102 } else {
103 list->next = another;
104 list->last = another->last;
105 }
106 list->total_len += another->total_len;
107}
108
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700109void append_ret_kill(struct filter_block *head)
110{
111 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
112 set_bpf_ret_kill(filter);
113 append_filter_block(head, filter, ONE_INSTR);
114}
115
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700116void append_ret_trap(struct filter_block *head)
117{
118 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
119 set_bpf_ret_trap(filter);
120 append_filter_block(head, filter, ONE_INSTR);
121}
122
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700123void append_ret_errno(struct filter_block *head, int errno_val)
124{
125 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
126 set_bpf_ret_errno(filter, errno_val);
127 append_filter_block(head, filter, ONE_INSTR);
128}
129
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700130void append_allow_syscall(struct filter_block *head, int nr)
131{
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700132 struct sock_filter *filter = new_instr_buf(ALLOW_SYSCALL_LEN);
133 size_t len = bpf_allow_syscall(filter, nr);
134 if (len != ALLOW_SYSCALL_LEN)
135 die("error building syscall number comparison");
136
137 append_filter_block(head, filter, len);
138}
139
140void allow_log_syscalls(struct filter_block *head)
141{
142 unsigned int i;
Kees Cook03b2af22014-12-18 17:11:13 -0800143 for (i = 0; i < log_syscalls_len; i++) {
144 warn("allowing syscall: %s", log_syscalls[i]);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700145 append_allow_syscall(head, lookup_syscall(log_syscalls[i]));
Kees Cook03b2af22014-12-18 17:11:13 -0800146 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700147}
148
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700149unsigned int get_label_id(struct bpf_labels *labels, const char *label_str)
150{
151 int label_id = bpf_label_id(labels, label_str);
152 if (label_id < 0)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700153 die("could not allocate BPF label string");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700154 return label_id;
155}
156
157unsigned int group_end_lbl(struct bpf_labels *labels, int nr, int idx)
158{
159 char lbl_str[MAX_BPF_LABEL_LEN];
160 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_%d_end", nr, idx);
161 return get_label_id(labels, lbl_str);
162}
163
164unsigned int success_lbl(struct bpf_labels *labels, int nr)
165{
166 char lbl_str[MAX_BPF_LABEL_LEN];
167 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_success", nr);
168 return get_label_id(labels, lbl_str);
169}
170
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800171int compile_atom(struct filter_block *head, char *atom,
172 struct bpf_labels *labels, int nr, int group_idx)
173{
174 /* Splits the atom. */
175 char *atom_ptr;
176 char *argidx_str = strtok_r(atom, " ", &atom_ptr);
177 char *operator_str = strtok_r(NULL, " ", &atom_ptr);
178 char *constant_str = strtok_r(NULL, " ", &atom_ptr);
179
180 if (argidx_str == NULL || operator_str == NULL || constant_str == NULL)
181 return -1;
182
183 int op = str_to_op(operator_str);
184 if (op < MIN_OPERATOR)
185 return -1;
186
187 if (strncmp(argidx_str, "arg", 3)) {
188 return -1;
189 }
190
191 char *argidx_ptr;
192 long int argidx = strtol(argidx_str + 3, &argidx_ptr, 10);
193 /*
194 * Checks to see if an actual argument index
195 * was parsed.
196 */
197 if (argidx_ptr == argidx_str + 3)
198 return -1;
199
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700200 char *constant_str_ptr;
201 long int c = parse_constant(constant_str, &constant_str_ptr);
202 if (constant_str_ptr == constant_str)
203 return -1;
204
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800205 /*
206 * Looks up the label for the end of the AND statement
207 * this atom belongs to.
208 */
209 unsigned int id = group_end_lbl(labels, nr, group_idx);
210
211 /*
212 * Builds a BPF comparison between a syscall argument
213 * and a constant.
214 * The comparison lives inside an AND statement.
215 * If the comparison succeeds, we continue
216 * to the next comparison.
217 * If this comparison fails, the whole AND statement
218 * will fail, so we jump to the end of this AND statement.
219 */
220 struct sock_filter *comp_block;
221 size_t len = bpf_arg_comp(&comp_block, op, argidx, c, id);
222 if (len == 0)
223 return -1;
224
225 append_filter_block(head, comp_block, len);
226 return 0;
227}
228
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700229int compile_errno(struct filter_block *head, char *ret_errno)
230{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800231 char *errno_ptr;
232
233 /* Splits the 'return' keyword and the actual errno value. */
234 char *ret_str = strtok_r(ret_errno, " ", &errno_ptr);
235 if (strncmp(ret_str, "return", strlen("return")))
236 return -1;
237
238 char *errno_val_str = strtok_r(NULL, " ", &errno_ptr);
239
240 if (errno_val_str) {
241 char *errno_val_ptr;
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700242 int errno_val = parse_constant(errno_val_str, &errno_val_ptr);
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800243 /* Checks to see if we parsed an actual errno. */
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700244 if (errno_val_ptr == errno_val_str || errno_val == -1)
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800245 return -1;
246
247 append_ret_errno(head, errno_val);
248 } else {
249 append_ret_kill(head);
250 }
251 return 0;
252}
253
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700254struct filter_block *compile_section(int nr, const char *policy_line,
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700255 unsigned int entry_lbl_id, struct bpf_labels *labels)
256{
257 /*
258 * |policy_line| should be an expression of the form:
259 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8"
260 *
261 * This is, an expression in DNF (disjunctive normal form);
262 * a disjunction ('||') of one or more conjunctions ('&&')
263 * of one or more atoms.
264 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700265 * Atoms are of the form "arg{DNUM} {OP} {NUM}"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700266 * where:
267 * - DNUM is a decimal number.
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800268 * - OP is an operator: ==, !=, or & (flags set).
269 * - NUM is an octal, decimal, or hexadecimal number.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700270 *
271 * When the syscall arguments make the expression true,
272 * the syscall is allowed. If not, the process is killed.
273 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700274 * To block a syscall without killing the process,
275 * |policy_line| can be of the form:
276 * "return <errno>"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700277 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700278 * This "return {NUM}" policy line will block the syscall,
279 * make it return -1 and set |errno| to NUM.
280 *
281 * A regular policy line can also include a "return <errno>" clause,
282 * separated by a semicolon (';'):
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700283 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}"
284 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700285 * If the syscall arguments don't make the expression true,
286 * the syscall will be blocked as above instead of killing the process.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700287 */
288
289 size_t len = 0;
290 int group_idx = 0;
291
292 /* Checks for overly long policy lines. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700293 if (strlen(policy_line) >= MAX_POLICY_LINE_LENGTH)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700294 return NULL;
295
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700296 /* We will modify |policy_line|, so let's make a copy. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700297 char *line = strndup(policy_line, MAX_POLICY_LINE_LENGTH);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700298 if (!line)
299 return NULL;
300
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700301 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700302 * We build the filter section as a collection of smaller
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700303 * "filter blocks" linked together in a singly-linked list.
304 */
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700305 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700306
307 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700308 * Filter sections begin with a label where the main filter
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700309 * will jump after checking the syscall number.
310 */
311 struct sock_filter *entry_label = new_instr_buf(ONE_INSTR);
312 set_bpf_lbl(entry_label, entry_lbl_id);
313 append_filter_block(head, entry_label, ONE_INSTR);
314
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700315 /* Checks whether we're unconditionally blocking this syscall. */
316 if (strncmp(line, "return", strlen("return")) == 0) {
317 if (compile_errno(head, line) < 0)
318 return NULL;
319 free(line);
320 return head;
321 }
322
323 /* Splits the optional "return <errno>" part. */
324 char *line_ptr;
325 char *arg_filter = strtok_r(line, ";", &line_ptr);
326 char *ret_errno = strtok_r(NULL, ";", &line_ptr);
327
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700328 /*
329 * Splits the policy line by '||' into conjunctions and each conjunction
330 * by '&&' into atoms.
331 */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800332 char *arg_filter_str = arg_filter;
333 char *group;
334 while ((group = tokenize(&arg_filter_str, "||")) != NULL) {
335 char *group_str = group;
336 char *comp;
337 while ((comp = tokenize(&group_str, "&&")) != NULL) {
338 /* Compiles each atom into a BPF block. */
339 if (compile_atom(head, comp, labels, nr, group_idx) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700340 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700341 }
342 /*
343 * If the AND statement succeeds, we're done,
344 * so jump to SUCCESS line.
345 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700346 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700347 struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS);
348 len = set_bpf_jump_lbl(group_end_block, id);
349 /*
350 * The end of each AND statement falls after the
351 * jump to SUCCESS.
352 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700353 id = group_end_lbl(labels, nr, group_idx++);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700354 len += set_bpf_lbl(group_end_block + len, id);
355 append_filter_block(head, group_end_block, len);
356 }
357
358 /*
359 * If no AND statements succeed, we end up here,
360 * because we never jumped to SUCCESS.
361 * If we have to return an errno, do it,
362 * otherwise just kill the task.
363 */
364 if (ret_errno) {
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800365 if (compile_errno(head, ret_errno) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700366 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700367 } else {
368 append_ret_kill(head);
369 }
370
371 /*
372 * Every time the filter succeeds we jump to a predefined SUCCESS
373 * label. Add that label and BPF RET_ALLOW code now.
374 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700375 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700376 struct sock_filter *success_block = new_instr_buf(TWO_INSTRS);
377 len = set_bpf_lbl(success_block, id);
378 len += set_bpf_ret_allow(success_block + len);
379 append_filter_block(head, success_block, len);
380
381 free(line);
382 return head;
383}
384
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800385int compile_filter(FILE *policy_file, struct sock_fprog *prog,
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700386 int log_failures)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700387{
388 char line[MAX_LINE_LENGTH];
389 int line_count = 0;
390
391 struct bpf_labels labels;
392 labels.count = 0;
393
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800394 if (!policy_file)
Jorge Lucangeli Obes6135db92012-07-27 13:37:30 -0700395 return -1;
396
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700397 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700398 struct filter_block *arg_blocks = NULL;
399
400 /* Start filter by validating arch. */
401 struct sock_filter *valid_arch = new_instr_buf(ARCH_VALIDATION_LEN);
402 size_t len = bpf_validate_arch(valid_arch);
403 append_filter_block(head, valid_arch, len);
404
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700405 /* Load syscall number. */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700406 struct sock_filter *load_nr = new_instr_buf(ONE_INSTR);
407 len = bpf_load_syscall_nr(load_nr);
408 append_filter_block(head, load_nr, len);
409
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700410 /* If we're logging failures, allow the necessary syscalls first. */
411 if (log_failures)
412 allow_log_syscalls(head);
413
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700414 /*
415 * Loop through all the lines in the policy file.
416 * Build a jump table for the syscall number.
417 * If the policy line has an arg filter, build the arg filter
418 * as well.
419 * Chain the filter sections together and dump them into
420 * the final buffer at the end.
421 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800422 while (fgets(line, sizeof(line), policy_file)) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700423 ++line_count;
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800424 char *policy_line = line;
425 char *syscall_name = strsep(&policy_line, ":");
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700426 int nr = -1;
427
428 syscall_name = strip(syscall_name);
429
430 /* Allow comments and empty lines. */
431 if (*syscall_name == '#' || *syscall_name == '\0')
432 continue;
433
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800434 if (!policy_line)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700435 return -1;
436
437 nr = lookup_syscall(syscall_name);
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700438 if (nr < 0) {
439 warn("compile_filter: nonexistent syscall '%s'",
440 syscall_name);
Jorge Lucangeli Obesbe351a22016-01-22 17:18:51 -0800441 if (log_failures) {
442 /*
443 * If we're logging failures, assume we're in a
444 * debugging case and continue.
445 * This is not super risky because an invalid
446 * syscall name is likely caused by a typo or by
447 * leftover lines from a different architecture.
448 * In either case, not including a policy line
449 * is equivalent to killing the process if the
450 * syscall is made, so there's no added attack
451 * surface.
452 */
453 continue;
454 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700455 return -1;
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700456 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700457
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800458 policy_line = strip(policy_line);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700459
460 /*
461 * For each syscall, add either a simple ALLOW,
462 * or an arg filter block.
463 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800464 if (strcmp(policy_line, "1") == 0) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700465 /* Add simple ALLOW. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700466 append_allow_syscall(head, nr);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700467 } else {
468 /*
469 * Create and jump to the label that will hold
470 * the arg filter block.
471 */
472 unsigned int id = bpf_label_id(&labels, syscall_name);
473 struct sock_filter *nr_comp =
474 new_instr_buf(ALLOW_SYSCALL_LEN);
475 bpf_allow_syscall_args(nr_comp, nr, id);
476 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
477
478 /* Build the arg filter block. */
479 struct filter_block *block =
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800480 compile_section(nr, policy_line, id, &labels);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700481
482 if (!block)
483 return -1;
484
485 if (arg_blocks) {
486 extend_filter_block_list(arg_blocks, block);
487 } else {
488 arg_blocks = block;
489 }
490 }
491 }
492
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700493 /*
494 * If none of the syscalls match, either fall back to KILL,
495 * or return TRAP.
496 */
497 if (!log_failures)
498 append_ret_kill(head);
499 else
500 append_ret_trap(head);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700501
502 /* Allocate the final buffer, now that we know its size. */
503 size_t final_filter_len = head->total_len +
504 (arg_blocks? arg_blocks->total_len : 0);
505 if (final_filter_len > BPF_MAXINSNS)
506 return -1;
507
508 struct sock_filter *final_filter =
509 calloc(final_filter_len, sizeof(struct sock_filter));
510
511 if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0)
512 return -1;
513
514 if (flatten_block_list(arg_blocks, final_filter,
515 head->total_len, final_filter_len) < 0)
516 return -1;
517
518 free_block_list(head);
519 free_block_list(arg_blocks);
520
521 bpf_resolve_jumps(&labels, final_filter, final_filter_len);
522
523 free_label_strings(&labels);
524
525 prog->filter = final_filter;
526 prog->len = final_filter_len;
527 return 0;
528}
529
530int flatten_block_list(struct filter_block *head, struct sock_filter *filter,
531 size_t index, size_t cap)
532{
533 size_t _index = index;
534
535 struct filter_block *curr;
536 size_t i;
537
538 for (curr = head; curr; curr = curr->next) {
539 for (i = 0; i < curr->len; i++) {
540 if (_index >= cap)
541 return -1;
542 filter[_index++] = curr->instrs[i];
543 }
544 }
545 return 0;
546}
547
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700548void free_block_list(struct filter_block *head)
549{
550 struct filter_block *current, *prev;
551
552 current = head;
553 while (current) {
554 free(current->instrs);
555 prev = current;
556 current = current->next;
557 free(prev);
558 }
559}