blob: 0e3d7b3324df0be3e341fd457ac46cc185466482 [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)
Jeff Vander Stoep1022fc32016-08-28 12:24:55 -070023 return 1;
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040024#endif
25 return 0;
26}
27
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070028int str_to_op(const char *op_str)
29{
30 if (!strcmp(op_str, "==")) {
31 return EQ;
32 } else if (!strcmp(op_str, "!=")) {
33 return NE;
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080034 } else if (!strcmp(op_str, "&")) {
35 return SET;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070036 } else {
37 return 0;
38 }
39}
40
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070041struct sock_filter *new_instr_buf(size_t count)
42{
43 struct sock_filter *buf = calloc(count, sizeof(struct sock_filter));
44 if (!buf)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -070045 die("could not allocate BPF instruction buffer");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070046
47 return buf;
48}
49
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070050struct filter_block *new_filter_block()
51{
52 struct filter_block *block = calloc(1, sizeof(struct filter_block));
53 if (!block)
54 die("could not allocate BPF filter block");
55
56 block->instrs = NULL;
57 block->last = block->next = NULL;
58
59 return block;
60}
61
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -040062void append_filter_block(struct filter_block *head, struct sock_filter *instrs,
63 size_t len)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070064{
65 struct filter_block *new_last;
66
67 /*
68 * If |head| has no filter assigned yet,
69 * we don't create a new node.
70 */
71 if (head->instrs == NULL) {
72 new_last = head;
73 } else {
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -070074 new_last = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070075 if (head->next != NULL) {
76 head->last->next = new_last;
77 head->last = new_last;
78 } else {
79 head->last = head->next = new_last;
80 }
81 head->total_len += len;
82 }
83
84 new_last->instrs = instrs;
85 new_last->total_len = new_last->len = len;
86 new_last->last = new_last->next = NULL;
87}
88
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070089void extend_filter_block_list(struct filter_block *list,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -040090 struct filter_block *another)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070091{
92 if (list->last != NULL) {
93 list->last->next = another;
94 list->last = another->last;
95 } else {
96 list->next = another;
97 list->last = another->last;
98 }
99 list->total_len += another->total_len;
100}
101
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700102void append_ret_kill(struct filter_block *head)
103{
104 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
105 set_bpf_ret_kill(filter);
106 append_filter_block(head, filter, ONE_INSTR);
107}
108
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700109void append_ret_trap(struct filter_block *head)
110{
111 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
112 set_bpf_ret_trap(filter);
113 append_filter_block(head, filter, ONE_INSTR);
114}
115
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700116void append_ret_errno(struct filter_block *head, int errno_val)
117{
118 struct sock_filter *filter = new_instr_buf(ONE_INSTR);
119 set_bpf_ret_errno(filter, errno_val);
120 append_filter_block(head, filter, ONE_INSTR);
121}
122
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700123void append_allow_syscall(struct filter_block *head, int nr)
124{
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700125 struct sock_filter *filter = new_instr_buf(ALLOW_SYSCALL_LEN);
126 size_t len = bpf_allow_syscall(filter, nr);
127 if (len != ALLOW_SYSCALL_LEN)
128 die("error building syscall number comparison");
129
130 append_filter_block(head, filter, len);
131}
132
133void allow_log_syscalls(struct filter_block *head)
134{
135 unsigned int i;
Kees Cook03b2af22014-12-18 17:11:13 -0800136 for (i = 0; i < log_syscalls_len; i++) {
137 warn("allowing syscall: %s", log_syscalls[i]);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700138 append_allow_syscall(head, lookup_syscall(log_syscalls[i]));
Kees Cook03b2af22014-12-18 17:11:13 -0800139 }
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700140}
141
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700142unsigned int get_label_id(struct bpf_labels *labels, const char *label_str)
143{
144 int label_id = bpf_label_id(labels, label_str);
145 if (label_id < 0)
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700146 die("could not allocate BPF label string");
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700147 return label_id;
148}
149
150unsigned int group_end_lbl(struct bpf_labels *labels, int nr, int idx)
151{
152 char lbl_str[MAX_BPF_LABEL_LEN];
153 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_%d_end", nr, idx);
154 return get_label_id(labels, lbl_str);
155}
156
157unsigned int success_lbl(struct bpf_labels *labels, int nr)
158{
159 char lbl_str[MAX_BPF_LABEL_LEN];
160 snprintf(lbl_str, MAX_BPF_LABEL_LEN, "%d_success", nr);
161 return get_label_id(labels, lbl_str);
162}
163
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800164int compile_atom(struct filter_block *head, char *atom,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400165 struct bpf_labels *labels, int nr, int group_idx)
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800166{
167 /* Splits the atom. */
168 char *atom_ptr;
169 char *argidx_str = strtok_r(atom, " ", &atom_ptr);
170 char *operator_str = strtok_r(NULL, " ", &atom_ptr);
171 char *constant_str = strtok_r(NULL, " ", &atom_ptr);
172
173 if (argidx_str == NULL || operator_str == NULL || constant_str == NULL)
174 return -1;
175
176 int op = str_to_op(operator_str);
177 if (op < MIN_OPERATOR)
178 return -1;
179
180 if (strncmp(argidx_str, "arg", 3)) {
181 return -1;
182 }
183
184 char *argidx_ptr;
185 long int argidx = strtol(argidx_str + 3, &argidx_ptr, 10);
186 /*
187 * Checks to see if an actual argument index
188 * was parsed.
189 */
190 if (argidx_ptr == argidx_str + 3)
191 return -1;
192
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700193 char *constant_str_ptr;
194 long int c = parse_constant(constant_str, &constant_str_ptr);
195 if (constant_str_ptr == constant_str)
196 return -1;
197
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800198 /*
199 * Looks up the label for the end of the AND statement
200 * this atom belongs to.
201 */
202 unsigned int id = group_end_lbl(labels, nr, group_idx);
203
204 /*
205 * Builds a BPF comparison between a syscall argument
206 * and a constant.
207 * The comparison lives inside an AND statement.
208 * If the comparison succeeds, we continue
209 * to the next comparison.
210 * If this comparison fails, the whole AND statement
211 * will fail, so we jump to the end of this AND statement.
212 */
213 struct sock_filter *comp_block;
214 size_t len = bpf_arg_comp(&comp_block, op, argidx, c, id);
215 if (len == 0)
216 return -1;
217
218 append_filter_block(head, comp_block, len);
219 return 0;
220}
221
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700222int compile_errno(struct filter_block *head, char *ret_errno,
223 int log_failures)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700224{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800225 char *errno_ptr;
226
227 /* Splits the 'return' keyword and the actual errno value. */
228 char *ret_str = strtok_r(ret_errno, " ", &errno_ptr);
229 if (strncmp(ret_str, "return", strlen("return")))
230 return -1;
231
232 char *errno_val_str = strtok_r(NULL, " ", &errno_ptr);
233
234 if (errno_val_str) {
235 char *errno_val_ptr;
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700236 int errno_val = parse_constant(errno_val_str, &errno_val_ptr);
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800237 /* Checks to see if we parsed an actual errno. */
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700238 if (errno_val_ptr == errno_val_str || errno_val == -1)
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800239 return -1;
240
241 append_ret_errno(head, errno_val);
242 } else {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700243 if (!log_failures)
244 append_ret_kill(head);
245 else
246 append_ret_trap(head);
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800247 }
248 return 0;
249}
250
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700251struct filter_block *compile_section(int nr, const char *policy_line,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400252 unsigned int entry_lbl_id,
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700253 struct bpf_labels *labels,
254 int log_failures)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700255{
256 /*
257 * |policy_line| should be an expression of the form:
258 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8"
259 *
260 * This is, an expression in DNF (disjunctive normal form);
261 * a disjunction ('||') of one or more conjunctions ('&&')
262 * of one or more atoms.
263 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700264 * Atoms are of the form "arg{DNUM} {OP} {NUM}"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700265 * where:
266 * - DNUM is a decimal number.
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800267 * - OP is an operator: ==, !=, or & (flags set).
268 * - NUM is an octal, decimal, or hexadecimal number.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700269 *
270 * When the syscall arguments make the expression true,
271 * the syscall is allowed. If not, the process is killed.
272 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700273 * To block a syscall without killing the process,
274 * |policy_line| can be of the form:
275 * "return <errno>"
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700276 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700277 * This "return {NUM}" policy line will block the syscall,
278 * make it return -1 and set |errno| to NUM.
279 *
280 * A regular policy line can also include a "return <errno>" clause,
281 * separated by a semicolon (';'):
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700282 * "arg0 == 3 && arg1 == 5 || arg0 == 0x8; return {NUM}"
283 *
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700284 * If the syscall arguments don't make the expression true,
285 * the syscall will be blocked as above instead of killing the process.
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700286 */
287
288 size_t len = 0;
289 int group_idx = 0;
290
291 /* Checks for overly long policy lines. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700292 if (strlen(policy_line) >= MAX_POLICY_LINE_LENGTH)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700293 return NULL;
294
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700295 /* We will modify |policy_line|, so let's make a copy. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700296 char *line = strndup(policy_line, MAX_POLICY_LINE_LENGTH);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700297 if (!line)
298 return NULL;
299
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700300 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700301 * We build the filter section as a collection of smaller
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700302 * "filter blocks" linked together in a singly-linked list.
303 */
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700304 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700305
306 /*
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700307 * Filter sections begin with a label where the main filter
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700308 * will jump after checking the syscall number.
309 */
310 struct sock_filter *entry_label = new_instr_buf(ONE_INSTR);
311 set_bpf_lbl(entry_label, entry_lbl_id);
312 append_filter_block(head, entry_label, ONE_INSTR);
313
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700314 /* Checks whether we're unconditionally blocking this syscall. */
315 if (strncmp(line, "return", strlen("return")) == 0) {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700316 if (compile_errno(head, line, log_failures) < 0)
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700317 return NULL;
318 free(line);
319 return head;
320 }
321
322 /* Splits the optional "return <errno>" part. */
323 char *line_ptr;
324 char *arg_filter = strtok_r(line, ";", &line_ptr);
325 char *ret_errno = strtok_r(NULL, ";", &line_ptr);
326
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700327 /*
328 * Splits the policy line by '||' into conjunctions and each conjunction
329 * by '&&' into atoms.
330 */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800331 char *arg_filter_str = arg_filter;
332 char *group;
333 while ((group = tokenize(&arg_filter_str, "||")) != NULL) {
334 char *group_str = group;
335 char *comp;
336 while ((comp = tokenize(&group_str, "&&")) != NULL) {
337 /* Compiles each atom into a BPF block. */
338 if (compile_atom(head, comp, labels, nr, group_idx) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700339 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700340 }
341 /*
342 * If the AND statement succeeds, we're done,
343 * so jump to SUCCESS line.
344 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700345 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700346 struct sock_filter *group_end_block = new_instr_buf(TWO_INSTRS);
347 len = set_bpf_jump_lbl(group_end_block, id);
348 /*
349 * The end of each AND statement falls after the
350 * jump to SUCCESS.
351 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700352 id = group_end_lbl(labels, nr, group_idx++);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700353 len += set_bpf_lbl(group_end_block + len, id);
354 append_filter_block(head, group_end_block, len);
355 }
356
357 /*
358 * If no AND statements succeed, we end up here,
359 * because we never jumped to SUCCESS.
360 * If we have to return an errno, do it,
361 * otherwise just kill the task.
362 */
363 if (ret_errno) {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700364 if (compile_errno(head, ret_errno, log_failures) < 0)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700365 return NULL;
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700366 } else {
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700367 if (!log_failures)
368 append_ret_kill(head);
369 else
370 append_ret_trap(head);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700371 }
372
373 /*
374 * Every time the filter succeeds we jump to a predefined SUCCESS
375 * label. Add that label and BPF RET_ALLOW code now.
376 */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700377 unsigned int id = success_lbl(labels, nr);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700378 struct sock_filter *success_block = new_instr_buf(TWO_INSTRS);
379 len = set_bpf_lbl(success_block, id);
380 len += set_bpf_ret_allow(success_block + len);
381 append_filter_block(head, success_block, len);
382
383 free(line);
384 return head;
385}
386
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400387int compile_filter(FILE *policy_file, struct sock_fprog *prog, int log_failures)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700388{
389 char line[MAX_LINE_LENGTH];
390 int line_count = 0;
391
392 struct bpf_labels labels;
393 labels.count = 0;
394
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800395 if (!policy_file)
Jorge Lucangeli Obes6135db92012-07-27 13:37:30 -0700396 return -1;
397
Jorge Lucangeli Obesd96baf42013-03-26 15:11:30 -0700398 struct filter_block *head = new_filter_block();
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700399 struct filter_block *arg_blocks = NULL;
400
401 /* Start filter by validating arch. */
402 struct sock_filter *valid_arch = new_instr_buf(ARCH_VALIDATION_LEN);
403 size_t len = bpf_validate_arch(valid_arch);
404 append_filter_block(head, valid_arch, len);
405
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700406 /* Load syscall number. */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700407 struct sock_filter *load_nr = new_instr_buf(ONE_INSTR);
408 len = bpf_load_syscall_nr(load_nr);
409 append_filter_block(head, load_nr, len);
410
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700411 /* If we're logging failures, allow the necessary syscalls first. */
412 if (log_failures)
413 allow_log_syscalls(head);
414
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700415 /*
416 * Loop through all the lines in the policy file.
417 * Build a jump table for the syscall number.
418 * If the policy line has an arg filter, build the arg filter
419 * as well.
420 * Chain the filter sections together and dump them into
421 * the final buffer at the end.
422 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800423 while (fgets(line, sizeof(line), policy_file)) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700424 ++line_count;
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800425 char *policy_line = line;
426 char *syscall_name = strsep(&policy_line, ":");
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700427 int nr = -1;
428
429 syscall_name = strip(syscall_name);
430
431 /* Allow comments and empty lines. */
432 if (*syscall_name == '#' || *syscall_name == '\0')
433 continue;
434
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800435 if (!policy_line)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700436 return -1;
437
438 nr = lookup_syscall(syscall_name);
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700439 if (nr < 0) {
440 warn("compile_filter: nonexistent syscall '%s'",
441 syscall_name);
Jorge Lucangeli Obesbe351a22016-01-22 17:18:51 -0800442 if (log_failures) {
443 /*
444 * If we're logging failures, assume we're in a
445 * debugging case and continue.
446 * This is not super risky because an invalid
447 * syscall name is likely caused by a typo or by
448 * leftover lines from a different architecture.
449 * In either case, not including a policy line
450 * is equivalent to killing the process if the
451 * syscall is made, so there's no added attack
452 * surface.
453 */
454 continue;
455 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700456 return -1;
Jorge Lucangeli Obes275b07f2012-08-28 11:50:29 -0700457 }
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700458
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800459 policy_line = strip(policy_line);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700460
461 /*
462 * For each syscall, add either a simple ALLOW,
463 * or an arg filter block.
464 */
Jorge Lucangeli Obesb0cea432013-02-04 11:55:30 -0800465 if (strcmp(policy_line, "1") == 0) {
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700466 /* Add simple ALLOW. */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700467 append_allow_syscall(head, nr);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700468 } else {
469 /*
470 * Create and jump to the label that will hold
471 * the arg filter block.
472 */
473 unsigned int id = bpf_label_id(&labels, syscall_name);
474 struct sock_filter *nr_comp =
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400475 new_instr_buf(ALLOW_SYSCALL_LEN);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700476 bpf_allow_syscall_args(nr_comp, nr, id);
477 append_filter_block(head, nr_comp, ALLOW_SYSCALL_LEN);
478
479 /* Build the arg filter block. */
480 struct filter_block *block =
Jennifer Pullmanc0bfae02016-05-26 17:00:36 -0700481 compile_section(nr, policy_line, id, &labels,
482 log_failures);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700483
484 if (!block)
485 return -1;
486
487 if (arg_blocks) {
488 extend_filter_block_list(arg_blocks, block);
489 } else {
490 arg_blocks = block;
491 }
492 }
493 }
494
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700495 /*
496 * If none of the syscalls match, either fall back to KILL,
497 * or return TRAP.
498 */
499 if (!log_failures)
500 append_ret_kill(head);
501 else
502 append_ret_trap(head);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700503
504 /* Allocate the final buffer, now that we know its size. */
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400505 size_t final_filter_len =
506 head->total_len + (arg_blocks ? arg_blocks->total_len : 0);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700507 if (final_filter_len > BPF_MAXINSNS)
508 return -1;
509
510 struct sock_filter *final_filter =
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400511 calloc(final_filter_len, sizeof(struct sock_filter));
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700512
513 if (flatten_block_list(head, final_filter, 0, final_filter_len) < 0)
514 return -1;
515
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400516 if (flatten_block_list(arg_blocks, final_filter, head->total_len,
517 final_filter_len) < 0)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700518 return -1;
519
520 free_block_list(head);
521 free_block_list(arg_blocks);
522
523 bpf_resolve_jumps(&labels, final_filter, final_filter_len);
524
525 free_label_strings(&labels);
526
527 prog->filter = final_filter;
528 prog->len = final_filter_len;
529 return 0;
530}
531
532int flatten_block_list(struct filter_block *head, struct sock_filter *filter,
Jorge Lucangeli Obes565e9782016-08-05 11:03:19 -0400533 size_t index, size_t cap)
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700534{
535 size_t _index = index;
536
537 struct filter_block *curr;
538 size_t i;
539
540 for (curr = head; curr; curr = curr->next) {
541 for (i = 0; i < curr->len; i++) {
542 if (_index >= cap)
543 return -1;
544 filter[_index++] = curr->instrs[i];
545 }
546 }
547 return 0;
548}
549
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700550void free_block_list(struct filter_block *head)
551{
552 struct filter_block *current, *prev;
553
554 current = head;
555 while (current) {
556 free(current->instrs);
557 prev = current;
558 current = current->next;
559 free(prev);
560 }
561}