| 1 |
/** |
| 2 |
* BPF Simulator |
| 3 |
* |
| 4 |
* Copyright (c) 2012 Red Hat <[email protected]> |
| 5 |
* Author: Paul Moore <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* This library is free software; you can redistribute it and/or modify it |
| 10 |
* under the terms of version 2.1 of the GNU Lesser General Public License as |
| 11 |
* published by the Free Software Foundation. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, but WITHOUT |
| 14 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 |
* for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with this library; if not, see <http://www.gnu.org/licenses>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <elf.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <inttypes.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <linux/audit.h> |
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
|
| 34 |
#include "bpf.h" |
| 35 |
#include "util.h" |
| 36 |
|
| 37 |
#define BPF_PRG_MAX_LEN 4096 |
| 38 |
|
| 39 |
/** |
| 40 |
* BPF simulator machine state |
| 41 |
*/ |
| 42 |
struct sim_state { |
| 43 |
uint32_t acc; |
| 44 |
uint32_t temp[BPF_SCRATCH_SIZE]; |
| 45 |
}; |
| 46 |
|
| 47 |
struct bpf_program { |
| 48 |
size_t i_cnt; |
| 49 |
bpf_instr_raw *i; |
| 50 |
}; |
| 51 |
|
| 52 |
static unsigned int opt_verbose = 0; |
| 53 |
|
| 54 |
/** |
| 55 |
* Print the usage information to stderr and exit |
| 56 |
* @param program the name of the current program being invoked |
| 57 |
* |
| 58 |
* Print the usage information and exit with EINVAL. |
| 59 |
* |
| 60 |
*/ |
| 61 |
static void exit_usage(const char *program) |
| 62 |
{ |
| 63 |
fprintf(stderr, |
| 64 |
"usage: %s -f <bpf_file> [-v] [-h]" |
| 65 |
" -a <arch> -s <syscall_num> [-0 <a0>] ... [-5 <a5>]\n", |
| 66 |
program); |
| 67 |
exit(EINVAL); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Handle a simulator fault |
| 72 |
* @param rc the error or return code |
| 73 |
* |
| 74 |
* Print a "FAULT" to stderr to indicate a simulator fault, and an errno value |
| 75 |
* if the simulator is running in verbose mode, then exit with EFAULT. |
| 76 |
* |
| 77 |
*/ |
| 78 |
static void exit_fault(unsigned int rc) |
| 79 |
{ |
| 80 |
if (opt_verbose) |
| 81 |
fprintf(stderr, "FAULT: errno = %d\n", rc); |
| 82 |
else |
| 83 |
fprintf(stderr, "FAULT\n"); |
| 84 |
exit(EFAULT); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Handle a BPF program error |
| 89 |
* @param rc the error or return code |
| 90 |
* @param line the line number |
| 91 |
* |
| 92 |
* Print an "ERROR" to stderr to indicate a program error, and an errno value |
| 93 |
* if the simulator is running in verbose mode, then exit with ENOEXEC. |
| 94 |
* |
| 95 |
*/ |
| 96 |
static void exit_error(unsigned int rc, unsigned int line) |
| 97 |
{ |
| 98 |
if (opt_verbose) |
| 99 |
fprintf(stderr, "ERROR: errno = %d, line = %d\n", rc, line); |
| 100 |
else |
| 101 |
fprintf(stderr, "ERROR\n"); |
| 102 |
exit(ENOEXEC); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Handle a simulator return/action |
| 107 |
* @param action the return value |
| 108 |
* @param line the line number |
| 109 |
* |
| 110 |
* Display the action to stdout and exit with 0. |
| 111 |
* |
| 112 |
*/ |
| 113 |
static void end_action(uint32_t action, unsigned int line) |
| 114 |
{ |
| 115 |
uint32_t act = action & SECCOMP_RET_ACTION_FULL; |
| 116 |
uint32_t data = action & SECCOMP_RET_DATA; |
| 117 |
|
| 118 |
switch (act) { |
| 119 |
case SECCOMP_RET_KILL_PROCESS: |
| 120 |
fprintf(stdout, "KILL_PROCESS\n"); |
| 121 |
break; |
| 122 |
case SECCOMP_RET_KILL_THREAD: |
| 123 |
fprintf(stdout, "KILL\n"); |
| 124 |
break; |
| 125 |
case SECCOMP_RET_TRAP: |
| 126 |
fprintf(stdout, "TRAP\n"); |
| 127 |
break; |
| 128 |
case SECCOMP_RET_ERRNO: |
| 129 |
fprintf(stdout, "ERRNO(%u)\n", data); |
| 130 |
break; |
| 131 |
case SECCOMP_RET_TRACE: |
| 132 |
fprintf(stdout, "TRACE(%u)\n", data); |
| 133 |
break; |
| 134 |
case SECCOMP_RET_LOG: |
| 135 |
fprintf(stdout, "LOG\n"); |
| 136 |
break; |
| 137 |
case SECCOMP_RET_ALLOW: |
| 138 |
fprintf(stdout, "ALLOW\n"); |
| 139 |
break; |
| 140 |
default: |
| 141 |
exit_error(EDOM, line); |
| 142 |
} |
| 143 |
|
| 144 |
exit(0); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Execute a BPF program |
| 149 |
* @param prg the loaded BPF program |
| 150 |
* @param sys_data the syscall record being tested |
| 151 |
* |
| 152 |
* Simulate the BPF program with the given syscall record. |
| 153 |
* |
| 154 |
*/ |
| 155 |
static void bpf_execute(const struct bpf_program *prg, |
| 156 |
const struct seccomp_data *sys_data) |
| 157 |
{ |
| 158 |
unsigned int ip, ip_c; |
| 159 |
struct sim_state state; |
| 160 |
bpf_instr_raw *bpf; |
| 161 |
unsigned char *sys_data_b = (unsigned char *)sys_data; |
| 162 |
uint16_t code; |
| 163 |
uint8_t jt; |
| 164 |
uint8_t jf; |
| 165 |
uint32_t k; |
| 166 |
|
| 167 |
/* initialize the machine state */ |
| 168 |
ip_c = 0; |
| 169 |
ip = 0; |
| 170 |
memset(&state, 0, sizeof(state)); |
| 171 |
|
| 172 |
while (ip < prg->i_cnt) { |
| 173 |
/* get the instruction and bump the ip */ |
| 174 |
ip_c = ip; |
| 175 |
bpf = &prg->i[ip++]; |
| 176 |
|
| 177 |
code = ttoh16(arch, bpf->code); |
| 178 |
jt = bpf->jt; |
| 179 |
jf = bpf->jf; |
| 180 |
k = ttoh32(arch, bpf->k); |
| 181 |
|
| 182 |
switch (code) { |
| 183 |
case BPF_LD+BPF_W+BPF_ABS: |
| 184 |
if (k < BPF_SYSCALL_MAX) { |
| 185 |
uint32_t val; |
| 186 |
memcpy(&val, &sys_data_b[k], sizeof(val)); |
| 187 |
state.acc = ttoh32(arch, val); |
| 188 |
} else |
| 189 |
exit_error(ERANGE, ip_c); |
| 190 |
break; |
| 191 |
case BPF_ALU+BPF_OR+BPF_K: |
| 192 |
state.acc |= k; |
| 193 |
break; |
| 194 |
case BPF_ALU+BPF_AND+BPF_K: |
| 195 |
state.acc &= k; |
| 196 |
break; |
| 197 |
case BPF_JMP+BPF_JA: |
| 198 |
ip += k; |
| 199 |
break; |
| 200 |
case BPF_JMP+BPF_JEQ+BPF_K: |
| 201 |
if (state.acc == k) |
| 202 |
ip += jt; |
| 203 |
else |
| 204 |
ip += jf; |
| 205 |
break; |
| 206 |
case BPF_JMP+BPF_JGT+BPF_K: |
| 207 |
if (state.acc > k) |
| 208 |
ip += jt; |
| 209 |
else |
| 210 |
ip += jf; |
| 211 |
break; |
| 212 |
case BPF_JMP+BPF_JGE+BPF_K: |
| 213 |
if (state.acc >= k) |
| 214 |
ip += jt; |
| 215 |
else |
| 216 |
ip += jf; |
| 217 |
break; |
| 218 |
case BPF_RET+BPF_K: |
| 219 |
end_action(k, ip_c); |
| 220 |
break; |
| 221 |
default: |
| 222 |
/* since we don't support the full bpf language just |
| 223 |
* yet, this could be either a fault or an error, we'll |
| 224 |
* treat it as a fault until we provide full support */ |
| 225 |
exit_fault(EOPNOTSUPP); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/* if we've reached here there is a problem with the program */ |
| 230 |
exit_error(ERANGE, ip_c); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* main |
| 235 |
*/ |
| 236 |
int main(int argc, char *argv[]) |
| 237 |
{ |
| 238 |
int opt; |
| 239 |
int iter; |
| 240 |
char *opt_file = NULL; |
| 241 |
FILE *file; |
| 242 |
size_t file_read_len; |
| 243 |
struct seccomp_data sys_data; |
| 244 |
struct bpf_program bpf_prg; |
| 245 |
|
| 246 |
/* initialize the syscall record */ |
| 247 |
memset(&sys_data, 0, sizeof(sys_data)); |
| 248 |
|
| 249 |
/* parse the command line */ |
| 250 |
while ((opt = getopt(argc, argv, "a:f:hs:v0:1:2:3:4:5:")) > 0) { |
| 251 |
switch (opt) { |
| 252 |
case 'a': |
| 253 |
if (strcmp(optarg, "x86") == 0) |
| 254 |
arch = AUDIT_ARCH_I386; |
| 255 |
else if (strcmp(optarg, "x86_64") == 0) |
| 256 |
arch = AUDIT_ARCH_X86_64; |
| 257 |
else if (strcmp(optarg, "x32") == 0) |
| 258 |
arch = AUDIT_ARCH_X86_64; |
| 259 |
else if (strcmp(optarg, "arm") == 0) |
| 260 |
arch = AUDIT_ARCH_ARM; |
| 261 |
else if (strcmp(optarg, "aarch64") == 0) |
| 262 |
arch = AUDIT_ARCH_AARCH64; |
| 263 |
else if (strcmp(optarg, "loongarch64") == 0) |
| 264 |
arch = AUDIT_ARCH_LOONGARCH64; |
| 265 |
else if (strcmp(optarg, "m68k") == 0) |
| 266 |
arch = AUDIT_ARCH_M68K; |
| 267 |
else if (strcmp(optarg, "mips") == 0) |
| 268 |
arch = AUDIT_ARCH_MIPS; |
| 269 |
else if (strcmp(optarg, "mipsel") == 0) |
| 270 |
arch = AUDIT_ARCH_MIPSEL; |
| 271 |
else if (strcmp(optarg, "mips64") == 0) |
| 272 |
arch = AUDIT_ARCH_MIPS64; |
| 273 |
else if (strcmp(optarg, "mipsel64") == 0) |
| 274 |
arch = AUDIT_ARCH_MIPSEL64; |
| 275 |
else if (strcmp(optarg, "mips64n32") == 0) |
| 276 |
arch = AUDIT_ARCH_MIPS64N32; |
| 277 |
else if (strcmp(optarg, "mipsel64n32") == 0) |
| 278 |
arch = AUDIT_ARCH_MIPSEL64N32; |
| 279 |
else if (strcmp(optarg, "parisc") == 0) |
| 280 |
arch = AUDIT_ARCH_PARISC; |
| 281 |
else if (strcmp(optarg, "parisc64") == 0) |
| 282 |
arch = AUDIT_ARCH_PARISC64; |
| 283 |
else if (strcmp(optarg, "ppc") == 0) |
| 284 |
arch = AUDIT_ARCH_PPC; |
| 285 |
else if (strcmp(optarg, "ppc64") == 0) |
| 286 |
arch = AUDIT_ARCH_PPC64; |
| 287 |
else if (strcmp(optarg, "ppc64le") == 0) |
| 288 |
arch = AUDIT_ARCH_PPC64LE; |
| 289 |
else if (strcmp(optarg, "s390") == 0) |
| 290 |
arch = AUDIT_ARCH_S390; |
| 291 |
else if (strcmp(optarg, "s390x") == 0) |
| 292 |
arch = AUDIT_ARCH_S390X; |
| 293 |
else if (strcmp(optarg, "riscv64") == 0) |
| 294 |
arch = AUDIT_ARCH_RISCV64; |
| 295 |
else if (strcmp(optarg, "sheb") == 0) |
| 296 |
arch = AUDIT_ARCH_SH; |
| 297 |
else if (strcmp(optarg, "sh") == 0) |
| 298 |
arch = AUDIT_ARCH_SHEL; |
| 299 |
else |
| 300 |
exit_fault(EINVAL); |
| 301 |
break; |
| 302 |
case 'f': |
| 303 |
if (opt_file) |
| 304 |
exit_fault(EINVAL); |
| 305 |
opt_file = strdup(optarg); |
| 306 |
if (opt_file == NULL) |
| 307 |
exit_fault(ENOMEM); |
| 308 |
break; |
| 309 |
case 's': |
| 310 |
sys_data.nr = strtol(optarg, NULL, 0); |
| 311 |
break; |
| 312 |
case 'v': |
| 313 |
opt_verbose = 1; |
| 314 |
break; |
| 315 |
case '0': |
| 316 |
sys_data.args[0] = strtoull(optarg, NULL, 0); |
| 317 |
break; |
| 318 |
case '1': |
| 319 |
sys_data.args[1] = strtoull(optarg, NULL, 0); |
| 320 |
break; |
| 321 |
case '2': |
| 322 |
sys_data.args[2] = strtoull(optarg, NULL, 0); |
| 323 |
break; |
| 324 |
case '3': |
| 325 |
sys_data.args[3] = strtoull(optarg, NULL, 0); |
| 326 |
break; |
| 327 |
case '4': |
| 328 |
sys_data.args[4] = strtoull(optarg, NULL, 0); |
| 329 |
break; |
| 330 |
case '5': |
| 331 |
sys_data.args[5] = strtoull(optarg, NULL, 0); |
| 332 |
break; |
| 333 |
case 'h': |
| 334 |
default: |
| 335 |
/* usage information */ |
| 336 |
exit_usage(argv[0]); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
/* adjust the endianness of sys_data to match the target */ |
| 341 |
sys_data.nr = htot32(arch, sys_data.nr); |
| 342 |
sys_data.arch = htot32(arch, arch); |
| 343 |
sys_data.instruction_pointer = htot64(arch, |
| 344 |
sys_data.instruction_pointer); |
| 345 |
for (iter = 0; iter < BPF_SYS_ARG_MAX; iter++) |
| 346 |
sys_data.args[iter] = htot64(arch, sys_data.args[iter]); |
| 347 |
|
| 348 |
/* allocate space for the bpf program */ |
| 349 |
/* XXX - we should make this dynamic */ |
| 350 |
bpf_prg.i_cnt = 0; |
| 351 |
bpf_prg.i = calloc(BPF_PRG_MAX_LEN, sizeof(*bpf_prg.i)); |
| 352 |
if (bpf_prg.i == NULL) |
| 353 |
exit_fault(ENOMEM); |
| 354 |
|
| 355 |
/* load the bpf program */ |
| 356 |
if (opt_file == NULL) |
| 357 |
exit_usage(argv[0]); |
| 358 |
file = fopen(opt_file, "r"); |
| 359 |
if (file == NULL) |
| 360 |
exit_fault(errno); |
| 361 |
do { |
| 362 |
file_read_len = fread(&(bpf_prg.i[bpf_prg.i_cnt]), |
| 363 |
sizeof(*bpf_prg.i), 1, file); |
| 364 |
if (file_read_len == 1) |
| 365 |
bpf_prg.i_cnt++; |
| 366 |
|
| 367 |
/* check the size */ |
| 368 |
if (bpf_prg.i_cnt == BPF_PRG_MAX_LEN) |
| 369 |
exit_fault(E2BIG); |
| 370 |
} while (file_read_len > 0); |
| 371 |
fclose(file); |
| 372 |
|
| 373 |
/* execute the bpf program */ |
| 374 |
bpf_execute(&bpf_prg, &sys_data); |
| 375 |
|
| 376 |
/* we should never reach here */ |
| 377 |
exit_fault(EFAULT); |
| 378 |
return 0; |
| 379 |
} |