| 1 |
#include "decoder/decoder.h" |
| 2 |
#include "decoder/check_prog.h" |
| 3 |
#include "lexical/parser.h" |
| 4 |
#include "lexical/token.h" |
| 5 |
#include "main.h" |
| 6 |
#include "resolver/resolver.h" |
| 7 |
#include "utils/vector.h" |
| 8 |
#include <assert.h> |
| 9 |
#include <linux/bpf_common.h> |
| 10 |
#include <linux/filter.h> |
| 11 |
#include <linux/seccomp.h> |
| 12 |
#include <seccomp.h> |
| 13 |
#include <stdbool.h> |
| 14 |
#include <stddef.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#define RSH2(value) ((value) >> 2) |
| 19 |
#define RSH4(value) ((value) >> 4) |
| 20 |
|
| 21 |
static const token_type abs_table[] = { |
| 22 |
[RSH2 (offsetof (seccomp_data, nr))] = ATTR_SYSCALL, |
| 23 |
[RSH2 (offsetof (seccomp_data, arch))] = ATTR_ARCH, |
| 24 |
[RSH2 (offsetof (seccomp_data, instruction_pointer))] = ATTR_LOWPC, |
| 25 |
[RSH2 (offsetof (seccomp_data, instruction_pointer) + 4)] = ATTR_HIGHPC, |
| 26 |
[RSH2 (offsetof (seccomp_data, args[0]))] = ATTR_LOWARG, |
| 27 |
[RSH2 (offsetof (seccomp_data, args[0]) + 4)] = ATTR_HIGHARG, |
| 28 |
[RSH2 (offsetof (seccomp_data, args[1]))] = ATTR_LOWARG, |
| 29 |
[RSH2 (offsetof (seccomp_data, args[1]) + 4)] = ATTR_HIGHARG, |
| 30 |
[RSH2 (offsetof (seccomp_data, args[2]))] = ATTR_LOWARG, |
| 31 |
[RSH2 (offsetof (seccomp_data, args[2]) + 4)] = ATTR_HIGHARG, |
| 32 |
[RSH2 (offsetof (seccomp_data, args[3]))] = ATTR_LOWARG, |
| 33 |
[RSH2 (offsetof (seccomp_data, args[3]) + 4)] = ATTR_HIGHARG, |
| 34 |
[RSH2 (offsetof (seccomp_data, args[4]))] = ATTR_LOWARG, |
| 35 |
[RSH2 (offsetof (seccomp_data, args[4]) + 4)] = ATTR_HIGHARG, |
| 36 |
[RSH2 (offsetof (seccomp_data, args[5]))] = ATTR_LOWARG, |
| 37 |
[RSH2 (offsetof (seccomp_data, args[5]) + 4)] = ATTR_HIGHARG, |
| 38 |
}; |
| 39 |
|
| 40 |
#define SCMP_DATA_LEN_STR "# 0x40" |
| 41 |
static void |
| 42 |
ld_ldx_line (filter f, statement_t *statement) |
| 43 |
{ |
| 44 |
statement->type = ASSIGN_LINE; |
| 45 |
assign_line_t *assign_line = &statement->assign_line; |
| 46 |
|
| 47 |
assign_line->left_var.type = (BPF_CLASS (f.code) == BPF_LD) ? A : X; |
| 48 |
assign_line->operator = EQUAL; |
| 49 |
|
| 50 |
obj_t *right = &assign_line->right_var; |
| 51 |
switch (BPF_MODE (f.code)) |
| 52 |
{ |
| 53 |
case BPF_IMM: |
| 54 |
right->type = NUMBER; |
| 55 |
right->data = f.k; |
| 56 |
return; |
| 57 |
case BPF_MEM: |
| 58 |
right->type = MEM; |
| 59 |
right->data = f.k; |
| 60 |
return; |
| 61 |
case BPF_ABS: |
| 62 |
right->type = abs_table[RSH2 (f.k)]; |
| 63 |
if (right->type == ATTR_LOWARG || right->type == ATTR_HIGHARG) |
| 64 |
right->data = (f.k - offsetof (seccomp_data, args[0])) / 0x8; |
| 65 |
return; |
| 66 |
case BPF_LEN: |
| 67 |
right->type = ATTR_LEN; |
| 68 |
statement->line_start = SCMP_DATA_LEN_STR; |
| 69 |
statement->comment = 0; |
| 70 |
statement->line_len = LITERAL_STRLEN (SCMP_DATA_LEN_STR); |
| 71 |
static_assert (sizeof (seccomp_data) == 0x40, |
| 72 |
"struct seccomp_data should be 0x40 bytes large"); |
| 73 |
return; |
| 74 |
default: |
| 75 |
assert (!"Unknown BPF_MODE for ld or ldx"); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
st_stx_line (filter f, statement_t *statement) |
| 81 |
{ |
| 82 |
statement->type = ASSIGN_LINE; |
| 83 |
assign_line_t *assign_line = &statement->assign_line; |
| 84 |
|
| 85 |
assign_line->left_var.type = MEM; |
| 86 |
assign_line->left_var.data = f.k; |
| 87 |
assign_line->operator = EQUAL; |
| 88 |
|
| 89 |
assign_line->right_var.type = (BPF_CLASS (f.code) == BPF_ST) ? A : X; |
| 90 |
} |
| 91 |
|
| 92 |
static const token_type operator_table[] = { |
| 93 |
[RSH4 (BPF_ADD)] = ADD_TO, [RSH4 (BPF_SUB)] = SUB_TO, |
| 94 |
[RSH4 (BPF_MUL)] = MULTI_TO, [RSH4 (BPF_DIV)] = DIVIDE_TO, |
| 95 |
[RSH4 (BPF_OR)] = OR_TO, [RSH4 (BPF_AND)] = AND_TO, |
| 96 |
[RSH4 (BPF_LSH)] = LSH_TO, [RSH4 (BPF_RSH)] = RSH_TO, |
| 97 |
[RSH4 (BPF_NEG)] = NEGATIVE, [RSH4 (BPF_XOR)] = XOR_TO, |
| 98 |
}; |
| 99 |
|
| 100 |
static void |
| 101 |
alu_line (filter f, statement_t *statement) |
| 102 |
{ |
| 103 |
statement->type = ASSIGN_LINE; |
| 104 |
assign_line_t *assign_line = &statement->assign_line; |
| 105 |
|
| 106 |
assign_line->left_var.type = A; |
| 107 |
assign_line->operator = operator_table[RSH4 (BPF_OP (f.code))]; |
| 108 |
|
| 109 |
if (BPF_SRC (f.code) == BPF_X) |
| 110 |
assign_line->right_var.type = X; |
| 111 |
else if (assign_line->operator == NEGATIVE) |
| 112 |
assign_line->right_var.type = A; |
| 113 |
else |
| 114 |
{ |
| 115 |
assign_line->right_var.type = NUMBER; |
| 116 |
assign_line->right_var.data = f.k; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static const token_type comparator_table[] = { |
| 121 |
[RSH4 (BPF_JEQ)] = EQUAL_EQUAL, |
| 122 |
[RSH4 (BPF_JGT)] = GREATER_THAN, |
| 123 |
[RSH4 (BPF_JGE)] = GREATER_EQUAL, |
| 124 |
[RSH4 (BPF_JSET)] = AND, |
| 125 |
}; |
| 126 |
|
| 127 |
static const token_type reverse_table[] = { |
| 128 |
[RSH4 (BPF_JEQ)] = BANG_EQUAL, |
| 129 |
[RSH4 (BPF_JGT)] = LESS_EQUAL, |
| 130 |
[RSH4 (BPF_JGE)] = LESS_THAN, |
| 131 |
}; |
| 132 |
|
| 133 |
static void |
| 134 |
condition (filter f, token_type *comparator, obj_t *cmpobj, bool *if_bang) |
| 135 |
{ |
| 136 |
uint32_t op = BPF_OP (f.code); |
| 137 |
if ((!*if_bang) || op == BPF_JSET) |
| 138 |
*comparator = comparator_table[RSH4 (op)]; |
| 139 |
else |
| 140 |
{ |
| 141 |
*comparator = reverse_table[RSH4 (op)]; |
| 142 |
*if_bang = false; |
| 143 |
} |
| 144 |
|
| 145 |
if (BPF_SRC (f.code) == BPF_X) |
| 146 |
cmpobj->type = X; |
| 147 |
else |
| 148 |
{ |
| 149 |
cmpobj->type = NUMBER; |
| 150 |
cmpobj->data = f.k; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
jump_line (filter f, statement_t *statement) |
| 156 |
{ |
| 157 |
statement->type = JUMP_LINE; |
| 158 |
jump_line_t *jump_line = &statement->jump_line; |
| 159 |
|
| 160 |
if (BPF_OP (f.code) == BPF_JA) |
| 161 |
{ |
| 162 |
jump_line->if_bang = false; |
| 163 |
jump_line->if_condition = false; |
| 164 |
jump_line->jt.code_nr = f.k; |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
jump_line->if_condition = true; |
| 169 |
|
| 170 |
if (f.jt == 0 && f.jf != 0) |
| 171 |
{ |
| 172 |
jump_line->if_bang = true; |
| 173 |
jump_line->jf.code_nr = f.jt; |
| 174 |
jump_line->jt.code_nr = f.jf; |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
jump_line->jt.code_nr = f.jt; |
| 179 |
jump_line->jf.code_nr = f.jf; |
| 180 |
} |
| 181 |
|
| 182 |
condition (f, &jump_line->comparator, &jump_line->cmpobj, |
| 183 |
&jump_line->if_bang); |
| 184 |
} |
| 185 |
|
| 186 |
token_type |
| 187 |
decode_return_k (obj_t *ret_obj, uint32_t k) |
| 188 |
{ |
| 189 |
token_type tk; |
| 190 |
switch (k & SECCOMP_RET_ACTION_FULL) |
| 191 |
{ |
| 192 |
case SCMP_ACT_KILL_PROCESS: |
| 193 |
tk = KILL_PROC; |
| 194 |
break; |
| 195 |
case SCMP_ACT_KILL: |
| 196 |
tk = KILL; |
| 197 |
break; |
| 198 |
case SCMP_ACT_ALLOW: |
| 199 |
tk = ALLOW; |
| 200 |
break; |
| 201 |
case SCMP_ACT_LOG: |
| 202 |
tk = LOG; |
| 203 |
break; |
| 204 |
case SCMP_ACT_NOTIFY: |
| 205 |
tk = NOTIFY; |
| 206 |
break; |
| 207 |
case SCMP_ACT_TRACE (0): |
| 208 |
ret_obj->data = k & SECCOMP_RET_DATA; |
| 209 |
tk = TRACE; |
| 210 |
break; |
| 211 |
case _SCMP_ACT_TRAP (0): |
| 212 |
ret_obj->data = k & SECCOMP_RET_DATA; |
| 213 |
tk = TRAP; |
| 214 |
break; |
| 215 |
case SCMP_ACT_ERRNO (0): |
| 216 |
ret_obj->data = k & SECCOMP_RET_DATA; |
| 217 |
tk = ERRNO; |
| 218 |
break; |
| 219 |
default: |
| 220 |
ret_obj->data = k; |
| 221 |
tk = NUMBER; |
| 222 |
} |
| 223 |
ret_obj->type = tk; |
| 224 |
return tk == NUMBER ? KILL_PROC : tk; |
| 225 |
} |
| 226 |
|
| 227 |
static void |
| 228 |
return_line (filter f, statement_t *statement) |
| 229 |
{ |
| 230 |
statement->type = RETURN_LINE; |
| 231 |
return_line_t *return_line = &statement->return_line; |
| 232 |
|
| 233 |
if (BPF_RVAL (f.code) == BPF_A) |
| 234 |
return_line->ret_obj.type = A; |
| 235 |
else |
| 236 |
decode_return_k (&return_line->ret_obj, f.k); |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
misc_line (filter f, statement_t *statement) |
| 241 |
{ |
| 242 |
statement->type = ASSIGN_LINE; |
| 243 |
assign_line_t *assign_line = &statement->assign_line; |
| 244 |
assign_line->operator = EQUAL; |
| 245 |
|
| 246 |
if (BPF_MISCOP (f.code) == BPF_TAX) |
| 247 |
{ |
| 248 |
assign_line->left_var.type = X; |
| 249 |
assign_line->right_var.type = A; |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
assign_line->left_var.type = A; |
| 254 |
assign_line->right_var.type = X; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
static void |
| 259 |
decode_filter (filter f, statement_t *statement) |
| 260 |
{ |
| 261 |
switch (BPF_CLASS (f.code)) |
| 262 |
{ |
| 263 |
case BPF_LD: |
| 264 |
case BPF_LDX: |
| 265 |
ld_ldx_line (f, statement); |
| 266 |
return; |
| 267 |
case BPF_ST: |
| 268 |
case BPF_STX: |
| 269 |
st_stx_line (f, statement); |
| 270 |
return; |
| 271 |
case BPF_ALU: |
| 272 |
alu_line (f, statement); |
| 273 |
break; |
| 274 |
case BPF_JMP: |
| 275 |
jump_line (f, statement); |
| 276 |
break; |
| 277 |
case BPF_RET: |
| 278 |
return_line (f, statement); |
| 279 |
break; |
| 280 |
case BPF_MISC: |
| 281 |
misc_line (f, statement); |
| 282 |
break; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
bool |
| 287 |
decode_filters (fprog *prog, vector_t *v, bool trustful) |
| 288 |
{ |
| 289 |
// make sure all filter are valid |
| 290 |
// give warning about fatal and normal errors |
| 291 |
bool error = trustful ? false : check_prog (prog); |
| 292 |
|
| 293 |
statement_t statement; |
| 294 |
push_vector (v, &statement); |
| 295 |
|
| 296 |
for (uint32_t i = 0; i < prog->len; i++) |
| 297 |
{ |
| 298 |
memset (&statement, '\0', sizeof (statement_t)); |
| 299 |
|
| 300 |
statement.text_nr = i + 1; |
| 301 |
statement.code_nr = i + 1; |
| 302 |
|
| 303 |
decode_filter (prog->filter[i], &statement); |
| 304 |
push_vector (v, &statement); |
| 305 |
} |
| 306 |
return error; |
| 307 |
} |