blob: 3fcbe4faa854414e5efbbb42a670695a5ee7c314 [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
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700232int compile_errno(struct filter_block *head, char *ret_errno,
233 int log_failures)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700234{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800235 char *errno_ptr;
236
237 /* Splits the 'return' keyword and the actual errno value. */
238 char *ret_str = strtok_r(ret_errno, " ", &errno_ptr);
239 if (strncmp(ret_str, "return", strlen("return")))
240 return -1;
241
242 char *errno_val_str = strtok_r(NULL, " ", &errno_ptr);
243
244 if (errno_val_str) {
245 char *errno_val_ptr;
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700246 int errno_val = parse_constant(errno_val_str, &errno_val_ptr);
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800247 /* Checks to see if we parsed an actual errno. */
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700248 if (errno_val_ptr == errno_val_str || errno_val == -1)
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800249 return -1;
250
251 append_ret_errno(head, errno_val);
252 } else {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700253 if (!log_failures)
254 append_ret_kill(head);
255 else
256 append_ret_trap(head);
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800257 }
258 return 0;
259}
260
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700261struct filter_block *compile_section(int nr, const char *policy_line,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400262 unsigned int entry_lbl_id,
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700263 struct bpf_labels *labels,
264 int log_failures)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700265{
266 /*
267 * |policy_line| should be an expression of the form:
268 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8"
269 *
270 * This is, an expression in DNF (disjunctive normal form);
271 * a disjunction ('||') of one or more conjunctions ('&&')
272 * of one or more atoms.
273 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700274 * Atoms are of the form "arg{DNUM} {OP} {NUM}"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700275 * where:
276 * - DNUM is a decimal number.
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800277 * - OP is an operator: ==, !=, or & (flags set).
278 * - NUM is an octal, decimal, or hexadecimal number.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700279 *
280 * When the syscall arguments make the expression true,
281 * the syscall is allowed. If not, the process is killed.
282 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700283 * To block a syscall without killing the process,
284 * |policy_line| can be of the form:
285 * "return <errno>"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700286 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700287 * This "return {NUM}" policy line will block the syscall,
288 * make it return -1 and set |errno| to NUM.
289 *
290 * A regular policy line can also include a "return <errno>" clause,
291 * separated by a semicolon (';'):
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700292 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}"
293 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700294 * If the syscall arguments don't make the expression true,
295 * the syscall will be blocked as above instead of killing the process.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700296 */
297
298 size_t len = 0;
299 int group_idx = 0;
300
301 /* Checks for overly long policy lines. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700302 if (strlen(policy_line) >= MAX_POLICY_LINE_LENGTH)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700303 return NULL;
304
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700305 /* We will modify |policy_line|, so let's make a copy. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700306 char *line = strndup(policy_line, MAX_POLICY_LINE_LENGTH);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700307 if (!line)
308 return NULL;
309
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700310 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700311 * We build the filter section as a collection of smaller
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700312 * "filter blocks" linked together in a singly-linked list.
313 */
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700314 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700315
316 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700317 * Filter sections begin with a label where the main filter
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700318 * will jump after checking the syscall number.
319 */
320 struct sock_filter *entry_label = new_instr_buf(ONE_INSTR);
321 set_bpf_lbl(entry_label, entry_lbl_id);
322 append_filter_block(head, entry_label, ONE_INSTR);
323
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700324 /* Checks whether we're unconditionally blocking this syscall. */
325 if (strncmp(line, "return", strlen("return")) == 0) {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700326 if (compile_errno(head, line, log_failures) < 0)
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700327 return NULL;
328 free(line);
329 return head;
330 }
331
332 /* Splits the optional "return <errno>" part. */
333 char *line_ptr;
334 char *arg_filter = strtok_r(line, ";", &line_ptr);
335 char *ret_errno = strtok_r(NULL, ";", &line_ptr);
336
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700337 /*
338 * Splits the policy line by '||' into conjunctions and each conjunction
339 * by '&&' into atoms.
340 */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800341 char *arg_filter_str = arg_filter;
342 char *group;
343 while ((group = tokenize(&arg_filter_str, "||")) != NULL) {
344 char *group_str = group;
345 char *comp;
346 while ((comp = tokenize(&group_str, "&&")) != NULL) {
347 /* Compiles each atom into a BPF block. */
348 if (compile_atom(head, comp, labels, nr, group_idx) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700349 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700350 }
351 /*
352 * If the AND statement succeeds, we're done,
353 * so jump to SUCCESS line.
354 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700355 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700356 struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS);
357 len = set_bpf_jump_lbl(group_end_block, id);
358 /*
359 * The end of each AND statement falls after the
360 * jump to SUCCESS.
361 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700362 id = group_end_lbl(labels, nr, group_idx++);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700363 len += set_bpf_lbl(group_end_block + len, id);
364 append_filter_block(head, group_end_block, len);
365 }
366
367 /*
368 * If no AND statements succeed, we end up here,
369 * because we never jumped to SUCCESS.
370 * If we have to return an errno, do it,
371 * otherwise just kill the task.
372 */
373 if (ret_errno) {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700374 if (compile_errno(head, ret_errno, log_failures) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700375 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700376 } else {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700377 if (!log_failures)
378 append_ret_kill(head);
379 else
380 append_ret_trap(head);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700381 }
382
383 /*
384 * Every time the filter succeeds we jump to a predefined SUCCESS
385 * label. Add that label and BPF RET_ALLOW code now.
386 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700387 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700388 struct sock_filter *success_block = new_instr_buf(TWO_INSTRS);
389 len = set_bpf_lbl(success_block, id);
390 len += set_bpf_ret_allow(success_block + len);
391 append_filter_block(head, success_block, len);
392
393 free(line);
394 return head;
395}
396
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400397int compile_filter(FILE *policy_file, struct sock_fprog *prog, int log_failures)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700398{
399 char line[MAX_LINE_LENGTH];
400 int line_count = 0;
401
402 struct bpf_labels labels;
403 labels.count = 0;
404
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800405 if (!policy_file)
Jorge Lucangeli Obes6135db92012-07-27 13:37:30 -0700406 return -1;
407
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700408 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700409 struct filter_block *arg_blocks = NULL;
410
411 /* Start filter by validating arch. */
412 struct sock_filter *valid_arch = new_instr_buf(ARCH_VALIDATION_LEN);
413 size_t len = bpf_validate_arch(valid_arch);
414 append_filter_block(head, valid_arch, len);
415
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700416 /* Load syscall number. */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700417 struct sock_filter *load_nr = new_instr_buf(ONE_INSTR);
418 len = bpf_load_syscall_nr(load_nr);
419 append_filter_block(head, load_nr, len);
420
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700421 /* If we're logging failures, allow the necessary syscalls first. */
422 if (log_failures)
423 allow_log_syscalls(head);
424
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700425 /*
426 * Loop through all the lines in the policy file.
427 * Build a jump table for the syscall number.
428 * If the policy line has an arg filter, build the arg filter
429 * as well.
430 * Chain the filter sections together and dump them into
431 * the final buffer at the end.
432 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800433 while (fgets(line, sizeof(line), policy_file)) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700434 ++line_count;
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800435 char *policy_line = line;
436 char *syscall_name = strsep(&policy_line, ":");
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700437 int nr = -1;
438
439 syscall_name = strip(syscall_name);
440
441 /* Allow comments and empty lines. */
442 if (*syscall_name == '#' || *syscall_name == '\0')
443 continue;
444
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800445 if (!policy_line)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700446 return -1;
447
448 nr = lookup_syscall(syscall_name);
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700449 if (nr < 0) {
450 warn("compile_filter: nonexistent syscall '%s'",
451 syscall_name);
Jorge Lucangeli Obesbe351a22016-01-22 17:18:51 -0800452 if (log_failures) {
453 /*
454 * If we're logging failures, assume we're in a
455 * debugging case and continue.
456 * This is not super risky because an invalid
457 * syscall name is likely caused by a typo or by
458 * leftover lines from a different architecture.
459 * In either case, not including a policy line
460 * is equivalent to killing the process if the
461 * syscall is made, so there's no added attack
462 * surface.
463 */
464 continue;
465 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700466 return -1;
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700467 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700468
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800469 policy_line = strip(policy_line);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700470
471 /*
472 * For each syscall, add either a simple ALLOW,
473 * or an arg filter block.
474 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800475 if (strcmp(policy_line, "1") == 0) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700476 /* Add simple ALLOW. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700477 append_allow_syscall(head, nr);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700478 } else {
479 /*
480 * Create and jump to the label that will hold
481 * the arg filter block.
482 */
483 unsigned int id = bpf_label_id(&labels, syscall_name);
484 struct sock_filter *nr_comp =
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400485 new_instr_buf(ALLOW_SYSCALL_LEN);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700486 bpf_allow_syscall_args(nr_comp, nr, id);
487 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
488
489 /* Build the arg filter block. */
490 struct filter_block *block =
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700491 compile_section(nr, policy_line, id, &labels,
492 log_failures);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700493
494 if (!block)
495 return -1;
496
497 if (arg_blocks) {
498 extend_filter_block_list(arg_blocks, block);
499 } else {
500 arg_blocks = block;
501 }
502 }
503 }
504
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700505 /*
506 * If none of the syscalls match, either fall back to KILL,
507 * or return TRAP.
508 */
509 if (!log_failures)
510 append_ret_kill(head);
511 else
512 append_ret_trap(head);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700513
514 /* Allocate the final buffer, now that we know its size. */
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400515 size_t final_filter_len =
516 head->total_len + (arg_blocks ? arg_blocks->total_len : 0);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700517 if (final_filter_len > BPF_MAXINSNS)
518 return -1;
519
520 struct sock_filter *final_filter =
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400521 calloc(final_filter_len, sizeof(struct sock_filter));
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700522
523 if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0)
524 return -1;
525
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400526 if (flatten_block_list(arg_blocks, final_filter, head->total_len,
527 final_filter_len) < 0)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700528 return -1;
529
530 free_block_list(head);
531 free_block_list(arg_blocks);
532
533 bpf_resolve_jumps(&labels, final_filter, final_filter_len);
534
535 free_label_strings(&labels);
536
537 prog->filter = final_filter;
538 prog->len = final_filter_len;
539 return 0;
540}
541
542int flatten_block_list(struct filter_block *head, struct sock_filter *filter,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400543 size_t index, size_t cap)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700544{
545 size_t _index = index;
546
547 struct filter_block *curr;
548 size_t i;
549
550 for (curr = head; curr; curr = curr->next) {
551 for (i = 0; i < curr->len; i++) {
552 if (_index >= cap)
553 return -1;
554 filter[_index++] = curr->instrs[i];
555 }
556 }
557 return 0;
558}
559
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700560void free_block_list(struct filter_block *head)
561{
562 struct filter_block *current, *prev;
563
564 current = head;
565 while (current) {
566 free(current->instrs);
567 prev = current;
568 current = current->next;
569 free(prev);
570 }
571}