Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seccomppolicy compiler: cBPF filter comparators #2903

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions contrib/codegen/generate_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ def eval_(expr, filt, label_t, label_f):

elif expr0_str == 'eq':
eval_equal(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '<':
eval_less(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '<=':
eval_less_or_equal(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '>':
eval_greater(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '>=':
eval_greater_or_equal(filt, expr[1], expr[2], label_t, label_f)

else:
print(expr0_str)
raise("unknown fn")
Expand Down Expand Up @@ -239,7 +252,7 @@ def eval_bit_and(filt, op1, op2, label_t, label_f):
if label_t and label_f:
filt.append(ReloCondJump("BPF_JMP | BPF_JEQ | BPF_K, 0", label_f, label_t))

def eval_equal(filt, op1, op2, label_t, label_f):
def gen_cmp(filt, op1, op2, label_t, label_f, cmp_instr):
op1_type, op2_type = type(op1), type(op2)

if op1_type is edn_format.Symbol and op2_type is edn_format.Symbol:
Expand All @@ -253,18 +266,32 @@ def eval_equal(filt, op1, op2, label_t, label_f):
# eval op1 and do operation with op2 imm
eval_(op1, filt, None, None)
# accu now contains the eval res of op1
filt.append(ReloCondJump("BPF_JMP | BPF_JEQ | BPF_K, %s" % str(op2), label_t, label_f))
filt.append(ReloCondJump(f"BPF_JMP | {cmp_instr} | BPF_K, %s" % str(op2), label_t, label_f))

elif op2_type is tuple and op1_type is not tuple:
# eval op2 and do operation with op1 imm
eval_(op2, None, None)
# accu now contains the eval res of op2
filt.append(ReloCondJump("BPF_JMP | BPF_JEQ | BPF_K, %s" % str(op1), label_t, label_f))
filt.append(ReloCondJump(f"BPF_JMP | {cmp_instr} | BPF_K, %s" % str(op1), label_t, label_f))
else:
# This is unsupported because I didn't pick a calling convention and this means that accu and x should be saved to scratch.
# It's very easy to achieve but there's no need for it yet. It's basically register allocation over BPF scratch.
raise("unsupported")
raise("unsupported")

def eval_equal(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JEQ")

def eval_less(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JLT")

def eval_less_or_equal(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JLE")

def eval_greater(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JGT")

def eval_greater_or_equal(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JGE")

def resplit_lines(lines):
i = 0
Expand Down
Loading